-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigmap.go
69 lines (59 loc) · 1.81 KB
/
configmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package resources
import (
"fmt"
"log"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type ConfigMaps struct {
clientSet *kubernetes.Clientset
namespace string
name string
}
func NewConfigMaps(clientSet *kubernetes.Clientset, namespace, name string) *ConfigMaps {
return &ConfigMaps{clientSet: clientSet, namespace: namespace, name: name}
}
func (cm *ConfigMaps) Get() {
if len(cm.name) > 0 {
cm.getConfigMap()
} else {
cm.getConfigMapList()
}
}
func (cm *ConfigMaps) Delete() {
err := cm.clientSet.CoreV1().ConfigMaps(cm.namespace).Delete(cm.name, &metav1.DeleteOptions{})
if err != nil {
log.Fatalf("[Error] Delete configmaps %s failed: %s\n", cm.name, err.Error())
} else {
fmt.Printf("configmaps \"%s\" deleted\n", cm.name)
}
}
func (cm *ConfigMaps) getConfigMap() {
configMap, err := cm.clientSet.CoreV1().ConfigMaps(cm.namespace).Get(cm.name, metav1.GetOptions{})
if err != nil {
log.Fatalln("[Error] GetConfigMap()", err.Error())
}
cm.printConfigMap(configMap)
}
func (cm *ConfigMaps) getConfigMapList() {
configMaps, err := cm.clientSet.CoreV1().ConfigMaps(cm.namespace).List(metav1.ListOptions{})
if err != nil {
log.Fatalln("[Error] GetConfigMaps()", err.Error())
}
cm.printConfigMaps(configMaps)
}
func (cm *ConfigMaps) printConfigMap(configMap *v1.ConfigMap) {
fmt.Printf("NAME\t\t\t DATA\t AGE\n")
fmt.Printf("%-24s %d\t %s\n", configMap.Name, len(configMap.Data), configMap.CreationTimestamp)
}
func (cm *ConfigMaps) printConfigMaps(configMaps *v1.ConfigMapList) {
if len(configMaps.Items) > 0 {
fmt.Printf("NAME\t\t\t DATA\t AGE\n")
for _, configMap := range configMaps.Items {
fmt.Printf("%-24s %d\t %s\n", configMap.Name, len(configMap.Data), configMap.CreationTimestamp)
}
} else {
fmt.Println(ERR_NO_RESOURCE)
}
}