-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathnode_controller.go
More file actions
155 lines (138 loc) · 5.48 KB
/
node_controller.go
File metadata and controls
155 lines (138 loc) · 5.48 KB
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
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"fmt"
core "k8s.io/api/core/v1"
k8sapierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"github.com/openshift/windows-machine-config-operator/pkg/cluster"
"github.com/openshift/windows-machine-config-operator/pkg/condition"
"github.com/openshift/windows-machine-config-operator/pkg/metadata"
"github.com/openshift/windows-machine-config-operator/pkg/nodeconfig"
"github.com/openshift/windows-machine-config-operator/pkg/secrets"
"github.com/openshift/windows-machine-config-operator/pkg/signer"
)
const (
// NodeController is the name of this controller in logs and other outputs.
NodeController = "node"
)
// nodeReconciler holds the info required to reconcile a Node object, inclduing that of the underlying Windows instance
type nodeReconciler struct {
instanceReconciler
}
// NewNodeReconciler returns a pointer to a new nodeReconciler
func NewNodeReconciler(mgr manager.Manager, clusterConfig cluster.Config, watchNamespace string) (*nodeReconciler, error) {
clientset, err := kubernetes.NewForConfig(mgr.GetConfig())
if err != nil {
return nil, fmt.Errorf("error creating kubernetes clientset: %w", err)
}
return &nodeReconciler{
instanceReconciler: instanceReconciler{
client: mgr.GetClient(),
log: ctrl.Log.WithName("controllers").WithName(NodeController),
k8sclientset: clientset,
clusterServiceCIDR: clusterConfig.Network().GetServiceCIDR(),
watchNamespace: watchNamespace,
recorder: mgr.GetEventRecorderFor(NodeController),
},
}, nil
}
// Reconcile is part of the main kubernetes reconciliation loop which reads that state of the cluster for a
// Node object and aims to move the current state of the cluster closer to the desired state.
func (r *nodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
// Prevent WMCO upgrades while Node objects are being processed
if err := condition.MarkAsBusy(ctx, r.client, r.watchNamespace, r.recorder, NodeController); err != nil {
return ctrl.Result{}, err
}
defer func() {
err = markAsFreeOnSuccess(ctx, r.client, r.watchNamespace, r.recorder, NodeController, result.Requeue, err)
}()
// Fetch Node reference
node := &core.Node{}
if err := r.client.Get(ctx, req.NamespacedName, node); err != nil {
if k8sapierrors.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 ctrl.Result{}, nil
}
// Error reading the object - return error to requeue the request.
return ctrl.Result{}, err
}
r.log.V(1).Info("reconciling", "name", req.NamespacedName.String())
if _, ok := node.GetAnnotations()[metadata.RebootAnnotation]; ok {
// Create a new signer using the private key that the instances will be reconciled with
signer, err := signer.Create(ctx, types.NamespacedName{Namespace: r.watchNamespace,
Name: secrets.PrivateKeySecret}, r.client)
if err != nil {
return ctrl.Result{}, fmt.Errorf("unable to create signer from private key secret: %w", err)
}
instanceInfo, err := r.instanceFromNode(ctx, node)
if err != nil {
return ctrl.Result{}, err
}
nc, err := nodeconfig.NewNodeConfig(r.client, r.k8sclientset, r.clusterServiceCIDR, r.watchNamespace,
instanceInfo, signer, nil, nil, r.platform)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to create new nodeconfig: %w", err)
}
defer func() {
err := nc.Close()
if err != nil {
r.log.Info("WARNING: error closing nodeconfig", "error", err.Error())
}
}()
if err := nc.SafeReboot(ctx); err != nil {
return ctrl.Result{}, fmt.Errorf("full instance reboot failed: %w", err)
}
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *nodeReconciler) SetupWithManager(mgr ctrl.Manager) error {
windowsNodePredicate := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return isWindowsNode(e.Object)
},
UpdateFunc: func(e event.UpdateEvent) bool {
return isWindowsNode(e.ObjectNew)
},
GenericFunc: func(e event.GenericEvent) bool {
return isWindowsNode(e.Object)
},
DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
}
return ctrl.NewControllerManagedBy(mgr).
For(&core.Node{}, builder.WithPredicates(windowsNodePredicate)).
Complete(r)
}
// isWindowsNode returns true if the given object is a Windows node
func isWindowsNode(obj runtime.Object) bool {
node, ok := obj.(*core.Node)
if !ok {
return false
}
value, ok := node.Labels[core.LabelOSStable]
return ok && value == "windows"
}