天天看點

client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

背景

上一步完成了client-go gin的簡單整合一(list清單相關操作),實作了簡單的namespace deployment service的name的輸出!現在我想輸出更多的内容,也當時深入一下kubernetes這些基礎!

1. client-go gin的簡單整合二(list清單相關進一步操作)

1. 從namespace開始

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

           
client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

首先我想輸出namespace的STATUS狀态和AGE!

以develop為例看一下還有什麼想輸出的資訊

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

           
client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

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
}
           

注:畢竟新手不太會處理資料,就做了如下處理,先能展現出自己先要的資料。後面再作深入的學習!

client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結
client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

同理status

client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

但是我這裡偷懶了......直接搞了一個string。短期來看應該沒有什麼問題吧?

client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

同理labels map[string]string

client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結
client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

運作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()
}

           
client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

浏覽器通路:http://127.0.0.1:8080/namespaces

如下:

client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

基本完成,AGE還沒有想好怎麼展現,是不是要算時間戳減去CreateTime?後面再去研究吧......

2.繼續deployment的進一步深入

[root@zhangpeng ~]# kubectl get deployment -o wide
           
client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

恩 起碼的是要把這些基本輸出的: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的輸出整一下了

client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

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
}
           
client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

Images也沒有考慮其他的,多個鏡像或者其他狀況,READY CONTAINERS還沒有想好怎麼展現!

go run main.go

http://127.0.0.1:8080/deployments

client-go gin的簡單整合二(list清單相關進一步操作)背景1. client-go gin的簡單整合二(list清單相關進一步操作)總結

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>

           
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
}
           

總結

  1. 算是基本上實作了list接口的自定義顯示?
  2. goland神器是不錯,檢視源碼,可惜還不能深入讀懂
  3. service ports ExternalIp,**deployment **READY CONTAINERS展現,還有image多鏡像的處理?
  4. AGE的計算