-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
k8s的node、pods、svc、namespace、deployment的查看和一些的基础增删改
- Loading branch information
Showing
14 changed files
with
517 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package apis_k8s | ||
|
||
import ( | ||
"cmdb-ops-flow/models/k8s" | ||
"cmdb-ops-flow/service/service_k8s" | ||
"cmdb-ops-flow/utils/msg" | ||
"cmdb-ops-flow/utils/result" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
func GetDeployment(c *gin.Context) { | ||
var data k8s.Deployment | ||
if err := c.ShouldBindJSON(&data); err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(400, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
list, err := service_k8s.GetDeployments(data.ID, data.Namespace) | ||
if err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
code := msg.SUCCSE | ||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package apis_k8s | ||
|
||
import ( | ||
"cmdb-ops-flow/models/k8s" | ||
"cmdb-ops-flow/service/service_k8s" | ||
"cmdb-ops-flow/utils/msg" | ||
"cmdb-ops-flow/utils/result" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
func GetallSvc(c *gin.Context) { | ||
var data k8s.Svc | ||
if err := c.ShouldBindJSON(&data); err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(400, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
list, err := service_k8s.Getsvs(data.ID, data.Namespace) | ||
if err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
code := msg.SUCCSE | ||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code))) | ||
} | ||
|
||
func AddSvc(c *gin.Context) { | ||
|
||
var data k8s.Svc | ||
if err := c.ShouldBindJSON(&data); err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(5001, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
list, err := service_k8s.AddSvc(data.ID, data) | ||
if err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
code := msg.SUCCSE | ||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code))) | ||
} | ||
func EditSvc(c *gin.Context) { | ||
|
||
var data k8s.Svc | ||
if err := c.ShouldBindJSON(&data); err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(5001, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
list, err := service_k8s.EditSvc(data.ID, data) | ||
if err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
code := msg.SUCCSE | ||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code))) | ||
} | ||
|
||
func DelSvc(c *gin.Context) { | ||
|
||
var data k8s.Svc | ||
if err := c.ShouldBindJSON(&data); err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(5001, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
code, err := service_k8s.Delsvs(data.ID, data.Namespace, data.Name) | ||
if err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, "删除成功", msg.GetErrMsg(code))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package k8s | ||
|
||
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
type Deployment struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
Namespace string `json:"namespace"` | ||
Replicas int32 `json:"replicas"` | ||
Labels Labels `json:"labels"` | ||
Annotations Annotations `json:"annotations"` | ||
Strategy Strategy `json:"strategy"` | ||
Selector Selector `json:"selector"` | ||
Pods []Pod `json:"template"` | ||
CreationTimestamp metav1.Time `json:"creation_timestamp"` | ||
} | ||
|
||
type Strategy struct { | ||
Type string `json:"type"` | ||
RollingUpdate RollingUpdateStrategy `json:"rollingUpdate"` | ||
} | ||
|
||
type RollingUpdateStrategy struct { | ||
MaxSurge int `json:"maxSurge"` | ||
MaxUnavailable int `json:"maxUnavailable"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package k8s | ||
|
||
type Quota struct { | ||
LimitsCpu string `json:"limits_cpu"` | ||
LimitsMem string `json:"limits_mem"` | ||
RequestsCpu string `json:"requests_cpu"` | ||
RequestsMem string `json:"requests_mem"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package k8s | ||
|
||
type Selector map[string]string | ||
|
||
type Svc struct { | ||
ID int `json:"id"` | ||
|
||
Name string `json:"name"` | ||
Namespace string `json:"namespace"` | ||
Type string `json:"type"` | ||
ClusterIp string `json:"cluster_ip"` | ||
Ports []string `json:"ports"` | ||
Selector Selector `json:"selector"` | ||
Labels Labels `json:"labels"` | ||
Annotations Annotations `json:"annotations"` | ||
EndPoints []string `json:"end_points"` | ||
NodePort int32 `json:"nodePort"` // 添加了 NodePort 字段 | ||
Protocol string `json:"protocol"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package service_k8s | ||
|
||
import ( | ||
"cmdb-ops-flow/models/k8s" | ||
"context" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
) | ||
|
||
func GetDeployments(id int, nsName string) ([]k8s.Deployment, error) { | ||
clientSet, err := k8s.GetKubeConfig(id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
deploymentList, err := clientSet.AppsV1().Deployments(nsName).List(context.TODO(), metav1.ListOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var deployments []k8s.Deployment | ||
|
||
for _, deployment := range deploymentList.Items { | ||
|
||
podList, err := clientSet.CoreV1().Pods(nsName).List(context.TODO(), metav1.ListOptions{ | ||
LabelSelector: labels.SelectorFromSet(deployment.Spec.Selector.MatchLabels).String(), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// 提取所需的 Pod 信息 | ||
var pods []k8s.Pod | ||
for _, item := range podList.Items { | ||
containers := make([]k8s.Container, 0, len(item.Status.ContainerStatuses)) | ||
for _, containerStatus := range item.Status.ContainerStatuses { | ||
container := k8s.Container{Name: containerStatus.Name, Ready: containerStatus.Ready, RestartCount: int(containerStatus.RestartCount), | ||
Image: containerStatus.Image, ImageId: containerStatus.ImageID, ContainerId: containerStatus.ContainerID} | ||
containers = append(containers, container) | ||
} | ||
pod := k8s.Pod{Name: item.Name, Namespace: item.Namespace, Status: string(item.Status.Phase), CreationTimestamp: item.CreationTimestamp.Time, | ||
Containers: containers, Labels: item.Labels, Annotations: item.Annotations, PodIp: item.Status.PodIP, NodeName: item.Spec.NodeName} | ||
pods = append(pods, pod) | ||
} | ||
|
||
// 将 Kubernetes 的 Deployment 转换为自定义的 Deployment 结构 | ||
deploy := k8s.Deployment{ | ||
Name: deployment.ObjectMeta.Name, | ||
Namespace: deployment.ObjectMeta.Namespace, | ||
Replicas: *deployment.Spec.Replicas, // 示例,假设 Replicas 是一个指针 | ||
Labels: deployment.ObjectMeta.Labels, | ||
Annotations: deployment.ObjectMeta.Annotations, | ||
Strategy: k8s.Strategy{ // 使用 k8s.Strategy 而不是直接的 Strategy | ||
Type: string(deployment.Spec.Strategy.Type), // 将 DeploymentStrategyType 转为字符串 | ||
RollingUpdate: k8s.RollingUpdateStrategy{ // 同样使用 k8s.RollingUpdateStrategy | ||
MaxSurge: deployment.Spec.Strategy.RollingUpdate.MaxSurge.IntValue(), | ||
MaxUnavailable: deployment.Spec.Strategy.RollingUpdate.MaxUnavailable.IntValue(), | ||
}, | ||
}, | ||
Selector: k8s.Selector(deployment.Spec.Selector.MatchLabels), // 转换为你自己定义的 Selector 类型 | ||
Pods: pods, | ||
CreationTimestamp: deployment.ObjectMeta.CreationTimestamp, | ||
} | ||
|
||
deployments = append(deployments, deploy) | ||
} | ||
|
||
return deployments, nil | ||
} |
Oops, something went wrong.