-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconciler.go
65 lines (51 loc) · 1.69 KB
/
reconciler.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
package main
import (
"context"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
type reconcileNodeLabels struct {
client client.Client
log logr.Logger
}
// make sure reconcileNodeLabels implement the Reconciler interface
var _ reconcile.Reconciler = &reconcileNodeLabels{}
func (r *reconcileNodeLabels) Reconcile(request reconcile.Request) (reconcile.Result, error) {
// set up a convenient log object so we don't have to type request over and over again
log := r.log.WithValues("request", request)
log.Info("Reconciling node", "node", request.Name)
node := &corev1.Node{}
err := r.client.Get(context.TODO(), request.NamespacedName, node)
if errors.IsNotFound(err) {
log.Error(nil, "Could not find Node")
return reconcile.Result{}, nil
}
if err != nil {
log.Error(err, "Could not fetch Node")
return reconcile.Result{}, err
}
nodeLabelMap.Lock()
// Reconcile an individual node's labels with its defined state in the node label map.
// As the node label map may be updated for cleared labels, this must be called with the
// label map's write lock held.
for label, set := range nodeLabelMap.GetLabels(request.Name) {
if set {
log.Info("Setting label", "label", label)
node.Labels[label] = "true"
} else {
log.Info("Clearing label", "label", label)
delete(node.Labels, label)
nodeLabelMap.RemoveLabelFromNode(request.Name, label)
}
}
nodeLabelMap.Unlock()
err = r.client.Update(context.TODO(), node)
if err != nil {
log.Error(err, "Could not write Node")
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}