关于云原生:clientgo-gin的简单整合一list列表相关操作

24次阅读

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

背景:

实现了 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 依赖

PS C:\Users\zhangpeng\GolandProjects\k8s-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


注:linux 环境操作省略 …… 如报错短少其余依赖 go get 自行依照提醒获取装置!

2. 将连贯客户端独自封装成一个办法文件

将后面测试环境写的独自的文件拆分。把客户端独自封装成一个独自文件:
lib/K8sClient.go

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

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

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

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

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

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

1. namespace:

http://127.0.0.1:8080/namespaces

2. deployment

http://127.0.0.1:8080/deployments

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

3. service

http://127.0.0.1:8080/service

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

根本实现了集体的目标。然而输入的内容太多了,很多货色不须要,想要进一步优化输入本人先要的信息!

2. 更进一步的 - 定制化输入须要的内容

1. 对于 namespace

namespace 我只想输入空间 name! 创立一个 struct, 并 make 创立一个切片:
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"
)

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
}


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

2. 对于 deployment

参照 namespace 设置 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}

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

3. 对于 service

src/service/Service.go 如下

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

反思:

1. 切片的应用,指针的应用 ……
2. 定制化输入更多的相干配置?比方 deployments 中镜像,正本数。namespace 中 label?创立工夫?
3.gin.Context 该让我怎么去了解 ……

正文完
 0