Skip to content

Commit

Permalink
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
Browse files Browse the repository at this point in the history
…m' secret
  • Loading branch information
I065450 committed Dec 18, 2024
1 parent 7aaf27d commit 9fcbeed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions controllers/serviceinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (r *ServiceInstanceReconciler) deleteInstance(ctx context.Context, serviceI
if serviceInstance.Labels != nil {
for key, secretName := range serviceInstance.Labels {
if strings.HasPrefix(key, common.InstanceSecretRefLabel) {
if err := utils.RemoveWatchForSecret(ctx, r.Client, types.NamespacedName{Name: secretName, Namespace: serviceInstance.Namespace}, string(serviceInstance.UID)); err != nil {
if err := utils.RemoveWatchForSecret(ctx, r.Client, types.NamespacedName{Name: secretName, Namespace: serviceInstance.Namespace}, string(serviceInstance.UID), key); err != nil {
log.Error(err, fmt.Sprintf("failed to unwatch secret %s", secretName))
}
}
Expand Down Expand Up @@ -569,7 +569,7 @@ func (r *ServiceInstanceReconciler) buildSMRequestParameters(ctx context.Context
if strings.HasPrefix(key, common.InstanceSecretRefLabel) {
if _, ok := instanceLabels[key]; !ok {
instanceLabelsChanged = true
if err := utils.RemoveWatchForSecret(ctx, r.Client, types.NamespacedName{Name: value, Namespace: serviceInstance.Namespace}, string(serviceInstance.UID)); err != nil {
if err := utils.RemoveWatchForSecret(ctx, r.Client, types.NamespacedName{Name: value, Namespace: serviceInstance.Namespace}, string(serviceInstance.UID), key); err != nil {
log.Error(err, fmt.Sprintf("failed to unwatch secret %s", value))
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions controllers/serviceinstance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,11 @@ var _ = Describe("ServiceInstance controller", func() {

deleteAndWait(ctx, paramsSecret)
waitForResourceCondition(ctx, serviceInstance, common.ConditionSucceeded, metav1.ConditionFalse, common.UpdateInProgress, "secrets \"instance-params-secret\" not found")

paramsSecret = createParamsSecret(ctx, "instance-params-secret", testNamespace)
waitForResourceCondition(ctx, serviceInstance, common.ConditionSucceeded, metav1.ConditionTrue, common.Updated, "")
checkSecretAnnotationsAndLabels(ctx, k8sClient, paramsSecret, []*v1.ServiceInstance{serviceInstance})

})
})
When("secret updated and instance don't watch secret", func() {
Expand Down
14 changes: 8 additions & 6 deletions internal/utils/controller_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,19 @@ func AddWatchForSecret(ctx context.Context, k8sClient client.Client, secret *cor
return k8sClient.Update(ctx, secret)
}

func RemoveWatchForSecret(ctx context.Context, k8sClient client.Client, secretKey apimachinerytypes.NamespacedName, instanceUID string) error {
func RemoveWatchForSecret(ctx context.Context, k8sClient client.Client, secretKey apimachinerytypes.NamespacedName, instanceUID string, key string) error {
secret := &corev1.Secret{}
if err := k8sClient.Get(ctx, secretKey, secret); err != nil {
return client.IgnoreNotFound(err)
}
delete(secret.Annotations, common.WatchSecretAnnotation+instanceUID)
if !IsSecretWatched(secret.Annotations) {
controllerutil.RemoveFinalizer(secret, common.FinalizerName)
if key == common.WatchSecretAnnotation+instanceUID {
delete(secret.Annotations, common.WatchSecretAnnotation+instanceUID)
if !IsSecretWatched(secret.Annotations) {
controllerutil.RemoveFinalizer(secret, common.FinalizerName)
}
return k8sClient.Update(ctx, secret)
}

return k8sClient.Update(ctx, secret)
return nil
}

func IsSecretWatched(secretAnnotations map[string]string) bool {
Expand Down

0 comments on commit 9fcbeed

Please sign in to comment.