【GO】K8s 管理系统项目10[API部分--Configmap]
【GO】K8s 管理系统项目[API部分--Configmap]
·
K8s 管理系统项目[API部分–Configmap]
1. 接口实现
service/dataselector.go
// configmap
type configMapCell corev1.ConfigMap
func (c configMapCell) GetCreation() time.Time {
return c.CreationTimestamp.Time
}
func (c configMapCell) GetName() string {
return c.Name
}
2. Configmap功能
service/configmap.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"
)
var ConfigMap configMap
type configMap struct{}
type ConfigMapsResp struct {
Items []corev1.ConfigMap `json:"items"`
Total int `json:"total"`
}
// 获取configmap列表,支持过滤、排序、分页
func (c *configMap) GetConfigMaps(filterName, namespace string, limit, page int) (configMapsResp *ConfigMapsResp, err error) {
//获取configMapList类型的configMap列表
configMapList, err := K8s.ClientSet.CoreV1().ConfigMaps(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
logger.Error(errors.New("获取ConfigMap列表失败, " + err.Error()))
return nil, errors.New("获取ConfigMap列表失败, " + err.Error())
}
//将configMapList中的configMap列表(Items),放进dataselector对象中,进行排序
selectableData := &dataSelector{
GenericDataList: c.toCells(configMapList.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类型的configmap列表转为v1.configmap列表
configMaps := c.fromCells(data.GenericDataList)
return &ConfigMapsResp{
Items: configMaps,
Total: total,
}, nil
}
// 获取configmap详情
func (c *configMap) GetConfigMapDetail(configMapName, namespace string) (configMap *corev1.ConfigMap, err error) {
configMap, err = K8s.ClientSet.CoreV1().ConfigMaps(namespace).Get(context.TODO(), configMapName, metav1.GetOptions{})
if err != nil {
logger.Error(errors.New("获取ConfigMap详情失败, " + err.Error()))
return nil, errors.New("获取ConfigMap详情失败, " + err.Error())
}
return configMap, nil
}
// 删除configmap
func (c *configMap) DeleteConfigMap(configMapName, namespace string) (err error) {
err = K8s.ClientSet.CoreV1().ConfigMaps(namespace).Delete(context.TODO(), configMapName, metav1.DeleteOptions{})
if err != nil {
logger.Error(errors.New("删除ConfigMap失败, " + err.Error()))
return errors.New("删除ConfigMap失败, " + err.Error())
}
return nil
}
// 更新configmap
func (c *configMap) UpdateConfigMap(namespace, content string) (err error) {
var configMap = &corev1.ConfigMap{}
err = json.Unmarshal([]byte(content), configMap)
if err != nil {
logger.Error(errors.New("反序列化失败, " + err.Error()))
return errors.New("反序列化失败, " + err.Error())
}
_, err = K8s.ClientSet.CoreV1().ConfigMaps(namespace).Update(context.TODO(), configMap, metav1.UpdateOptions{})
if err != nil {
logger.Error(errors.New("更新ConfigMap失败, " + err.Error()))
return errors.New("更新ConfigMap失败, " + err.Error())
}
return nil
}
func (c *configMap) toCells(std []corev1.ConfigMap) []DataCell {
cells := make([]DataCell, len(std))
for i := range std {
cells[i] = configMapCell(std[i])
}
return cells
}
func (c *configMap) fromCells(cells []DataCell) []corev1.ConfigMap {
configMaps := make([]corev1.ConfigMap, len(cells))
for i := range cells {
configMaps[i] = corev1.ConfigMap(cells[i].(configMapCell))
}
return configMaps
}
3. 获取Configmap列表
controller/configmap.go
package controller
import (
"k8s-plantform/service"
"net/http"
"github.com/gin-gonic/gin"
"github.com/wonderivan/logger"
)
var ConfigMap configMap
type configMap struct{}
// 获取configmap列表,支持过滤、排序、分页
func (c *configMap) GetConfigMaps(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.ConfigMap.GetConfigMaps(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": "获取ConfigMap列表成功",
"data": data,
})
}
// 获取configmap详情
func (c *configMap) GetConfigMapDetail(ctx *gin.Context) {
params := new(struct {
ConfigMapName string `form:"configmap_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.ConfigMap.GetConfigMapDetail(params.ConfigMapName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "获取ConfigMap详情成功",
"data": data,
})
}
// 删除configmap
func (c *configMap) DeleteConfigMap(ctx *gin.Context) {
params := new(struct {
ConfigMapName string `json:"configmap_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.ConfigMap.DeleteConfigMap(params.ConfigMapName, params.Namespace)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "删除ConfigMap成功",
"data": nil,
})
}
// 更新configmap
func (c *configMap) UpdateConfigMap(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.ConfigMap.UpdateConfigMap(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": "更新ConfigMap成功",
"data": nil,
})
}
4. 定义路由
controller/router.go
// Configmaps
GET("/api/k8s/configmaps", ConfigMap.GetConfigMaps).
GET("/api/k8s/configmap/detail", ConfigMap.GetConfigMapDetail).
DELETE("/api/k8s/configmap/del", ConfigMap.DeleteConfigMap).
PUT("/api/k8s/configmap/update", ConfigMap.UpdateConfigMap)
5. 测试Configmap方法
5.1 获取Configmap
5.2 获取Configmap详情
其余等做好前端一起测试
更多推荐
已为社区贡献47条内容
所有评论(0)