forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
331 lines (261 loc) · 9.34 KB
/
controller.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package controller
import (
"fmt"
"log"
"github.com/kyma-project/kyma/components/environments/internal"
"github.com/kyma-project/kyma/components/environments/internal/limit_range"
"github.com/kyma-project/kyma/components/environments/internal/namespaces"
rq "github.com/kyma-project/kyma/components/environments/internal/resource-quota"
"github.com/kyma-project/kyma/components/environments/internal/roles"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
const (
rolesAnnotName = "kyma-roles"
labelSelector = "env=true"
istioInjectionLabel = "istio-injection"
istioInjectionLabelValue = "enabled"
resourceQuotaName = "kyma-default"
limitRangeName = "kyma-default"
)
var listOptions = metav1.ListOptions{LabelSelector: labelSelector}
type environments struct {
Clientset *kubernetes.Clientset
NamespacesClient namespaces.NamespacesClientInterface
RolesClient roles.RolesClientInterface
LimitRangeClient limit_range.LimitRangesClientInterface
ResourceQuotaClient rq.ResourceQuotaClientInterface
Config *EnvironmentsConfig
ErrorHandlers internal.ErrorHandlersInterface
}
func NewController(clientset *kubernetes.Clientset, config *EnvironmentsConfig) (cache.Controller, error) {
envs := environments{
Clientset: clientset,
Config: config,
NamespacesClient: &namespaces.NamespacesClient{Clientset: clientset},
RolesClient: &roles.RolesClient{Clientset: clientset},
LimitRangeClient: &limit_range.LimitRangeClient{Clientset: clientset},
ResourceQuotaClient: &rq.ResourceQuotaClient{Clientset: clientset},
ErrorHandlers: &internal.ErrorHandlers{},
}
_, controller := cache.NewInformer(
enviromentsWatcher{clientset.CoreV1().Namespaces()},
&v1.Namespace{},
0,
cache.ResourceEventHandlerFuncs{
AddFunc: envs.onAdd,
UpdateFunc: func(oldObj, newObj interface{}) {},
DeleteFunc: envs.onDelete,
})
return controller, nil
}
func (envs *environments) onAdd(obj interface{}) {
namespace := obj.(*v1.Namespace)
log.Printf("[CONTROLLER] onAdd triggered for %s\n", namespace.Name)
envs.AddRolesForEnvironment(namespace)
envs.LabelWithIstioInjection(namespace)
envs.CreateLimitRangeForEnv(namespace)
envs.CreateResourceQuota(namespace)
}
func (envs *environments) onDelete(obj interface{}) {
namespace := obj.(*v1.Namespace)
log.Printf("[CONTROLLER] onDelete triggered for %s\n", namespace.Name)
envs.RemoveRolesFromEnvironment(namespace)
envs.RemoveIstioInjectionLabel(namespace)
envs.DeleteLimitRange(namespace)
envs.DeleteResourceQuota(namespace)
}
func hasRoles(namespace *v1.Namespace) bool {
return contains(namespace.ObjectMeta.Annotations, rolesAnnotName)
}
func (envs *environments) AddRolesForEnvironment(environment *v1.Namespace) error {
namespace, err := envs.NamespacesClient.GetNamespace(environment.Name)
if envs.ErrorHandlers.CheckError("Error while getting namespace.", err) {
return err
}
if hasRoles(namespace) {
log.Println("Namespace already have default roles.")
return nil
}
rolesToBootstrap, err := envs.RolesClient.GetList(envs.Config.Namespace, listOptions)
if envs.ErrorHandlers.CheckError("Error on fetching roles to bootstrap.", err) {
return err
}
for _, role := range rolesToBootstrap.Items {
roleCopy := role.DeepCopy()
roleCopy.ObjectMeta = metav1.ObjectMeta{
Name: role.ObjectMeta.Name,
Namespace: namespace.Name,
}
_, err = envs.RolesClient.CreateRole(roleCopy, namespace.Name)
envs.ErrorHandlers.LogError(fmt.Sprintf("Error on creating %s role", roleCopy.ObjectMeta.Name), err)
}
err = envs.annotateObject(namespace, rolesAnnotName)
if envs.ErrorHandlers.CheckError("Error on updating namespace.", err) {
return err
}
return nil
}
func (envs *environments) LabelWithIstioInjection(environment *v1.Namespace) error {
namespace, err := envs.NamespacesClient.GetNamespace(environment.Name)
if envs.ErrorHandlers.CheckError("Error while getting namespace.", err) {
return err
}
err = envs.labelNamespace(namespace, istioInjectionLabel, istioInjectionLabelValue)
if envs.ErrorHandlers.CheckError("Error on updating namespace.", err) {
return err
}
return nil
}
func (envs *environments) CreateLimitRangeForEnv(environment *v1.Namespace) error {
err := envs.LimitRangeClient.CreateLimitRange(environment.Name, &v1.LimitRange{
ObjectMeta: metav1.ObjectMeta{
Name: limitRangeName,
},
Spec: v1.LimitRangeSpec{
Limits: []v1.LimitRangeItem{
{
Type: v1.LimitTypeContainer,
Default: v1.ResourceList{
v1.ResourceMemory: *envs.Config.LimitRangeMemory.Default.AsQuantity(),
},
DefaultRequest: v1.ResourceList{
v1.ResourceMemory: *envs.Config.LimitRangeMemory.DefaultRequest.AsQuantity(),
},
Max: v1.ResourceList{
v1.ResourceMemory: *envs.Config.LimitRangeMemory.Max.AsQuantity(),
},
},
},
},
})
if envs.ErrorHandlers.CheckError("Error on creating limit range", err) {
return err
}
return nil
}
func (envs *environments) CreateResourceQuota(namespace *v1.Namespace) error {
err := envs.ResourceQuotaClient.CreateResourceQuota(namespace.Name, &v1.ResourceQuota{
ObjectMeta: metav1.ObjectMeta{
Name: resourceQuotaName,
},
Spec: v1.ResourceQuotaSpec{
Hard: v1.ResourceList{
v1.ResourceRequestsMemory: *envs.Config.ResourceQuota.RequestsMemory.AsQuantity(),
v1.ResourceLimitsMemory: *envs.Config.ResourceQuota.LimitsMemory.AsQuantity(),
},
},
})
if envs.ErrorHandlers.CheckError("Error on creating resource quota", err) {
return err
}
return nil
}
func (envs *environments) annotateObject(namespace *v1.Namespace, annotName string) error {
origAnnotations := namespace.GetAnnotations()
if origAnnotations == nil {
origAnnotations = make(map[string]string)
}
origAnnotations[annotName] = "true"
namespaceCopy := namespace.DeepCopy()
namespaceCopy.SetAnnotations(origAnnotations)
_, err := envs.NamespacesClient.UpdateNamespace(namespaceCopy)
if envs.ErrorHandlers.CheckError("Error annotating object", err) {
return err
}
return nil
}
func (envs *environments) labelNamespace(namespace *v1.Namespace, labelName string, labelValue string) error {
origLabels := namespace.GetLabels()
origLabels[labelName] = labelValue
namespaceCopy := namespace.DeepCopy()
namespaceCopy.SetLabels(origLabels)
_, err := envs.NamespacesClient.UpdateNamespace(namespaceCopy)
if envs.ErrorHandlers.CheckError("Error labelling object", err) {
return err
}
return nil
}
func (envs *environments) RemoveRolesFromEnvironment(environment *v1.Namespace) error {
namespace, err := envs.NamespacesClient.GetNamespace(environment.Name)
if envs.ErrorHandlers.CheckError("Error while getting namespace.", err) {
return err
}
if !hasRoles(namespace) {
return nil
}
err = envs.unannotateObject(namespace, rolesAnnotName)
if envs.ErrorHandlers.CheckError("Error on updating namespace.", err) {
return err
}
rolesToDelete, err := envs.RolesClient.GetList(envs.Config.Namespace, listOptions)
if envs.ErrorHandlers.CheckError("Error on fetching roles to delete.", err) {
return err
}
for _, role := range rolesToDelete.Items {
err = envs.RolesClient.DeleteRole(role.ObjectMeta.Name, namespace.Name)
envs.ErrorHandlers.LogError(fmt.Sprintf("Error on deleting %s role", role.ObjectMeta.Name), err)
}
return nil
}
func (envs *environments) RemoveIstioInjectionLabel(environment *v1.Namespace) error {
namespace, err := envs.NamespacesClient.GetNamespace(environment.Name)
if envs.ErrorHandlers.CheckError("Error while getting namespace.", err) {
return err
}
err = envs.removeLabelFromNamespace(namespace, istioInjectionLabel)
if envs.ErrorHandlers.CheckError("Error on updating namespace.", err) {
return err
}
return nil
}
func (envs *environments) DeleteLimitRange(environment *v1.Namespace) error {
err := envs.LimitRangeClient.DeleteLimitRange(environment.Name)
if envs.ErrorHandlers.CheckError("Error on deleting limit range.", err) {
return err
}
return nil
}
func (envs *environments) DeleteResourceQuota(namespace *v1.Namespace) error {
err := envs.ResourceQuotaClient.DeleteResourceQuota(namespace.Name)
if envs.ErrorHandlers.CheckError("Error on deleting resource quota.", err) {
return err
}
return nil
}
func (envs *environments) unannotateObject(namespace *v1.Namespace, annotName string) error {
origAnnotations := namespace.GetAnnotations()
if origAnnotations == nil {
log.Println("Unable to unannotate. Provided object is not annotated!")
return nil
}
delete(origAnnotations, annotName)
namespaceCopy := namespace.DeepCopy()
namespaceCopy.SetAnnotations(origAnnotations)
_, err := envs.NamespacesClient.UpdateNamespace(namespaceCopy)
if envs.ErrorHandlers.CheckError("Error unannotating object", err) {
return err
}
return nil
}
func (envs *environments) removeLabelFromNamespace(namespace *v1.Namespace, labelName string) error {
origLabels := namespace.GetLabels()
delete(origLabels, labelName)
namespaceCopy := namespace.DeepCopy()
namespaceCopy.SetLabels(origLabels)
_, err := envs.NamespacesClient.UpdateNamespace(namespaceCopy)
if envs.ErrorHandlers.CheckError("Error removing label", err) {
return err
}
return nil
}
func contains(slice map[string]string, item string) bool {
for key := range slice {
if key == item {
return true
}
}
return false
}