前端: Vue+element plus
后端: go+gin
kubernetes v1.24.2
golang v1.18.3
后端代码地址GitHub - yunixiangfeng/k8s-platform: K8s管理系统后端: go+gin
Go 快速入门
Gin Web框架
K8s管理系统项目实战[API开发]
项目背景,整体设计,Client-go,框架搭建
一、项目背景
随着容器技术的广泛应用,kubernetes逐渐成为业内的核心技术,是容器编排技术的首选工具。而k8s管理平台在日常的容器维护中也发挥着举足轻重的作用,但随着k8s的定制化功能越来越多,dashboard已经无法满足日常的维护需求,且dashboard的源码学习成本较高,抽象程度较高,二次发成本也就比较高。
本项目使用当下较主流的前端vue+element plus及后端go+gin框架,打造与dashboard对标的k8s管理功能,且可定制化程度高,可根据自身需求,进行灵活定制开发。
二、前后端分离概述
前后端分离已成为互联网项目开发的业界标准使用方式,通过nginx+tomcat的方式( 也可以中间加一个node.js)有效的进行解耦,并且前后端分离会为以后的大型分布式架构、弹性计算架构、微服务架构、多端化服务( 多种客户端,例如:浏览器,车载终端,安卓,IOS等等)打下坚实的基础。这个步要是系统架构从进化成人的必经之路。
前后分离的优势 :
1.可以实现真正的前后端解耦,前端服务器使用nginx。
2.发现bug,可以快速定位是谁的问题,不会出现互相踢皮球的现象
3.在大并发情况下,可以同时水平扩展前后端服务器
4.增加代码的维护性&易读性(前后端耦在一起的代码读起来相当费劲 )
5.提升开发效率,因为可以前后端并行开发,而不是像以前的强依赖
三、功能设计
四、client-go介绍
1、简介
client-go是kubernetes官方提供的go语言的客户端库,go应用使用该库可以访问kubernetes的API Server,这样我们就能通过编程来对kubermetes资源进行增删改查操作:
除了提供丰富的API用于操kubernetes资源,client-go还为controller和operator提供了重要支持client-go的informer机制可以将controller关注的资源变化及时带给此controller,使controller能够及时响应变化。
通过client-go提供的客户端对象与kubernetes的API Server进行交豆,而client-go提供了以下四种客户端对象
(1)RESTClient:这是最基础的客户端对象,仅对HTTPRequest进行了封装,实现RESTFul风格API,这个对象的使用并不方便,因为很多参数都要使用者来设置,于是client-go基于RESTClient又实现了三种新的客户端对象;
(2)ClientSet;把Resoure和Version也封装成方法了,用起亲更简单直接,一个资源是一个客户端,多个资源就对应了多个客户端所以ClientSet就是多个客户端的集合了,这样就理解了,不过ClientSet只能访问内置资源,访问不了自定义资源
(3)DynamiClient;可以访问内置资源和自定义资源,个人感觉有点像Java的集合操作,拿出的内容是Object类型,按实际情况自己去做强制转换,当然了也会有强转失败的风险:
(4)DiscoveryClient: 用于发现kubernetes的API Server支持的Group、Version、Resources等信息;
// kubectl api-resources
2、代码示例
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
metav1 "k8s.io/apimeachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
func main() {
// 声明kubeconfig配置文件
var kubeconfig *string
// 获取home环境变量,拿到$HOME/.kube/config配置文件
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optinal) absolute path to the kubeconfig file")
} else {
// 否则就根据kubeconfig传到config的路径
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// 将kubeconfig格式化为rest.config类型
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// 通过config创建clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// 通过client-go sdk获取pods列表,命名空间是default
pods, err := clientset.CoreV1().Pods("default").List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
// 打印出pods列表的长度
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
}
3、常用方法
// 获取pod列表
podList, err := K8S.ClientSet.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
// 获取pod详情
pod, err := K8S.ClientSet.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.ListOptions{})
// 删除pod
err = K8S.ClientSet.CoreV1().Pods(namespace).Delete(context.TODO(), podName, metav1.DeleteOptions{})
// 更新pod(完整yaml)
pod, err = K8S.ClientSet.CoreV1().Pods(namespace).Update(context.TODO(), pod, metav1.UpdateOptions{})
// 获取deployment副本数
scale, err := K8S.ClientSet.AppsV1().Deployment(namespace).GetScale(context.TODO(), deploymentName, metav1.GetOption{})
// 创建deployment
deployment, err = K8S.ClientSet.AppsV1().Deployments(data.Namespace).Create(context.TODO(), deployment, metav1.CreateOption{})
// 更新deployment(部分yaml)
deployment, err = K8S.ClientSet.AppsV1().Deployments(namespace).Patch(context.TODO(), deploymentName, "application/strategic-merge-patch-json", patchByte, metav1.PatchOption{})
初始化
创建项目
D:\Workspace\Go\src\k8s-platform
D:\Workspace\Go\src\k8s-demo
main.go
安装模块
go get k8s.io/client-go/tools/clientcmd
go get k8s.io/api/core/v1
go get k8s.io/apimachinery/pkg/apis/meta/v1
go get github.com/gin-gonic/gin
go get github.com/wonderivan/logger
go get gorm.io/gorm
go get gorm.io/driver/sqlite
go get gorm.io/driver/mysql
PS D:\Workspace\Go\src\k8s-platform> go mod init k8s-platform
go: creating new go.mod: module k8s-platform
go: to add module requirements and sums:
go mod tidy
main.go
package main
import (
"context"
"fmt"
"os"
metav1 "k8s.io/apimeachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
func main() {
// // 声明kubeconfig配置文件
// var kubeconfig *string
// // 获取home环境变量,拿到$HOME/.kube/config配置文件
// if home := homeDir(); home != "" {
// kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optinal) absolute path to the kubeconfig file")
// } else {
// // 否则就根据kubeconfig传到config的路径
// kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
// }
// flag.Parse()
// 将kubeconfig格式化为rest.config类型
// config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
config, err := clientcmd.BuildConfigFromFlags("", "C:\\Users\\Administrator\\.kube\\config")
if err != nil {
panic(err.Error())
}
// 通过config创建clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// 通过client-go sdk获取pods列表,命名空间是default
podList, err := clientset.CoreV1().Pods("default").List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
// // 打印出pods列表的长度
// fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
for _, pod := range podList.Items {
fmt.Println(pod.Name, pod.Namespace)
}
}
五、项目目录结构
项目目录含义
目录名 作用
config 定义全局配置,如:监听地址,管理员账号等
controller controller层,定义路由规则及接口入参和响应
service 服务层,处理接口的业务逻辑
dao 数据库操作,包含数据的增删改查
model 定义数据库表的字段
db 用于初始化数据库连接及配置
middle 中间件层,添加全局的逻辑处理,如跨域,jwt验证等
utils 工具目录,定义常用工具,如token解析,文件操作等
go.mod 定义项目的依赖包以及版本
main.go 项目主入口,main函数
创建目录
controller config service dao db model middle utils
六、框架搭建
1、创建项目
// mkdir tiga
// PS D:\Workspace\Go\src\tiga> go mod init tiga
mkdir k8s-platform
PS D:\Workspace\Go\src\k8s-platform> go mod init tiga
2、初始化gin工程
(1)创建项目目录结构
(2)创建config/config.go文件,配置启动监听端口
定义监听的常量
package config
const (
ListenAddr = "0.0.0.0:9090"
)
(3)创建controller/router.go文件,初始化api路由规则,编写测试api接口
package controller
import (
"net/http"
"github.com/gin-gonic/gin"
)
// // 初始化router类型对象,首字母大写,用于跨包调用
// var Router router
// // 声明一个router的结构体
// type router struct{}
// func (r *router) InitApiRouter(router *gin.Engine) {
// router.GET("/", Index)
// }
// func Index(ctx *gin.Context) {
// ctx.JSON(200, gin.H{
// "code": 200,
// "msg": "In index",
// })
// }
// 实例化router结构体,可使用该对象点出首字母大写的方法(包外调用)
var Router router
// 创建router的结构体
type router struct{}
// 初始化路由规则,创建测试api接口
func (r *router) InitApiRouter(router *gin.Engine) {
router.GET("/testapi", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"msg": "testapi success!",
"data": nil,
})
})
}
(4)main.go文件中,启动gin程序
package main
import (
"k8s-platform/config"
"k8s-platform/controller"
"github.com/gin-gonic/gin"
)
func main() {
// 初始化gin对象
r := gin.Default()
// 初始化路由规则
controller.Router.InitApiRouter(r)
// gin程序启动
r.Run(config.ListenAddr)
}
启动服务
PS D:\Workspace\Go\src\k8s-platform> go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /testapi --> k8s-platform/controller.(*router).InitApiRouter.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on 0.0.0.0:9090
使用apifox测试
3、初始化k8s Client
(1)在config.go中添加kubeconfig文件路径
定义KubeConfig常量
package config
const (
ListenAddr = "0.0.0.0:9090"
KubeConfig = "C:\\Users\\Administrator\\.kube\\config"
)
(2)创建service/init.go文件,初始化k8s client
package service
import (
"k8s-platform/config"
"github.com/wonderivan/logger"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
// 用于初始化k8s client
var K8s k8s
type k8s struct {
Clientset *kubernetes.Clientset
}
// 初始化k8s
func (k *k8s) Init() {
// 将kubeconfig格式化为rest.config类型
// config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
config, err := clientcmd.BuildConfigFromFlags("", config.KubeConfig)
if err != nil {
panic("获取K8s配置失败:" + err.Error())
} else {
logger.Info("获取K8s配置成功!")
}
// 通过config创建clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic("创建K8s client失败:" + err.Error())
} else {
logger.Info("创建K8s client 成功!")
}
k.Clientset = clientset
}
main中初始化k8s client
(3)main.go中添加k8s client启动项
package main
import (
"k8s-platform/config"
"k8s-platform/controller"
"k8s-platform/service"
"github.com/gin-gonic/gin"
)
func main() {
// 初始化k8s client
service.K8s.Init() // 可以使用service.K8s.clientset 进行跨包调用
// 初始化gin对象
r := gin.Default()
// 初始化路由规则
controller.Router.InitApiRouter(r)
// gin程序启动
r.Run(config.ListenAddr)
}
4、测试API接口
浏览器可以正常访问到url
http://localhost:9090/testapi
{"data":null,"msg":"testapi success!"}
API开发:工作负载资源
七、API开发
1、开发&响应流程
Model层: 实体层>> 表数据与类的映射关系,这里的类就是strcut
Dao层: 持久层/数据访问层>> 主要与数据库交互,增删改查
Service层: 业务层 >> 控制业务逻辑,主要处理业务模块的功能逻辑
Controller层: 控制层>> 接收请求参数,调用不同的Service层代码来控制业务流程
2、工作负载
2.1 数组的排序、过滤、分页
service/dataselector.go用来处理数组的排序,过滤,分页
(1)定义数据结构
package service
import "time"
// dataselector用于排序,过滤,分页的数据类型
type dataSelector struct {
GenericDataList []DataCell
DataSelect *DataSelectQuery
}
// DataCell接口,用于各种资源List的类型转换,转换后可以使用dataselector的排序,过滤,分页方法
type DataCell interface {
GetCreation() time.Time
GetName() string
}
// DataSelectQuery 定义过滤和分页的结构体,过滤:Name 分页:Limit和Page
type DataSelectQuery struct {
Filter *FilterQuery
Paginate *PaginateQuery
}
// FilterQuery用于查询 过滤:Name
type FilterQuery struct {
Name string
}
// 分页:Limit和Page Limit是单页的数据条数,Page是第几页
type PaginateQuery struct {
Page int
Limit int
}
(2)排序
自定义类型排序参考文档:https://segmentfault.com/a/1190000008062661
// 实现自定义的排序方法,需要重写Len,Swap,Less方法
// Len用于获取数组的长度
func (d *dataSelector) Len() int {
return len(d.GenericDataList)
}
// Swap用于数据比较大小后的位置变更
func (d *dataSelector) Swap(i, j int) {
d.GenericDataList[i], d.GenericDataList[j] = d.GenericDataList[j], d.GenericDataList[i]
}
// Less用于比较大小
func (d *dataSelector) Less(i, j int) bool {
return d.GenericDataList[i].GetCreation().Before(d.GenericDataList[j].GetCreation())
}
// 重写以上三个方法,用sort.Sort 方法触发排序
func (d *dataSelector) Sort() *dataSelector {
sort.Sort(d)
return d
}
(3)过滤
// Filter方法用于过滤,比较数据Name属性,若包含则返回
func (d *dataSelector) Filter() *dataSelector {
if d.DataSelect.Filter.Name == "" {
return d
}
filtered := []DataCell{}
for _, value := range d.GenericDataList {
// 定义是否匹配的标签变量,默认是匹配的
matches := true
objName := value.GetName()
if !strings.Contains(objName, d.DataSelect.Filter.Name) {
matches = false
continue
}
if matches {
filtered = append(filtered, value)
}
}
d.GenericDataList = filtered
return d
}
(4)分页
// Paginate分页,根据Limit和Page的传参,取一定范围内的数据返回
func (d *dataSelector) Paginate() *dataSelector {
limit := d.DataSelect.Paginate.Limit
page := d.DataSelect.Paginate.Page
// 验证参数合法,若参数不合法,则返回所有数据
if limit <= 0 || page <= 0 {
return d
}
// 举例:25个元素的数组,limit是10, page是3,startIndex是20,endIndex是29(实际上endIndex是24)
startIndex := limit * (page - 1)
endIndex := limit*page - 1
// 处理最后一页,这时候就把endIndex由30改为25了
if len(d.GenericDataList) < endIndex {
endIndex = len(d.GenericDataList) - 1
}
d.GenericDataList = d.GenericDataList[startIndex:endIndex]
return d
}
(5)定义podCell类型,实现DataCell接口,用于类型转换
// 定义podCell, 重写GetCreation和GetName方法后,可以进行数据转换
// covev1.Pod --> podCell --> DataCell
// appsv1.Deployment --> deployCell --> DataCell
type podCell corev1.pod
// 重写DataCell接口的两个方法
func (p podCell) GetCreation() time.Time {
return p.CreationTimestamp.Time
}
func (p podCell) GetName() string {
return p.Name()
}
service/pod.go
(6)定义DataCell到Pod类型转换的方法
断言:判断一个变量是否属于某一种类型,前提是这一类型实现了这一变量类型接口
// 类型转换方法corev1.Pod --> DataCell,DataCell-->corev1.Pod
func (p *pod) toCells(pods []corev1.Pod) []DataCell {
cells := make([]DataCell, len(pods))
for i := range pods {
cells[i] = podCell(pods[i])
}
return cells
}
func (p *pod) fromCells(cells []DataCell) []corev1.Pod {
pods := make([]corev1.Pod, len(cells))
for i := range cells {
// cells[i].(podCell)是将DataCell类型转换成podCell
pods[i] = corev1.Pod(cells[i].(podCell))
}
return pods
}
2.2 Pod 获取Pod信息
service/pod.go
Pod封装转换
(1)定义数据类型
var Pod pod
type pod struct{}
// 定义列表的返回内容,Items是pod元素列表,Total是元素数量
type PodsResp struct {
Total int `json:"total"`
Items []corev1.Pod `json:"items`
}
(2)获取pod列表
支持过滤,排序,分页
// 获取pod列表,支持过滤、排序、分页
func (p *pod) GetPods(filterName, namespace string, limit, page int) (podsResp *PodsResp, err error) {
//context.TODO() 用于声明一个空的context上下文,用于List方法内设置这个请求超时
//metav1.ListOptions{} 用于过滤List数据,如label,field等
podList, err := K8s.Clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
logger.Info("获取Pod列表失败," + err.Error())
// 返回给上一层,最终返回给前端,前端捕获到后打印出来
return nil, errors.New("获取Pod列表失败," + err.Error())
}
// 实例化dataselector结构体,组装数据
selectableData := &dataSelector{
GenericDataList: p.toCells(podList.Items),
DataSelect: &DataSelectQuery{
Filter: &FilterQuery{Name: filterName},
Paginate: &PaginateQuery{
Limit: limit,
Page: page,
},
},
}
// 先过滤
filtered := selectableData.Filter()
total := len(filtered.GenericDataList)
// 排序和分页
data := filtered.Sort().Paginate()
// 将DataCell类型转成Pod
pods := p.fromCells(data.GenericDataList)
return &PodsResp{
Total: total,
Items: pods,
}, nil
}
// 类型转换方法corev1.Pod --> DataCell,DataCell-->corev1.Pod
func (p *pod) toCells(pods []corev1.Pod) []DataCell {
cells := make([]DataCell, len(pods))
for i := range pods {
cells[i] = podCell(pods[i])
}
return cells
}
func (p *pod) fromCells(cells []DataCell) []corev1.Pod {
pods := make([]corev1.Pod, len(cells))
for i := range cells {
// cells[i].(podCell)是将DataCell类型转换成podCell
pods[i] = corev1.Pod(cells[i].(podCell))
}
return pods
}
暂时到controller\pod.go
见(9)编写Controller层的pod代码
暂时到controller\router.go
配置router
import (
"github.com/gin-gonic/gin"
)
// 初始化router类型的对象,首字母大写,用于跨包调用
var Router router
// 声明一个router的结构体
type router struct{}
func (r *router) InitApiRouter(router *gin.Engine) {
router.
GET("/api/k8s/pods", Pod.GetPods)
}
启动服务,通过apifox测试
获取所有pod的信息
加了参数就可以对namespace,分页等进行过滤
get http://0.0.0.0:9090/api/k8s/pods?filter_name=&namespace=default&limit=10&page=1
PS C:\Users\Administrator\Desktop\k8s-platform> go run main.go
2023-05-06 15:51:46 [INFO] [C:/Users/Administrator/Desktop/k8s-platform/service/init.go:26] 获取K8s配置成功!
2023-05-06 15:51:46 [INFO] [C:/Users/Administrator/Desktop/k8s-platform/service/init.go:33] 创建K8s client 成功!
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /api/k8s/pods --> k8s-platform/controller.(*pod).GetPods-fm (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on 0.0.0.0:9090
2023-05-06 15:52:34 [INFO] [C:/Users/Administrator/Desktop/k8s-platform/service/pod.go:28] 获取Pod列表失败,Get "https://192.168.204.129:6443/api/v1/namespaces/default/pods": dial tcp 192.168.204.129:6443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
[GIN] 2023/05/06 - 15:52:34 | 500 | 21.0291897s | 127.0.0.1 | GET "/api/k8s/pods?filter_name=&namespace=default&limit=10&page=1"
不加参数就是获取到所有pod信息
get http://0.0.0.0:9090/api/k8s/pods
fix:修改代码错误
可以只过滤某一个pod
get http://0.0.0.0:9090/api/k8s/pods?filter_name=nginx&namespace=default&limit=10&page=1
(3)获取Pod详情
service/pod.go
func (p *pod) GetPodDetail(podName string, namespace string) (pod *corev1.Pod, err error) {
pod, err = K8s.ClientSet.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
logger.Error("获取Pod详情失败," + err.Error())
return nil, errors.New("获取Pod详情失败," + err.Error())
}
return pod, nil
}
(4)删除Pod
func (p *pod) DeletePod(podName string, namespace string) (err error) {
err = K8s.ClientSet.CoreV1().Pods(namespace).Delete(context.TODO(), podName, metav1.DeleteOptions{})
if err != nil {
logger.Error("删除Pod详情失败," + err.Error())
return errors.New("删除Pod详情失败," + err.Error())
}
return nil
}
(5)更新Pod
func (p *pod) UpdatePod(podName string, namespace, content string) (err error) {
var pod = &corev1.Pod{}
// 反序列化为Pod对象
err = json.Unmarshal([]byte(content), pod)
if err != nil {
logger.Error("反序列化失败," + err.Error())
return errors.New("反序列化失败," + err.Error())
}
// 更新pod
_, err = K8s.ClientSet.CoreV1().Pods(namespace).Update(context.TODO(), pod, metav1.UpdateOptions{})
if err != nil {
logger.Error("更新Pod失败," + err.Error())
return errors.New("更新Pod失败," + err.Error())
}
return nil
}
(6) 获取Pod中的容器名
func (p *pod) GetPodContainer(podName string, namespace string) (containers []string, err error) {
pod, err := p.GetPodDetail(podName, namespace)
if err != nil {
return nil, err
}
for _, container := range pod.Spec.Containers {
containers = append(containers, container.Name)
}
return containers, nil
}
(7)获取容器日志
// 获取Pod内容器日志
func (p *pod) GetPodLog(containerName string, podName string, namespace string) (log string, err error) {
//设置日志配置,容器名,获取内容的配置
lineLimit := int64(config.PodLogTailLine)
option := &corev1.PodLogOptions{
Container: containerName,
TailLines: &lineLimit,
}
// 获取一个request实例
req := K8s.ClientSet.CoreV1().Pods(namespace).GetLogs(podName, option)
// 发起stream连接,获取到Response.body
podLogs, err := req.Stream(context.TODO())
if err != nil {
logger.Error("更新Pod失败," + err.Error())
return "", errors.New("更新Pod失败," + err.Error())
}
defer podLogs.Close()
// 将Response.body 写入到缓存区,目的为了转换成string类型
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
logger.Error("复制podLog失败," + err.Error())
return "", errors.New("复制podLog失败," + err.Error())
}
return buf.String(), nil
}
设置config.PodLogTailLine
暂时到config\config.go
package config
const (
ListenAddr = "0.0.0.0:9090"
KubeConfig = "C:\\Users\\Administrator\\.kube\\config"
// tail的日志行数
// tail -n 2000
PodLogTailLine = 2000
)
(8)获取每个namespace的pod数量
// 获取每个namespace中pod的数量
func(p *pod) GetPodNumPerNp() (podsNps []*PodsNp, err error) {
//获取namespace列表
namespaceList, err := K8s.ClientSet.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, namespace := range namespaceList.Items {
//获取pod列表
podList, err := K8s.ClientSet.CoreV1().Pods(namespace.Name).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
//组装数据
podsNp := &PodsNp{
Namespace: namespace.Name,
PodNum: len(podList.Items),
}
//添加到podsNps数组中
podsNps = append(podsNps, podsNp)
}
return podsNps, nil
}
声明PodsNp struct
package service
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"k8s-platform/config"
"github.com/wonderivan/logger"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var Pod pod
type pod struct{}
// 定义列表的返回内容,Items是pod元素列表,Total是元素数量
type PodsResp struct {
Total int `json:"total"`
Items []corev1.Pod `json:"items"`
}
type PodsNp struct {
Namespace string
PodNum int
}
controller/pod.go
(9)编写Controller层的pod代码
package controller
import (
"k8s-platform/service"
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm/logger"
)
var Pod pod
type pod struct{}
// 获取Pod列表,支持分页、过滤、排序
func (p *pod) GetPods(ctx *gin.Context) {
// 处理入参
// 匿名结构体,用于定义入参,get请求为form格式,其他请求为json格式
params := new(struct {
FilterName string `form:"filter_name"`
Namespace string `form:"namespace"`
Limit int `form:"limit"`
Page int `form:"page"`
})
// form格式使用Bind方法,json格式使用ShouldBindJSON方法
if err := ctx.Bind(params); err != nil {
logger.Error("Bind绑定参数失败" + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": "Bind绑定参数失败" + err.Error(),
"data": nil,
})
return
}
data, err := service.Pod.GetPods(params.FilterName, params.Namespace, params.Limit, params.Page)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": "Bind绑定参数失败" + err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Pod列表成功",
"data": data,
})
}
获取pod详情
controller/pod.go
// 获取pod详情
func (p *pod) GetPodDetail(ctx *gin.Context) {
// 处理入参
// 匿名结构体,用于定义入参,get请求为form格式,其他请求为json格式
params := new(struct {
PodName string `form:"pod_name"`
Namespace string `form:"namespace"`
})
// form格式使用Bind方法,json格式使用ShouldBindJSON方法
if err := ctx.Bind(params); err != nil {
logger.Error("Bind绑定参数失败" + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": "Bind绑定参数失败" + err.Error(),
"data": nil,
})
return
}
data, err := service.Pod.GetPodDetail(params.PodName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Pod列表成功",
"data": data,
})
}
加一条路由规则
controller/router.go
func (r *router) InitApiRouter(router *gin.Engine) {
router.
GET("/api/k8s/pods", Pod.GetPods).
GET("/api/k8s/pod/detail", Pod.GetPodDetail)
}
删除pod
// 删除pod
func (p *pod) DeletePod(ctx *gin.Context) {
// 处理入参
// 匿名结构体,用于定义入参,get请求为form格式,其他请求为json格式
params := new(struct {
PodName string `form:"pod_name"`
Namespace string `form:"namespace"`
})
// form格式使用Bind方法,json格式使用ShouldBindJSON方法
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind绑定参数失败" + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": "Bind绑定参数失败" + err.Error(),
"data": nil,
})
return
}
err := service.Pod.DeletePod(params.PodName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "删除Pod成功",
"data": nil,
})
}
加一条路由规则
controller/router.go
// 初始化路由规则
func (r *router) InitApiRouter(router *gin.Engine) {
router.
GET("/api/k8s/pods", Pod.GetPods).
GET("/api/k8s/pod/detail", Pod.GetPodDetail).
POST("/api/k8s/pods", Pod.DeletePod)
}
关于Pod其他功能
更新pod
获取pod容器
获取pod中容器日志
获取每个namespace的pod数量
// 更新pod
func (p *pod) UpdatePod(ctx *gin.Context) {
params := new(struct {
PodName string `json:"pod_name"`
Namespace string `json:"namespace"`
Content string `json:"content"`
})
//PUT请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.Pod.UpdatePod(params.PodName, params.Namespace, params.Content)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "更新Pod成功",
"data": nil,
})
}
// 获取pod容器
func (p *pod) GetPodContainer(ctx *gin.Context) {
params := new(struct {
PodName string `form:"pod_name"`
Namespace string `form:"namespace"`
})
//GET请求,绑定参数方法改为ctx.Bind
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Pod.GetPodContainer(params.PodName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Pod容器成功",
"data": data,
})
}
// 获取pod中容器日志
func (p *pod) GetPodLog(ctx *gin.Context) {
params := new(struct {
ContainerName string `form:"container_name"`
PodName string `form:"pod_name"`
Namespace string `form:"namespace"`
})
//GET请求,绑定参数方法改为ctx.Bind
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Pod.GetPodLog(params.ContainerName, params.PodName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Pod中容器日志成功",
"data": data,
})
}
// 获取每个namespace的pod数量
func (p *pod) GetPodNumPerNp(ctx *gin.Context) {
data, err := service.Pod.GetPodNumPerNp()
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取每个namespace的pod数量成功",
"data": data,
})
}
controller/router.go
(10)定义路由规则
type router struct{}
func (r *router) InitApiRouter(router *gin.Engine) {
router.
GET("/api/k8s/pods", Pod.GetPods).
GET("/api/k8s/pods/detail", Pod.GetPodDetail).
DELETE("/api/k8s/pod/del", Pod.DeletePod).
PUT("/api/k8s/pod/update", Pod.UpdatePod).
GET("/api/k8s/pod/container", Pod.GetPodContainer).
GET("/api/k8s/pod/log", Pod.GetPodLog).
GET("/api/k8s/pod/numnp", Pod.GetPodNumPerNp)
}
(11)使用apifox做接口测试
启动服务
1、get http://0.0.0.0:9090/api/k8s/pods?namespace=kube-system
2、GetPodDetail
get /api/k8s/pods/detail
参数pod_name 参数namespace
3、DeletePod
post /api/k8s/pods
请求体body 填写pod_name和namespace
4、DeletePod
delete /api/k8s/pod/del
请求体body 填写pod_name和namespace
5、UpdatePod
get /api/k8s/pod/update
先获得一段json
get /api/k8s/pods/detail
Query 参数pod_name 参数namespace
得到响应,将内容复制处理来略做改动,追加的test label
再次请求就可以看到这个新追加的test label
6、GetPodContainer
get /api/k8s/pod/container
Query 参数pod_name 参数namespace
7、 GetPodLog
get /api/k8s/pod/log
Query 参数pod_name 参数namespace 参数container_name
8、GetPodNumPerNp(命名空间pod数量)
get /api/k8s/pod/numnp
不需要传参,直接返回所有命名空间pod的数量
2.2 Deployment
(1)列表
(2)获取Deployment详情
(3)删除Deployment
(4) 更新Deployment
service/dataselector.go
实现DataCell接口
// corev1 "k8s.io/api/core/v1"
type deploymentCell appsv1.Deployment
func (d deploymentCell) GetCreation() time.Time {
return d.CreationTimestamp.Time
}
func (d deploymentCell) GetName() string {
return d.Name
}
新建service/deployment.go
ackage service
import appsv1 "k8s.io/api/apps/v1"
var Deployment deployment
type deployment struct{}
类型转换
// 类型转换
func (d *deployment) toCells(deployments []appsv1.Deployment) []DataCell {
cells := make([]DataCell, len(deployments))
for i := range deployments {
cells[i] = deploymentCell(deployments[i])
}
return cells
}
func (d *deployment) fromCells(cells []DataCell) []appsv1.Deployment {
deployments := make([]appsv1.Deployment, len(cells))
for i := range cells {
// 是将DataCell类型转成podCell
deployments[i] = appsv1.Deployment(cells[i].(deploymentCell))
}
return deployments
}
开发deployment功能,包括:
(1)列表
// 获取deployment列表,支持过滤、排序、分页
func (d *deployment) GetDeployments(filterName, namespace string, limit, page int) (deploymentsResp *DeploymentsResp, err error) {
//获取deploymentList类型的deployment列表
deploymentList, err := K8s.ClientSet.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
logger.Error(errors.New("获取Deployment列表失败, " + err.Error()))
return nil, errors.New("获取Deployment列表失败, " + err.Error())
}
//将deploymentList中的deployment列表(Items),放进dataselector对象中,进行排序
selectableData := &dataSelector{
GenericDataList: d.toCells(deploymentList.Items),
DataSelect: &DataSelectQuery{
Filter: &FilterQuery{Name: filterName},
Paginate: &PaginateQuery{
Limit: limit,
Page: page,
},
},
}
filtered := selectableData.Filter()
total := len(filtered.GenericDataList)
data := filtered.Sort().Paginate()
//将[]DataCell类型的deployment列表转为appsv1.deployment列表
deployments := d.fromCells(data.GenericDataList)
return &DeploymentsResp{
Items: deployments,
Total: total,
}, nil
}
// 定义列表的返回内容,Items是deployment元素列表,Total为deployment元素数量
type DeploymentsResp struct {
Items []appsv1.Deployment `json:"items"`
Total int `json:"total"`
}
(2)获取Deployment详情
// 获取deployment详情
func (d *deployment) GetDeploymentDetail(deploymentName, namespace string) (deployment *appsv1.Deployment, err error) {
deployment, err = K8s.ClientSet.AppsV1().Deployments(namespace).Get(context.TODO(), deploymentName, metav1.GetOptions{})
if err != nil {
logger.Error(errors.New("获取Deployment详情失败, " + err.Error()))
return nil, errors.New("获取Deployment详情失败, " + err.Error())
}
return deployment, nil
}
(3)修改Deployment副本数
// 设置deployment副本数
func (d *deployment) ScaleDeployment(deploymentName, namespace string, scaleNum int) (replica int32, err error) {
//获取autoscalingv1.Scale类型的对象,能点出当前的副本数
scale, err := K8s.ClientSet.AppsV1().Deployments(namespace).GetScale(context.TODO(), deploymentName, metav1.GetOptions{})
if err != nil {
logger.Error(errors.New("获取Deployment副本数信息失败, " + err.Error()))
return 0, errors.New("获取Deployment副本数信息失败, " + err.Error())
}
//修改副本数
scale.Spec.Replicas = int32(scaleNum)
//更新副本数,传入scale对象
newScale, err := K8s.ClientSet.AppsV1().Deployments(namespace).UpdateScale(context.TODO(), deploymentName, scale, metav1.UpdateOptions{})
if err != nil {
logger.Error(errors.New("更新Deployment副本数信息失败, " + err.Error()))
return 0, errors.New("更新Deployment副本数信息失败, " + err.Error())
}
return newScale.Spec.Replicas, nil
}
(4) 创建Deployment
// 创建deployment,接收DeployCreate对象
func (d *deployment) CreateDeployment(data *DeployCreate) (err error) {
//将data中的属性组装成appsv1.Deployment对象,并将入参数据放入
deployment := &appsv1.Deployment{
//ObjectMeta中定义资源名、命名空间以及标签
ObjectMeta: metav1.ObjectMeta{
Name: data.Name,
Namespace: data.Namespace,
Labels: data.Label,
},
//Spec中定义副本数、选择器、以及pod属性
Spec: appsv1.DeploymentSpec{
Replicas: &data.Replicas,
Selector: &metav1.LabelSelector{
MatchLabels: data.Label,
},
Template: corev1.PodTemplateSpec{
//定义pod名和标签
ObjectMeta: metav1.ObjectMeta{
Name: data.Name,
Labels: data.Label,
},
//定义容器名、镜像和端口
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: data.Name,
Image: data.Image,
Ports: []corev1.ContainerPort{
{
Name: "http",
Protocol: corev1.ProtocolTCP,
ContainerPort: data.ContainerPort,
},
},
},
},
},
},
},
//Status定义资源的运行状态,这里由于是新建,传入空的appsv1.DeploymentStatus{}对象即可
Status: appsv1.DeploymentStatus{},
}
//判断是否打开健康检查功能,若打开,则定义ReadinessProbe和LivenessProbe
if data.HealthCheck {
//设置第一个容器的ReadinessProbe,因为我们pod中只有一个容器,所以直接使用index 0即可
//若pod中有多个容器,则这里需要使用for循环去定义了
deployment.Spec.Template.Spec.Containers[0].ReadinessProbe = &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: data.HealthPath,
//intstr.IntOrString的作用是端口可以定义为整型,也可以定义为字符串
//Type=0则表示表示该结构体实例内的数据为整型,转json时只使用IntVal的数据
//Type=1则表示表示该结构体实例内的数据为字符串,转json时只使用StrVal的数据
Port: intstr.IntOrString{
Type: 0,
IntVal: data.ContainerPort,
},
},
},
//初始化等待时间
InitialDelaySeconds: 5,
//超时时间
TimeoutSeconds: 5,
//执行间隔
PeriodSeconds: 5,
}
deployment.Spec.Template.Spec.Containers[0].LivenessProbe = &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: data.HealthPath,
Port: intstr.IntOrString{
Type: 0,
IntVal: data.ContainerPort,
},
},
},
InitialDelaySeconds: 15,
TimeoutSeconds: 5,
PeriodSeconds: 5,
}
//定义容器的limit和request资源
deployment.Spec.Template.Spec.Containers[0].Resources.Limits = map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: resource.MustParse(data.Cpu),
corev1.ResourceMemory: resource.MustParse(data.Memory),
}
deployment.Spec.Template.Spec.Containers[0].Resources.Requests = map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: resource.MustParse(data.Cpu),
corev1.ResourceMemory: resource.MustParse(data.Memory),
}
}
//调用sdk创建deployment
_, err = K8s.ClientSet.AppsV1().Deployments(data.Namespace).Create(context.TODO(), deployment, metav1.CreateOptions{})
if err != nil {
logger.Error(errors.New("创建Deployment失败, " + err.Error()))
return errors.New("创建Deployment失败, " + err.Error())
}
return nil
}
// 定义DeployCreate结构体,用于创建deployment需要的参数属性的定义
type DeployCreate struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Replicas int32 `json:"replicas"`
Image string `json:"image"`
Label map[string]string `json:"label"`
Cpu string `json:"cpu"`
Memory string `json:"memory"`
ContainerPort int32 `json:"container_port"`
HealthCheck bool `json:"health_check"`
HealthPath string `json:"health_path"`
}
(5)删除Deployment
// 删除deployment
func (d *deployment) DeleteDeployment(deploymentName, namespace string) (err error) {
err = K8s.ClientSet.AppsV1().Deployments(namespace).Delete(context.TODO(), deploymentName, metav1.DeleteOptions{})
if err != nil {
logger.Error(errors.New("删除Deployment失败, " + err.Error()))
return errors.New("删除Deployment失败, " + err.Error())
}
return nil
}
(6) 重启Deployment
// 重启deployment
func (d *deployment) RestartDeployment(deploymentName, namespace string) (err error) {
//此功能等同于一下kubectl命令
//kubectl deployment ${service} -p \
//'{"spec":{"template":{"spec":{"containers":[{"name":"'"${service}"'","env":[{"name":"RESTART_","value":"'$(date +%s)'"}]}]}}}}'
//使用patchData Map组装数据
patchData := map[string]interface{}{
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"containers": []map[string]interface{}{
{"name": deploymentName,
"env": []map[string]string{{
"name": "RESTART_",
"value": strconv.FormatInt(time.Now().Unix(), 10),
}},
},
},
},
},
},
}
//序列化为字节,因为patch方法只接收字节类型参数
patchByte, err := json.Marshal(patchData)
if err != nil {
logger.Error(errors.New("json序列化失败, " + err.Error()))
return errors.New("json序列化失败, " + err.Error())
}
//调用patch方法更新deployment
_, err = K8s.ClientSet.AppsV1().Deployments(namespace).Patch(context.TODO(), deploymentName, "application/strategic-merge-patch+json", patchByte, metav1.PatchOptions{})
if err != nil {
logger.Error(errors.New("重启Deployment失败, " + err.Error()))
return errors.New("重启Deployment失败, " + err.Error())
}
return nil
}
加个功能 deployment上显示上次重启时间
通过前端取出{"name":"RESTART_","value":"'$(date +%s)'"}
(7)更新Deployment
// 更新deployment
func (d *deployment) UpdateDeployment(namespace, content string) (err error) {
var deploy = &appsv1.Deployment{}
err = json.Unmarshal([]byte(content), deploy)
if err != nil {
logger.Error(errors.New("反序列化失败, " + err.Error()))
return errors.New("反序列化失败, " + err.Error())
}
_, err = K8s.ClientSet.AppsV1().Deployments(namespace).Update(context.TODO(), deploy, metav1.UpdateOptions{})
if err != nil {
logger.Error(errors.New("更新Deployment失败, " + err.Error()))
return errors.New("更新Deployment失败, " + err.Error())
}
return nil
}
(8)获取每个namespace的Deployment数量
// 获取每个namespace的deployment数量
func (d *deployment) GetDeployNumPerNp() (deploysNps []*DeploysNp, err error) {
namespaceList, err := K8s.ClientSet.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, namespace := range namespaceList.Items {
deploymentList, err := K8s.ClientSet.AppsV1().Deployments(namespace.Name).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
deploysNp := &DeploysNp{
Namespace: namespace.Name,
DeployNum: len(deploymentList.Items),
}
deploysNps = append(deploysNps, deploysNp)
}
return deploysNps, nil
}
// 定义DeploysNp类型,用于返回namespace中deployment的数量
type DeploysNp struct {
Namespace string `json:"namespace"`
DeployNum int `json:"deployment_num"`
}
controller/deployment.go
package controller
import (
"fmt"
"k8s-platform/service"
"net/http"
"github.com/gin-gonic/gin"
"github.com/wonderivan/logger"
)
var Deployment deployment
type deployment struct{}
// 获取deployment列表,支持过滤、排序、分页
func (d *deployment) GetDeployments(ctx *gin.Context) {
params := new(struct {
FilterName string `form:"filter_name"`
Namespace string `form:"namespace"`
Page int `form:"page"`
Limit int `form:"limit"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Deployment.GetDeployments(params.FilterName, params.Namespace, params.Limit, params.Page)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Deployment列表成功",
"data": data,
})
}
// 获取deployment详情
func (d *deployment) GetDeploymentDetail(ctx *gin.Context) {
params := new(struct {
DeploymentName string `form:"deployment_name"`
Namespace string `form:"namespace"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Deployment.GetDeploymentDetail(params.DeploymentName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Deployment详情成功",
"data": data,
})
}
// 创建deployment
func (d *deployment) CreateDeployment(ctx *gin.Context) {
var (
deployCreate = new(service.DeployCreate)
err error
)
if err = ctx.ShouldBindJSON(deployCreate); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
if err = service.Deployment.CreateDeployment(deployCreate); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "创建Deployment成功",
"data": nil,
})
}
// 设置deployment副本数
func (d *deployment) ScaleDeployment(ctx *gin.Context) {
params := new(struct {
DeploymentName string `json:"deployment_name"`
Namespace string `json:"namespace"`
ScaleNum int `json:"scale_num"`
})
//PUT请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Deployment.ScaleDeployment(params.DeploymentName, params.Namespace, params.ScaleNum)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "设置Deployment副本数成功",
"data": fmt.Sprintf("最新副本数: %d", data),
})
}
// 删除deployment
func (d *deployment) DeleteDeployment(ctx *gin.Context) {
params := new(struct {
DeploymentName string `json:"deployment_name"`
Namespace string `json:"namespace"`
})
//DELETE请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.Deployment.DeleteDeployment(params.DeploymentName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "删除Deployment成功",
"data": nil,
})
}
// 重启deployment
func (d *deployment) RestartDeployment(ctx *gin.Context) {
params := new(struct {
DeploymentName string `json:"deployment_name"`
Namespace string `json:"namespace"`
})
//PUT请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.Deployment.RestartDeployment(params.DeploymentName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "重启Deployment成功",
"data": nil,
})
}
// 更新deployment
func (d *deployment) UpdateDeployment(ctx *gin.Context) {
params := new(struct {
Namespace string `json:"namespace"`
Content string `json:"content"`
})
//PUT请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.Deployment.UpdateDeployment(params.Namespace, params.Content)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "更新Deployment成功",
"data": nil,
})
}
// 获取每个namespace的pod数量
func (d *deployment) GetDeployNumPerNp(ctx *gin.Context) {
data, err := service.Deployment.GetDeployNumPerNp()
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取每个namespace的deployment数量成功",
"data": data,
})
}
配置路由
func (r *router) InitApiRouter(router *gin.Engine) {
router.
// Pods
GET("/api/k8s/pods", Pod.GetPods).
GET("/api/k8s/pod/detail", Pod.GetPodDetail).
DELETE("/api/k8s/pod/del", Pod.DeletePod).
PUT("/api/k8s/pod/update", Pod.UpdatePod).
GET("/api/k8s/pod/container", Pod.GetPodContainer).
GET("/api/k8s/pod/log", Pod.GetPodLog).
GET("/api/k8s/pod/numnp", Pod.GetPodNumPerNp).
//deployment操作
GET("/api/k8s/deployments", Deployment.GetDeployments).
GET("/api/k8s/deployment/detail", Deployment.GetDeploymentDetail).
PUT("/api/k8s/deployment/scale", Deployment.ScaleDeployment).
DELETE("/api/k8s/deployment/del", Deployment.DeleteDeployment).
PUT("/api/k8s/deployment/restart", Deployment.RestartDeployment).
PUT("/api/k8s/deployment/update", Deployment.UpdateDeployment).
GET("/api/k8s/deployment/numnp", Deployment.GetDeployNumPerNp).
POST("/api/k8s/deployment/create", Deployment.CreateDeployment)
}
在apifox进行api测试
2.3 DaemonSet
service/dataselector.go
接口实现
// daemonCell
type daemonSetCell appsv1.DaemonSet
func(d daemonSetCell) GetCreation() time.Time {
return d.CreationTimestamp.Time
}
func(d daemonSetCell) GetName() string {
return d.Name
}
DaemontSet功能
service/daemonset.go
from和to方法
func (d *daemonSet) toCells(std []appsv1.DaemonSet) []DataCell {
cells := make([]DataCell, len(std))
for i := range std {
cells[i] = daemonSetCell(std[i])
}
return cells
}
func (d *daemonSet) fromCells(cells []DataCell) []appsv1.DaemonSet {
daemonSets := make([]appsv1.DaemonSet, len(cells))
for i := range cells {
daemonSets[i] = appsv1.DaemonSet(cells[i].(daemonSetCell))
}
return daemonSets
}
(1)列表
// 获取daemonset列表,支持过滤、排序、分页
func (d *daemonSet) GetDaemonSets(filterName, namespace string, limit, page int) (daemonSetsResp *DaemonSetsResp, err error) {
//获取daemonSetList类型的daemonSet列表
daemonSetList, err := K8s.ClientSet.AppsV1().DaemonSets(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
logger.Error(errors.New("获取DaemonSet列表失败, " + err.Error()))
return nil, errors.New("获取DaemonSet列表失败, " + err.Error())
}
//将daemonSetList中的daemonSet列表(Items),放进dataselector对象中,进行排序
selectableData := &dataSelector{
GenericDataList: d.toCells(daemonSetList.Items),
DataSelect: &DataSelectQuery{
Filter: &FilterQuery{Name: filterName},
Paginate: &PaginateQuery{
Limit: limit,
Page: page,
},
},
}
filtered := selectableData.Filter()
total := len(filtered.GenericDataList)
data := filtered.Sort().Paginate()
//将[]DataCell类型的daemonset列表转为v1.daemonset列表
daemonSets := d.fromCells(data.GenericDataList)
return &DaemonSetsResp{
Items: daemonSets,
Total: total,
}, nil
}
(2)获取DaemonSet详情
// 获取daemonset详情
func (d *daemonSet) GetDaemonSetDetail(daemonSetName, namespace string) (daemonSet *appsv1.DaemonSet, err error) {
daemonSet, err = K8s.ClientSet.AppsV1().DaemonSets(namespace).Get(context.TODO(), daemonSetName, metav1.GetOptions{})
if err != nil {
logger.Error(errors.New("获取DaemonSet详情失败, " + err.Error()))
return nil, errors.New("获取DaemonSet详情失败, " + err.Error())
}
return daemonSet, nil
}
(3)删除DaemonSet
// 删除daemonset
func (d *daemonSet) DeleteDaemonSet(daemonSetName, namespace string) (err error) {
err = K8s.ClientSet.AppsV1().DaemonSets(namespace).Delete(context.TODO(), daemonSetName, metav1.DeleteOptions{})
if err != nil {
logger.Error(errors.New("删除DaemonSet失败, " + err.Error()))
return errors.New("删除DaemonSet失败, " + err.Error())
}
return nil
}
(4) 更新DaemonSet
// 更新daemonset
func (d *daemonSet) UpdateDaemonSet(namespace, content string) (err error) {
var daemonSet = &appsv1.DaemonSet{}
err = json.Unmarshal([]byte(content), daemonSet)
if err != nil {
logger.Error(errors.New("反序列化失败, " + err.Error()))
return errors.New("反序列化失败, " + err.Error())
}
_, err = K8s.ClientSet.AppsV1().DaemonSets(namespace).Update(context.TODO(), daemonSet, metav1.UpdateOptions{})
if err != nil {
logger.Error(errors.New("更新DaemonSet失败, " + err.Error()))
return errors.New("更新DaemonSet失败, " + err.Error())
}
return nil
}
controller/daemonset.go
获取DaemonSet列表、获取daemonset详情、删除daemonset、更新daemonset
package controller
import (
"k8s-platform/service"
"net/http"
"github.com/gin-gonic/gin"
"github.com/wonderivan/logger"
)
var DaemonSet daemonSet
type daemonSet struct{}
//获取daemonset列表,支持过滤、排序、分页
func (d *daemonSet) GetDaemonSets(ctx *gin.Context) {
params := new(struct {
FilterName string `form:"filter_name"`
Namespace string `form:"namespace"`
Page int `form:"page"`
Limit int `form:"limit"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.DaemonSet.GetDaemonSets(params.FilterName, params.Namespace, params.Limit, params.Page)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取DaemonSet列表成功",
"data": data,
})
}
//获取daemonset详情
func (d *daemonSet) GetDaemonSetDetail(ctx *gin.Context) {
params := new(struct {
DaemonSetName string `form:"daemonset_name"`
Namespace string `form:"namespace"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.DaemonSet.GetDaemonSetDetail(params.DaemonSetName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取DaemonSet详情成功",
"data": data,
})
}
//删除daemonset
func (d *daemonSet) DeleteDaemonSet(ctx *gin.Context) {
params := new(struct {
DaemonSetName string `json:"daemonset_name"`
Namespace string `json:"namespace"`
})
//DELETE请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.DaemonSet.DeleteDaemonSet(params.DaemonSetName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "删除DaemonSet成功",
"data": nil,
})
}
//更新daemonset
func (d *daemonSet) UpdateDaemonSet(ctx *gin.Context) {
params := new(struct {
Namespace string `json:"namespace"`
Content string `json:"content"`
})
//PUT请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.DaemonSet.UpdateDaemonSet(params.Namespace, params.Content)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "更新DaemonSet成功",
"data": nil,
})
}
添加路由规则
controller/router.go
//daemonset操作
GET("/api/k8s/daemonsets", DaemonSet.GetDaemonSets).
GET("/api/k8s/daemonset/detail", DaemonSet.GetDaemonSetDetail).
DELETE("/api/k8s/daemonset/del", DaemonSet.DeleteDaemonSet).
PUT("/api/k8s/daemonset/update", DaemonSet.UpdateDaemonSet)
测试api接口
2.4 StatefulSet
service/dataselector.go
接口实现
// statefulSetCell
type statefulSetCell appsv1.StatefulSet
func(s statefulSetCell) GetCreation() time.Time {
return s.CreationTimestamp.Time
}
func(s statefulSetCell) GetName() string {
return s.Name
}
service/statefulset.go
StatefulSet功能
from和to方法
func(s *statefulSet) toCells(std []appsv1.StatefulSet) []DataCell {
cells := make([]DataCell, len(std))
for i := range std {
cells[i] = statefulSetCell(std[i])
}
return cells
}
func(s *statefulSet) fromCells(cells []DataCell) []appsv1.StatefulSet {
statefulSets := make([]appsv1.StatefulSet, len(cells))
for i := range cells {
statefulSets[i] = appsv1.StatefulSet(cells[i].(statefulSetCell))
}
return statefulSets
}
(1)列表
(2)获取StatefulSet详情
(3)删除StatefulSet
(4) 更新StatefulSet
package service
import (
"context"
"encoding/json"
"errors"
"github.com/wonderivan/logger"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var StatefulSet statefulSet
type statefulSet struct{}
type StatusfulSetsResp struct {
Items []appsv1.StatefulSet `json:"items"`
Total int `json:"total"`
}
// 获取statefulset列表,支持过滤、排序、分页
func (s *statefulSet) GetStatefulSets(filterName, namespace string, limit, page int) (statusfulSetsResp *StatusfulSetsResp, err error) {
//获取statefulSetList类型的statefulSet列表
statefulSetList, err := K8s.ClientSet.AppsV1().StatefulSets(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
logger.Error(errors.New("获取StatefulSet列表失败, " + err.Error()))
return nil, errors.New("获取StatefulSet列表失败, " + err.Error())
}
//将statefulSetList中的StatefulSet列表(Items),放进dataselector对象中,进行排序
selectableData := &dataSelector{
GenericDataList: s.toCells(statefulSetList.Items),
DataSelect: &DataSelectQuery{
Filter: &FilterQuery{Name: filterName},
Paginate: &PaginateQuery{
Limit: limit,
Page: page,
},
},
}
filtered := selectableData.Filter()
total := len(filtered.GenericDataList)
data := filtered.Sort().Paginate()
//将[]DataCell类型的statefulset列表转为v1.statefulset列表
statefulSets := s.fromCells(data.GenericDataList)
return &StatusfulSetsResp{
Items: statefulSets,
Total: total,
}, nil
}
// 获取statefulset详情
func (s *statefulSet) GetStatefulSetDetail(statefulSetName, namespace string) (statefulSet *appsv1.StatefulSet, err error) {
statefulSet, err = K8s.ClientSet.AppsV1().StatefulSets(namespace).Get(context.TODO(), statefulSetName, metav1.GetOptions{})
if err != nil {
logger.Error(errors.New("获取StatefulSet详情失败, " + err.Error()))
return nil, errors.New("获取StatefulSet详情失败, " + err.Error())
}
return statefulSet, nil
}
// 删除statefulset
func (s *statefulSet) DeleteStatefulSet(statefulSetName, namespace string) (err error) {
err = K8s.ClientSet.AppsV1().StatefulSets(namespace).Delete(context.TODO(), statefulSetName, metav1.DeleteOptions{})
if err != nil {
logger.Error(errors.New("删除StatefulSet失败, " + err.Error()))
return errors.New("删除StatefulSet失败, " + err.Error())
}
return nil
}
// 更新statefulset
func (s *statefulSet) UpdateStatefulSet(namespace, content string) (err error) {
var statefulSet = &appsv1.StatefulSet{}
err = json.Unmarshal([]byte(content), statefulSet)
if err != nil {
logger.Error(errors.New("反序列化失败, " + err.Error()))
return errors.New("反序列化失败, " + err.Error())
}
_, err = K8s.ClientSet.AppsV1().StatefulSets(namespace).Update(context.TODO(), statefulSet, metav1.UpdateOptions{})
if err != nil {
logger.Error(errors.New("更新StatefulSet失败, " + err.Error()))
return errors.New("更新StatefulSet失败, " + err.Error())
}
return nil
}
func (s *statefulSet) toCells(std []appsv1.StatefulSet) []DataCell {
cells := make([]DataCell, len(std))
for i := range std {
cells[i] = statefulSetCell(std[i])
}
return cells
}
func (s *statefulSet) fromCells(cells []DataCell) []appsv1.StatefulSet {
statefulSets := make([]appsv1.StatefulSet, len(cells))
for i := range cells {
statefulSets[i] = appsv1.StatefulSet(cells[i].(statefulSetCell))
}
return statefulSets
}
controller/statefulset
package controller
import (
"k8s-platform/service"
"net/http"
"github.com/gin-gonic/gin"
"github.com/wonderivan/logger"
)
var StatefulSet statefulSet
type statefulSet struct{}
//获取statefulset列表,支持过滤、排序、分页
func (s *statefulSet) GetStatefulSets(ctx *gin.Context) {
params := new(struct {
FilterName string `form:"filter_name"`
Namespace string `form:"namespace"`
Page int `form:"page"`
Limit int `form:"limit"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.StatefulSet.GetStatefulSets(params.FilterName, params.Namespace, params.Limit, params.Page)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取StatefulSet列表成功",
"data": data,
})
}
//获取statefulset详情
func (s *statefulSet) GetStatefulSetDetail(ctx *gin.Context) {
params := new(struct {
StatefulSetName string `form:"statefulset_name"`
Namespace string `form:"namespace"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.StatefulSet.GetStatefulSetDetail(params.StatefulSetName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取StatefulSet详情成功",
"data": data,
})
}
//删除statefulset
func (s *statefulSet) DeleteStatefulSet(ctx *gin.Context) {
params := new(struct {
StatefulSetName string `json:"statefulset_name"`
Namespace string `json:"namespace"`
})
//DELETE请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.StatefulSet.DeleteStatefulSet(params.StatefulSetName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "删除StatefulSet成功",
"data": nil,
})
}
//更新statefulSet
func (s *statefulSet) UpdateStatefulSet(ctx *gin.Context) {
params := new(struct {
Namespace string `json:"namespace"`
Content string `json:"content"`
})
//PUT请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.StatefulSet.UpdateStatefulSet(params.Namespace, params.Content)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "更新StatefulSet成功",
"data": nil,
})
}
controller/router.go
添加路由
//statefulset操作
GET("/api/k8s/statefulsets", StatefulSet.GetStatefulSets).
GET("/api/k8s/statefulset/detail", StatefulSet.GetStatefulSetDetail).
DELETE("/api/k8s/statefulset/del", StatefulSet.DeleteStatefulSet).
PUT("/api/k8s/statefulset/update", StatefulSet.UpdateStatefulSet)
测试api接口
API开发:K8S集群资源
3、集群
3.1 Node
service/dataselector.go
接口实现
type nodeCell corev1.Node
func(n nodeCell) GetCreation() time.Time {
return n.CreationTimestamp.Time
}
func(n nodeCell) GetName() string {
return n.Name
}
service/node.go
重写方法toCells、fromCells
Node功能
(1)列表
(2)获取Node详情
package service
import (
"context"
"errors"
"github.com/wonderivan/logger"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var Node node
type node struct{}
type NodesResp struct {
Items []corev1.Node `json:"items"`
Total int `json:"total"`
}
// 获取node列表,支持过滤、排序、分页
func (n *node) GetNodes(filterName string, limit, page int) (nodesResp *NodesResp, err error) {
//获取nodeList类型的node列表
nodeList, err := K8s.ClientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
logger.Error(errors.New("获取Node列表失败, " + err.Error()))
return nil, errors.New("获取Node列表失败, " + err.Error())
}
//将nodeList中的node列表(Items),放进dataselector对象中,进行排序
selectableData := &dataSelector{
GenericDataList: n.toCells(nodeList.Items),
DataSelect: &DataSelectQuery{
Filter: &FilterQuery{Name: filterName},
Paginate: &PaginateQuery{
Limit: limit,
Page: page,
},
},
}
filtered := selectableData.Filter()
total := len(filtered.GenericDataList)
data := filtered.Sort().Paginate()
//将[]DataCell类型的node列表转为v1.node列表
nodes := n.fromCells(data.GenericDataList)
return &NodesResp{
Items: nodes,
Total: total,
}, nil
}
// 获取node详情
func (n *node) GetNodeDetail(nodeName string) (node *corev1.Node, err error) {
node, err = K8s.ClientSet.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
if err != nil {
logger.Error(errors.New("获取Node详情失败, " + err.Error()))
return nil, errors.New("获取Node详情失败, " + err.Error())
}
return node, nil
}
func (n *node) toCells(std []corev1.Node) []DataCell {
cells := make([]DataCell, len(std))
for i := range std {
cells[i] = nodeCell(std[i])
}
return cells
}
func (n *node) fromCells(cells []DataCell) []corev1.Node {
nodes := make([]corev1.Node, len(cells))
for i := range cells {
nodes[i] = corev1.Node(cells[i].(nodeCell))
}
return nodes
}
controller/node.go
package controller
import (
"github.com/gin-gonic/gin"
"github.com/wonderivan/logger"
"k8s-platform/service"
"net/http"
)
var Node node
type node struct {}
//获取node列表,支持过滤、排序、分页
func(n *node) GetNodes(ctx *gin.Context) {
params := new(struct {
FilterName string `form:"filter_name"`
Page int `form:"page"`
Limit int `form:"limit"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Node.GetNodes(params.FilterName, params.Limit, params.Page)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Node列表成功",
"data": data,
})
}
//获取node详情
func(n *node) GetNodeDetail(ctx *gin.Context) {
params := new(struct {
NodeName string `form:"node_name"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Node.GetNodeDetail(params.NodeName)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Node详情成功",
"data": data,
})
}
controller/router.go
定义路由
//node操作
GET("/api/k8s/nodes", Node.GetNodes).
GET("/api/k8s/node/detail", Node.GetNodeDetail)
测试
3.2 Namespace
接口实现
service/dataselector.go
type namespaceCell corev1.Namespace
func(n namespaceCell) GetCreation() time.Time {
return n.CreationTimestamp.Time
}
func(n namespaceCell) GetName() string {
return n.Name
}
(1)列表
(2)获取Namespace详情
(3)删除Namespace
service/namespace.go
package service
import (
"context"
"errors"
"github.com/wonderivan/logger"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var Namespace namespace
type namespace struct{}
type NamespacesResp struct {
Items []corev1.Namespace `json:"items"`
Total int `json:"total"`
}
// 获取namespace列表,支持过滤、排序、分页
func (n *namespace) GetNamespaces(filterName string, limit, page int) (namespacesResp *NamespacesResp, err error) {
//获取namespaceList类型的namespace列表
namespaceList, err := K8s.ClientSet.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
logger.Error(errors.New("获取Namespace列表失败, " + err.Error()))
return nil, errors.New("获取Namespace列表失败, " + err.Error())
}
//将namespaceList中的namespace列表(Items),放进dataselector对象中,进行排序
selectableData := &dataSelector{
GenericDataList: n.toCells(namespaceList.Items),
DataSelect: &DataSelectQuery{
Filter: &FilterQuery{Name: filterName},
Paginate: &PaginateQuery{
Limit: limit,
Page: page,
},
},
}
filtered := selectableData.Filter()
total := len(filtered.GenericDataList)
data := filtered.Sort().Paginate()
//将[]DataCell类型的namespace列表转为v1.namespace列表
namespaces := n.fromCells(data.GenericDataList)
return &NamespacesResp{
Items: namespaces,
Total: total,
}, nil
}
// 获取namespace详情
func (n *namespace) GetNamespaceDetail(namespaceName string) (namespace *corev1.Namespace, err error) {
namespace, err = K8s.ClientSet.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
if err != nil {
logger.Error(errors.New("获取Namespace详情失败, " + err.Error()))
return nil, errors.New("获取Namespace详情失败, " + err.Error())
}
return namespace, nil
}
// 删除namespace
func (n *namespace) DeleteNamespace(namespaceName string) (err error) {
err = K8s.ClientSet.CoreV1().Namespaces().Delete(context.TODO(), namespaceName, metav1.DeleteOptions{})
if err != nil {
logger.Error(errors.New("删除Namespace失败, " + err.Error()))
return errors.New("删除Namespace失败, " + err.Error())
}
return nil
}
func (n *namespace) toCells(std []corev1.Namespace) []DataCell {
cells := make([]DataCell, len(std))
for i := range std {
cells[i] = namespaceCell(std[i])
}
return cells
}
func (n *namespace) fromCells(cells []DataCell) []corev1.Namespace {
namespaces := make([]corev1.Namespace, len(cells))
for i := range cells {
namespaces[i] = corev1.Namespace(cells[i].(namespaceCell))
}
return namespaces
}
controller/namespace.go
package controller
import (
"k8s-platform/service"
"net/http"
"github.com/gin-gonic/gin"
"github.com/wonderivan/logger"
)
var Namespace namespace
type namespace struct{}
// 获取namespace列表,支持过滤、排序、分页
func (n *namespace) GetNamespaces(ctx *gin.Context) {
params := new(struct {
FilterName string `form:"filter_name"`
Page int `form:"page"`
Limit int `form:"limit"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Namespace.GetNamespaces(params.FilterName, params.Limit, params.Page)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Namespace列表成功",
"data": data,
})
}
// 获取namespace详情
func (n *namespace) GetNamespaceDetail(ctx *gin.Context) {
params := new(struct {
NamespaceName string `form:"namespace_name"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Namespace.GetNamespaceDetail(params.NamespaceName)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Namespace详情成功",
"data": data,
})
}
// 删除namespace
func (n *namespace) DeleteNamespace(ctx *gin.Context) {
params := new(struct {
NamespaceName string `json:"namespace_name"`
})
//DELETE请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.Namespace.DeleteNamespace(params.NamespaceName)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "删除Namespace成功",
"data": nil,
})
}
定义路由
controller/router.go
//namespace操作
GET("/api/k8s/namespaces", Namespace.GetNamespaces).
GET("/api/k8s/namespace/detail", Namespace.GetNamespaceDetail).
DELETE("/api/k8s/namespace/del", Namespace.DeleteNamespace)
测试Namespace方法
3.3 PersistentValume
接口实现
service/dataselector.go
type pvCell corev1.PersistentVolume
func (p pvCell) GetCreation() time.Time {
return p.CreationTimestamp.Time
}
func (p pvCell) GetName() string {
return p.Name
}
(1)列表
(2)获取Pv详情
(3)删除Pv
service/pv.go
package service
import (
"context"
"errors"
"github.com/wonderivan/logger"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var Pv pv
type pv struct{}
type PvsResp struct {
Items []corev1.PersistentVolume `json:"items"`
Total int `json:"total"`
}
// 获取pv列表,支持过滤、排序、分页
func (p *pv) GetPvs(filterName string, limit, page int) (pvsResp *PvsResp, err error) {
//获取pvList类型的pv列表
pvList, err := K8s.ClientSet.CoreV1().PersistentVolumes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
logger.Error(errors.New("获取Pv列表失败, " + err.Error()))
return nil, errors.New("获取Pv列表失败, " + err.Error())
}
//将pvList中的pv列表(Items),放进dataselector对象中,进行排序
selectableData := &dataSelector{
GenericDataList: p.toCells(pvList.Items),
DataSelect: &DataSelectQuery{
Filter: &FilterQuery{Name: filterName},
Paginate: &PaginateQuery{
Limit: limit,
Page: page,
},
},
}
filtered := selectableData.Filter()
total := len(filtered.GenericDataList)
data := filtered.Sort().Paginate()
//将[]DataCell类型的pv列表转为v1.pv列表
pvs := p.fromCells(data.GenericDataList)
return &PvsResp{
Items: pvs,
Total: total,
}, nil
}
// 获取pv详情
func (p *pv) GetPvDetail(pvName string) (pv *corev1.PersistentVolume, err error) {
pv, err = K8s.ClientSet.CoreV1().PersistentVolumes().Get(context.TODO(), pvName, metav1.GetOptions{})
if err != nil {
logger.Error(errors.New("获取Pv详情失败, " + err.Error()))
return nil, errors.New("获取Pv详情失败, " + err.Error())
}
return pv, nil
}
// 删除pv
func (p *pv) DeletePv(pvName string) (err error) {
err = K8s.ClientSet.CoreV1().PersistentVolumes().Delete(context.TODO(), pvName, metav1.DeleteOptions{})
if err != nil {
logger.Error(errors.New("删除Pv失败, " + err.Error()))
return errors.New("删除Pv失败, " + err.Error())
}
return nil
}
func (p *pv) toCells(std []corev1.PersistentVolume) []DataCell {
cells := make([]DataCell, len(std))
for i := range std {
cells[i] = pvCell(std[i])
}
return cells
}
func (p *pv) fromCells(cells []DataCell) []corev1.PersistentVolume {
pvs := make([]corev1.PersistentVolume, len(cells))
for i := range cells {
pvs[i] = corev1.PersistentVolume(cells[i].(pvCell))
}
return pvs
}
controller/pv.go
package controller
import (
"k8s-platform/service"
"net/http"
"github.com/gin-gonic/gin"
"github.com/wonderivan/logger"
)
var Pv pv
type pv struct{}
//获取pv列表,支持过滤、排序、分页
func (p *pv) GetPvs(ctx *gin.Context) {
params := new(struct {
FilterName string `form:"filter_name"`
Page int `form:"page"`
Limit int `form:"limit"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Pv.GetPvs(params.FilterName, params.Limit, params.Page)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Pv列表成功",
"data": data,
})
}
//获取pv详情
func (p *pv) GetPvDetail(ctx *gin.Context) {
params := new(struct {
PvName string `form:"pv_name"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Pv.GetPvDetail(params.PvName)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Pv详情成功",
"data": data,
})
}
//删除pv
func (p *pv) DeletePv(ctx *gin.Context) {
params := new(struct {
PvName string `json:"pv_name"`
})
//DELETE请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.Pv.DeletePv(params.PvName)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "删除Pv成功",
"data": nil,
})
}
添加路由规则
controller/router.go
//pv操作
GET("/api/k8s/pvs", Pv.GetPvs).
GET("/api/k8s/pv/detail", Pv.GetPvDetail)
测试api接口
创建一个nfs类型pv
API开发:负载均衡资源
4、负载均衡
4.1 Service
接口实现
service/dataselector.go
// service
type serviceCell corev1.Service
func(s serviceCell) GetCreation() time.Time {
return s.CreationTimestamp.Time
}
func(s serviceCell) GetName() string {
return s.Name
}
(1)列表
(2)获取Service详情
(3)创建Service
(4) 删除Service
(5) 更新Service
创建Service
对标yaml
service/service.go
package service
import (
"context"
"encoding/json"
"errors"
"github.com/wonderivan/logger"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
var Servicev1 servicev1
type servicev1 struct{}
type ServicesResp struct {
Items []corev1.Service `json:"items"`
Total int `json:"total"`
}
type ServiceCreate struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Type string `json:"type"`
ContainerPort int32 `json:"container_port"`
Port int32 `json:"port"`
NodePort int32 `json:"node_port"`
Label map[string]string `json:"label"`
}
// 获取service列表,支持过滤、排序、分页
func (s *servicev1) GetServices(filterName, namespace string, limit, page int) (servicesResp *ServicesResp, err error) {
//获取serviceList类型的service列表
serviceList, err := K8s.ClientSet.CoreV1().Services(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
logger.Error(errors.New("获取Service列表失败, " + err.Error()))
return nil, errors.New("获取Service列表失败, " + err.Error())
}
//将serviceList中的service列表(Items),放进dataselector对象中,进行排序
selectableData := &dataSelector{
GenericDataList: s.toCells(serviceList.Items),
DataSelect: &DataSelectQuery{
Filter: &FilterQuery{Name: filterName},
Paginate: &PaginateQuery{
Limit: limit,
Page: page,
},
},
}
filtered := selectableData.Filter()
total := len(filtered.GenericDataList)
data := filtered.Sort().Paginate()
//将[]DataCell类型的service列表转为v1.service列表
services := s.fromCells(data.GenericDataList)
return &ServicesResp{
Items: services,
Total: total,
}, nil
}
// 获取service详情
func (s *servicev1) GetServicetDetail(serviceName, namespace string) (service *corev1.Service, err error) {
service, err = K8s.ClientSet.CoreV1().Services(namespace).Get(context.TODO(), serviceName, metav1.GetOptions{})
if err != nil {
logger.Error(errors.New("获取Service详情失败, " + err.Error()))
return nil, errors.New("获取Service详情失败, " + err.Error())
}
return service, nil
}
// 创建service,,接收ServiceCreate对象
func (s *servicev1) CreateService(data *ServiceCreate) (err error) {
//将data中的数据组装成corev1.Service对象
service := &corev1.Service{
//ObjectMeta中定义资源名、命名空间以及标签
ObjectMeta: metav1.ObjectMeta{
Name: data.Name,
Namespace: data.Namespace,
Labels: data.Label,
},
//Spec中定义类型,端口,选择器
Spec: corev1.ServiceSpec{
Type: corev1.ServiceType(data.Type),
Ports: []corev1.ServicePort{
{
Name: "http",
Port: data.Port,
Protocol: "TCP",
TargetPort: intstr.IntOrString{
Type: 0,
IntVal: data.ContainerPort,
},
},
},
Selector: data.Label,
},
}
//默认ClusterIP,这里是判断NodePort,添加配置
if data.NodePort != 0 && data.Type == "NodePort" {
service.Spec.Ports[0].NodePort = data.NodePort
}
//创建Service
_, err = K8s.ClientSet.CoreV1().Services(data.Namespace).Create(context.TODO(), service, metav1.CreateOptions{})
if err != nil {
logger.Error(errors.New("创建Service失败, " + err.Error()))
return errors.New("创建Service失败, " + err.Error())
}
return nil
}
// 删除service
func (s *servicev1) DeleteService(serviceName, namespace string) (err error) {
err = K8s.ClientSet.CoreV1().Services(namespace).Delete(context.TODO(), serviceName, metav1.DeleteOptions{})
if err != nil {
logger.Error(errors.New("删除Service失败, " + err.Error()))
return errors.New("删除Service失败, " + err.Error())
}
return nil
}
// 更新service
func (s *servicev1) UpdateService(namespace, content string) (err error) {
var service = &corev1.Service{}
err = json.Unmarshal([]byte(content), service)
if err != nil {
logger.Error(errors.New("反序列化失败, " + err.Error()))
return errors.New("反序列化失败, " + err.Error())
}
_, err = K8s.ClientSet.CoreV1().Services(namespace).Update(context.TODO(), service, metav1.UpdateOptions{})
if err != nil {
logger.Error(errors.New("更新service失败, " + err.Error()))
return errors.New("更新service失败, " + err.Error())
}
return nil
}
func (s *servicev1) toCells(std []corev1.Service) []DataCell {
cells := make([]DataCell, len(std))
for i := range std {
cells[i] = serviceCell(std[i])
}
return cells
}
func (s *servicev1) fromCells(cells []DataCell) []corev1.Service {
services := make([]corev1.Service, len(cells))
for i := range cells {
services[i] = corev1.Service(cells[i].(serviceCell))
}
return services
}
controller/service.go
添加路由规则
controller/router.go
//service操作
GET("/api/k8s/services", Servicev1.GetServices).
GET("/api/k8s/service/detail", Servicev1.GetServiceDetail).
DELETE("/api/k8s/service/del", Servicev1.DeleteService).
PUT("/api/k8s/service/update", Servicev1.UpdateService).
POST("/api/k8s/service/create", Servicev1.CreateService)
测试api接口
4.2 Ingress
接口实现
service/dataselector.go
import (
"sort"
"strings"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
nwv1 "k8s.io/api/networking/v1"
)
// Ingress
type ingressCell nwv1.Ingress
func(i ingressCell) GetCreation() time.Time {
return i.CreationTimestamp.Time
}
func(i ingressCell) GetName() string {
return i.Name
}
(1)列表
(2)获取Ingress详情
(3)创建Ingress
(4) 删除Ingress
(5) 更新Ingress
创建Ingress
对标yaml
service/ingress.go
package service
import (
"context"
"encoding/json"
"errors"
"github.com/wonderivan/logger"
nwv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var Ingress ingress
type ingress struct{}
type IngressesResp struct {
Items []nwv1.Ingress `json:"items"`
Total int `json:"total"`
}
// 定义ServiceCreate结构体,用于创建service需要的参数属性的定义
type IngressCreate struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Label map[string]string `json:"label"`
Hosts map[string][]*HttpPath `json:"hosts"`
}
// 定义ingress的path结构体
type HttpPath struct {
Path string `json:"path"`
PathType nwv1.PathType `json:"path_type"`
ServiceName string `json:"service_name"`
ServicePort int32 `json:"service_port"`
}
// 获取ingress列表,支持过滤、排序、分页
func (i *ingress) GetIngresses(filterName, namespace string, limit, page int) (ingressesResp *IngressesResp, err error) {
//获取ingressList类型的ingress列表
ingressList, err := K8s.ClientSet.NetworkingV1().Ingresses(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
logger.Error(errors.New("获取Ingress列表失败, " + err.Error()))
return nil, errors.New("获取Ingress列表失败, " + err.Error())
}
//将ingressList中的ingress列表(Items),放进dataselector对象中,进行排序
selectableData := &dataSelector{
GenericDataList: i.toCells(ingressList.Items),
DataSelect: &DataSelectQuery{
Filter: &FilterQuery{Name: filterName},
Paginate: &PaginateQuery{
Limit: limit,
Page: page,
},
},
}
filtered := selectableData.Filter()
total := len(filtered.GenericDataList)
data := filtered.Sort().Paginate()
//将[]DataCell类型的ingress列表转为v1.ingress列表
ingresss := i.fromCells(data.GenericDataList)
return &IngressesResp{
Items: ingresss,
Total: total,
}, nil
}
// 获取ingress详情
func (i *ingress) GetIngresstDetail(ingressName, namespace string) (ingress *nwv1.Ingress, err error) {
ingress, err = K8s.ClientSet.NetworkingV1().Ingresses(namespace).Get(context.TODO(), ingressName, metav1.GetOptions{})
if err != nil {
logger.Error(errors.New("获取Ingress详情失败, " + err.Error()))
return nil, errors.New("获取Ingress详情失败, " + err.Error())
}
return ingress, nil
}
// 创建ingress
func (i *ingress) CreateIngress(data *IngressCreate) (err error) {
//声明nwv1.IngressRule和nwv1.HTTPIngressPath变量,后面组装数据于鏊用到
var ingressRules []nwv1.IngressRule
var httpIngressPATHs []nwv1.HTTPIngressPath
//将data中的数据组装成nwv1.Ingress对象
ingress := &nwv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: data.Name,
Namespace: data.Namespace,
Labels: data.Label,
},
Status: nwv1.IngressStatus{},
}
//第一层for循环是将host组装成nwv1.IngressRule类型的对象
// 一个host对应一个ingressrule,每个ingressrule中包含一个host和多个path
for key, value := range data.Hosts {
ir := nwv1.IngressRule{
Host: key,
//这里现将nwv1.HTTPIngressRuleValue类型中的Paths置为空,后面组装好数据再赋值
IngressRuleValue: nwv1.IngressRuleValue{
HTTP: &nwv1.HTTPIngressRuleValue{Paths: nil},
},
}
//第二层for循环是将path组装成nwv1.HTTPIngressPath类型的对象
for _, httpPath := range value {
hip := nwv1.HTTPIngressPath{
Path: httpPath.Path,
PathType: &httpPath.PathType,
Backend: nwv1.IngressBackend{
Service: &nwv1.IngressServiceBackend{
Name: httpPath.ServiceName,
Port: nwv1.ServiceBackendPort{
Number: httpPath.ServicePort,
},
},
},
}
//将每个hip对象组装成数组
httpIngressPATHs = append(httpIngressPATHs, hip)
}
//给Paths赋值,前面置为空了
ir.IngressRuleValue.HTTP.Paths = httpIngressPATHs
//将每个ir对象组装成数组,这个ir对象就是IngressRule,每个元素是一个host和多个path
ingressRules = append(ingressRules, ir)
}
//将ingressRules对象加入到ingress的规则中
ingress.Spec.Rules = ingressRules
//创建ingress
_, err = K8s.ClientSet.NetworkingV1().Ingresses(data.Namespace).Create(context.TODO(), ingress, metav1.CreateOptions{})
if err != nil {
logger.Error(errors.New("创建Ingress失败, " + err.Error()))
return errors.New("创建Ingress失败, " + err.Error())
}
return nil
}
// 删除ingress
func (i *ingress) DeleteIngress(ingressName, namespace string) (err error) {
err = K8s.ClientSet.NetworkingV1().Ingresses(namespace).Delete(context.TODO(), ingressName, metav1.DeleteOptions{})
if err != nil {
logger.Error(errors.New("删除Ingress失败, " + err.Error()))
return errors.New("删除Ingress失败, " + err.Error())
}
return nil
}
// 更新ingress
func (i *ingress) UpdateIngress(namespace, content string) (err error) {
var ingress = &nwv1.Ingress{}
err = json.Unmarshal([]byte(content), ingress)
if err != nil {
logger.Error(errors.New("反序列化失败, " + err.Error()))
return errors.New("反序列化失败, " + err.Error())
}
_, err = K8s.ClientSet.NetworkingV1().Ingresses(namespace).Update(context.TODO(), ingress, metav1.UpdateOptions{})
if err != nil {
logger.Error(errors.New("更新ingress失败, " + err.Error()))
return errors.New("更新ingress失败, " + err.Error())
}
return nil
}
func (i *ingress) toCells(std []nwv1.Ingress) []DataCell {
cells := make([]DataCell, len(std))
for i := range std {
cells[i] = ingressCell(std[i])
}
return cells
}
func (i *ingress) fromCells(cells []DataCell) []nwv1.Ingress {
ingresss := make([]nwv1.Ingress, len(cells))
for i := range cells {
ingresss[i] = nwv1.Ingress(cells[i].(ingressCell))
}
return ingresss
}
controller/ingress.go
package controller
import (
"k8s-platform/service"
"net/http"
"github.com/gin-gonic/gin"
"github.com/wonderivan/logger"
)
var Ingress ingress
type ingress struct{}
//获取ingress列表,支持过滤、排序、分页
func (i *ingress) GetIngresses(ctx *gin.Context) {
params := new(struct {
FilterName string `form:"filter_name"`
Namespace string `form:"namespace"`
Page int `form:"page"`
Limit int `form:"limit"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Ingress.GetIngresses(params.FilterName, params.Namespace, params.Limit, params.Page)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Ingresst列表成功",
"data": data,
})
}
//获取ingress详情
func (i *ingress) GetIngressDetail(ctx *gin.Context) {
params := new(struct {
IngressName string `form:"ingress_name"`
Namespace string `form:"namespace"`
})
if err := ctx.Bind(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
data, err := service.Ingress.GetIngresstDetail(params.IngressName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取Ingress详情成功",
"data": data,
})
}
//删除ingress
func (i *ingress) DeleteIngress(ctx *gin.Context) {
params := new(struct {
IngressName string `json:"ingress_name"`
Namespace string `json:"namespace"`
})
//DELETE请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.Ingress.DeleteIngress(params.IngressName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "删除Ingress成功",
"data": nil,
})
}
//创建ingress
func (i *ingress) CreateIngress(ctx *gin.Context) {
var (
ingressCreate = new(service.IngressCreate)
err error
)
if err = ctx.ShouldBindJSON(ingressCreate); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
if err = service.Ingress.CreateIngress(ingressCreate); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "创建Ingress成功",
"data": nil,
})
}
//更新ingress
func (i *ingress) UpdateIngress(ctx *gin.Context) {
params := new(struct {
Namespace string `json:"namespace"`
Content string `json:"content"`
})
//PUT请求,绑定参数方法改为ctx.ShouldBindJSON
if err := ctx.ShouldBindJSON(params); err != nil {
logger.Error("Bind请求参数失败, " + err.Error())
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
err := service.Ingress.UpdateIngress(params.Namespace, params.Content)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "更新Ingress成功",
"data": nil,
})
}
定义路由
controller/router.go
//ingress操作
GET("/api/k8s/ingresses", Ingress.GetIngresses).
GET("/api/k8s/ingress/detail", Ingress.GetIngressDetail).
DELETE("/api/k8s/ingress/del", Ingress.DeleteIngress).
PUT("/api/k8s/ingress/update", Ingress.UpdateIngress).
POST("/api/k8s/ingress/create", Ingress.CreateIngress)
使用apifox测试api接口
K8s管理系统项目实战[API开发]-2
5、存储与配置
5.1 ConfigMap
5.2 Secret
5.3 PersistentVolumeClaim
6、工作流
6.1 流程设计
6.2 数据库操作(GORM)
(1)初始化数据库
db/db.go
6.3 Workflow
service/workflow.go
(1)列表
(2)获取Workflow详情
(3)新增Workflow
(4)表数据列表
7、中间件
7.1 什么是中间件
7.2 gin中间件用法
7.2 Cors跨域
7.3 JWT token验证
8、WebShell终端
8.1 kubectl exec原理
8.2 实现思路
8.3 代码实现文章来源:https://www.toymoban.com/news/detail-517596.html
9、总结文章来源地址https://www.toymoban.com/news/detail-517596.html
到了这里,关于K8S管理系统项目实战[API开发]-1的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!