-
Notifications
You must be signed in to change notification settings - Fork 115
/
configmap.go
89 lines (73 loc) · 2.46 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package k8s
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sapiv1 "github.com/lyft/clutch/backend/api/k8s/v1"
)
func (s *svc) DescribeConfigMap(ctx context.Context, clientset, cluster, namespace, name string) (*k8sapiv1.ConfigMap, error) {
cs, err := s.manager.GetK8sClientset(ctx, clientset, cluster, namespace)
if err != nil {
return nil, err
}
configMapList, err := cs.CoreV1().ConfigMaps(cs.Namespace()).List(ctx, metav1.ListOptions{
FieldSelector: "metadata.name=" + name,
})
if err != nil {
return nil, err
}
if len(configMapList.Items) == 1 {
return protoForConfigMap(cs.Cluster(), &configMapList.Items[0]), nil
} else if len(configMapList.Items) > 1 {
return nil, status.Errorf(codes.FailedPrecondition, "located multiple config maps with name '%s'", name)
}
return nil, status.Error(codes.NotFound, "unable to locate specified config map")
}
func (s *svc) DeleteConfigMap(ctx context.Context, clientset, cluster, namespace, name string) error {
cs, err := s.manager.GetK8sClientset(ctx, clientset, cluster, namespace)
if err != nil {
return err
}
opts := metav1.DeleteOptions{}
return cs.CoreV1().ConfigMaps(cs.Namespace()).Delete(ctx, name, opts)
}
func (s *svc) ListConfigMaps(ctx context.Context, clientset, cluster, namespace string, listOptions *k8sapiv1.ListOptions) ([]*k8sapiv1.ConfigMap, error) {
cs, err := s.manager.GetK8sClientset(ctx, clientset, cluster, namespace)
if err != nil {
return nil, err
}
opts, err := ApplyListOptions(listOptions)
if err != nil {
return nil, err
}
configMapList, err := cs.CoreV1().ConfigMaps(cs.Namespace()).List(ctx, opts)
if err != nil {
return nil, err
}
var configMaps []*k8sapiv1.ConfigMap
for _, cm := range configMapList.Items {
configMap := cm
configMaps = append(configMaps, protoForConfigMap(cs.Cluster(), &configMap))
}
return configMaps, nil
}
func protoForConfigMap(cluster string, k8sconfigMap *v1.ConfigMap) *k8sapiv1.ConfigMap {
clusterName := GetKubeClusterName(k8sconfigMap)
if clusterName == "" {
clusterName = cluster
}
// required fields
ret := &k8sapiv1.ConfigMap{
Cluster: clusterName,
Namespace: k8sconfigMap.Namespace,
Name: k8sconfigMap.Name,
Labels: k8sconfigMap.Labels,
Annotations: k8sconfigMap.Annotations,
// optional fields
Data: k8sconfigMap.Data,
BinaryData: k8sconfigMap.BinaryData,
}
return ret
}