-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
62 lines (49 loc) · 1.37 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
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
labels map[string]string
}
// 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)
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
}
// Set the label
if node.Labels == nil {
node.Labels = map[string]string{}
}
// Remove old labels
for k := range node.Labels {
if matchesLabelPrefix(k) {
delete(node.Labels, k)
}
}
for k, v := range r.labels {
node.Labels[k] = v
}
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
}