关于kubernetes:clientgo-gin的简单整合二list列表相关进一步操作

2次阅读

共计 5109 个字符,预计需要花费 13 分钟才能阅读完成。

背景

上一步实现了 client-go gin 的简略整合一(list 列表相干操作), 实现了简略的 namespace deployment service 的 name 的输入!当初我想输入更多的内容,也过后深刻一下 kubernetes 这些根底!

1. client-go gin 的简略整合二(list 列表相干进一步操作)

1. 从 namespace 开始

[root@zhangpeng ~]# kubectl get ns -o wide


首先我想输入 namespace 的 STATUS 状态和 AGE!
以 develop 为例看一下还有什么想输入的信息

[root@zhangpeng ~]# kubectl get ns develop -o yaml


creationTimestamp labels status状态在这里也是能够体现的!
入手吧
src/service/Namespace.go

package service

import (
    "context"
    "github.com/gin-gonic/gin"
    . "k8s-demo1/src/lib"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "time"
)

type Time struct {time.Time `protobuf:"-"`}
type Namespace struct {
    Name       string
    CreateTime Time `json:"CreateTime"`
    Status     string
    Labels     map[string]string
}

func ListNamespace(g *gin.Context) {ns, err := K8sClient.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
    if err != nil {g.Error(err)
        return
    }
    ret := make([]*Namespace, 0)
    for _, item := range ns.Items {
        ret = append(ret, &Namespace{
            Name:       item.Name,
            CreateTime: Time(item.CreationTimestamp),
            Status:     string(item.Status.Phase),
            Labels:     item.Labels,
        })

    }
    g.JSON(200, ret)
    return
}

注:毕竟老手不太会解决数据,就做了如下解决,先能展现出本人先要的数据。前面再作深刻的学习!


同理 status

然而我这里偷懒了 …… 间接搞了一个 string。短期来看应该没有什么问题吧?

同理 labels map[string]string


运行 main.go,main.go 仍然是原来的没有进行其余批改如下:

package main

import (
    "github.com/gin-gonic/gin"
    "k8s-demo1/src/service"
)

func main() {r := gin.Default()
    r.GET("/", func(context *gin.Context) {context.JSON(200, "hello")
    })
    r.GET("/namespaces", service.ListNamespace)
    r.GET("/deployments", service.ListDeployment)
    r.GET("/service", service.ListService)
    r.Run()}


浏览器拜访:http://127.0.0.1:8080/namespaces,如下

根本实现,AGE 还没有想好怎么展示,是不是要算工夫戳减去 CreateTime? 前面再去钻研吧 ……

2. 持续 deployment 的进一步深刻

[root@zhangpeng ~]# kubectl get deployment -o wide


恩 起码的是要把这些根本输入的:READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR!
具体看一眼 nginx deployment 看一眼还有什么要输入的:

[root@zhangpeng ~]# kubectl get deployment nginx -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2022-05-01T13:47:52Z"
  generation: 1
  labels:
    app: nginx
    env: dev
  name: nginx
  namespace: default
  resourceVersion: "16449910"
  uid: ec1423a5-1268-40ea-bbf5-15576a332755
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
      env: dev
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
        env: dev
      name: nginx
    spec:
      containers:
      - image: nginx:1.16.1
        imagePullPolicy: IfNotPresent
        name: nginx
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2022-05-01T13:48:10Z"
    lastUpdateTime: "2022-05-01T13:48:10Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2022-05-01T13:47:52Z"
    lastUpdateTime: "2022-05-01T13:48:10Z"
    message: ReplicaSet "nginx-7b5d9df6b8" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

这还没有想好须要什么,就先依照 kubectl get deployment -o wide 的输入整一下了

src/service/Deployment.go

package service

import (
    "context"
    "github.com/gin-gonic/gin"
    . "k8s-demo1/src/lib"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type Deployment struct {
    Name                string
    Replicas            int32
    AvailableReplicas   int32
    UnavailableReplicas int32
    Images              string
    Labels              map[string]string
}

func ListDeployment(g *gin.Context) {ns := g.Query("ns")

    dps, err := K8sClient.AppsV1().Deployments(ns).List(context.Background(), metav1.ListOptions{})
    if err != nil {g.Error(err)
    }
    ret := make([]*Deployment, 0)
    for _, item := range dps.Items {

        ret = append(ret, &Deployment{
            Name:                item.Name,
            Replicas:            item.Status.Replicas,
            AvailableReplicas:   item.Status.AvailableReplicas,
            UnavailableReplicas: item.Status.UnavailableReplicas,
            Images:              item.Spec.Template.Spec.Containers[0].Image,
            Labels:              item.Labels,
        })

    }
    g.JSON(200, ret)
    return
}


Images 也没有思考其余的,多个镜像或者其余情况,READY CONTAINERS还没有想好怎么展示!
go run main.go
http://127.0.0.1:8080/deployments

http://127.0.0.1:8080/deployments?ns=kube-system

3. 同理 service

[root@zhangpeng .kube]# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE   SELECTOR
kubernetes   ClusterIP   192.168.0.1   <none>        443/TCP   55d   <none>

根本就输入 NAME TYPE CLUSTER-IP EXTERNAL-IP PORTS SELECTOR

package service

import (
    "context"
    "github.com/gin-gonic/gin"
    . "k8s-demo1/src/lib"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type Service struct {
    Name       string
    Type       string
    ClusterIp  string
    ExternalIp []string
    Ports      []string
    Select     map[string]string
}

func ListService(g *gin.Context) {ns := g.Query("ns")
    svc, err := K8sClient.CoreV1().Services(ns).List(context.Background(), metav1.ListOptions{})
    if err != nil {g.Error(err)
        return
    }
    ret := make([]*Service, 0)
    for _, item := range svc.Items {
        ret = append(ret, &Service{
            Name:       item.Name,
            Type:       string(item.Spec.Type),
            ClusterIp:  item.Spec.ClusterIP,
            ExternalIp: item.Spec.ExternalIPs,
            Select:     item.Spec.Selector,
        })

    }
    g.JSON(200, ret)
    return
}

留神:ExternalIp貌似会有问题 都是 null 还没有先好怎么取数据 Type 偷懒了间接 string 了!ports也没有想到怎么获得
go run main.go
http://127.0.0.1:8080/service

http://127.0.0.1:8080/service?ns=default

第一局部 list 总算能看一下了 …. 除了没有实现的 ……

总结

  1. 算是基本上实现了 list 接口的自定义显示?
  2. goland 神器是不错,查看源码,惋惜还不能深刻读懂
  3. service ports ExternalIp,deployment READY CONTAINERS 展示,还有 image 多镜像的解决?
  4. AGE 的计算
正文完
 0