-
Notifications
You must be signed in to change notification settings - Fork 32
/
k8s_configmap.go
88 lines (74 loc) · 2 KB
/
k8s_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
package k8s
import (
"context"
v1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (c *Client) GetConfigMap(ctx context.Context, name string) (*v1.ConfigMap, error) {
cm, err := c.clientset.CoreV1().ConfigMaps(c.namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, ErrGettingConfigmap.WithParams(name).Wrap(err)
}
return cm, nil
}
func (c *Client) ConfigMapExists(ctx context.Context, name string) (bool, error) {
_, err := c.clientset.CoreV1().ConfigMaps(c.namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrs.IsNotFound(err) {
return false, nil
}
return false, ErrGettingConfigmap.WithParams(name).Wrap(err)
}
return true, nil
}
func (c *Client) CreateConfigMap(
ctx context.Context,
name string,
labels, data map[string]string,
) (*v1.ConfigMap, error) {
exists, err := c.ConfigMapExists(ctx, name)
if err != nil {
return nil, err
}
if exists {
return nil, ErrConfigmapAlreadyExists.WithParams(name)
}
cm, err := prepareConfigMap(c.namespace, name, labels, data)
if err != nil {
return nil, err
}
created, err := c.clientset.CoreV1().ConfigMaps(c.namespace).Create(ctx, cm, metav1.CreateOptions{})
if err != nil {
return nil, ErrCreatingConfigmap.WithParams(name).Wrap(err)
}
return created, nil
}
func (c *Client) DeleteConfigMap(ctx context.Context, name string) error {
exists, err := c.ConfigMapExists(ctx, name)
if err != nil {
return err
}
if !exists {
return ErrConfigmapDoesNotExist.WithParams(name)
}
err = c.clientset.CoreV1().ConfigMaps(c.namespace).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
return ErrDeletingConfigmap.WithParams(name).Wrap(err)
}
return nil
}
func prepareConfigMap(
namespace, name string,
labels, data map[string]string,
) (*v1.ConfigMap, error) {
cm := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Data: data,
}
return cm, nil
}