Skip to content

Commit 2ebbec7

Browse files
Merge pull request #2741 from sebsoto/fixContext
WINC-1378: Handle interrupt signals with the proper use of contexts
2 parents 0c8f236 + a09b184 commit 2ebbec7

27 files changed

+257
-242
lines changed

cmd/daemon/bootstrap.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ limitations under the License.
1919
package main
2020

2121
import (
22-
"context"
23-
2422
"github.com/spf13/cobra"
2523
"k8s.io/client-go/tools/clientcmd"
2624
"k8s.io/klog/v2"
25+
ctrl "sigs.k8s.io/controller-runtime"
2726

2827
"github.com/openshift/windows-machine-config-operator/pkg/daemon/controller"
2928
)
@@ -53,7 +52,7 @@ func runBootstrapCmd(cmd *cobra.Command, args []string) {
5352
if err != nil {
5453
klog.Exitf("error building config: %s", err.Error())
5554
}
56-
sc, err := controller.NewServiceController(context.TODO(), "", namespace, controller.Options{Config: cfg})
55+
sc, err := controller.NewServiceController(ctrl.SetupSignalHandler(), "", namespace, controller.Options{Config: cfg})
5756
if err != nil {
5857
klog.Exitf("error creating Service Controller: %s", err.Error())
5958
}

cmd/operator/main.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"flag"
65
"fmt"
76
"os"
@@ -106,14 +105,15 @@ func main() {
106105
}
107106

108107
// get cluster configuration
109-
clusterConfig, err := cluster.NewConfig(cfg)
108+
ctx := ctrl.SetupSignalHandler()
109+
clusterConfig, err := cluster.NewConfig(ctx, cfg)
110110
if err != nil {
111111
setupLog.Error(err, "failed to get cluster configuration")
112112
os.Exit(1)
113113
}
114114

115115
// validate cluster for required configurations
116-
if err := clusterConfig.Validate(); err != nil {
116+
if err := clusterConfig.Validate(ctx); err != nil {
117117
setupLog.Error(err, "failed to validate required cluster configuration")
118118
os.Exit(1)
119119
}
@@ -146,7 +146,6 @@ func main() {
146146
os.Exit(1)
147147
}
148148

149-
ctx := context.TODO()
150149
// Become the leader before proceeding
151150
err = leader.Become(ctx, "windows-machine-config-operator-lock")
152151
if err != nil {
@@ -229,16 +228,16 @@ func main() {
229228
setupLog.Error(err, "unable to create Secret reconciler")
230229
os.Exit(1)
231230
}
232-
if err = secretReconciler.SetupWithManager(mgr); err != nil {
231+
if err = secretReconciler.SetupWithManager(ctx, mgr); err != nil {
233232
setupLog.Error(err, "unable to create Secret controller")
234233
os.Exit(1)
235234
}
236-
if err := secretReconciler.RemoveInvalidAnnotationsFromLinuxNodes(mgr.GetConfig()); err != nil {
235+
if err := secretReconciler.RemoveInvalidAnnotationsFromLinuxNodes(ctx, mgr.GetConfig()); err != nil {
237236
setupLog.Error(err, "error removing invalid annotations from Linux nodes")
238237
}
239238

240239
proxyEnabled := cluster.IsProxyEnabled()
241-
configMapReconciler, err := controllers.NewConfigMapReconciler(mgr, clusterConfig, watchNamespace, proxyEnabled)
240+
configMapReconciler, err := controllers.NewConfigMapReconciler(ctx, mgr, clusterConfig, watchNamespace, proxyEnabled)
242241
if err != nil {
243242
setupLog.Error(err, "unable to create ConfigMap reconciler")
244243
os.Exit(1)
@@ -284,7 +283,7 @@ func main() {
284283
// controllers are generated by Operator SDK.
285284

286285
// Create the singleton Windows services ConfigMap
287-
if err := configMapReconciler.EnsureServicesConfigMapExists(); err != nil {
286+
if err := configMapReconciler.EnsureServicesConfigMapExists(ctx); err != nil {
288287
setupLog.Error(err, "error ensuring object exists", "singleton", types.NamespacedName{Namespace: watchNamespace,
289288
Name: servicescm.Name})
290289
os.Exit(1)
@@ -298,14 +297,14 @@ func main() {
298297
// If proxy is enabled, disabled, or edited during WMCO runtime, the WMCO pod will be restarted by OLM. This could
299298
// happen in the middle of node configuration, at which the controllers will reconcile once the WMCO pod restarts
300299
if proxyEnabled {
301-
if err := configMapReconciler.EnsureTrustedCAConfigMapExists(); err != nil {
300+
if err := configMapReconciler.EnsureTrustedCAConfigMapExists(ctx); err != nil {
302301
setupLog.Error(err, "error ensuring trusted CA ConfigMap exists", "namespace", watchNamespace)
303302
os.Exit(1)
304303
}
305304
}
306305

307306
setupLog.Info("starting manager")
308-
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
307+
if err := mgr.Start(ctx); err != nil {
309308
setupLog.Error(err, "problem running manager")
310309

311310
os.Exit(1)

controllers/certificatesigningrequests_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ func (r *certificateSigningRequestsReconciler) Reconcile(ctx context.Context,
8080
_ = r.log.WithValues(CSRController, req.NamespacedName)
8181

8282
// Prevent WMCO upgrades while CSRs are being processed
83-
if err := condition.MarkAsBusy(r.client, r.watchNamespace, r.recorder, CSRController); err != nil {
83+
if err := condition.MarkAsBusy(ctx, r.client, r.watchNamespace, r.recorder, CSRController); err != nil {
8484
return ctrl.Result{}, err
8585
}
8686
defer func() {
87-
reconcileErr = markAsFreeOnSuccess(r.client, r.watchNamespace, r.recorder, CSRController,
87+
reconcileErr = markAsFreeOnSuccess(ctx, r.client, r.watchNamespace, r.recorder, CSRController,
8888
result.Requeue, reconcileErr)
8989
}()
9090

@@ -121,7 +121,7 @@ func (r *certificateSigningRequestsReconciler) reconcileCSR(ctx context.Context,
121121
return fmt.Errorf("could not create WMCO CSR Approver: %w", err)
122122
}
123123

124-
return csrApprover.Approve()
124+
return csrApprover.Approve(ctx)
125125
})
126126
if err != nil {
127127
// Max retries were hit, or unrelated issue like permissions or a network error

controllers/configmap_controller.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type ConfigMapReconciler struct {
9090
}
9191

9292
// NewConfigMapReconciler returns a pointer to a ConfigMapReconciler
93-
func NewConfigMapReconciler(mgr manager.Manager, clusterConfig cluster.Config, watchNamespace string,
93+
func NewConfigMapReconciler(ctx context.Context, mgr manager.Manager, clusterConfig cluster.Config, watchNamespace string,
9494
proxyEnabled bool) (*ConfigMapReconciler, error) {
9595
clientset, err := kubernetes.NewForConfig(mgr.GetConfig())
9696
if err != nil {
@@ -101,7 +101,7 @@ func NewConfigMapReconciler(mgr manager.Manager, clusterConfig cluster.Config, w
101101
if err != nil {
102102
return nil, err
103103
}
104-
svcData, err := generateServicesManifest(directClient, clusterConfig.Network().VXLANPort(), clusterConfig.Platform())
104+
svcData, err := generateServicesManifest(ctx, directClient, clusterConfig.Network().VXLANPort(), clusterConfig.Platform())
105105
if err != nil {
106106
return nil, err
107107
}
@@ -132,22 +132,22 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context,
132132

133133
var err error
134134
// Prevent WMCO upgrades while BYOH nodes are being processed.
135-
if err := condition.MarkAsBusy(r.client, r.watchNamespace, r.recorder, ConfigMapController); err != nil {
135+
if err := condition.MarkAsBusy(ctx, r.client, r.watchNamespace, r.recorder, ConfigMapController); err != nil {
136136
return ctrl.Result{}, err
137137
}
138138
defer func() {
139-
reconcileErr = markAsFreeOnSuccess(r.client, r.watchNamespace, r.recorder, ConfigMapController,
139+
reconcileErr = markAsFreeOnSuccess(ctx, r.client, r.watchNamespace, r.recorder, ConfigMapController,
140140
result.Requeue, reconcileErr)
141141
}()
142142

143143
// Create a new signer using the private key that the instances will be configured with
144-
r.signer, err = signer.Create(kubeTypes.NamespacedName{Namespace: r.watchNamespace,
144+
r.signer, err = signer.Create(ctx, kubeTypes.NamespacedName{Namespace: r.watchNamespace,
145145
Name: secrets.PrivateKeySecret}, r.client)
146146
if err != nil {
147147
return ctrl.Result{}, fmt.Errorf("unable to create signer from private key secret: %w", err)
148148
}
149149

150-
servicesManifest, err := generateServicesManifest(r.client, r.VXLANPort, r.platform)
150+
servicesManifest, err := generateServicesManifest(ctx, r.client, r.VXLANPort, r.platform)
151151
if err != nil {
152152
return ctrl.Result{}, err
153153
}
@@ -273,23 +273,23 @@ func (r *ConfigMapReconciler) reconcileNodes(ctx context.Context, windowsInstanc
273273

274274
r.log.Info("processing", "instances in", wiparser.InstanceConfigMap)
275275
// For each instance, ensure that it is configured into a node
276-
if err := r.ensureInstancesAreUpToDate(instances); err != nil {
276+
if err := r.ensureInstancesAreUpToDate(ctx, instances); err != nil {
277277
r.recorder.Eventf(windowsInstances, core.EventTypeWarning, "InstanceSetupFailure", err.Error())
278278
return err
279279
}
280280

281281
// Ensure that only instances currently specified by the ConfigMap are joined to the cluster as nodes
282-
if err = r.deconfigureInstances(instances, nodes); err != nil {
282+
if err = r.deconfigureInstances(ctx, instances, nodes); err != nil {
283283
return fmt.Errorf("error removing undesired nodes from cluster: %w", err)
284284
}
285285

286286
return nil
287287
}
288288

289289
// ensureInstancesAreUpToDate configures all instances that require configuration
290-
func (r *ConfigMapReconciler) ensureInstancesAreUpToDate(instances []*instance.Info) error {
290+
func (r *ConfigMapReconciler) ensureInstancesAreUpToDate(ctx context.Context, instances []*instance.Info) error {
291291
// Get private key to encrypt instance usernames
292-
privateKeyBytes, err := secrets.GetPrivateKey(kubeTypes.NamespacedName{Namespace: r.watchNamespace,
292+
privateKeyBytes, err := secrets.GetPrivateKey(ctx, kubeTypes.NamespacedName{Namespace: r.watchNamespace,
293293
Name: secrets.PrivateKeySecret}, r.client)
294294
if err != nil {
295295
return err
@@ -304,7 +304,7 @@ func (r *ConfigMapReconciler) ensureInstancesAreUpToDate(instances []*instance.I
304304
if err != nil {
305305
return fmt.Errorf("unable to encrypt username for instance %s: %w", instanceInfo.Address, err)
306306
}
307-
err = r.ensureInstanceIsUpToDate(instanceInfo, map[string]string{BYOHLabel: "true", nodeconfig.WorkerLabel: ""},
307+
err = r.ensureInstanceIsUpToDate(ctx, instanceInfo, map[string]string{BYOHLabel: "true", nodeconfig.WorkerLabel: ""},
308308
map[string]string{UsernameAnnotation: encryptedUsername})
309309
if err != nil {
310310
// It is better to return early like this, instead of trying to configure as many instances as possible in a
@@ -321,7 +321,7 @@ func (r *ConfigMapReconciler) ensureInstancesAreUpToDate(instances []*instance.I
321321

322322
// deconfigureInstances removes all BYOH nodes that are not specified in the given instances slice, and
323323
// deconfigures the instances associated with them. The nodes parameter should be a list of all Windows BYOH nodes.
324-
func (r *ConfigMapReconciler) deconfigureInstances(instances []*instance.Info, nodes *core.NodeList) error {
324+
func (r *ConfigMapReconciler) deconfigureInstances(ctx context.Context, instances []*instance.Info, nodes *core.NodeList) error {
325325
windowsInstances := &core.ConfigMap{ObjectMeta: meta.ObjectMeta{Name: wiparser.InstanceConfigMap,
326326
Namespace: r.watchNamespace}}
327327
for _, node := range nodes.Items {
@@ -331,7 +331,7 @@ func (r *ConfigMapReconciler) deconfigureInstances(instances []*instance.Info, n
331331
}
332332

333333
// no instance found in the provided list, remove the node from the cluster
334-
if err := r.deconfigureInstance(&node); err != nil {
334+
if err := r.deconfigureInstance(ctx, &node); err != nil {
335335
return fmt.Errorf("unable to deconfigure instance with node %s: %w", node.GetName(), err)
336336
}
337337
r.recorder.Eventf(windowsInstances, core.EventTypeNormal, "InstanceTeardown",
@@ -451,12 +451,12 @@ func (r *ConfigMapReconciler) createServicesConfigMap(ctx context.Context) (*cor
451451
// createServicesConfigMapOnBootup creates a valid ServicesConfigMap
452452
// ConfigMapReconciler.createServicesConfigMap() cannot be used in its stead as the cache has not been
453453
// populated yet, which is why the typed client is used here as it calls the API server directly.
454-
func (r *ConfigMapReconciler) createServicesConfigMapOnBootup() error {
454+
func (r *ConfigMapReconciler) createServicesConfigMapOnBootup(ctx context.Context) error {
455455
windowsServices, err := servicescm.Generate(servicescm.Name, r.watchNamespace, r.servicesManifest)
456456
if err != nil {
457457
return err
458458
}
459-
cm, err := r.k8sclientset.CoreV1().ConfigMaps(r.watchNamespace).Create(context.TODO(), windowsServices,
459+
cm, err := r.k8sclientset.CoreV1().ConfigMaps(r.watchNamespace).Create(ctx, windowsServices,
460460
meta.CreateOptions{})
461461
if err != nil {
462462
return err
@@ -466,13 +466,13 @@ func (r *ConfigMapReconciler) createServicesConfigMapOnBootup() error {
466466
}
467467

468468
// EnsureServicesConfigMapExists ensures that the ServicesConfigMap is present and valid on operator bootup
469-
func (r *ConfigMapReconciler) EnsureServicesConfigMapExists() error {
470-
windowsServices, err := r.k8sclientset.CoreV1().ConfigMaps(r.watchNamespace).Get(context.TODO(), servicescm.Name,
469+
func (r *ConfigMapReconciler) EnsureServicesConfigMapExists(ctx context.Context) error {
470+
windowsServices, err := r.k8sclientset.CoreV1().ConfigMaps(r.watchNamespace).Get(ctx, servicescm.Name,
471471
meta.GetOptions{})
472472
if err != nil {
473473
if k8sapierrors.IsNotFound(err) {
474474
// If ConfigMap is not found, create it and return
475-
return r.createServicesConfigMapOnBootup()
475+
return r.createServicesConfigMapOnBootup(ctx)
476476
}
477477
return err
478478
}
@@ -484,13 +484,13 @@ func (r *ConfigMapReconciler) EnsureServicesConfigMapExists() error {
484484
}
485485

486486
// Delete and re-create the ConfigMap with the proper values
487-
if err = r.k8sclientset.CoreV1().ConfigMaps(r.watchNamespace).Delete(context.TODO(), windowsServices.Name,
487+
if err = r.k8sclientset.CoreV1().ConfigMaps(r.watchNamespace).Delete(ctx, windowsServices.Name,
488488
meta.DeleteOptions{}); err != nil {
489489
return err
490490
}
491491
r.log.Info("Deleted invalid resource", "ConfigMap",
492492
kubeTypes.NamespacedName{Namespace: r.watchNamespace, Name: servicescm.Name})
493-
return r.createServicesConfigMapOnBootup()
493+
return r.createServicesConfigMapOnBootup(ctx)
494494
}
495495

496496
// createProxyCertsCM creates the trusted CA ConfigMap with the expected spec
@@ -529,7 +529,7 @@ func (r *ConfigMapReconciler) ensureTrustedCABundleInNodes(ctx context.Context)
529529

530530
// ensureTrustedCABundleInNodes places the trusted CA bundle data into a file on the given node
531531
func (r *ConfigMapReconciler) ensureTrustedCABundleInNode(ctx context.Context, node core.Node) error {
532-
winInstance, err := r.instanceFromNode(&node)
532+
winInstance, err := r.instanceFromNode(ctx, &node)
533533
if err != nil {
534534
return err
535535
}
@@ -538,7 +538,7 @@ func (r *ConfigMapReconciler) ensureTrustedCABundleInNode(ctx context.Context, n
538538
if err != nil {
539539
return fmt.Errorf("failed to create new nodeconfig: %w", err)
540540
}
541-
return nc.SyncTrustedCABundle()
541+
return nc.SyncTrustedCABundle(ctx)
542542
}
543543

544544
// ensureProxyCertsCMIsValid ensures the trusted CA ConfigMap has the expected injection request. Patches the object if not.
@@ -555,7 +555,7 @@ func (r *ConfigMapReconciler) ensureProxyCertsCMIsValid(ctx context.Context, inj
555555
return fmt.Errorf("unable to generate patch request body for label %s: %w", InjectionRequestLabel, err)
556556
}
557557

558-
if _, err = r.k8sclientset.CoreV1().ConfigMaps(r.watchNamespace).Patch(context.TODO(),
558+
if _, err = r.k8sclientset.CoreV1().ConfigMaps(r.watchNamespace).Patch(ctx,
559559
certificates.ProxyCertsConfigMap, kubeTypes.JSONPatchType, patchData, meta.PatchOptions{}); err != nil {
560560
return fmt.Errorf("unable to apply patch %s to resource %s/%s: %w", patchData, r.watchNamespace,
561561
certificates.ProxyCertsConfigMap, err)
@@ -566,16 +566,16 @@ func (r *ConfigMapReconciler) ensureProxyCertsCMIsValid(ctx context.Context, inj
566566

567567
// EnsureTrustedCAConfigMapExists ensures the trusted CA ConfigMap exists as expected.
568568
// Creates it if it doesn't exist, patches it if it exists with improper spec.
569-
func (r *ConfigMapReconciler) EnsureTrustedCAConfigMapExists() error {
570-
trustedCA, err := r.k8sclientset.CoreV1().ConfigMaps(r.watchNamespace).Get(context.TODO(),
569+
func (r *ConfigMapReconciler) EnsureTrustedCAConfigMapExists(ctx context.Context) error {
570+
trustedCA, err := r.k8sclientset.CoreV1().ConfigMaps(r.watchNamespace).Get(ctx,
571571
certificates.ProxyCertsConfigMap, meta.GetOptions{})
572572
if err != nil {
573573
if !k8sapierrors.IsNotFound(err) {
574574
return err
575575
}
576-
return r.createProxyCertsCM(context.TODO())
576+
return r.createProxyCertsCM(ctx)
577577
}
578-
return r.ensureProxyCertsCMIsValid(context.TODO(), trustedCA.GetLabels()[InjectionRequestLabel])
578+
return r.ensureProxyCertsCMIsValid(ctx, trustedCA.GetLabels()[InjectionRequestLabel])
579579
}
580580

581581
// EnsureWICDRBAC ensures the WICD RBAC resources exist as expected
@@ -691,8 +691,8 @@ func (r *ConfigMapReconciler) ensureWICDClusterRoleBinding(ctx context.Context)
691691
// generateServicesManifest generates and regenerates the services manifest.
692692
// this gets called when the configmap reconciler is first created, to create the services manifest,
693693
// and also when the rendered-worker configmap is changed, to regenerate it.
694-
func generateServicesManifest(client client.Client, port string, platform oconfig.PlatformType) (*servicescm.Data, error) {
695-
ign, err := ignition.New(client)
694+
func generateServicesManifest(ctx context.Context, client client.Client, port string, platform oconfig.PlatformType) (*servicescm.Data, error) {
695+
ign, err := ignition.New(ctx, client)
696696
if err != nil {
697697
return nil, fmt.Errorf("error creating ignition object: %w", err)
698698
}

controllers/controllerconfig_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (r *ControllerConfigReconciler) Reconcile(ctx context.Context, req ctrl.Req
8080
}
8181
return ctrl.Result{}, err
8282
}
83-
r.signer, err = signer.Create(kubeTypes.NamespacedName{Namespace: r.watchNamespace,
83+
r.signer, err = signer.Create(ctx, kubeTypes.NamespacedName{Namespace: r.watchNamespace,
8484
Name: secrets.PrivateKeySecret}, r.client)
8585
if err != nil {
8686
return ctrl.Result{}, fmt.Errorf("unable to create signer from private key secret: %w", err)
@@ -93,7 +93,7 @@ func (r *ControllerConfigReconciler) Reconcile(ctx context.Context, req ctrl.Req
9393
}
9494
// loop Windows nodes and trigger kubelet CA update
9595
for _, winNode := range winNodes.Items {
96-
if err := r.updateKubeletCA(winNode, cc.Spec.KubeAPIServerServingCAData); err != nil {
96+
if err := r.updateKubeletCA(ctx, winNode, cc.Spec.KubeAPIServerServingCAData); err != nil {
9797
return ctrl.Result{}, fmt.Errorf("error updating kubelet CA certificate in node %s: %w", winNode.Name, err)
9898
}
9999
}

0 commit comments

Comments
 (0)