K8s 管理系统项目[API部分–Daemonset]
1. 接口实现
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
}
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 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 DaemonSet daemonSet
type 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 controller
import (
"github.com/gin-gonic/gin"
"github.com/wonderivan/logger"
"k8s-plantform/service"
"net/http"
)
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,
})
}
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
这个结合前端一起测