K8s 管理系统项目[API部分–StatefulSet]
1. 接口实现
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
}
2. StatefulSet功能
service/statefulset.go
2.1 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
}
2.2 获取statefulset详情
// 获取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
}
2.2 删除statefulset
// 删除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
}
2.3 更新statefulset
// 更新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
}
2.4 整个StatefulSet.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 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
}
3. 获取StatefulSet列表
controller/statefulset
package controller
import (
"github.com/gin-gonic/gin"
"github.com/wonderivan/logger"
"k8s-plantform/service"
"net/http"
)
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,
})
}
4. 定义路由
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)
5. 测试StatefulSet方法
5.1 获取statefulset
/api/k8s/statefulsets
5.2 获取statefulset详情
/api/k8s/statefulset/detail
5.3 删除statefulset
/api/k8s/statefulset/del
root@master-01:~# kubectl get statefulsets.apps -n k8s
NAME READY AGE
nginx 2/2 4m33s
root@master-01:~# kubectl get statefulsets.apps -n k8s
No resources found in k8s namespace.
5.4 更新statefulset
/api/k8s/statefulset/update
这个届时也和前端一起测试