Skip to content

Commit

Permalink
Merge pull request #915 from rabi/context
Browse files Browse the repository at this point in the history
Pass context based on signal handlers to managers
  • Loading branch information
openshift-merge-bot[bot] authored Jul 11, 2024
2 parents ddc1f81 + a5c8b55 commit adfbfd5
Show file tree
Hide file tree
Showing 7 changed files with 33 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 @@ -474,9 +474,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 @@ -488,7 +489,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 @@ -500,7 +501,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 @@ -512,7 +513,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 @@ -524,7 +525,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 @@ -551,7 +552,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 @@ -574,9 +575,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 @@ -600,7 +602,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
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,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 @@ -199,7 +201,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 @@ -208,7 +210,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 @@ -226,7 +228,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 @@ -296,7 +298,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 @@ -330,7 +330,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 @@ -344,7 +344,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 adfbfd5

Please sign in to comment.