-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespace.go
219 lines (185 loc) · 5.94 KB
/
namespace.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"encoding/json"
"fmt"
"strings"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
labels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/cache"
)
func (c *Controller) syncNamespace(key string) error {
log.WithField("namespace", key).Info("Syncing namespace")
_, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
runtime.HandleError(fmt.Errorf("invalid resource key: %s", key))
return nil
}
ns, err := c.namespacesLister.Get(name)
if err != nil {
return err
}
nsLabels := ns.GetLabels()
go c.syncSecretsToNamespace(name, nsLabels)
go c.syncConfigMapsToNamespace(name, nsLabels)
return nil
}
func (c *Controller) syncSecretsToNamespace(ns string, nsLabels map[string]string) {
secrets, err := c.secretsLister.List(labels.Everything())
if err != nil {
log.Error(err)
}
//Create missing secrets
for _, s := range secrets {
if _, ok := s.Annotations[syncAnnotation]; !ok {
//Skip objects that dont have our annotation
continue
}
l := strings.Split(s.Annotations[syncAnnotation], "=")
if len(l) > 2 {
log.WithFields(log.Fields{"secret": s.Name, "annotation": s.Annotations, "split": l, "splitlen": len(l), "namespace": ns}).Warn("Annotation not valid")
continue
}
if s.Annotations[syncAnnotation] == "" || nsLabels[l[0]] == l[1] {
_, err := c.secretsLister.Secrets(ns).Get(s.Name)
if errors.IsNotFound(err) {
log.WithFields(log.Fields{"secret": s.Name, "namespace": ns}).Info("Adding secret")
newSecret := createNewSecret(s)
_, err = c.kubeclientset.CoreV1().Secrets(ns).Create(newSecret)
if err != nil {
log.Error(err)
}
} else if err != nil {
log.Error(err)
}
}
}
c.deleteDeprecatedSecretsFromNs(ns, nsLabels)
}
func (c *Controller) syncConfigMapsToNamespace(ns string, nsLabels map[string]string) {
configMaps, err := c.configMapsLister.List(labels.Everything())
if err != nil {
log.Error(err)
}
for _, cm := range configMaps {
if _, ok := cm.Annotations[syncAnnotation]; !ok {
//Skip objects that dont have our annotation
continue
}
l := strings.Split(cm.Annotations[syncAnnotation], "=")
if len(l) > 2 {
log.WithFields(log.Fields{"configmap": cm.Name, "annotation": cm.Annotations, "split": l, "splitlen": len(l), "namespace": ns}).Warn("Annotation not valid")
continue
}
if cm.Annotations[syncAnnotation] == "" || nsLabels[l[0]] == l[1] {
_, err := c.configMapsLister.ConfigMaps(ns).Get(cm.Name)
if errors.IsNotFound(err) {
log.WithFields(log.Fields{"configmap": cm.Name, "namespace": ns}).Info("Adding configmap")
newConfigMap := createNewConfigMap(cm)
_, err = c.kubeclientset.CoreV1().ConfigMaps(ns).Create(newConfigMap)
if err != nil {
log.Error(err)
}
} else if err != nil {
log.Error(err)
}
}
}
c.deleteDeprecatedConfigMapsFromNs(ns, nsLabels)
}
func (c *Controller) namespacesForLabel(label string) (sets.String, error) {
var namespaces []*v1.Namespace
var err error
if label != "" {
l := strings.Split(label, "=")
if len(l) != 2 {
return nil, fmt.Errorf("%s not valid label", label)
}
labelsmap := labels.Set{
l[0]: l[1],
}
namespaces, err = c.namespacesLister.List(labels.SelectorFromSet(labelsmap))
} else {
namespaces, err = c.namespacesLister.List(labels.Everything())
}
if err != nil {
return nil, err
}
ns := sets.NewString()
for _, obj := range namespaces {
ns.Insert(obj.Name)
}
return ns, nil
}
func (c *Controller) deleteDeprecatedConfigMapsFromNs(ns string, nsLabels map[string]string) {
//Delete configmaps that dont match to labels anymore
configMaps, err := c.configMapsLister.ConfigMaps(ns).List(labels.Everything())
for _, configMap := range configMaps {
cma, ok := configMap.Annotations[fmt.Sprintf("%s-%s", syncAnnotation, "metadata")]
if !ok {
continue
}
l, succeed := jsonLabelToArray(cma)
if !succeed || len(l) == 0 {
continue
}
//Label still exists
if len(l) == 2 && nsLabels[l[0]] == l[1] {
log.Debug("ConfigMap matched labels")
continue
}
log.WithFields(log.Fields{"nsLabels": nsLabels, "l": l, "wtf": nsLabels[l[0]]}).Debug("Configmap didnt match labels")
err = c.kubeclientset.CoreV1().ConfigMaps(ns).Delete(configMap.Name, &metav1.DeleteOptions{})
if err != nil {
log.Error(err)
}
log.WithFields(log.Fields{"configmap": configMap.Name, "namespace": ns}).Info("ConfigMap deleted")
}
}
func (c *Controller) deleteDeprecatedSecretsFromNs(ns string, nsLabels map[string]string) {
//Delete secrets that dont match to labels anymore
secrets, err := c.secretsLister.Secrets(ns).List(labels.Everything())
for _, secret := range secrets {
sa, ok := secret.Annotations[fmt.Sprintf("%s-%s", syncAnnotation, "metadata")]
if !ok {
continue
}
l, succeed := jsonLabelToArray(sa)
if !succeed || len(l) == 0 {
continue
}
//Label still exists
if len(l) == 2 && nsLabels[l[0]] == l[1] {
log.Debug("Secret matched labels")
continue
}
log.WithFields(log.Fields{"nsLabels": nsLabels, "l": l, "wtf": nsLabels[l[0]]}).Debug("Secret didnt match labels")
err = c.kubeclientset.CoreV1().Secrets(ns).Delete(secret.Name, &metav1.DeleteOptions{})
if err != nil {
log.Error(err)
}
log.WithFields(log.Fields{"secret": secret.Name, "namespace": ns}).Info("Secret deleted")
}
}
func jsonLabelToArray(jsonLabel string) (label []string, succeed bool) {
m := make(map[string]string)
err := json.Unmarshal([]byte(jsonLabel), &m)
if err != nil {
log.WithFields(log.Fields{"data": jsonLabel}).Error(err)
return []string{}, false
}
//Global configMap
if m["label"] == "" {
return []string{}, true
}
label = strings.Split(m["label"], "=")
if len(label) > 2 {
log.WithFields(log.Fields{"data": jsonLabel, "label": label, "labellen": len(label)}).Warn("Annotation not valid")
return []string{}, false
}
return label, true
}