From 8ddc587a4178ceb903fff6f88032123ffd21ad12 Mon Sep 17 00:00:00 2001 From: kerenlahav <45451976+kerenlahav@users.noreply.github.com> Date: Mon, 17 Jun 2024 10:42:22 +0300 Subject: [PATCH] bug fix - cross namespace resources (#446) --- controllers/servicebinding_controller.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/controllers/servicebinding_controller.go b/controllers/servicebinding_controller.go index 64f7257c..a4f85d1f 100644 --- a/controllers/servicebinding_controller.go +++ b/controllers/servicebinding_controller.go @@ -205,7 +205,7 @@ func (r *ServiceBindingReconciler) Reconcile(ctx context.Context, req ctrl.Reque return ctrl.Result{}, utils.UpdateStatus(ctx, r.Client, serviceBinding) } - smClient, err := r.GetSMClient(ctx, r.SecretResolver, serviceBinding.Namespace, serviceInstance.Spec.BTPAccessCredentialsSecret) + smClient, err := r.GetSMClient(ctx, r.SecretResolver, getBTPAccessSecretNamespace(serviceBinding), serviceInstance.Spec.BTPAccessCredentialsSecret) if err != nil { return utils.MarkAsTransientError(ctx, r.Client, common.Unknown, err, serviceBinding) } @@ -228,7 +228,7 @@ func (r *ServiceBindingReconciler) Reconcile(ctx context.Context, req ctrl.Reque func (r *ServiceBindingReconciler) updateSecret(ctx context.Context, serviceBinding *servicesv1.ServiceBinding, serviceInstance *servicesv1.ServiceInstance, log logr.Logger) error { log.Info("Updating secret according to the new template") - smClient, err := r.GetSMClient(ctx, r.SecretResolver, serviceBinding.Namespace, serviceInstance.Spec.BTPAccessCredentialsSecret) + smClient, err := r.GetSMClient(ctx, r.SecretResolver, getBTPAccessSecretNamespace(serviceBinding), serviceInstance.Spec.BTPAccessCredentialsSecret) if err != nil { return err } @@ -323,7 +323,7 @@ func (r *ServiceBindingReconciler) createBinding(ctx context.Context, smClient s func (r *ServiceBindingReconciler) delete(ctx context.Context, serviceBinding *servicesv1.ServiceBinding, btpAccessCredentialsSecret string) (ctrl.Result, error) { log := utils.GetLogger(ctx) if controllerutil.ContainsFinalizer(serviceBinding, common.FinalizerName) { - smClient, err := r.GetSMClient(ctx, r.SecretResolver, serviceBinding.Namespace, btpAccessCredentialsSecret) + smClient, err := r.GetSMClient(ctx, r.SecretResolver, getBTPAccessSecretNamespace(serviceBinding), btpAccessCredentialsSecret) if err != nil { return utils.MarkAsTransientError(ctx, r.Client, common.Unknown, err, serviceBinding) } @@ -386,7 +386,7 @@ func (r *ServiceBindingReconciler) poll(ctx context.Context, serviceBinding *ser log := utils.GetLogger(ctx) log.Info(fmt.Sprintf("resource is in progress, found operation url %s", serviceBinding.Status.OperationURL)) - smClient, err := r.GetSMClient(ctx, r.SecretResolver, serviceBinding.Namespace, btpAccessCredentialsSecret) + smClient, err := r.GetSMClient(ctx, r.SecretResolver, getBTPAccessSecretNamespace(serviceBinding), btpAccessCredentialsSecret) if err != nil { return utils.MarkAsTransientError(ctx, r.Client, common.Unknown, err, serviceBinding) } @@ -942,7 +942,7 @@ func (r *ServiceBindingReconciler) rotateCredentials(ctx context.Context, bindin } if len(bindings.Items) == 0 { - smClient, err := r.GetSMClient(ctx, r.SecretResolver, binding.Namespace, btpAccessCredentialsSecret) + smClient, err := r.GetSMClient(ctx, r.SecretResolver, getBTPAccessSecretNamespace(binding), btpAccessCredentialsSecret) if err != nil { return err } @@ -1171,3 +1171,11 @@ func singleKeyMap(credentialsMap map[string][]byte, key string) (map[string][]by key: credBytes, }, nil } + +func getBTPAccessSecretNamespace(serviceBinding *servicesv1.ServiceBinding) string { + btpAccessSecretNamespace := serviceBinding.Namespace + if len(serviceBinding.Spec.ServiceInstanceNamespace) > 0 { + btpAccessSecretNamespace = serviceBinding.Spec.ServiceInstanceNamespace + } + return btpAccessSecretNamespace +}