Skip to content

Commit

Permalink
Pass context based on signal handlers to managers
Browse files Browse the repository at this point in the history
And don't use context.Background() which is an empty context
when context is available.

Signed-off-by: rabi <[email protected]>
  • Loading branch information
rabi committed Jul 8, 2024
1 parent d633866 commit a5c8b55
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 27 deletions.
13 changes: 7 additions & 6 deletions controllers/client/openstackclient_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,10 @@ var allWatchFields = []string{
}

// SetupWithManager sets up the controller with the Manager.
func (r *OpenStackClientReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *OpenStackClientReconciler) SetupWithManager(
ctx context.Context, mgr ctrl.Manager) error {
// index caBundleSecretNameField
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &clientv1.OpenStackClient{}, caBundleSecretNameField, func(rawObj client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(ctx, &clientv1.OpenStackClient{}, caBundleSecretNameField, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*clientv1.OpenStackClient)
if cr.Spec.CaBundleSecretName == "" {
Expand All @@ -449,7 +450,7 @@ func (r *OpenStackClientReconciler) SetupWithManager(mgr ctrl.Manager) error {
return err
}
// index openStackConfigMap
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &clientv1.OpenStackClient{}, openStackConfigMapField, func(rawObj client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(ctx, &clientv1.OpenStackClient{}, openStackConfigMapField, func(rawObj client.Object) []string {
// Extract the configmap name from the spec, if one is provided
cr := rawObj.(*clientv1.OpenStackClient)
if cr.Spec.OpenStackConfigMap == nil {
Expand All @@ -463,7 +464,7 @@ func (r *OpenStackClientReconciler) SetupWithManager(mgr ctrl.Manager) error {
return err
}
// index openStackConfigSecret
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &clientv1.OpenStackClient{}, openStackConfigSecretField, func(rawObj client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(ctx, &clientv1.OpenStackClient{}, openStackConfigSecretField, func(rawObj client.Object) []string {
// Extract the configmap name from the spec, if one is provided
cr := rawObj.(*clientv1.OpenStackClient)
if cr.Spec.OpenStackConfigSecret == nil {
Expand All @@ -477,7 +478,7 @@ func (r *OpenStackClientReconciler) SetupWithManager(mgr ctrl.Manager) error {
return err
}

Log := r.GetLogger(context.Background())
Log := r.GetLogger(ctx)
metricStorageFn := func(ctx context.Context, o client.Object) []reconcile.Request {
result := []reconcile.Request{}

Expand All @@ -486,7 +487,7 @@ func (r *OpenStackClientReconciler) SetupWithManager(mgr ctrl.Manager) error {
listOpts := []client.ListOption{
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), openstackclients, listOpts...); err != nil {
if err := r.Client.List(ctx, openstackclients, listOpts...); err != nil {
Log.Error(err, "Unable to retrieve OpenstackClient CRs %v")
return nil
}
Expand Down
13 changes: 7 additions & 6 deletions controllers/core/openstackcontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,10 @@ var allWatchFields = []string{
}

// SetupWithManager sets up the controller with the Manager.
func (r *OpenStackControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *OpenStackControlPlaneReconciler) SetupWithManager(
ctx context.Context, mgr ctrl.Manager) error {
// index passwordSecretField
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &corev1beta1.OpenStackControlPlane{}, passwordSecretField, func(rawObj client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(ctx, &corev1beta1.OpenStackControlPlane{}, passwordSecretField, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*corev1beta1.OpenStackControlPlane)
if cr.Spec.Secret == "" {
Expand All @@ -487,7 +488,7 @@ func (r *OpenStackControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager) err
}

// index caBundleSecretNameField
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &corev1beta1.OpenStackControlPlane{}, tlsCABundleSecretNameField, func(rawObj client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(ctx, &corev1beta1.OpenStackControlPlane{}, tlsCABundleSecretNameField, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*corev1beta1.OpenStackControlPlane)
if cr.Spec.TLS.CaBundleSecretName == "" {
Expand All @@ -499,7 +500,7 @@ func (r *OpenStackControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager) err
}

// index tlsIngressCACustomIssuer
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &corev1beta1.OpenStackControlPlane{}, tlsIngressCACustomIssuer, func(rawObj client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(ctx, &corev1beta1.OpenStackControlPlane{}, tlsIngressCACustomIssuer, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*corev1beta1.OpenStackControlPlane)
if cr.Spec.TLS.Ingress.Ca.CustomIssuer == nil {
Expand All @@ -511,7 +512,7 @@ func (r *OpenStackControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager) err
}

// index tlsPodLevelInternalCACustomIssuer
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &corev1beta1.OpenStackControlPlane{}, tlsPodLevelInternalCACustomIssuer, func(rawObj client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(ctx, &corev1beta1.OpenStackControlPlane{}, tlsPodLevelInternalCACustomIssuer, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*corev1beta1.OpenStackControlPlane)
if cr.Spec.TLS.PodLevel.Internal.Ca.CustomIssuer == nil {
Expand All @@ -523,7 +524,7 @@ func (r *OpenStackControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager) err
}

// index tlsPodLevelOvnCACustomIssuer
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &corev1beta1.OpenStackControlPlane{}, tlsPodLevelOvnCACustomIssuer, func(rawObj client.Object) []string {
if err := mgr.GetFieldIndexer().IndexField(ctx, &corev1beta1.OpenStackControlPlane{}, tlsPodLevelOvnCACustomIssuer, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*corev1beta1.OpenStackControlPlane)
if cr.Spec.TLS.PodLevel.Ovn.Ca.CustomIssuer == nil {
Expand Down
16 changes: 9 additions & 7 deletions controllers/dataplane/openstackdataplanenodeset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ func (r *OpenStackDataPlaneNodeSetReconciler) Reconcile(ctx context.Context, req
}
}

isDeploymentReady, isDeploymentRunning, isDeploymentFailed, failedDeployment, err := checkDeployment(helper, instance)
isDeploymentReady, isDeploymentRunning, isDeploymentFailed, failedDeployment, err := checkDeployment(
ctx, helper, instance)
if !isDeploymentFailed && err != nil {
instance.Status.Conditions.MarkFalse(
condition.DeploymentReadyCondition,
Expand Down Expand Up @@ -462,7 +463,7 @@ func (r *OpenStackDataPlaneNodeSetReconciler) Reconcile(ctx context.Context, req
return ctrl.Result{}, err
}

func checkDeployment(helper *helper.Helper,
func checkDeployment(ctx context.Context, helper *helper.Helper,
instance *dataplanev1.OpenStackDataPlaneNodeSet,
) (bool, bool, bool, string, error) {
// Get all completed deployments
Expand All @@ -471,7 +472,7 @@ func checkDeployment(helper *helper.Helper,
opts := []client.ListOption{
client.InNamespace(instance.Namespace),
}
err := helper.GetClient().List(context.Background(), deployments, opts...)
err := helper.GetClient().List(ctx, deployments, opts...)
if err != nil {
helper.GetLogger().Error(err, "Unable to retrieve OpenStackDataPlaneDeployment CRs %v")
return false, false, false, failedDeployment, err
Expand Down Expand Up @@ -548,7 +549,7 @@ func checkDeployment(helper *helper.Helper,
Namespace: instance.Namespace,
Name: serviceName,
}
err := helper.GetClient().Get(context.Background(), name, service)
err := helper.GetClient().Get(ctx, name, service)
if err != nil {
helper.GetLogger().Error(err, "Unable to retrieve OpenStackDataPlaneService %v")
return false, false, false, failedDeployment, err
Expand All @@ -571,9 +572,10 @@ func checkDeployment(helper *helper.Helper,
}

// SetupWithManager sets up the controller with the Manager.
func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager(
ctx context.Context, mgr ctrl.Manager) error {
// index for ConfigMaps listed on ansibleVarsFrom
if err := mgr.GetFieldIndexer().IndexField(context.Background(),
if err := mgr.GetFieldIndexer().IndexField(ctx,
&dataplanev1.OpenStackDataPlaneNodeSet{}, "spec.ansibleVarsFrom.ansible.configMaps",
func(rawObj client.Object) []string {
nodeSet := rawObj.(*dataplanev1.OpenStackDataPlaneNodeSet)
Expand All @@ -597,7 +599,7 @@ func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager(mgr ctrl.Manager)
}

// index for Secrets listed on ansibleVarsFrom
if err := mgr.GetFieldIndexer().IndexField(context.Background(),
if err := mgr.GetFieldIndexer().IndexField(ctx,
&dataplanev1.OpenStackDataPlaneNodeSet{}, "spec.ansibleVarsFrom.ansible.secrets",
func(rawObj client.Object) []string {
nodeSet := rawObj.(*dataplanev1.OpenStackDataPlaneNodeSet)
Expand Down
11 changes: 7 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
placementv1 "github.com/openstack-k8s-operators/placement-operator/api/v1beta1"
swiftv1 "github.com/openstack-k8s-operators/swift-operator/api/v1beta1"
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"

// Note(lpiwowar): Please, do not remove! This import is necessary in order
// to make the test-operator part of the openstack-operator-index.
_ "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
Expand Down Expand Up @@ -180,6 +181,8 @@ func main() {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Setup the context that's going to be used in controllers and for the manager.
ctx := ctrl.SetupSignalHandler()

cfg, err := config.GetConfig()
if err != nil {
Expand All @@ -196,7 +199,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Kclient: kclient,
}).SetupWithManager(mgr); err != nil {
}).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpenStackControlPlane")
os.Exit(1)
}
Expand All @@ -205,7 +208,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Kclient: kclient,
}).SetupWithManager(mgr); err != nil {
}).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpenStackClient")
os.Exit(1)
}
Expand All @@ -223,7 +226,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Kclient: kclient,
}).SetupWithManager(mgr); err != nil {
}).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpenStackDataPlaneNodeSet")
os.Exit(1)
}
Expand Down Expand Up @@ -293,7 +296,7 @@ func main() {
}

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/dataplane/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func GetDeploymentHashesForService(
Namespace: namespace,
}
service := &dataplanev1.OpenStackDataPlaneService{}
err := helper.GetClient().Get(context.Background(), namespacedName, service)
err := helper.GetClient().Get(ctx, namespacedName, service)
if err != nil {
helper.GetLogger().Error(err, "Unable to retrieve OpenStackDataPlaneService %v")
return err
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/ctlplane/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Kclient: kclient,
}).SetupWithManager(k8sManager)
}).SetupWithManager(ctx, k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&core_ctrl.OpenStackVersionReconciler{
Expand All @@ -336,7 +336,7 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Kclient: kclient,
}).SetupWithManager(k8sManager)
}).SetupWithManager(ctx, k8sManager)
Expect(err).ToNot(HaveOccurred())

go func() {
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/dataplane/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Kclient: kclient,
}).SetupWithManager(k8sManager)
}).SetupWithManager(ctx, k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&dataplanecontrollers.OpenStackDataPlaneDeploymentReconciler{
Expand Down

0 comments on commit a5c8b55

Please sign in to comment.