-
Notifications
You must be signed in to change notification settings - Fork 104
/
store.go
435 lines (378 loc) · 13.7 KB
/
store.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package dependencies
import (
"context"
"fmt"
"strings"
"sync"
"github.com/go-logr/logr"
"github.com/DataDog/datadog-operator/controllers/datadogagent/component"
"github.com/DataDog/datadog-operator/controllers/datadogagent/object"
"github.com/DataDog/datadog-operator/pkg/equality"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/version"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// operatorStoreLabelKey used to identified which resource is managed by the store.
operatorStoreLabelKey = "operator.datadoghq.com/managed-by-store"
)
// StoreClient dependencies store client interface
type StoreClient interface {
AddOrUpdate(kind kubernetes.ObjectKind, obj client.Object) error
Get(kind kubernetes.ObjectKind, namespace, name string) (client.Object, bool)
GetOrCreate(kind kubernetes.ObjectKind, namespace, name string) (client.Object, bool)
GetVersionInfo() *version.Info
GetPlatformInfo() kubernetes.PlatformInfo
Delete(kind kubernetes.ObjectKind, namespace string, name string) bool
DeleteAll(ctx context.Context, k8sClient client.Client) []error
Logger() logr.Logger
}
// NewStore returns a new Store instance
func NewStore(owner metav1.Object, options *StoreOptions) *Store {
store := &Store{
deps: make(map[kubernetes.ObjectKind]map[string]client.Object),
owner: owner,
}
if options != nil {
store.supportCilium = options.SupportCilium
store.versionInfo = options.VersionInfo
store.platformInfo = options.PlatformInfo
store.logger = options.Logger
store.scheme = options.Scheme
}
return store
}
// Store Kubernetes resource dependencies store
// this store helps to keep track of every resources that the different agent deployments depend on.
type Store struct {
deps map[kubernetes.ObjectKind]map[string]client.Object
mutex sync.RWMutex
supportCilium bool
versionInfo *version.Info
platformInfo kubernetes.PlatformInfo
scheme *runtime.Scheme
logger logr.Logger
owner metav1.Object
}
// StoreOptions use to provide to NewStore() function some Store creation options.
type StoreOptions struct {
SupportCilium bool
VersionInfo *version.Info
PlatformInfo kubernetes.PlatformInfo
Scheme *runtime.Scheme
Logger logr.Logger
}
// AddOrUpdate used to add or update an object in the Store
// kind correspond to the object kind, and id can be `namespace/name` identifier of just
// `name` if we are talking about a cluster scope object like `ClusterRole`.
func (ds *Store) AddOrUpdate(kind kubernetes.ObjectKind, obj client.Object) error {
ds.mutex.Lock()
defer ds.mutex.Unlock()
if _, found := ds.deps[kind]; !found {
ds.deps[kind] = map[string]client.Object{}
}
id := buildID(obj.GetNamespace(), obj.GetName())
if obj.GetLabels() == nil {
obj.SetLabels(map[string]string{})
}
obj.GetLabels()[operatorStoreLabelKey] = "true"
if ds.owner != nil {
defaultLabels := object.GetDefaultLabels(ds.owner, ds.owner.GetName(), component.GetAgentVersion(ds.owner))
if len(defaultLabels) > 0 {
for key, val := range defaultLabels {
obj.GetLabels()[key] = val
}
}
defaultAnnotations := object.GetDefaultAnnotations(ds.owner)
if len(defaultAnnotations) > 0 {
if obj.GetAnnotations() == nil {
obj.SetAnnotations(map[string]string{})
}
for key, val := range defaultAnnotations {
obj.GetAnnotations()[key] = val
}
}
// Owner-reference should not be added to cluster level objects
if shouldSetOwnerReference(kind, obj.GetNamespace(), ds.owner.GetNamespace()) {
if err := object.SetOwnerReference(ds.owner, obj, ds.scheme); err != nil {
return fmt.Errorf("store.AddOrUpdate, %w", err)
}
}
}
ds.deps[kind][id] = obj
return nil
}
// AddOrUpdateStore used to add or update an object in the Store
// kind correspond to the object kind, and id can be `namespace/name` identifier of just
// `name` if we are talking about a cluster scope object like `ClusterRole`.
func (ds *Store) AddOrUpdateStore(kind kubernetes.ObjectKind, obj client.Object) *Store {
_ = ds.AddOrUpdate(kind, obj)
return ds
}
// Get returns the client.Object instance if it was previously added in the Store.
// kind correspond to the object kind, and id can be `namespace/name` identifier of just
// `name` if we are talking about a cluster scope object like `ClusterRole`.
// It also return a boolean to know if the Object was found in the Store.
func (ds *Store) Get(kind kubernetes.ObjectKind, namespace string, name string) (client.Object, bool) {
ds.mutex.RLock()
defer ds.mutex.RUnlock()
if _, found := ds.deps[kind]; !found {
return nil, false
}
id := buildID(namespace, name)
if obj, found := ds.deps[kind][id]; found {
return obj, true
}
return nil, false
}
// GetOrCreate returns the client.Object instance.
// - if it was previously added in the Store, it returns the corresponding object
// - if it wasn't previously added in the Store, it returns a new instance of the object Kind with
// the corresponding name and namespace.
//
// `kind“ correspond to the object kind, and id can be `namespace/name` identifier of just
// `name` if we are talking about a cluster scope object like `ClusterRole`.
// It also return a boolean to know if the Object was found in the Store.
func (ds *Store) GetOrCreate(kind kubernetes.ObjectKind, namespace, name string) (client.Object, bool) {
obj, found := ds.Get(kind, namespace, name)
if found {
return obj, found
}
obj = kubernetes.ObjectFromKind(kind, ds.platformInfo)
obj.SetName(name)
obj.SetNamespace(namespace)
return obj, found
}
// Delete deletes an item from the store by kind, namespace and name.
func (ds *Store) Delete(kind kubernetes.ObjectKind, namespace string, name string) bool {
ds.mutex.RLock()
defer ds.mutex.RUnlock()
if _, found := ds.deps[kind]; !found {
return false
}
id := buildID(namespace, name)
if _, found := ds.deps[kind][id]; found {
delete(ds.deps[kind], id)
return true
}
return false
}
// Apply use to create/update resources in the api-server
func (ds *Store) Apply(ctx context.Context, k8sClient client.Client) []error {
ds.mutex.RLock()
defer ds.mutex.RUnlock()
var errs []error
var objsToCreate []client.Object
var objsToUpdate []client.Object
for kind := range ds.deps {
for objID, objStore := range ds.deps[kind] {
objNSName := buildObjectKey(objID)
objAPIServer := kubernetes.ObjectFromKind(kind, ds.platformInfo)
err := k8sClient.Get(ctx, objNSName, objAPIServer)
if err != nil && apierrors.IsNotFound(err) {
ds.logger.V(2).Info("dependencies.store Add object to create", "obj.namespace", objStore.GetNamespace(), "obj.name", objStore.GetName(), "obj.kind", kind)
objsToCreate = append(objsToCreate, objStore)
continue
} else if err != nil {
errs = append(errs, err)
continue
}
// ServicesKind is a special case; the cluster IPs are immutable and resource version must be set.
if kind == kubernetes.ServicesKind {
objStore.(*v1.Service).Spec.ClusterIP = objAPIServer.(*v1.Service).Spec.ClusterIP
objStore.(*v1.Service).Spec.ClusterIPs = objAPIServer.(*v1.Service).Spec.ClusterIPs
objStore.SetResourceVersion(objAPIServer.GetResourceVersion())
}
// The APIServiceKind resource version must be set.
if kind == kubernetes.APIServiceKind {
objStore.SetResourceVersion(objAPIServer.GetResourceVersion())
}
if !equality.IsEqualObject(kind, objStore, objAPIServer) {
ds.logger.V(2).Info("dependencies.store Add object to update", "obj.namespace", objStore.GetNamespace(), "obj.name", objStore.GetName(), "obj.kind", kind)
objsToUpdate = append(objsToUpdate, objStore)
continue
}
}
}
ds.logger.V(2).Info("dependencies.store objsToCreate", "nb", len(objsToCreate))
for _, obj := range objsToCreate {
if err := k8sClient.Create(ctx, obj); err != nil {
ds.logger.Error(err, "dependencies.store Create", "obj.namespace", obj.GetNamespace(), "obj.name", obj.GetName())
errs = append(errs, err)
}
}
ds.logger.V(2).Info("dependencies.store objsToUpdate", "nb", len(objsToUpdate))
for _, obj := range objsToUpdate {
if err := k8sClient.Update(ctx, obj); err != nil {
ds.logger.Error(err, "dependencies.store Update", "obj.namespace", obj.GetNamespace(), "obj.name", obj.GetName())
errs = append(errs, err)
}
}
return errs
}
// Cleanup use to cleanup resources that are not needed anymore
func (ds *Store) Cleanup(ctx context.Context, k8sClient client.Client) []error {
ds.mutex.RLock()
defer ds.mutex.RUnlock()
var errs []error
requirementLabel, _ := labels.NewRequirement(operatorStoreLabelKey, selection.Exists, nil)
listOptions := &client.ListOptions{
LabelSelector: labels.NewSelector().Add(*requirementLabel),
}
for _, kind := range ds.platformInfo.GetAgentResourcesKind(ds.supportCilium) {
objList := kubernetes.ObjectListFromKind(kind, ds.platformInfo)
if err := k8sClient.List(ctx, objList, listOptions); err != nil {
errs = append(errs, err)
continue
}
objsToDelete, err := ds.listObjectToDelete(objList, ds.deps[kind])
if err != nil {
errs = append(errs, err)
continue
}
errs = append(errs, deleteObjects(ctx, k8sClient, objsToDelete)...)
}
return errs
}
// GetVersionInfo returns the Kubernetes version
func (ds *Store) GetVersionInfo() *version.Info {
return ds.versionInfo
}
// GetPlatformInfo returns api-resources info
func (ds *Store) GetPlatformInfo() kubernetes.PlatformInfo {
return ds.platformInfo
}
// Logger returns the log client
func (ds *Store) Logger() logr.Logger {
return ds.logger
}
// DeleteAll deletes all the resources that are in the Store
func (ds *Store) DeleteAll(ctx context.Context, k8sClient client.Client) []error {
ds.mutex.RLock()
defer ds.mutex.RUnlock()
var objsToDelete []client.Object
for _, kind := range ds.platformInfo.GetAgentResourcesKind(ds.supportCilium) {
requirementLabel, _ := labels.NewRequirement(operatorStoreLabelKey, selection.Exists, nil)
listOptions := &client.ListOptions{
LabelSelector: labels.NewSelector().Add(*requirementLabel),
}
objList := kubernetes.ObjectListFromKind(kind, ds.platformInfo)
if err := k8sClient.List(ctx, objList, listOptions); err != nil {
return []error{err}
}
items, err := apimeta.ExtractList(objList)
if err != nil {
return []error{err}
}
for _, objAPIServer := range items {
objMeta, _ := apimeta.Accessor(objAPIServer)
idObj := buildID(objMeta.GetNamespace(), objMeta.GetName())
if _, found := ds.deps[kind][idObj]; found {
partialObj := &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Name: objMeta.GetName(),
Namespace: objMeta.GetNamespace(),
},
}
partialObj.TypeMeta.SetGroupVersionKind(objAPIServer.GetObjectKind().GroupVersionKind())
objsToDelete = append(objsToDelete, partialObj)
}
}
}
return deleteObjects(ctx, k8sClient, objsToDelete)
}
func (ds *Store) listObjectToDelete(objList client.ObjectList, cacheObjects map[string]client.Object) ([]client.Object, error) {
items, err := apimeta.ExtractList(objList)
if err != nil {
return nil, err
}
var objsToDelete []client.Object
for _, objAPIServer := range items {
objMeta, _ := apimeta.Accessor(objAPIServer)
idObj := buildID(objMeta.GetNamespace(), objMeta.GetName())
if _, found := cacheObjects[idObj]; !found {
labels := objMeta.GetLabels()
// only delete dependencies associated with the currently reconciled dda
if partOfValue, found := labels[kubernetes.AppKubernetesPartOfLabelKey]; found {
partialDDA := &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Name: ds.owner.GetName(),
Namespace: ds.owner.GetNamespace(),
},
}
if partOfValue == object.NewPartOfLabelValue(partialDDA).String() {
partialObj := &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Name: objMeta.GetName(),
Namespace: objMeta.GetNamespace(),
},
}
partialObj.TypeMeta.SetGroupVersionKind(objAPIServer.GetObjectKind().GroupVersionKind())
objsToDelete = append(objsToDelete, partialObj)
}
}
}
}
return objsToDelete, nil
}
func deleteObjects(ctx context.Context, k8sClient client.Client, objsToDelete []client.Object) []error {
var errs []error
for _, partialObj := range objsToDelete {
err := k8sClient.Delete(ctx, partialObj)
if err != nil {
if apierrors.IsNotFound(err) || apierrors.IsGone(err) {
continue
}
errs = append(errs, err)
}
}
return errs
}
func buildID(ns, name string) string {
if ns == "" {
return name
}
return fmt.Sprintf("%s/%s", ns, name)
}
func buildObjectKey(key string) types.NamespacedName {
keySplit := strings.Split(key, string(types.Separator))
var ns, name string
if len(keySplit) > 1 {
ns = keySplit[0]
name = keySplit[1]
} else {
name = key
}
return types.NamespacedName{
Namespace: ns,
Name: name,
}
}
func shouldSetOwnerReference(kind kubernetes.ObjectKind, objNamespace, ownerNamespace string) bool {
// Owner-reference should not be added to cluster level objects
switch kind {
case kubernetes.ClusterRoleBindingKind:
return false
case kubernetes.ClusterRolesKind:
return false
case kubernetes.APIServiceKind:
return false
}
// Owner-reference should not be added to namespaced resources in a different namespace than the owner
if objNamespace != "" && ownerNamespace != "" && objNamespace != ownerNamespace {
return false
}
return true
}