diff --git a/cmd/controller/main.go b/cmd/controller/main.go index 5864b1ec1..4c31d5ef6 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -80,7 +80,7 @@ func main() { MaxConcurrentReconciles: concurrentReconciles, } - mgr, err = controller.NewManager(restConfig, managerOptions, controllerOptions) + mgr, err = controller.NewManager(ctx, restConfig, managerOptions, controllerOptions) if err != nil { mainLog.Error(err, "unable to start manager") os.Exit(1) diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index ac3e0ec13..a0dd18c70 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -32,13 +32,13 @@ var ( const TaskRunLabel = "tekton.dev/taskRun" -func NewManager(cfg *rest.Config, managerOptions ctrl.Options, controllerOptions controller.Options) (ctrl.Manager, error) { +func NewManager(ctx context.Context, cfg *rest.Config, managerOptions ctrl.Options, controllerOptions controller.Options) (ctrl.Manager, error) { // do not check tekton in kcp // we have seen in e2e testing that this path can get invoked prior to the TaskRun CRD getting generated, // and controller-runtime does not retry on missing CRDs. // so we are going to wait on the CRDs existing before moving forward. apiextensionsClient := apiextensionsclient.NewForConfigOrDie(cfg) - if err := wait.PollUntilContextTimeout(context.Background(), time.Second*5, time.Minute*5, true, func(ctx context.Context) (done bool, err error) { + if err := wait.PollUntilContextTimeout(ctx, time.Second*5, time.Minute*5, true, func(ctx context.Context) (done bool, err error) { _, err = apiextensionsClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, "taskruns.tekton.dev", metav1.GetOptions{}) if err != nil { controllerLog.Info("get of taskrun CRD failed with: " + err.Error()) @@ -104,15 +104,20 @@ func NewManager(cfg *rest.Config, managerOptions ctrl.Options, controllerOptions ticker := time.NewTicker(time.Hour * 24) go func() { - for range ticker.C { - taskrun.UpdateHostPools(operatorNamespace, mgr.GetClient(), &controllerLog) + for { + select { + case <-ticker.C: + taskrun.UpdateHostPools(ctx, operatorNamespace, mgr.GetClient(), &controllerLog) + case <-ctx.Done(): + return + } } }() timer := time.NewTimer(time.Minute) go func() { <-timer.C //update the nodes on startup - taskrun.UpdateHostPools(operatorNamespace, mgr.GetClient(), &controllerLog) + taskrun.UpdateHostPools(ctx, operatorNamespace, mgr.GetClient(), &controllerLog) }() if err := mpcmetrics.AddTaskRunMetricsExporter(mgr); err != nil { diff --git a/pkg/reconciler/taskrun/hostupdate_test.go b/pkg/reconciler/taskrun/hostupdate_test.go index 450282782..33d18697e 100644 --- a/pkg/reconciler/taskrun/hostupdate_test.go +++ b/pkg/reconciler/taskrun/hostupdate_test.go @@ -104,7 +104,7 @@ var _ = Describe("HostUpdateTaskRunTest", func() { // when: host pools are updated log := logr.FromContextOrDiscard(ctx) - UpdateHostPools(testNamespace, k8sClient, &log) + UpdateHostPools(ctx, testNamespace, k8sClient, &log) // then: no host pool update tasks are created list := v1.TaskRunList{} @@ -150,7 +150,7 @@ var _ = Describe("HostUpdateTaskRunTest", func() { // when: host pools are updated log := logr.FromContextOrDiscard(ctx) - UpdateHostPools(testNamespace, k8sClient, &log) + UpdateHostPools(ctx, testNamespace, k8sClient, &log) // when: spawned threads run to completion waitGroup.Wait() @@ -192,7 +192,7 @@ var _ = Describe("HostUpdateTaskRunTest", func() { // when: host pools are updated log := logr.FromContextOrDiscard(ctx) - UpdateHostPools(testNamespace, k8sClient, &log) + UpdateHostPools(ctx, testNamespace, k8sClient, &log) // test everything in TaskRun creation that is not part of the table testing Eventually(func(g Gomega) { diff --git a/pkg/reconciler/taskrun/updates.go b/pkg/reconciler/taskrun/updates.go index f407140da..e623c8218 100644 --- a/pkg/reconciler/taskrun/updates.go +++ b/pkg/reconciler/taskrun/updates.go @@ -15,10 +15,10 @@ import ( ) // UpdateHostPools Run the host update task periodically -func UpdateHostPools(operatorNamespace string, client client.Client, log *logr.Logger) { +func UpdateHostPools(ctx context.Context, operatorNamespace string, client client.Client, log *logr.Logger) { log.Info("running pooled host update") cm := v12.ConfigMap{} - err := client.Get(context.Background(), types.NamespacedName{Namespace: operatorNamespace, Name: HostConfig}, &cm) + err := client.Get(ctx, types.NamespacedName{Namespace: operatorNamespace, Name: HostConfig}, &cm) if err != nil { log.Error(err, "Failed to read config to update hosts", "audit", "true") return @@ -105,7 +105,7 @@ func UpdateHostPools(operatorNamespace string, client client.Client, log *logr.L Value: *v1.NewStructuredValues(hostsConcurrency[host.Name]), }, } - err = client.Create(context.Background(), &provision) + err = client.Create(ctx, &provision) }() } }