-
Notifications
You must be signed in to change notification settings - Fork 103
/
controller_reconcile_v2_common.go
369 lines (313 loc) · 15.7 KB
/
controller_reconcile_v2_common.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
// 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 datadogagent
import (
"context"
"fmt"
"time"
datadoghqv2alpha1 "github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
"github.com/DataDog/datadog-operator/pkg/controller/utils/comparison"
"github.com/DataDog/datadog-operator/pkg/controller/utils/datadog"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
edsv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1"
)
const (
updateSucceeded = "UpdateSucceeded"
createSucceeded = "CreateSucceeded"
)
type updateDepStatusComponentFunc func(deployment *appsv1.Deployment, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string)
type updateDSStatusComponentFunc func(daemonset *appsv1.DaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string)
type updateEDSStatusComponentFunc func(eds *edsv1alpha1.ExtendedDaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string)
func (r *Reconciler) createOrUpdateDeployment(parentLogger logr.Logger, dda *datadoghqv2alpha1.DatadogAgent, deployment *appsv1.Deployment, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateStatusFunc updateDepStatusComponentFunc) (reconcile.Result, error) {
logger := parentLogger.WithValues("deployment.Namespace", deployment.Namespace, "deployment.Name", deployment.Name)
var result reconcile.Result
var err error
// Set DatadogAgent instance as the owner and controller
if err = controllerutil.SetControllerReference(dda, deployment, r.scheme); err != nil {
return reconcile.Result{}, err
}
// From here the PodTemplateSpec should be ready, we can generate the hash that will be used to compare this deployment with the current one (if it exists).
var hash string
hash, err = comparison.SetMD5DatadogAgentGenerationAnnotation(&deployment.ObjectMeta, deployment.Spec)
if err != nil {
return result, err
}
// Get the current deployment and compare
nsName := types.NamespacedName{
Name: deployment.GetName(),
Namespace: deployment.GetNamespace(),
}
currentDeployment := &appsv1.Deployment{}
alreadyExists := true
err = r.client.Get(context.TODO(), nsName, currentDeployment)
if err != nil {
if apierrors.IsNotFound(err) {
logger.Info("deployment is not found")
alreadyExists = false
} else {
logger.Error(err, "unexpected error during deployment get")
return reconcile.Result{}, err
}
}
if alreadyExists {
// check if same hash
needUpdate := !comparison.IsSameSpecMD5Hash(hash, currentDeployment.GetAnnotations())
if !needUpdate {
// no need to update hasn't changed
now := metav1.NewTime(time.Now())
updateStatusFunc(currentDeployment, newStatus, now, metav1.ConditionTrue, "DeploymentUpToDate", "Deployment up-to-date")
return reconcile.Result{}, nil
}
logger.Info("Updating Deployment")
// TODO: these parameters can be added to the override.PodTemplateSpec. (It exists in v1alpha1)
keepAnnotationsFilter := ""
keepLabelsFilter := ""
// Copy possibly changed fields
updateDeployment := deployment.DeepCopy()
updateDeployment.Spec = *deployment.Spec.DeepCopy()
updateDeployment.Spec.Replicas = getReplicas(currentDeployment.Spec.Replicas, updateDeployment.Spec.Replicas)
updateDeployment.Annotations = mergeAnnotationsLabels(logger, currentDeployment.GetAnnotations(), deployment.GetAnnotations(), keepAnnotationsFilter)
updateDeployment.Labels = mergeAnnotationsLabels(logger, currentDeployment.GetLabels(), deployment.GetLabels(), keepLabelsFilter)
now := metav1.NewTime(time.Now())
err = kubernetes.UpdateFromObject(context.TODO(), r.client, updateDeployment, currentDeployment.ObjectMeta)
if err != nil {
updateStatusFunc(nil, newStatus, now, metav1.ConditionFalse, updateSucceeded, "Unable to update Deployment")
return reconcile.Result{}, err
}
event := buildEventInfo(updateDeployment.Name, updateDeployment.Namespace, deploymentKind, datadog.UpdateEvent)
r.recordEvent(dda, event)
updateStatusFunc(updateDeployment, newStatus, now, metav1.ConditionTrue, updateSucceeded, "Deployment updated")
} else {
now := metav1.NewTime(time.Now())
err = r.client.Create(context.TODO(), deployment)
if err != nil {
updateStatusFunc(nil, newStatus, now, metav1.ConditionFalse, createSucceeded, "Unable to create Deployment")
return reconcile.Result{}, err
}
event := buildEventInfo(deployment.Name, deployment.Namespace, deploymentKind, datadog.CreationEvent)
r.recordEvent(dda, event)
updateStatusFunc(deployment, newStatus, now, metav1.ConditionTrue, createSucceeded, "Deployment created")
}
logger.Info("Creating Deployment")
return result, err
}
func (r *Reconciler) createOrUpdateDaemonset(parentLogger logr.Logger, dda *datadoghqv2alpha1.DatadogAgent, daemonset *appsv1.DaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateStatusFunc updateDSStatusComponentFunc) (reconcile.Result, error) {
logger := parentLogger.WithValues("daemonset.Namespace", daemonset.Namespace, "daemonset.Name", daemonset.Name)
var result reconcile.Result
var err error
// Set DatadogAgent instance as the owner and controller
if err = controllerutil.SetControllerReference(dda, daemonset, r.scheme); err != nil {
return reconcile.Result{}, err
}
// Get the current daemonset and compare
nsName := types.NamespacedName{
Name: daemonset.GetName(),
Namespace: daemonset.GetNamespace(),
}
currentDaemonset := &appsv1.DaemonSet{}
alreadyExists := true
err = r.client.Get(context.TODO(), nsName, currentDaemonset)
if err != nil {
if apierrors.IsNotFound(err) {
logger.Info("daemonset is not found")
alreadyExists = false
} else {
logger.Error(err, "unexpected error during daemonset get")
return reconcile.Result{}, err
}
}
if alreadyExists {
// When overriding node labels in <1.7.0, the hash could be updated
// without updating the pod template spec in <1.7.0 since pod template
// labels were copied over directly from the existing daemonset.
// With operator <1.7.0, it would look like:
// 1. Set override node label `abc: def`
// a. Daemonset annotation: `agentspechash: 12345`
// 2. Change label to `abc: xyz`
// a. Daemonset annotation: `agentspechash: 67890`
// b. Pod template spec still has `abc: def` (set in step 1)
// To ensure the pod template label updates, we compare the existing
// daemonset's pod template labels with the new daemonset's pod
// template labels.
var currentDaemonsetPodTemplateLabelHash string
currentDaemonsetPodTemplateLabelHash, err = comparison.GenerateMD5ForSpec(currentDaemonset.Spec.Template.Labels)
if err != nil {
return result, err
}
// TODO: remove in 1.8.0 when v1alpha1 is removed
// Spec.Selector is an immutable field and changing it leads to an error.
// Template.Labels must include Spec.Selector.
// See https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/#pod-selector
daemonset.Spec.Selector = currentDaemonset.Spec.Selector
daemonset.Spec.Template.Labels = ensureSelectorInPodTemplateLabels(logger, daemonset.Spec.Selector, daemonset.Spec.Template.Labels)
// From here the PodTemplateSpec should be ready, we can generate the hash that will be used to compare this daemonset with the current one (if it exists).
var hash, daemonsetPodTemplateLabelHash string
hash, err = comparison.SetMD5DatadogAgentGenerationAnnotation(&daemonset.ObjectMeta, daemonset.Spec)
if err != nil {
return result, err
}
// create a separate hash to compare pod template labels
daemonsetPodTemplateLabelHash, err = comparison.GenerateMD5ForSpec(daemonset.Spec.Template.Labels)
if err != nil {
return result, err
}
// check if same hash
needUpdate := !comparison.IsSameSpecMD5Hash(hash, currentDaemonset.GetAnnotations()) || currentDaemonsetPodTemplateLabelHash != daemonsetPodTemplateLabelHash
if !needUpdate {
// Even if the DaemonSet is still the same, its status might have
// changed (for example, the number of pods ready). This call is
// needed to keep the agent status updated.
now := metav1.NewTime(time.Now())
newStatus.AgentList = datadoghqv2alpha1.UpdateDaemonSetStatus(currentDaemonset, newStatus.AgentList, &now)
newStatus.Agent = datadoghqv2alpha1.UpdateCombinedDaemonSetStatus(newStatus.AgentList)
// Stop reconcile loop since DaemonSet hasn't changed
return reconcile.Result{}, nil
}
logger.Info("Updating Daemonset")
// TODO: these parameters can be added to the override.PodTemplateSpec. (It exists in v1alpha1)
keepAnnotationsFilter := ""
keepLabelsFilter := ""
// Copy possibly changed fields
updateDaemonset := daemonset.DeepCopy()
updateDaemonset.Spec = *daemonset.Spec.DeepCopy()
updateDaemonset.Annotations = mergeAnnotationsLabels(logger, currentDaemonset.GetAnnotations(), daemonset.GetAnnotations(), keepAnnotationsFilter)
updateDaemonset.Labels = mergeAnnotationsLabels(logger, currentDaemonset.GetLabels(), daemonset.GetLabels(), keepLabelsFilter)
now := metav1.NewTime(time.Now())
err = kubernetes.UpdateFromObject(context.TODO(), r.client, updateDaemonset, currentDaemonset.ObjectMeta)
if err != nil {
updateStatusFunc(updateDaemonset, newStatus, now, metav1.ConditionFalse, updateSucceeded, "Unable to update Daemonset")
return reconcile.Result{}, err
}
event := buildEventInfo(updateDaemonset.Name, updateDaemonset.Namespace, daemonSetKind, datadog.UpdateEvent)
r.recordEvent(dda, event)
updateStatusFunc(updateDaemonset, newStatus, now, metav1.ConditionTrue, updateSucceeded, "Daemonset updated")
} else {
// From here the PodTemplateSpec should be ready, we can generate the hash that will be added to this daemonset.
_, err = comparison.SetMD5DatadogAgentGenerationAnnotation(&daemonset.ObjectMeta, daemonset.Spec)
if err != nil {
return result, err
}
now := metav1.NewTime(time.Now())
err = r.client.Create(context.TODO(), daemonset)
if err != nil {
updateStatusFunc(nil, newStatus, now, metav1.ConditionFalse, createSucceeded, "Unable to create Daemonset")
return reconcile.Result{}, err
}
event := buildEventInfo(daemonset.Name, daemonset.Namespace, daemonSetKind, datadog.CreationEvent)
r.recordEvent(dda, event)
updateStatusFunc(daemonset, newStatus, now, metav1.ConditionTrue, createSucceeded, "Daemonset created")
}
logger.Info("Creating Daemonset")
return result, err
}
func (r *Reconciler) createOrUpdateExtendedDaemonset(parentLogger logr.Logger, dda *datadoghqv2alpha1.DatadogAgent, eds *edsv1alpha1.ExtendedDaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateStatusFunc updateEDSStatusComponentFunc) (reconcile.Result, error) {
logger := parentLogger.WithValues("ExtendedDaemonSet.Namespace", eds.Namespace, "ExtendedDaemonSet.Name", eds.Name)
var result reconcile.Result
var err error
// Set DatadogAgent instance as the owner and controller
if err = controllerutil.SetControllerReference(dda, eds, r.scheme); err != nil {
return reconcile.Result{}, err
}
// From here the PodTemplateSpec should be ready, we can generate the hash that will be used to compare this extendeddaemonset with the current one (if it exists).
var hash string
hash, err = comparison.SetMD5DatadogAgentGenerationAnnotation(&eds.ObjectMeta, eds.Spec)
if err != nil {
return result, err
}
// Get the current extendeddaemonset and compare
nsName := types.NamespacedName{
Name: eds.GetName(),
Namespace: eds.GetNamespace(),
}
currentEDS := &edsv1alpha1.ExtendedDaemonSet{}
alreadyExists := true
err = r.client.Get(context.TODO(), nsName, currentEDS)
if err != nil {
if apierrors.IsNotFound(err) {
logger.Info("ExtendedDaemonSet is not found")
alreadyExists = false
} else {
logger.Error(err, "unexpected error during ExtendedDaemonSet get")
return reconcile.Result{}, err
}
}
if alreadyExists {
// check if same hash
needUpdate := !comparison.IsSameSpecMD5Hash(hash, currentEDS.GetAnnotations())
if !needUpdate {
// Even if the EDS is still the same, its status might have
// changed (for example, the number of pods ready). This call is
// needed to keep the agent status updated.
now := metav1.NewTime(time.Now())
newStatus.AgentList = datadoghqv2alpha1.UpdateExtendedDaemonSetStatus(currentEDS, newStatus.AgentList, &now)
newStatus.Agent = datadoghqv2alpha1.UpdateCombinedDaemonSetStatus(newStatus.AgentList)
// Stop reconcile loop since EDS hasn't changed
return reconcile.Result{}, nil
}
logger.Info("Updating ExtendedDaemonSet")
// TODO: these parameters can be added to the override.PodTemplateSpec. (It exists in v1alpha1)
keepAnnotationsFilter := ""
keepLabelsFilter := ""
// Copy possibly changed fields
updateEDS := eds.DeepCopy()
updateEDS.Spec = *eds.Spec.DeepCopy()
updateEDS.Annotations = mergeAnnotationsLabels(logger, currentEDS.GetAnnotations(), eds.GetAnnotations(), keepAnnotationsFilter)
updateEDS.Labels = mergeAnnotationsLabels(logger, currentEDS.GetLabels(), eds.GetLabels(), keepLabelsFilter)
now := metav1.NewTime(time.Now())
err = kubernetes.UpdateFromObject(context.TODO(), r.client, updateEDS, currentEDS.ObjectMeta)
if err != nil {
updateStatusFunc(updateEDS, newStatus, now, metav1.ConditionFalse, updateSucceeded, "Unable to update ExtendedDaemonSet")
return reconcile.Result{}, err
}
event := buildEventInfo(updateEDS.Name, updateEDS.Namespace, extendedDaemonSetKind, datadog.UpdateEvent)
r.recordEvent(dda, event)
updateStatusFunc(updateEDS, newStatus, now, metav1.ConditionTrue, updateSucceeded, "ExtendedDaemonSet updated")
} else {
now := metav1.NewTime(time.Now())
err = r.client.Create(context.TODO(), eds)
if err != nil {
updateStatusFunc(nil, newStatus, now, metav1.ConditionFalse, createSucceeded, "Unable to create ExtendedDaemonSet")
return reconcile.Result{}, err
}
event := buildEventInfo(eds.Name, eds.Namespace, extendedDaemonSetKind, datadog.CreationEvent)
r.recordEvent(dda, event)
updateStatusFunc(eds, newStatus, now, metav1.ConditionTrue, createSucceeded, "ExtendedDaemonSet created")
}
logger.Info("Creating ExtendedDaemonSet")
return result, err
}
// TODO: remove in 1.8.0 when v1alpha1 is removed
// ensureSelectorInPodTemplateLabels checks that a label selector's MatchLabels
// are present in the pod template labels. If the label is missing, it adds it
// to the pod template labels. If the value doesn't match, it changes the label
// value to match the selector.
// If the selector labels aren't present in the pod template labels, there will
// be a `selector does not match template labels` error when updating the agent
func ensureSelectorInPodTemplateLabels(logger logr.Logger, selector *metav1.LabelSelector, labels map[string]string) map[string]string {
if selector != nil {
if labels == nil {
labels = map[string]string{}
}
for k, v := range selector.MatchLabels {
value, ok := labels[k]
if !ok {
logger.Info("Selector not in template labels, adding to template labels", "selector label", fmt.Sprintf("%s: %s", k, v))
labels[k] = v
}
if value != v {
logger.Info("Selector value does not match template labels, modifying template labels", "selector label", fmt.Sprintf("%s: %s", k, v), "template label", fmt.Sprintf("%s: %s", k, value))
labels[k] = v
}
}
}
return labels
}