forked from kyma-incubator/compass
/
configmap.go
108 lines (98 loc) · 2.75 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package v1_client
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
timeout = time.Second * 10
)
type ConfigMaps interface {
Get(name, namespace string) (*v1.ConfigMap, error)
Create(configMap v1.ConfigMap) error
Update(configMap v1.ConfigMap) error
Delete(configMap v1.ConfigMap) error
}
type ConfigMapClient struct {
client client.Client
log logrus.FieldLogger
}
func NewConfigMapClient(client client.Client, log logrus.FieldLogger) *ConfigMapClient {
return &ConfigMapClient{client: client, log: log}
}
func (c *ConfigMapClient) Get(name, namespace string) (*v1.ConfigMap, error) {
configMap := v1.ConfigMap{}
err := wait.PollImmediate(time.Second, timeout, func() (bool, error) {
err := c.client.Get(context.Background(), client.ObjectKey{Name: name, Namespace: namespace}, &configMap)
if err != nil {
if apiErrors.IsNotFound(err) {
return false, err
}
c.log.Errorf("while creating config map: %v", err)
return false, nil
}
return true, nil
})
if err != nil {
return nil, errors.Wrap(err, "while getting config map")
}
return &configMap, nil
}
func (c *ConfigMapClient) Create(configMap v1.ConfigMap) error {
err := wait.PollImmediate(time.Second, timeout, func() (bool, error) {
err := c.client.Create(context.Background(), &configMap)
if err != nil {
if apiErrors.IsAlreadyExists(err) {
err = c.Update(configMap)
if err != nil {
return false, errors.Wrap(err, "while updating a config map")
}
return true, nil
}
c.log.Errorf("while creating config map: %v", err)
return false, nil
}
return true, nil
})
if err != nil {
return errors.Wrap(err, "while creating config map")
}
return nil
}
func (c *ConfigMapClient) Update(configMap v1.ConfigMap) error {
err := wait.PollImmediate(time.Second, timeout, func() (bool, error) {
err := c.client.Update(context.Background(), &configMap)
if err != nil {
c.log.Errorf("while updating config map: %v", err)
return false, nil
}
return true, nil
})
if err != nil {
return errors.Wrap(err, "while waiting for config map update")
}
return nil
}
func (c *ConfigMapClient) Delete(configMap v1.ConfigMap) error {
err := wait.PollImmediate(time.Second, timeout, func() (bool, error) {
err := c.client.Delete(context.Background(), &configMap)
if err != nil {
if apiErrors.IsNotFound(err) {
c.log.Warn("config map not found")
return true, nil
}
c.log.Errorf("while deleting config map: %v", err)
return false, nil
}
return true, nil
})
if err != nil {
return errors.Wrap(err, "while waiting for config map delete")
}
return nil
}