-
Notifications
You must be signed in to change notification settings - Fork 7
/
node_controller.go
129 lines (114 loc) · 4.12 KB
/
node_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
/*
* MIT License
*
* Copyright (c) since 2021, flomesh.io Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package servicelb
import (
"context"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
fctx "github.com/flomesh-io/fsm/pkg/context"
"github.com/flomesh-io/fsm/pkg/controllers"
)
// NodeReconciler reconciles a Node object
type nodeReconciler struct {
recorder record.EventRecorder
fctx *fctx.ControllerContext
}
// NewNodeReconciler returns a new reconcile.Reconciler
func NewNodeReconciler(ctx *fctx.ControllerContext) controllers.Reconciler {
return &nodeReconciler{
recorder: ctx.Manager.GetEventRecorderFor("ServiceLB"),
fctx: ctx,
}
}
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the Node object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.10.0/pkg/reconcile
func (r *nodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// Fetch the Node instance
node := &corev1.Node{}
if err := r.fctx.Get(
ctx,
req.NamespacedName,
node,
); err != nil {
if errors.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
log.Info().Msgf("Node resource not found. Ignoring since object must be deleted")
return ctrl.Result{}, nil
}
// Error reading the object - requeue the request.
log.Error().Msgf("Failed to get Node, %v", err)
return ctrl.Result{}, err
}
if _, ok := node.Labels[daemonsetNodeLabel]; !ok {
return ctrl.Result{}, nil
}
if err := r.updateDaemonSets(ctx); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func (r *nodeReconciler) updateDaemonSets(ctx context.Context) error {
log.Info().Msgf("Updating DaemonSets due to node labels change ...")
daemonsets := &appv1.DaemonSetList{}
if err := r.fctx.List(
ctx,
daemonsets,
client.InNamespace(corev1.NamespaceAll),
client.MatchingLabels{
nodeSelectorLabel: "false",
},
); err != nil {
return err
}
for _, ds := range daemonsets.Items {
ds := ds // fix lint, G601: Implicit memory aliasing in for loop.
ds.Spec.Template.Spec.NodeSelector = map[string]string{
daemonsetNodeLabel: "true",
}
ds.Labels[nodeSelectorLabel] = "true"
if err := r.fctx.Update(ctx, &ds); err != nil {
return err
}
}
return nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *nodeReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Node{}).
Complete(r)
}