/
configmap_controller.go
409 lines (358 loc) · 15.2 KB
/
configmap_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
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
package configmap
import (
"context"
"strconv"
"strings"
"time"
controllerruntime "sigs.k8s.io/controller-runtime"
"github.com/openshift/file-integrity-operator/pkg/apis/fileintegrity/v1alpha1"
"github.com/openshift/file-integrity-operator/pkg/controller/metrics"
"github.com/go-logr/logr"
"github.com/openshift/file-integrity-operator/pkg/common"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
kerr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
var configMapControllerLog = logf.Log.WithName("controller_configmap")
// Add creates a new ConfigMap Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func AddConfigmapController(mgr manager.Manager, met *metrics.Metrics) error {
return addConfigmapController(mgr, newConfigmapReconciler(mgr, met))
}
// newReconciler returns a new reconcile.Reconciler
func newConfigmapReconciler(mgr manager.Manager, met *metrics.Metrics) reconcile.Reconciler {
return &ReconcileConfigMap{Client: mgr.GetClient(), Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("configmapctrl"),
Metrics: met,
}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func addConfigmapController(mgr manager.Manager, r reconcile.Reconciler) error {
return controllerruntime.NewControllerManagedBy(mgr).
Named("configmap-controller").
For(&corev1.ConfigMap{}).
Owns(&corev1.Pod{}).
Complete(r)
}
// blank assignment to verify that ReconcileConfigMap implements reconcile.Reconciler
var _ reconcile.Reconciler = &ReconcileConfigMap{}
// Reconcile reads that state of the cluster for a ConfigMap object and makes changes based on the state read
// and what is in the ConfigMap.Spec
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ReconcileConfigMap) ConfigMapReconcile(request reconcile.Request) (reconcile.Result, error) {
reqLogger := configMapControllerLog.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling ConfigMap")
// Fetch the ConfigMap instance
instance := &corev1.ConfigMap{}
err := r.Client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if kerr.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
if common.IsAideConfig(instance.Labels) {
return r.reconcileAideConfAndHandleReinitDs(instance, reqLogger)
} else if common.IsIntegrityLog(instance.Labels) {
return r.handleIntegrityLog(instance, reqLogger)
}
return reconcile.Result{}, nil
}
func (r *ReconcileConfigMap) reconcileAideConfAndHandleReinitDs(instance *corev1.ConfigMap,
logger logr.Logger) (reconcile.Result, error) {
// only continue if the configmap received an update through the user-provided config
nodeListString, ok := instance.Annotations[common.AideConfigUpdatedAnnotationKey]
if !ok {
return reconcile.Result{}, nil
}
nodeName := ""
nodeList := []string{}
if nodeListString != "" {
nodeList = strings.Split(nodeListString, ",")
nodeName = nodeList[len(nodeList)-1]
}
ownerName, err := common.GetConfigMapOwnerName(instance)
if err != nil {
return reconcile.Result{}, err
}
integrityInstance := &v1alpha1.FileIntegrity{}
err = r.Client.Get(context.TODO(), types.NamespacedName{Name: ownerName, Namespace: instance.Namespace},
integrityInstance)
if err != nil {
return reconcile.Result{}, err
}
logger.Info("reconciling re-init", "cm.Name", instance.Name, "owner", integrityInstance.Name,
"node", nodeName)
// handling the re-init daemonSets: these are created by the FileIntegrity controller when the AIDE config has been
// updated by the user. They touch a file on the node host and then sleep. The file signals to the AIDE pod
// daemonSets that they need to back up and re-initialize the AIDE database.
reinitDSName := common.ReinitDaemonSetNodeName(integrityInstance.Name, nodeName)
if nodeName == "" {
// if there is all node reinit we need to remove any single node reinit daemonSets
// we can get list of DaemonSets with label selector
dsList := &appsv1.DaemonSetList{}
labelSelector := labels.SelectorFromSet(map[string]string{common.IntegrityReinitOwnerLabelKey: integrityInstance.Name})
listOps := &client.ListOptions{Namespace: instance.Namespace, LabelSelector: labelSelector}
err = r.Client.List(context.Background(), dsList, listOps)
if err != nil {
logger.Error(err, "error listing re-init daemonSets")
return reconcile.Result{}, err
}
for _, ds := range dsList.Items {
if ds.Name != reinitDSName {
// clean up any daemonSets that are not re-reinit all daemonSets
if deleteErr := r.Client.Delete(context.TODO(), &ds); deleteErr != nil {
if kerr.IsNotFound(deleteErr) {
continue
}
logger.Error(deleteErr, "error deleting re-init daemonSet", "ds.Name", ds.Name)
return reconcile.Result{}, deleteErr
}
r.Metrics.IncFileIntegrityReinitDaemonsetDelete()
}
}
}
reinitDS := &appsv1.DaemonSet{}
err = r.Client.Get(context.TODO(), types.NamespacedName{Name: reinitDSName,
Namespace: common.FileIntegrityNamespace}, reinitDS)
if err != nil && kerr.IsNotFound(err) {
return reconcile.Result{Requeue: true}, nil
} else if err != nil {
logger.Error(err, "error getting re-init daemonSet")
return reconcile.Result{}, err
}
// not ready, requeue
if !common.DaemonSetIsReady(reinitDS) {
return reconcile.Result{RequeueAfter: time.Duration(time.Second)}, nil
}
// reinit daemonSet is ready, so we're finished with it
if deleteErr := r.Client.Delete(context.TODO(), reinitDS); deleteErr != nil {
return reconcile.Result{}, deleteErr
}
r.Metrics.IncFileIntegrityReinitDaemonsetDelete()
// unset update annotation
conf := instance.DeepCopy()
// remove last node from annotation
if len(nodeList) > 1 {
nodeList = nodeList[:len(nodeList)-1]
conf.Annotations[common.AideConfigUpdatedAnnotationKey] = strings.Join(nodeList, ",")
} else {
// if there are no more nodes or just one left, remove the annotation
delete(conf.Annotations, common.AideConfigUpdatedAnnotationKey)
}
if updateErr := r.Client.Update(context.TODO(), conf); updateErr != nil {
logger.Error(updateErr, "error clearing configMap annotations")
return reconcile.Result{}, updateErr
}
return reconcile.Result{}, nil
}
func (r *ReconcileConfigMap) handleIntegrityLog(cm *corev1.ConfigMap, logger logr.Logger) (reconcile.Result, error) {
owner, err := common.GetConfigMapOwnerName(cm)
if err != nil {
logger.Error(err, "Malformed ConfigMap: Could not get owner. Cannot retry.")
return reconcile.Result{}, nil
}
node, err := common.GetConfigMapNodeName(cm)
if err != nil {
logger.Error(err, "Malformed ConfigMap: Could not get node. Cannot retry.")
return reconcile.Result{}, nil
}
cachedfi := &v1alpha1.FileIntegrity{}
err = r.Client.Get(context.TODO(), types.NamespacedName{Name: owner, Namespace: cm.Namespace}, cachedfi)
if err != nil {
return reconcile.Result{}, err
}
fi := cachedfi.DeepCopy()
if common.IsIntegrityLogAnError(cm) {
errorMsg, containsErrorAnnotation := cm.Annotations[common.IntegrityLogErrorAnnotationKey]
if !containsErrorAnnotation {
_, containsContentKey := cm.Data[common.IntegrityLogContentKey]
if !containsContentKey {
errorMsg = "log ConfigMap doesn't contain content"
} else {
errorMsg = "unknown error"
}
}
status := v1alpha1.FileIntegrityScanResult{
Condition: v1alpha1.NodeConditionErrored,
LastProbeTime: cm.GetCreationTimestamp(),
ErrorMsg: errorMsg,
}
if err := r.createOrUpdateNodeStatus(node, fi, status); err != nil {
return reconcile.Result{}, err
}
} else if common.IsIntegrityLogAFailure(cm) {
failedCM := getConfigMapForFailureLog(cm)
if err = r.Client.Create(context.TODO(), failedCM); err != nil {
// Update if it already existed
if kerr.IsAlreadyExists(err) {
if err = r.Client.Update(context.TODO(), failedCM); err != nil {
return reconcile.Result{}, err
}
} else {
return reconcile.Result{}, err
}
}
status := v1alpha1.FileIntegrityScanResult{
Condition: v1alpha1.NodeConditionFailed,
LastProbeTime: cm.GetCreationTimestamp(),
ResultConfigMapName: failedCM.Name,
ResultConfigMapNamespace: failedCM.Namespace,
}
status.FilesAdded, _ = strconv.Atoi(failedCM.Annotations[common.IntegrityLogFilesAddedAnnotation])
status.FilesRemoved, _ = strconv.Atoi(failedCM.Annotations[common.IntegrityLogFilesRemovedAnnotation])
status.FilesChanged, _ = strconv.Atoi(failedCM.Annotations[common.IntegrityLogFilesChangedAnnotation])
if err := r.createOrUpdateNodeStatus(node, fi, status); err != nil {
return reconcile.Result{}, err
}
} else {
status := v1alpha1.FileIntegrityScanResult{
Condition: v1alpha1.NodeConditionSucceeded,
LastProbeTime: cm.GetCreationTimestamp(),
}
if err := r.createOrUpdateNodeStatus(node, fi, status); err != nil {
return reconcile.Result{}, err
}
}
// No need to keep the ConfigMap, the log collector will try to create
// another one on its next run
if err = r.Client.Delete(context.TODO(), cm); err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
// Creates or updates a FileIntegrityNodeStatus object for the node. If a result exists for a node matching the new result, we update that result.
// At the most there will be three results per status. One for each condition type. The most recently updated reflects the current result.
func (r *ReconcileConfigMap) createOrUpdateNodeStatus(node string, instance *v1alpha1.FileIntegrity, new v1alpha1.FileIntegrityScanResult) error {
nodeStatus := &v1alpha1.FileIntegrityNodeStatus{}
err := r.Client.Get(context.TODO(), types.NamespacedName{Namespace: instance.Namespace, Name: instance.Name + "-" + node}, nodeStatus)
if err != nil && !kerr.IsNotFound(err) {
return err
}
if kerr.IsNotFound(err) {
// This node does not have a corresponding FileIntegrityNodeStatus yet, create with this initial result.
nodeStatus = &v1alpha1.FileIntegrityNodeStatus{
ObjectMeta: metav1.ObjectMeta{
Name: instance.Name + "-" + node,
Namespace: instance.Namespace,
Labels: map[string]string{
common.IntegrityOwnerLabelKey: instance.Name,
},
},
NodeName: node,
Results: []v1alpha1.FileIntegrityScanResult{},
}
nodeStatus.Results = append(nodeStatus.Results, new)
nodeStatus.LastResult = *new.DeepCopy()
refErr := controllerutil.SetControllerReference(instance, nodeStatus, r.Scheme)
if refErr != nil {
return refErr
}
createErr := r.Client.Create(context.TODO(), nodeStatus)
if createErr != nil {
return createErr
}
updateNodeStatusMetrics(r, nodeStatus)
createNodeStatusEvent(r, instance, nodeStatus)
return nil
}
updateResults := make([]v1alpha1.FileIntegrityScanResult, 0)
// Filter to keep the other results. We only want to replace one of the same.
for _, result := range nodeStatus.Results {
if result.Condition != new.Condition {
updateResults = append(updateResults, result)
if isLatestScanResult(result, nodeStatus) {
nodeStatus.LastResult = *result.DeepCopy()
}
}
}
statusCopy := nodeStatus.DeepCopy()
updateResults = append(updateResults, new)
statusCopy.Results = updateResults
if isLatestScanResult(new, nodeStatus) {
statusCopy.LastResult = *new.DeepCopy()
}
updateErr := r.Client.Update(context.TODO(), statusCopy)
if updateErr != nil {
return updateErr
}
// Create an event if there was a transition or an updated Failure
if conditionIsNewFailureOrTransition(nodeStatus, statusCopy) {
updateNodeStatusMetrics(r, statusCopy)
createNodeStatusEvent(r, instance, statusCopy)
}
return nil
}
// isLatestScanResult returns true if result is newer than nodeStatus.LastResult
func isLatestScanResult(result v1alpha1.FileIntegrityScanResult, nodeStatus *v1alpha1.FileIntegrityNodeStatus) bool {
return result.LastProbeTime.After(nodeStatus.LastResult.LastProbeTime.Time)
}
// conditionIsNewFailureOrTransition return true if cur has an updated failure count over prev (if both were failed conditions),
// or if cur's condition is different than prev.
func conditionIsNewFailureOrTransition(prev, cur *v1alpha1.FileIntegrityNodeStatus) bool {
if cur.LastResult.Condition == v1alpha1.NodeConditionFailed && prev.LastResult.Condition == v1alpha1.NodeConditionFailed {
return cur.LastResult.FilesRemoved != prev.LastResult.FilesRemoved ||
cur.LastResult.FilesAdded != prev.LastResult.FilesAdded ||
cur.LastResult.FilesChanged != prev.LastResult.FilesChanged
} else if cur.LastResult.Condition != prev.LastResult.Condition {
return true
}
return false
}
// createNodeStatusEvent creates an event to report the latest check result
func createNodeStatusEvent(r *ReconcileConfigMap, fi *v1alpha1.FileIntegrity, status *v1alpha1.FileIntegrityNodeStatus) {
switch status.LastResult.Condition {
case v1alpha1.NodeConditionSucceeded:
r.Recorder.Eventf(fi, corev1.EventTypeNormal, "NodeIntegrityStatus", "no changes to node %s",
status.NodeName)
case v1alpha1.NodeConditionFailed:
r.Recorder.Eventf(fi, corev1.EventTypeWarning, "NodeIntegrityStatus",
"node %s has changed! a:%d,c:%d,r:%d log:%s/%s", status.NodeName,
status.LastResult.FilesAdded, status.LastResult.FilesChanged, status.LastResult.FilesRemoved,
status.LastResult.ResultConfigMapNamespace, status.LastResult.ResultConfigMapName)
case v1alpha1.NodeConditionErrored:
r.Recorder.Eventf(fi, corev1.EventTypeWarning, "NodeIntegrityStatus",
"node %s has an error! %s", status.NodeName, status.LastResult.ErrorMsg)
}
}
func getConfigMapForFailureLog(cm *corev1.ConfigMap) *corev1.ConfigMap {
failedCM := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: cm.Name + "-failed",
Namespace: cm.Namespace,
Labels: cm.Labels,
Annotations: cm.Annotations,
},
Data: cm.Data,
}
// We remove the log label so we don't queue the new ConfigMap
delete(failedCM.Labels, common.IntegrityLogLabelKey)
// We mark is as a result
failedCM.Labels[common.IntegrityLogResultLabelKey] = ""
return failedCM
}
func updateNodeStatusMetrics(r *ReconcileConfigMap, status *v1alpha1.FileIntegrityNodeStatus) {
r.Metrics.IncFileIntegrityNodeStatus(string(status.LastResult.Condition), status.NodeName)
switch status.LastResult.Condition {
case v1alpha1.NodeConditionSucceeded:
r.Metrics.SetFileIntegrityNodeStatusGaugeGood(status.NodeName)
case v1alpha1.NodeConditionFailed:
r.Metrics.SetFileIntegrityNodeStatusGaugeBad(status.NodeName)
case v1alpha1.NodeConditionErrored:
r.Metrics.IncFileIntegrityNodeStatusError(status.LastResult.ErrorMsg, status.NodeName)
}
}