Skip to content

Commit

Permalink
k8s的node、pods、svc、namespace、deployment的查看和一些的基础增删改
Browse files Browse the repository at this point in the history
  • Loading branch information
qishu321 committed Aug 23, 2023
1 parent 3082c00 commit 73167af
Show file tree
Hide file tree
Showing 14 changed files with 517 additions and 10 deletions.
25 changes: 25 additions & 0 deletions api/apis_k8s/deployment.go
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)))
}
37 changes: 30 additions & 7 deletions api/apis_k8s/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ func GetallNamespace(c *gin.Context) {
}

func AddNamespace(c *gin.Context) {
//var data struct {
// ID int `json:"id"`
//}
//if err := c.ShouldBindJSON(&data); err != nil {
// c.JSON(http.StatusOK, (&result.Result{}).Error(500, err.Error(), msg.GetErrMsg(msg.ERROR)))
// return
//}

var data k8s.NameSpace
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(5001, err.Error(), msg.GetErrMsg(msg.ERROR)))
Expand All @@ -47,3 +41,32 @@ func AddNamespace(c *gin.Context) {
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))
}
func DelNamespace(c *gin.Context) {

var data k8s.NameSpace
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.DelNamespace(data.ID, 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)))
}
func EditNamespace(c *gin.Context) {

var data k8s.NameSpace
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.EditNamespace(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)))
}
17 changes: 16 additions & 1 deletion api/apis_k8s/pods.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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"
Expand All @@ -16,7 +17,21 @@ func GetAllPods(c *gin.Context) {
c.JSON(http.StatusOK, (&result.Result{}).Error(400, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service_k8s.GetPods(data.ID)
list, err := service_k8s.GetallPods(data.ID)
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 GetPods(c *gin.Context) {
var data k8s.Pod
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.GetPods(data.ID, data.Namespace)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
Expand Down
71 changes: 71 additions & 0 deletions api/apis_k8s/svc.go
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)))
}
26 changes: 26 additions & 0 deletions models/k8s/deployment.go
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"`
}
3 changes: 3 additions & 0 deletions models/k8s/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
type Labels map[string]string
type Annotations map[string]string

//type LimitRange map[string]string
//type ResourceQuota map[string]string

type Node struct {
Name string `json:"name"`
Status string `json:"status"`
Expand Down
6 changes: 5 additions & 1 deletion models/k8s/pods.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package k8s

import "time"
import (
"time"
)

type Pod struct {
ID int `json:"id"`

Name string `json:"name"`
PodIp string `json:"pod_ip"`
Status string `json:"status"`
Expand Down
8 changes: 8 additions & 0 deletions models/k8s/quotas.go
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"`
}
19 changes: 19 additions & 0 deletions models/k8s/svc.go
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"`
}
11 changes: 11 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,21 @@ func InitRouter() {
api_k8s.POST("/kube/config/addconfig", api.AddKubeConfig)
api_k8s.POST("/kube/config/getconfig", api.GetKubeConfig)
api_k8s.POST("/kube/config/getallPods", apis_k8s.GetAllPods)
api_k8s.POST("/kube/config/getPods", apis_k8s.GetPods)

api_k8s.POST("/kube/config/getallNodes", apis_k8s.GetAllNodes)

api_k8s.POST("/kube/config/getDeployment", apis_k8s.GetDeployment)

api_k8s.POST("/kube/config/getallNamespace", apis_k8s.GetallNamespace)
api_k8s.POST("/kube/config/addNamespace", apis_k8s.AddNamespace)
api_k8s.POST("/kube/config/editNamespace", apis_k8s.EditNamespace)
api_k8s.POST("/kube/config/delNamespace", apis_k8s.DelNamespace)

api_k8s.POST("/kube/config/getallSvc", apis_k8s.GetallSvc)
api_k8s.POST("/kube/config/delSvc", apis_k8s.DelSvc)
api_k8s.POST("/kube/config/addSvc", apis_k8s.AddSvc)
api_k8s.POST("/kube/config/editSvc", apis_k8s.EditSvc)

api_k8s.POST("/kube/config/getVersion", apis_k8s.GetVersion)

Expand Down
69 changes: 69 additions & 0 deletions service/service_k8s/deployment.go
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
}
Loading

0 comments on commit 73167af

Please sign in to comment.