【GO】 K8s 管理系统项目[API部分--Daemonset]

news/2024/11/24 9:32:52/

K8s 管理系统项目[API部分–Daemonset]

1. 接口实现

service/dataselector.go

// daemonCell
type daemonSetCell appsv1.DaemonSetfunc(d daemonSetCell) GetCreation() time.Time {return d.CreationTimestamp.Time
}func(d daemonSetCell) GetName() string {return d.Name
}

2. DaemontSet功能

service/daemonset.go

2.1 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
}

2.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
}

2.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
}

2.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
}

2.5 整个Daemonset.go

package serviceimport ("context""encoding/json""errors""github.com/wonderivan/logger"appsv1 "k8s.io/api/apps/v1"metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)var DaemonSet daemonSettype daemonSet struct{}type DaemonSetsResp struct {Items []appsv1.DaemonSet `json:"items"`Total int                `json:"total"`
}// 获取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
}// 获取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
}// 删除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
}// 更新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
}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
}

3. 获取DaemonSet列表

controller/daemonset.go

package controllerimport ("github.com/gin-gonic/gin""github.com/wonderivan/logger""k8s-plantform/service""net/http"
)var DaemonSet daemonSettype 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.ShouldBindJSONif 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.ShouldBindJSONif 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,})
}

4. 定义路由

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)

5. 测试Daemonset方法

5.1 获取Daemonset

/api/k8s/daemonsets

请添加图片描述

5.2 获取Daemonset详情

/api/k8s/daemonset/detail

请添加图片描述

5.3 删除Daemonset

/api/k8s/daemonset/del
root@master-01:~# kubectl get daemonsets.apps -n k8s
NAME                    DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
fluentd-elasticsearch   3         3         0       3            0           <none>          10s

请添加图片描述

root@master-01:~# kubectl get daemonsets.apps -n k8s
No resources found in k8s namespace.

5.4 更新Daemonset

/api/k8s/daemonset/update

这个结合前端一起测


http://www.ppmy.cn/news/5038.html

相关文章

kettle - get请求

文章目录前言kettle - get请求前言 如果您觉得有用的话&#xff0c;记得给博主点个赞&#xff0c;评论&#xff0c;收藏一键三连啊&#xff0c;写作不易啊^ _ ^。   而且听说点赞的人每天的运气都不会太差&#xff0c;实在白嫖的话&#xff0c;那欢迎常来啊!!! kettle - get请…

[附源码]计算机毕业设计Python高校商铺管理系统论文(程序+源码+LW文档)

该项目含有源码、文档、程序、数据库、配套开发软件、软件安装教程 项目运行 环境配置&#xff1a; Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术&#xff1a; django python Vue 等等组成&#xff0c;B/S模式 pychram管理等…

毕业设计 单片机GPS定位位置记录系统 - 物联网 嵌入式 stm32

文章目录0 前言1 简介2 主要器件3 实现效果4 硬件设计概述硬件模块的连接**AB32VG1主控MCU**5 软件说明6 部分核心代码7 最后0 前言 &#x1f525; 这两年开始毕业设计和毕业答辩的要求和难度不断提升&#xff0c;传统的毕设题目缺少创新和亮点&#xff0c;往往达不到毕业答辩…

Spring Boot 访问Redis的三种方式

前言 最近在极客时间上面学习丁雪丰老师的《玩转 Spring 全家桶》&#xff0c;其中讲到访问Redis的方式&#xff0c;我专门把他们抽出来&#xff0c;在一起对比下&#xff0c;体验一下三种方式开发上面的不同&#xff0c; 分别是这三种方式 RedisTemplate JPA Repository Cach…

React源码分析6-hooks源码

本文将讲解 hooks 的执行过程以及常用的 hooks 的源码。 hooks 相关数据结构 要理解 hooks 的执行过程&#xff0c;首先想要大家对 hooks 相关的数据结构有所了解&#xff0c;便于后面大家顺畅地阅读代码。 Hook 每一个 hooks 方法都会生成一个类型为 Hook 的对象&#xff…

中望3D二次开发 控制台命令转PDF

中望3D的外部开发模式命令非常少&#xff0c;没有办法使用远程办法打开文件&#xff0c;将图纸转换为PDF&#xff08;听说以后的版本会有&#xff0c;但是在2022版本上是没有的&#xff09;&#xff1b; ps&#xff1a;远程方式&#xff0c;意思就是远程电脑必须开启中望3D软件…

driftingblues3靶机(auth.log日志、命令执行)

环境准备 靶机链接&#xff1a;百度网盘 请输入提取码 提取码&#xff1a;yc07 虚拟机网络链接模式&#xff1a;桥接模式 攻击机系统&#xff1a;kali linux 2021.1 信息收集 1.探测目标靶机开放端口和服务情况 2.用dirsearch扫描出目录 dirsearch -u 192.168.1.101 漏洞…

雪花算法原理

SnowFlake算法生成id的结果是一个64bit大小的整数&#xff0c;它的结构如下图&#xff1a;1bit&#xff0c;不用&#xff0c;因为二进制中最高位是符号位&#xff0c;1表示负数&#xff0c;0表示正数。生成的id一般都是用整数&#xff0c;所以最高位固定为0。41bit时间戳&#…