diff --git a/controllers/clusterversion_controller.go b/controllers/clusterversion_controller.go index 3a4073847..f397eca03 100644 --- a/controllers/clusterversion_controller.go +++ b/controllers/clusterversion_controller.go @@ -55,10 +55,10 @@ type ClusterVersionReconciler struct { func (r *ClusterVersionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { logger := log.FromContext(ctx) instance := configv1.ClusterVersion{} - if err := r.Client.Get(context.TODO(), req.NamespacedName, &instance); err != nil { + if err := r.Client.Get(ctx, req.NamespacedName, &instance); err != nil { return ctrl.Result{}, err } - if err := r.ensureConsolePlugin(instance.Status.Desired.Version); err != nil { + if err := r.ensureConsolePlugin(ctx, instance.Status.Desired.Version); err != nil { logger.Error(err, "Could not ensure compatibility for ODF consolePlugin") return ctrl.Result{}, err } @@ -68,13 +68,14 @@ func (r *ClusterVersionReconciler) Reconcile(ctx context.Context, req ctrl.Reque // SetupWithManager sets up the controller with the Manager. func (r *ClusterVersionReconciler) SetupWithManager(mgr ctrl.Manager) error { + ctx := context.TODO() err := mgr.Add(manager.RunnableFunc(func(context.Context) error { - clusterVersion, err := util.DetermineOpenShiftVersion(r.Client) + clusterVersion, err := util.DetermineOpenShiftVersion(ctx, r.Client) if err != nil { return err } - return r.ensureConsolePlugin(clusterVersion) + return r.ensureConsolePlugin(ctx, clusterVersion) })) if err != nil { return err @@ -85,15 +86,15 @@ func (r *ClusterVersionReconciler) SetupWithManager(mgr ctrl.Manager) error { Complete(r) } -func (r *ClusterVersionReconciler) ensureConsolePlugin(clusterVersion string) error { - logger := log.FromContext(context.TODO()) +func (r *ClusterVersionReconciler) ensureConsolePlugin(ctx context.Context, clusterVersion string) error { + logger := log.FromContext(ctx) // The base path to where the request are sent basePath := console.GetBasePath(clusterVersion) nginxConf := console.GetNginxConfiguration() // Get ODF console Deployment odfConsoleDeployment := console.GetDeployment(OperatorNamespace) - err := r.Client.Get(context.TODO(), types.NamespacedName{ + err := r.Client.Get(ctx, types.NamespacedName{ Name: odfConsoleDeployment.Name, Namespace: odfConsoleDeployment.Namespace, }, odfConsoleDeployment) @@ -103,7 +104,7 @@ func (r *ClusterVersionReconciler) ensureConsolePlugin(clusterVersion string) er // Create/Update ODF console ConfigMap (nginx configuration) odfConsoleConfigMap := console.GetNginxConfConfigMap(OperatorNamespace) - _, err = controllerutil.CreateOrUpdate(context.TODO(), r.Client, odfConsoleConfigMap, func() error { + _, err = controllerutil.CreateOrUpdate(ctx, r.Client, odfConsoleConfigMap, func() error { if odfConsoleConfigMapData := odfConsoleConfigMap.Data["nginx.conf"]; odfConsoleConfigMapData != nginxConf { logger.Info(fmt.Sprintf("Set the ConfigMap odf-console-nginx-conf data as '%s'", nginxConf)) odfConsoleConfigMap.Data["nginx.conf"] = nginxConf @@ -116,7 +117,7 @@ func (r *ClusterVersionReconciler) ensureConsolePlugin(clusterVersion string) er // Create/Update ODF console Service odfConsoleService := console.GetService(r.ConsolePort, OperatorNamespace) - _, err = controllerutil.CreateOrUpdate(context.TODO(), r.Client, odfConsoleService, func() error { + _, err = controllerutil.CreateOrUpdate(ctx, r.Client, odfConsoleService, func() error { return controllerutil.SetControllerReference(odfConsoleDeployment, odfConsoleService, r.Scheme) }) if err != nil && !errors.IsAlreadyExists(err) { @@ -125,7 +126,7 @@ func (r *ClusterVersionReconciler) ensureConsolePlugin(clusterVersion string) er // Create/Update ODF console ConsolePlugin odfConsolePlugin := console.GetConsolePluginCR(r.ConsolePort, OperatorNamespace) - _, err = controllerutil.CreateOrUpdate(context.TODO(), r.Client, odfConsolePlugin, func() error { + _, err = controllerutil.CreateOrUpdate(ctx, r.Client, odfConsolePlugin, func() error { if currentBasePath := odfConsolePlugin.Spec.Service.BasePath; currentBasePath != basePath { logger.Info(fmt.Sprintf("Set the BasePath for odf-console plugin as '%s'", basePath)) odfConsolePlugin.Spec.Service.BasePath = basePath diff --git a/controllers/delete.go b/controllers/delete.go index 81871bcc7..0a2bf617d 100644 --- a/controllers/delete.go +++ b/controllers/delete.go @@ -30,18 +30,18 @@ import ( odfv1alpha1 "github.com/red-hat-storage/odf-operator/api/v1alpha1" ) -func (r *StorageSystemReconciler) deleteResources(instance *odfv1alpha1.StorageSystem, logger logr.Logger) error { +func (r *StorageSystemReconciler) deleteResources(ctx context.Context, instance *odfv1alpha1.StorageSystem, logger logr.Logger) error { var backendStorage client.Object - r.deleteQuickStarts(logger, instance) + r.deleteQuickStarts(ctx, logger, instance) if instance.Spec.Kind == VendorStorageCluster() { backendStorage = &ocsv1.StorageCluster{} } else if instance.Spec.Kind == VendorFlashSystemCluster() { backendStorage = &ibmv1alpha1.FlashSystemCluster{} } - err := r.Client.Get(context.TODO(), types.NamespacedName{ + err := r.Client.Get(ctx, types.NamespacedName{ Name: instance.Spec.Name, Namespace: instance.Spec.Namespace}, backendStorage) @@ -53,7 +53,7 @@ func (r *StorageSystemReconciler) deleteResources(instance *odfv1alpha1.StorageS return err } - err = r.Client.Delete(context.TODO(), backendStorage) + err = r.Client.Delete(ctx, backendStorage) if err != nil { logger.Error(err, "Failed to delete", "Kind", instance.Spec.Kind, "Name", instance.Spec.Name) return err @@ -64,10 +64,10 @@ func (r *StorageSystemReconciler) deleteResources(instance *odfv1alpha1.StorageS return fmt.Errorf("Waiting for %s %s to be deleted", instance.Spec.Kind, instance.Spec.Name) } -func (r *StorageSystemReconciler) areAllStorageSystemsMarkedForDeletion(namespace string) (bool, error) { +func (r *StorageSystemReconciler) areAllStorageSystemsMarkedForDeletion(ctx context.Context, namespace string) (bool, error) { var storageSystems odfv1alpha1.StorageSystemList - err := r.Client.List(context.TODO(), &storageSystems, &client.ListOptions{Namespace: namespace}) + err := r.Client.List(ctx, &storageSystems, &client.ListOptions{Namespace: namespace}) if err != nil { return false, err } diff --git a/controllers/delete_test.go b/controllers/delete_test.go index d458699f6..656655c49 100644 --- a/controllers/delete_test.go +++ b/controllers/delete_test.go @@ -67,6 +67,7 @@ func TestDeleteResources(t *testing.T) { } for i, tc := range testCases { + ctx := context.TODO() t.Logf("Case %d: %s\n", i+1, tc.label) fakeStorageSystem := GetFakeStorageSystem(tc.kind) @@ -83,24 +84,24 @@ func TestDeleteResources(t *testing.T) { fakeStorageSystem.Spec.Name = vendorSystem.GetName() fakeStorageSystem.Spec.Namespace = vendorSystem.GetNamespace() - err = fakeReconciler.Client.Create(context.TODO(), vendorSystem) + err = fakeReconciler.Client.Create(ctx, vendorSystem) assert.NoError(t, err) } - err = fakeReconciler.deleteResources(fakeStorageSystem, fakeReconciler.Log) + err = fakeReconciler.deleteResources(ctx, fakeStorageSystem, fakeReconciler.Log) assert.True(t, (tc.expectedError == (err != nil))) // verify resource does not exist if tc.kind == VendorStorageCluster() { storageCluster := &ocsv1.StorageCluster{} - err = fakeReconciler.Client.Get(context.TODO(), types.NamespacedName{ + err = fakeReconciler.Client.Get(ctx, types.NamespacedName{ Name: fakeStorageSystem.Spec.Name, Namespace: fakeStorageSystem.Namespace}, storageCluster) assert.True(t, errors.IsNotFound(err)) } else if tc.kind == VendorFlashSystemCluster() { flashSystemCluster := &ibmv1alpha1.FlashSystemCluster{} - err = fakeReconciler.Client.Get(context.TODO(), types.NamespacedName{ + err = fakeReconciler.Client.Get(ctx, types.NamespacedName{ Name: fakeStorageSystem.Spec.Name, Namespace: fakeStorageSystem.Namespace}, flashSystemCluster) assert.True(t, errors.IsNotFound(err)) diff --git a/controllers/quickstarts.go b/controllers/quickstarts.go index 1f74ec6e4..7e900f76e 100644 --- a/controllers/quickstarts.go +++ b/controllers/quickstarts.go @@ -28,7 +28,7 @@ import ( odfv1alpha1 "github.com/red-hat-storage/odf-operator/api/v1alpha1" ) -func (r *StorageSystemReconciler) ensureQuickStarts(logger logr.Logger) error { +func (r *StorageSystemReconciler) ensureQuickStarts(ctx context.Context, logger logr.Logger) error { if len(AllQuickStarts) == 0 { logger.Info("No quickstarts found") return nil @@ -41,10 +41,10 @@ func (r *StorageSystemReconciler) ensureQuickStarts(logger logr.Logger) error { continue } found := consolev1.ConsoleQuickStart{} - err = r.Client.Get(context.TODO(), types.NamespacedName{Name: cqs.Name, Namespace: cqs.Namespace}, &found) + err = r.Client.Get(ctx, types.NamespacedName{Name: cqs.Name, Namespace: cqs.Namespace}, &found) if err != nil { if errors.IsNotFound(err) { - err = r.Client.Create(context.TODO(), &cqs) + err = r.Client.Create(ctx, &cqs) if err != nil { logger.Error(err, "Failed to create quickstart", "Name", cqs.Name, "Namespace", cqs.Namespace) return nil @@ -56,7 +56,7 @@ func (r *StorageSystemReconciler) ensureQuickStarts(logger logr.Logger) error { return nil } found.Spec = cqs.Spec - err = r.Client.Update(context.TODO(), &found) + err = r.Client.Update(ctx, &found) if err != nil { logger.Error(err, "Failed to update quickstart", "Name", cqs.Name, "Namespace", cqs.Namespace) return nil @@ -66,12 +66,12 @@ func (r *StorageSystemReconciler) ensureQuickStarts(logger logr.Logger) error { return nil } -func (r *StorageSystemReconciler) deleteQuickStarts(logger logr.Logger, instance *odfv1alpha1.StorageSystem) { +func (r *StorageSystemReconciler) deleteQuickStarts(ctx context.Context, logger logr.Logger, instance *odfv1alpha1.StorageSystem) { if len(AllQuickStarts) == 0 { logger.Info("No quickstarts found.") } - allSSDeleted, err := r.areAllStorageSystemsMarkedForDeletion(instance.Namespace) + allSSDeleted, err := r.areAllStorageSystemsMarkedForDeletion(ctx, instance.Namespace) if err != nil { // Log the error but not fail the operator logger.Error(err, "Failed to List", "Kind", "StorageSystem") @@ -90,7 +90,7 @@ func (r *StorageSystemReconciler) deleteQuickStarts(logger logr.Logger, instance continue } - err = r.Client.Delete(context.TODO(), &cqs) + err = r.Client.Delete(ctx, &cqs) if err != nil { if errors.IsNotFound(err) { continue diff --git a/controllers/quickstarts_test.go b/controllers/quickstarts_test.go index 76246794c..e530b579d 100644 --- a/controllers/quickstarts_test.go +++ b/controllers/quickstarts_test.go @@ -63,7 +63,7 @@ func TestEnsureQuickStarts(t *testing.T) { } fakeReconciler := GetFakeStorageSystemReconciler(t) - err := fakeReconciler.ensureQuickStarts(fakeReconciler.Log) + err := fakeReconciler.ensureQuickStarts(context.TODO(), fakeReconciler.Log) assert.NoError(t, err) for _, c := range cases { qs := consolev1.ConsoleQuickStart{} @@ -140,23 +140,23 @@ func TestDeleteQuickStarts(t *testing.T) { for i, tc := range testCases { t.Logf("Case %d: %s\n", i+1, tc.label) - + ctx := context.TODO() fakeReconciler := GetFakeStorageSystemReconciler(t) - err := fakeReconciler.ensureQuickStarts(fakeReconciler.Log) + err := fakeReconciler.ensureQuickStarts(ctx, fakeReconciler.Log) assert.NoError(t, err) var quickstarts []consolev1.ConsoleQuickStart = getActualQuickStarts(t, cases, fakeReconciler) assert.Equal(t, 2, len(quickstarts)) for i := range tc.createStorageSystems { - err = fakeReconciler.Client.Create(context.TODO(), &tc.createStorageSystems[i]) + err = fakeReconciler.Client.Create(ctx, &tc.createStorageSystems[i]) assert.NoError(t, err) } for i := range tc.deleteStorageSystems { - err := fakeReconciler.Client.Delete(context.TODO(), &tc.deleteStorageSystems[i]) + err := fakeReconciler.Client.Delete(ctx, &tc.deleteStorageSystems[i]) assert.NoError(t, err) - err = fakeReconciler.deleteResources(&tc.deleteStorageSystems[i], fakeReconciler.Log) + err = fakeReconciler.deleteResources(ctx, &tc.deleteStorageSystems[i], fakeReconciler.Log) assert.NoError(t, err) } quickstarts = getActualQuickStarts(t, cases, fakeReconciler) diff --git a/controllers/storagecluster_controller.go b/controllers/storagecluster_controller.go index 38dce09f6..0b2a46d04 100644 --- a/controllers/storagecluster_controller.go +++ b/controllers/storagecluster_controller.go @@ -57,7 +57,7 @@ func (r *StorageClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque logger := log.FromContext(ctx) instance := &ocsv1.StorageCluster{} - err := r.Client.Get(context.TODO(), req.NamespacedName, instance) + err := r.Client.Get(ctx, req.NamespacedName, instance) if err != nil { if errors.IsNotFound(err) { logger.Info("StorageCluster instance not found.") @@ -71,7 +71,7 @@ func (r *StorageClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque // get list of StorageSystems storageSystemList := &odfv1alpha1.StorageSystemList{} - err = r.Client.List(context.TODO(), storageSystemList, &client.ListOptions{Namespace: instance.Namespace}) + err = r.Client.List(ctx, storageSystemList, &client.ListOptions{Namespace: instance.Namespace}) if err != nil { logger.Error(err, "Failed to list the StorageSystem.") return ctrl.Result{}, err @@ -94,7 +94,7 @@ func (r *StorageClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque } // create StorageSystem for storageCluster - err = r.Client.Create(context.TODO(), storageSystem) + err = r.Client.Create(ctx, storageSystem) if err != nil { logger.Error(err, "Failed to create StorageSystem.", "StorageSystem", storageSystem.Name) return ctrl.Result{}, err diff --git a/controllers/storagesystem_controller.go b/controllers/storagesystem_controller.go index 3c3571a9c..ea9bbcfa6 100644 --- a/controllers/storagesystem_controller.go +++ b/controllers/storagesystem_controller.go @@ -69,7 +69,7 @@ func (r *StorageSystemReconciler) Reconcile(ctx context.Context, req ctrl.Reques logger := r.Log.WithValues("instance", req.NamespacedName) instance := &odfv1alpha1.StorageSystem{} - err := r.Client.Get(context.TODO(), req.NamespacedName, instance) + err := r.Client.Get(ctx, req.NamespacedName, instance) if err != nil { if errors.IsNotFound(err) { logger.Info("storagesystem instance not found") @@ -84,10 +84,10 @@ func (r *StorageSystemReconciler) Reconcile(ctx context.Context, req ctrl.Reques metrics.ReportODFSystemMapMetrics(instance.Name, instance.Spec.Name, instance.Spec.Namespace, string(instance.Spec.Kind)) // Reconcile changes - result, reconcileError := r.reconcile(instance, logger) + result, reconcileError := r.reconcile(ctx, instance, logger) // Apply status changes - statusError := r.Client.Status().Update(context.TODO(), instance) + statusError := r.Client.Status().Update(ctx, instance) if statusError != nil { logger.Error(statusError, "failed to update status") } @@ -103,7 +103,7 @@ func (r *StorageSystemReconciler) Reconcile(ctx context.Context, req ctrl.Reques } } -func (r *StorageSystemReconciler) reconcile(instance *odfv1alpha1.StorageSystem, logger logr.Logger) (ctrl.Result, error) { +func (r *StorageSystemReconciler) reconcile(ctx context.Context, instance *odfv1alpha1.StorageSystem, logger logr.Logger) (ctrl.Result, error) { var err error @@ -123,14 +123,14 @@ func (r *StorageSystemReconciler) reconcile(instance *odfv1alpha1.StorageSystem, if util.FindInSlice(instance.GetFinalizers(), storageSystemFinalizer) { SetDeletionInProgressConditions(&instance.Status.Conditions, "Deleting", "Deletion is in progress") - err = r.deleteResources(instance, logger) + err = r.deleteResources(ctx, instance, logger) if err != nil { return ctrl.Result{}, err } logger.Info("storagesystem is in deletion phase remove finalizer", "Finalizer", storageSystemFinalizer) instance.ObjectMeta.Finalizers = util.RemoveFromSlice(instance.ObjectMeta.Finalizers, storageSystemFinalizer) - if err := r.updateStorageSystem(instance); err != nil { + if err := r.updateStorageSystem(ctx, instance); err != nil { logger.Error(err, "failed to remove finalizer from storagesystem", "Finalizer", storageSystemFinalizer) return ctrl.Result{}, err } @@ -143,28 +143,28 @@ func (r *StorageSystemReconciler) reconcile(instance *odfv1alpha1.StorageSystem, if !util.FindInSlice(instance.GetFinalizers(), storageSystemFinalizer) { logger.Info("finalizer not found Add finalizer", "Finalizer", storageSystemFinalizer) instance.ObjectMeta.Finalizers = append(instance.ObjectMeta.Finalizers, storageSystemFinalizer) - if err = r.updateStorageSystem(instance); err != nil { + if err = r.updateStorageSystem(ctx, instance); err != nil { logger.Error(err, "failed to update storagesystem with finalizer", "Finalizer", storageSystemFinalizer) return ctrl.Result{}, err } } - err = r.ensureQuickStarts(logger) + err = r.ensureQuickStarts(ctx, logger) if err != nil { return ctrl.Result{}, err } - err = r.ensureSubscriptions(instance, logger) + err = r.ensureSubscriptions(ctx, instance, logger) if err != nil { return ctrl.Result{}, err } - err = r.isVendorCsvReady(instance, logger) + err = r.isVendorCsvReady(ctx, instance, logger) if err != nil { return ctrl.Result{}, err } - err = r.isVendorSystemPresent(instance, logger) + err = r.isVendorSystemPresent(ctx, instance, logger) if err != nil { return ctrl.Result{}, err } @@ -174,11 +174,11 @@ func (r *StorageSystemReconciler) reconcile(instance *odfv1alpha1.StorageSystem, return ctrl.Result{}, nil } -func (r *StorageSystemReconciler) updateStorageSystem(instance *odfv1alpha1.StorageSystem) error { +func (r *StorageSystemReconciler) updateStorageSystem(ctx context.Context, instance *odfv1alpha1.StorageSystem) error { // save the status locally before the Update call, as update call does not update the status and we lost it instanceStatus := instance.Status.DeepCopy() - err := r.Client.Update(context.TODO(), instance) + err := r.Client.Update(ctx, instance) instance.Status = *(instanceStatus) return err } @@ -197,7 +197,7 @@ func (r *StorageSystemReconciler) validateStorageSystemSpec(instance *odfv1alpha return nil } -func (r *StorageSystemReconciler) ensureSubscriptions(instance *odfv1alpha1.StorageSystem, logger logr.Logger) error { +func (r *StorageSystemReconciler) ensureSubscriptions(ctx context.Context, instance *odfv1alpha1.StorageSystem, logger logr.Logger) error { var err error @@ -207,7 +207,7 @@ func (r *StorageSystemReconciler) ensureSubscriptions(instance *odfv1alpha1.Stor } for _, desiredSubscription := range subs { - err = EnsureDesiredSubscription(r.Client, desiredSubscription) + err = EnsureDesiredSubscription(ctx, r.Client, desiredSubscription) if err != nil && !errors.IsAlreadyExists(err) { logger.Error(err, "failed to ensure subscription", "Subscription", desiredSubscription.Name) return err @@ -217,14 +217,14 @@ func (r *StorageSystemReconciler) ensureSubscriptions(instance *odfv1alpha1.Stor return nil } -func (r *StorageSystemReconciler) isVendorCsvReady(instance *odfv1alpha1.StorageSystem, logger logr.Logger) error { +func (r *StorageSystemReconciler) isVendorCsvReady(ctx context.Context, instance *odfv1alpha1.StorageSystem, logger logr.Logger) error { csvNames := GetVendorCsvNames(instance.Spec.Kind) var returnErr error for _, csvName := range csvNames { - csvObj, err := EnsureVendorCsv(r.Client, csvName) + csvObj, err := EnsureVendorCsv(ctx, r.Client, csvName) if err != nil { logger.Error(err, "failed to validate CSV", "ClusterServiceVersion", csvName) multierr.AppendInto(&returnErr, err) diff --git a/controllers/subscription_controller.go b/controllers/subscription_controller.go index 694872aff..2afd65686 100644 --- a/controllers/subscription_controller.go +++ b/controllers/subscription_controller.go @@ -65,7 +65,7 @@ func (r *SubscriptionReconciler) Reconcile(ctx context.Context, req ctrl.Request logger := log.FromContext(ctx) instance := &operatorv1alpha1.Subscription{} - err := r.Client.Get(context.TODO(), req.NamespacedName, instance) + err := r.Client.Get(ctx, req.NamespacedName, instance) if err != nil { if errors.IsNotFound(err) { logger.Info("Subscription instance not found.") @@ -75,12 +75,12 @@ func (r *SubscriptionReconciler) Reconcile(ctx context.Context, req ctrl.Request return ctrl.Result{}, err } - err = r.setOperatorCondition(logger, req.NamespacedName.Namespace) + err = r.setOperatorCondition(ctx, logger, req.NamespacedName.Namespace) if err != nil { return ctrl.Result{}, err } - err = r.ensureSubscriptions(logger, req.NamespacedName.Namespace) + err = r.ensureSubscriptions(ctx, logger, req.NamespacedName.Namespace) if err != nil { return ctrl.Result{}, err } @@ -88,7 +88,7 @@ func (r *SubscriptionReconciler) Reconcile(ctx context.Context, req ctrl.Request return ctrl.Result{}, nil } -func (r *SubscriptionReconciler) ensureSubscriptions(logger logr.Logger, namespace string) error { +func (r *SubscriptionReconciler) ensureSubscriptions(ctx context.Context, logger logr.Logger, namespace string) error { var err error @@ -96,7 +96,7 @@ func (r *SubscriptionReconciler) ensureSubscriptions(logger logr.Logger, namespa subsList[StorageClusterKind] = GetSubscriptions(StorageClusterKind) ssList := &odfv1alpha1.StorageSystemList{} - err = r.Client.List(context.TODO(), ssList, &client.ListOptions{Namespace: namespace}) + err = r.Client.List(ctx, ssList, &client.ListOptions{Namespace: namespace}) if err != nil { return err } @@ -107,7 +107,7 @@ func (r *SubscriptionReconciler) ensureSubscriptions(logger logr.Logger, namespa for _, subs := range subsList { for _, sub := range subs { - errSub := EnsureDesiredSubscription(r.Client, sub) + errSub := EnsureDesiredSubscription(ctx, r.Client, sub) if errSub != nil { logger.Error(errSub, "failed to ensure Subscription", "Subscription", sub.Name) err = fmt.Errorf("failed to ensure Subscriptions") @@ -122,7 +122,7 @@ func (r *SubscriptionReconciler) ensureSubscriptions(logger logr.Logger, namespa for kind := range subsList { for _, csvName := range GetVendorCsvNames(kind) { - _, csvErr := EnsureVendorCsv(r.Client, csvName) + _, csvErr := EnsureVendorCsv(ctx, r.Client, csvName) if csvErr != nil { multierr.AppendInto(&err, csvErr) } @@ -132,9 +132,9 @@ func (r *SubscriptionReconciler) ensureSubscriptions(logger logr.Logger, namespa return err } -func (r *SubscriptionReconciler) setOperatorCondition(logger logr.Logger, namespace string) error { +func (r *SubscriptionReconciler) setOperatorCondition(ctx context.Context, logger logr.Logger, namespace string) error { ocdList := &operatorv2.OperatorConditionList{} - err := r.Client.List(context.TODO(), ocdList, client.InNamespace(namespace)) + err := r.Client.List(ctx, ocdList, client.InNamespace(namespace)) if err != nil { logger.Error(err, "failed to list OperatorConditions") return err @@ -159,7 +159,7 @@ func (r *SubscriptionReconciler) setOperatorCondition(logger logr.Logger, namesp // operator is not upgradeable msg := fmt.Sprintf("%s:%s", ocd.GetName(), cond.Message) logger.Info("setting operator upgradeable status", "status", cond.Status) - return r.OperatorCondition.Set(context.TODO(), cond.Status, + return r.OperatorCondition.Set(ctx, cond.Status, conditions.WithReason(cond.Reason), conditions.WithMessage(msg)) } } @@ -167,7 +167,7 @@ func (r *SubscriptionReconciler) setOperatorCondition(logger logr.Logger, namesp // all operators are upgradeable status := metav1.ConditionTrue logger.Info("setting operator upgradeable status", "status", status) - return r.OperatorCondition.Set(context.TODO(), status, + return r.OperatorCondition.Set(ctx, status, conditions.WithReason("Dependents"), conditions.WithMessage("No dependent reports not upgradeable status")) } @@ -266,7 +266,7 @@ func (r *SubscriptionReconciler) SetupWithManager(mgr ctrl.Manager) error { return []reconcile.Request{} } logger := log.FromContext(ctx) - sub, err := GetOdfSubscription(r.Client) + sub, err := GetOdfSubscription(ctx, r.Client) if err != nil { logger.Error(err, "failed to get ODF Subscription") return []reconcile.Request{} diff --git a/controllers/subscriptions.go b/controllers/subscriptions.go index 1d998f666..97dee4308 100644 --- a/controllers/subscriptions.go +++ b/controllers/subscriptions.go @@ -40,9 +40,9 @@ import ( // // NOTE(jarrpa): We can't use client.MatchingFields to limit the list results // because fake.Client does not support them. -func CheckExistingSubscriptions(cli client.Client, desiredSubscription *operatorv1alpha1.Subscription) (*operatorv1alpha1.Subscription, error) { +func CheckExistingSubscriptions(ctx context.Context, cli client.Client, desiredSubscription *operatorv1alpha1.Subscription) (*operatorv1alpha1.Subscription, error) { - odfSub, err := GetOdfSubscription(cli) + odfSub, err := GetOdfSubscription(ctx, cli) if err != nil { return nil, err } @@ -52,7 +52,7 @@ func CheckExistingSubscriptions(cli client.Client, desiredSubscription *operator } subsList := &operatorv1alpha1.SubscriptionList{} - err = cli.List(context.TODO(), subsList, &client.ListOptions{Namespace: desiredSubscription.Namespace}) + err = cli.List(ctx, subsList, &client.ListOptions{Namespace: desiredSubscription.Namespace}) if err != nil { return nil, err } @@ -160,11 +160,11 @@ func getMergedEnvVars(envList1, envList2 []corev1.EnvVar) []corev1.EnvVar { return updatedEnvVars } -func EnsureDesiredSubscription(cli client.Client, desiredSubscription *operatorv1alpha1.Subscription) error { +func EnsureDesiredSubscription(ctx context.Context, cli client.Client, desiredSubscription *operatorv1alpha1.Subscription) error { var err error - desiredSubscription, err = CheckExistingSubscriptions(cli, desiredSubscription) + desiredSubscription, err = CheckExistingSubscriptions(ctx, cli, desiredSubscription) if err != nil { return err } @@ -172,9 +172,9 @@ func EnsureDesiredSubscription(cli client.Client, desiredSubscription *operatorv // create/update subscription sub := &operatorv1alpha1.Subscription{} sub.ObjectMeta = desiredSubscription.ObjectMeta - _, err = controllerutil.CreateOrUpdate(context.TODO(), cli, sub, func() error { + _, err = controllerutil.CreateOrUpdate(ctx, cli, sub, func() error { sub.Spec = desiredSubscription.Spec - return SetOdfSubControllerReference(cli, sub) + return SetOdfSubControllerReference(ctx, cli, sub) }) if err != nil && !errors.IsAlreadyExists(err) { return err @@ -183,9 +183,9 @@ func EnsureDesiredSubscription(cli client.Client, desiredSubscription *operatorv return nil } -func SetOdfSubControllerReference(cli client.Client, obj client.Object) error { +func SetOdfSubControllerReference(ctx context.Context, cli client.Client, obj client.Object) error { - odfSub, err := GetOdfSubscription(cli) + odfSub, err := GetOdfSubscription(ctx, cli) if err != nil { return err } @@ -199,10 +199,10 @@ func SetOdfSubControllerReference(cli client.Client, obj client.Object) error { } // GetOdfSubscription returns the subscription for odf-operator. -func GetOdfSubscription(cli client.Client) (*operatorv1alpha1.Subscription, error) { +func GetOdfSubscription(ctx context.Context, cli client.Client) (*operatorv1alpha1.Subscription, error) { subsList := &operatorv1alpha1.SubscriptionList{} - err := cli.List(context.TODO(), subsList, &client.ListOptions{Namespace: OperatorNamespace}) + err := cli.List(ctx, subsList, &client.ListOptions{Namespace: OperatorNamespace}) if err != nil { return nil, err } @@ -230,25 +230,25 @@ func GetVendorCsvNames(kind odfv1alpha1.StorageKind) []string { return csvNames } -func EnsureVendorCsv(cli client.Client, csvName string) (*operatorv1alpha1.ClusterServiceVersion, error) { +func EnsureVendorCsv(ctx context.Context, cli client.Client, csvName string) (*operatorv1alpha1.ClusterServiceVersion, error) { var err error csvObj := &operatorv1alpha1.ClusterServiceVersion{} - err = cli.Get(context.TODO(), types.NamespacedName{ + err = cli.Get(ctx, types.NamespacedName{ Name: csvName, Namespace: OperatorNamespace}, csvObj) if err != nil { if errors.IsNotFound(err) { - approvalErr := ApproveInstallPlanForCsv(cli, csvName) + approvalErr := ApproveInstallPlanForCsv(ctx, cli, csvName) if approvalErr != nil { return nil, approvalErr } } return nil, err } - _, err = controllerutil.CreateOrUpdate(context.TODO(), cli, csvObj, func() error { + _, err = controllerutil.CreateOrUpdate(ctx, cli, csvObj, func() error { csvObj.OwnerReferences = []metav1.OwnerReference{} - return SetOdfSubControllerReference(cli, csvObj) + return SetOdfSubControllerReference(ctx, cli, csvObj) }) if err != nil && !errors.IsAlreadyExists(err) { return nil, err @@ -267,13 +267,13 @@ func EnsureVendorCsv(cli client.Client, csvName string) (*operatorv1alpha1.Clust // ApproveInstallPlanForCsv approve the manual approval installPlan for the given CSV // and returns an error if none found -func ApproveInstallPlanForCsv(cli client.Client, csvName string) error { +func ApproveInstallPlanForCsv(ctx context.Context, cli client.Client, csvName string) error { var finalError error var foundInstallPlan bool installPlans := &operatorv1alpha1.InstallPlanList{} - err := cli.List(context.TODO(), installPlans, &client.ListOptions{Namespace: OperatorNamespace}) + err := cli.List(ctx, installPlans, &client.ListOptions{Namespace: OperatorNamespace}) if err != nil { return err @@ -286,7 +286,7 @@ func ApproveInstallPlanForCsv(cli client.Client, csvName string) error { !installPlan.Spec.Approved { installPlans.Items[i].Spec.Approved = true - err = cli.Update(context.TODO(), &installPlans.Items[i]) + err = cli.Update(ctx, &installPlans.Items[i]) if err != nil { multierr.AppendInto(&finalError, fmt.Errorf( "Failed to approve installplan %s", installPlan.Name)) diff --git a/controllers/subscriptions_test.go b/controllers/subscriptions_test.go index 9a0975323..1a2256177 100644 --- a/controllers/subscriptions_test.go +++ b/controllers/subscriptions_test.go @@ -51,7 +51,7 @@ func TestEnsureSubscription(t *testing.T) { for _, kind := range KnownKinds { var err error - + ctx := context.TODO() fakeStorageSystem := GetFakeStorageSystem(kind) fakeReconciler := GetFakeStorageSystemReconciler(t, fakeStorageSystem) subs := GetSubscriptions(kind) @@ -60,7 +60,7 @@ func TestEnsureSubscription(t *testing.T) { for _, subscription := range subs { sub := subscription.DeepCopy() sub.Spec.Channel = "fake-channel" - err = fakeReconciler.Client.Create(context.TODO(), sub) + err = fakeReconciler.Client.Create(ctx, sub) assert.NoError(t, err) } } @@ -85,10 +85,10 @@ func TestEnsureSubscription(t *testing.T) { }, }, } - err = fakeReconciler.Client.Create(context.TODO(), odfSub) + err = fakeReconciler.Client.Create(ctx, odfSub) assert.NoError(t, err) - err = fakeReconciler.ensureSubscriptions(fakeStorageSystem, fakeReconciler.Log) + err = fakeReconciler.ensureSubscriptions(ctx, fakeStorageSystem, fakeReconciler.Log) assert.NoError(t, err) for _, expectedSubscription := range subs { @@ -101,7 +101,7 @@ func TestEnsureSubscription(t *testing.T) { } actualSubscription := &operatorv1alpha1.Subscription{} - err = fakeReconciler.Client.Get(context.TODO(), types.NamespacedName{Name: expectedSubscription.Name, Namespace: expectedSubscription.Namespace}, actualSubscription) + err = fakeReconciler.Client.Get(ctx, types.NamespacedName{Name: expectedSubscription.Name, Namespace: expectedSubscription.Namespace}, actualSubscription) assert.NoError(t, err) assert.Equal(t, expectedSubscription.Spec, actualSubscription.Spec) diff --git a/controllers/vendors.go b/controllers/vendors.go index 65cc23358..4dd028927 100644 --- a/controllers/vendors.go +++ b/controllers/vendors.go @@ -57,7 +57,7 @@ func VendorFlashSystemCluster() odfv1alpha1.StorageKind { return FlashSystemKind } -func (r *StorageSystemReconciler) isVendorSystemPresent(instance *odfv1alpha1.StorageSystem, logger logr.Logger) error { +func (r *StorageSystemReconciler) isVendorSystemPresent(ctx context.Context, instance *odfv1alpha1.StorageSystem, logger logr.Logger) error { var vendorSystem client.Object @@ -69,11 +69,11 @@ func (r *StorageSystemReconciler) isVendorSystemPresent(instance *odfv1alpha1.St vendorSystem = &ibmv1alpha1.FlashSystemCluster{} } - err := r.Client.Get(context.TODO(), types.NamespacedName{Name: instance.Spec.Name, Namespace: instance.Spec.Namespace}, vendorSystem) + err := r.Client.Get(ctx, types.NamespacedName{Name: instance.Spec.Name, Namespace: instance.Spec.Namespace}, vendorSystem) if err == nil { logger.Info("Vendor system found", "Name", instance.Spec.Name) SetVendorSystemPresentCondition(&instance.Status.Conditions, corev1.ConditionTrue, "Found", "") - _, err = controllerutil.CreateOrUpdate(context.TODO(), r.Client, vendorSystem, func() error { + _, err = controllerutil.CreateOrUpdate(ctx, r.Client, vendorSystem, func() error { return controllerutil.SetOwnerReference(instance, vendorSystem, r.Scheme) }) if err != nil && !errors.IsAlreadyExists(err) { diff --git a/controllers/vendors_test.go b/controllers/vendors_test.go index 39c9d69b0..f87cde2c7 100644 --- a/controllers/vendors_test.go +++ b/controllers/vendors_test.go @@ -51,7 +51,7 @@ func TestIsVendorSystemPresent(t *testing.T) { for i, tc := range testCases { t.Logf("Case %d: %s\n", i+1, tc.label) - + ctx := context.TODO() fakeStorageSystem := GetFakeStorageSystem(StorageClusterKind) fakeReconciler := GetFakeStorageSystemReconciler(t, fakeStorageSystem) @@ -62,11 +62,11 @@ func TestIsVendorSystemPresent(t *testing.T) { Namespace: fakeStorageSystem.Spec.Namespace, }, } - err := fakeReconciler.Client.Create(context.TODO(), storageCluster) + err := fakeReconciler.Client.Create(ctx, storageCluster) assert.NoError(t, err) } - err := fakeReconciler.isVendorSystemPresent(fakeStorageSystem, fakeReconciler.Log) + err := fakeReconciler.isVendorSystemPresent(ctx, fakeStorageSystem, fakeReconciler.Log) assert.Equal(t, tc.hasBackEndStorage, conditionsv1.IsStatusConditionTrue( diff --git a/pkg/util/openshift.go b/pkg/util/openshift.go index 2fb74bd83..5af14a6da 100644 --- a/pkg/util/openshift.go +++ b/pkg/util/openshift.go @@ -25,10 +25,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -func DetermineOpenShiftVersion(client client.Client) (string, error) { +func DetermineOpenShiftVersion(ctx context.Context, client client.Client) (string, error) { // Determine ocp version clusterVersionList := configv1.ClusterVersionList{} - if err := client.List(context.TODO(), &clusterVersionList); err != nil { + if err := client.List(ctx, &clusterVersionList); err != nil { return "", err } clusterVersion := ""