mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 10:58:35 +08:00
【修改】k8s 配置
This commit is contained in:
102
apps/devops/services/k8s/configmap/configmap.go
Normal file
102
apps/devops/services/k8s/configmap/configmap.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package configmap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"pandax/apps/devops/entity/k8s"
|
||||
k8scommon "pandax/apps/devops/services/k8s/common"
|
||||
"pandax/apps/devops/services/k8s/dataselect"
|
||||
"pandax/base/global"
|
||||
)
|
||||
|
||||
// ConfigMapList contains a list of Config Maps in the cluster.
|
||||
type ConfigMapList struct {
|
||||
ListMeta k8s.ListMeta `json:"listMeta"`
|
||||
|
||||
// Unordered list of Config Maps
|
||||
Items []ConfigMap `json:"items"`
|
||||
}
|
||||
|
||||
// ConfigMap API resource provides mechanisms to inject containers with configuration data while keeping
|
||||
// containers agnostic of Kubernetes
|
||||
type ConfigMap struct {
|
||||
ObjectMeta k8s.ObjectMeta `json:"objectMeta"`
|
||||
TypeMeta k8s.TypeMeta `json:"typeMeta"`
|
||||
}
|
||||
|
||||
// GetConfigMapList returns a list of all ConfigMaps in the cluster.
|
||||
func GetConfigMapList(client kubernetes.Interface, nsQuery *k8scommon.NamespaceQuery, dsQuery *dataselect.DataSelectQuery) (*ConfigMapList, error) {
|
||||
global.Log.Info(fmt.Sprintf("Getting list config maps in the namespace %s", nsQuery.ToRequestParam()))
|
||||
channels := &k8scommon.ResourceChannels{
|
||||
ConfigMapList: k8scommon.GetConfigMapListChannel(client, nsQuery, 1),
|
||||
}
|
||||
|
||||
return GetConfigMapListFromChannels(channels, dsQuery)
|
||||
}
|
||||
|
||||
// GetConfigMapListFromChannels returns a list of all Config Maps in the cluster reading required resource list once from the channels.
|
||||
func GetConfigMapListFromChannels(channels *k8scommon.ResourceChannels, dsQuery *dataselect.DataSelectQuery) (*ConfigMapList, error) {
|
||||
configMaps := <-channels.ConfigMapList.List
|
||||
err := <-channels.ConfigMapList.Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := toConfigMapList(configMaps.Items, dsQuery)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func toConfigMap(meta metaV1.ObjectMeta) ConfigMap {
|
||||
return ConfigMap{
|
||||
ObjectMeta: k8s.NewObjectMeta(meta),
|
||||
TypeMeta: k8s.NewTypeMeta(k8s.ResourceKindConfigMap),
|
||||
}
|
||||
}
|
||||
|
||||
func toConfigMapList(configMaps []v1.ConfigMap, dsQuery *dataselect.DataSelectQuery) *ConfigMapList {
|
||||
result := &ConfigMapList{
|
||||
Items: make([]ConfigMap, 0),
|
||||
ListMeta: k8s.ListMeta{TotalItems: len(configMaps)},
|
||||
}
|
||||
|
||||
configMapCells, filteredTotal := dataselect.GenericDataSelectWithFilter(toCells(configMaps), dsQuery)
|
||||
configMaps = fromCells(configMapCells)
|
||||
result.ListMeta = k8s.ListMeta{TotalItems: filteredTotal}
|
||||
|
||||
for _, item := range configMaps {
|
||||
result.Items = append(result.Items, toConfigMap(item.ObjectMeta))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func DeleteConfigMap(client *kubernetes.Clientset, namespace string, name string) error {
|
||||
global.Log.Info(fmt.Sprintf("请求删除ConfigMap: %v, namespace: %v", name, namespace))
|
||||
return client.CoreV1().ConfigMaps(namespace).Delete(
|
||||
context.TODO(),
|
||||
name,
|
||||
metaV1.DeleteOptions{},
|
||||
)
|
||||
}
|
||||
|
||||
func DeleteCollectionConfigMap(client *kubernetes.Clientset, configMapList []k8s.ConfigMapData) (err error) {
|
||||
global.Log.Info("批量删除ConfigMap开始")
|
||||
for _, v := range configMapList {
|
||||
global.Log.Info(fmt.Sprintf("delete configMap:%v, ns: %v", v.Name, v.Namespace))
|
||||
err := client.CoreV1().ConfigMaps(v.Namespace).Delete(
|
||||
context.TODO(),
|
||||
v.Name,
|
||||
metaV1.DeleteOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
global.Log.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
global.Log.Info("删除ConfigMap已完成")
|
||||
return nil
|
||||
}
|
||||
40
apps/devops/services/k8s/configmap/configmap_common.go
Normal file
40
apps/devops/services/k8s/configmap/configmap_common.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package configmap
|
||||
|
||||
import (
|
||||
api "k8s.io/api/core/v1"
|
||||
"pandax/apps/devops/services/k8s/dataselect"
|
||||
)
|
||||
|
||||
// The code below allows to perform complex data section on []api.ConfigMap
|
||||
|
||||
type ConfigMapCell api.ConfigMap
|
||||
|
||||
func (self ConfigMapCell) GetProperty(name dataselect.PropertyName) dataselect.ComparableValue {
|
||||
switch name {
|
||||
case dataselect.NameProperty:
|
||||
return dataselect.StdComparableString(self.ObjectMeta.Name)
|
||||
case dataselect.CreationTimestampProperty:
|
||||
return dataselect.StdComparableTime(self.ObjectMeta.CreationTimestamp.Time)
|
||||
case dataselect.NamespaceProperty:
|
||||
return dataselect.StdComparableString(self.ObjectMeta.Namespace)
|
||||
default:
|
||||
// if name is not supported then just return a constant dummy value, sort will have no effect.
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func toCells(std []api.ConfigMap) []dataselect.DataCell {
|
||||
cells := make([]dataselect.DataCell, len(std))
|
||||
for i := range std {
|
||||
cells[i] = ConfigMapCell(std[i])
|
||||
}
|
||||
return cells
|
||||
}
|
||||
|
||||
func fromCells(cells []dataselect.DataCell) []api.ConfigMap {
|
||||
std := make([]api.ConfigMap, len(cells))
|
||||
for i := range std {
|
||||
std[i] = api.ConfigMap(cells[i].(ConfigMapCell))
|
||||
}
|
||||
return std
|
||||
}
|
||||
42
apps/devops/services/k8s/configmap/configmap_detail.go
Normal file
42
apps/devops/services/k8s/configmap/configmap_detail.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package configmap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"pandax/base/global"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
// ConfigMapDetail API resource provides mechanisms to inject containers with configuration data while keeping
|
||||
// containers agnostic of Kubernetes
|
||||
type ConfigMapDetail struct {
|
||||
// Extends list item structure.
|
||||
ConfigMap `json:",inline"`
|
||||
|
||||
// Data contains the configuration data.
|
||||
// Each key must be a valid DNS_SUBDOMAIN with an optional leading dot.
|
||||
Data map[string]string `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// GetConfigMapDetail returns detailed information about a config map
|
||||
func GetConfigMapDetail(client kubernetes.Interface, namespace, name string) (*ConfigMapDetail, error) {
|
||||
global.Log.Info(fmt.Sprintf("Getting details of %s config map in %s namespace", name, namespace))
|
||||
|
||||
rawConfigMap, err := client.CoreV1().ConfigMaps(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return getConfigMapDetail(rawConfigMap), nil
|
||||
}
|
||||
|
||||
func getConfigMapDetail(rawConfigMap *v1.ConfigMap) *ConfigMapDetail {
|
||||
return &ConfigMapDetail{
|
||||
ConfigMap: toConfigMap(rawConfigMap.ObjectMeta),
|
||||
Data: rawConfigMap.Data,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user