client-go gin的简单整合一(list列表相关操作)

2022-05-06 10:27:52 浏览数 (1)

背景:

完成了client-go连接kubernetes集群-delete相关操作,稍微看过一些B站go圈里最会写js的奇淼 的go 与gin的视频,还有沈叔的一些课程:https://www.jtthink.com/。一般都是习惯先动手的,自己动手操作,有问题就看沈叔的视频与解决思路!

1. client-go gin的简单整合一

注:以下操作环境可能有些许区别(在家写东西用的windows,公司的办公环境个人安装了一台rocky linux)。一下所有路径为相对路径在k8s-demo1项目目录下!

1. go get 安装gin依赖

代码语言:txt复制
PS C:UserszhangpengGolandProjectsk8s-demo1> go get github.com/gin-gonic/gin
go get: added github.com/gin-contrib/sse v0.1.0
go get: added github.com/gin-gonic/gin v1.7.7
go get: added github.com/go-playground/locales v0.13.0
go get: added github.com/go-playground/universal-translator v0.17.0
go get: added github.com/go-playground/validator/v10 v10.4.1
go get: added github.com/leodido/go-urn v1.2.0
go get: added github.com/mattn/go-isatty v0.0.12
go get: added github.com/ugorji/go/codec v1.1.7
image.pngimage.png

注:linux环境操作省略......如报错缺少其他依赖go get自行按照提示获取安装!

2. 将连接客户端单独封装成一个方法文件

将前面测试环境写的单独的文件拆分。把客户端单独封装成一个单独文件:

src/lib/K8sClient.go

代码语言:txt复制
package lib

import (
	"flag"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/client-go/util/homedir"
	"path/filepath"
)

var K8sClient *kubernetes.Clientset

func init() {
	var kubeconfig *string
	if home := homedir.HomeDir(); home != "" {
		kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
	} else {
		kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
	}
	flag.Parse()
	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		panic(err.Error())
	}

	// create the clientset
	c, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}
	K8sClient = c
}

3. 创建service目录创建namespace deployment service对应service文件

以namespace deployment service为例:

src/service/Namespace.go

代码语言:txt复制
package service

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

func ListNamespace(g *gin.Context) {
	ns, err := K8sClient.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
	if err != nil {
		g.Error(err)
		return
	}
	g.JSON(200, ns)
}

src/service/Deployment.go

代码语言:txt复制
package service

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

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)
	}
	g.JSON(200, dps)
	return
}

src/service/Service.go

代码语言:txt复制
package service

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

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
	}
	g.JSON(200, svc)
}

注:正常的应该是lib.K8sClient引用,import设置为了 . "k8s-demo1/src/lib"!

4.编辑main.go 设置路由

main.go

代码语言:txt复制
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()
}

5. 运行main.go,浏览器访问是否正常返回

可以命令行控制台go run main.go 也可以goland直接run main.go

image.pngimage.png

注:此集群非前面的ack集群,为本地内网一个测试集群!故namespace等相关信息输出并不与前面部分对应

1. namespace:

http://127.0.0.1:8080/namespaces

image.pngimage.png

2. deployment

http://127.0.0.1:8080/deployments

image.pngimage.png

http://127.0.0.1:8080/deployments?ns=default 指定命名空间

image.pngimage.png

3. service

http://127.0.0.1:8080/service

image.pngimage.png

http://127.0.0.1:8080/service?ns=default 指定命名空间

image.pngimage.png

基本实现了个人的目的。但是输出的内容太多了,很多东西不需要,想要进一步优化输出自己先要的信息!

2. 更进一步的-定制化输出需要的内容

1.关于namespace

namespace我只想输出空间name! 创建一个struct,并make创建一个切片:

src/service/Namespace.go

代码语言:txt复制
package service

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

type Namespace struct {
	Name 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,
		})

	}
	g.JSON(200, ret)
	return
}
image.pngimage.png

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

image.pngimage.png

2. 关于deployment

参照namespace设置src/service/Deployment.go

代码语言:txt复制
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
}

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,
		})

	}
	g.JSON(200, ret)
	return
}

运行main.go 访问http://127.0.0.1:8080/deployments http://127.0.0.1:8080/deployments?ns=default

image.pngimage.png
image.pngimage.png

3. 关于service

src/service/Service.go如下

代码语言:txt复制
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
}

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,
		})

	}
	g.JSON(200, ret)
	return
}

运行main.go 访问http://127.0.0.1:8080/service http://127.0.0.1:8080/service?ns=default

image.pngimage.png
image.pngimage.png

反思:

1.切片的使用,指针的使用......

2.定制化输出更多的相关配置?比如deployments中镜像,副本数。namespace中label?创建时间?

3.gin.Context该让我怎么去理解......

0 人点赞