Skip to content

Commit

Permalink
Merge pull request #3616 from klapkov/fix_instance_purging
Browse files Browse the repository at this point in the history
  • Loading branch information
danail-branekov authored Nov 26, 2024
2 parents a562177 + 35a1f9b commit c7bfdf1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
33 changes: 29 additions & 4 deletions api/repositories/service_instance_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,17 +473,20 @@ func (r *ServiceInstanceRepo) DeleteServiceInstance(ctx context.Context, authInf
},
}

if err := userClient.Get(ctx, client.ObjectKeyFromObject(serviceInstance), serviceInstance); err != nil {
if err = userClient.Get(ctx, client.ObjectKeyFromObject(serviceInstance), serviceInstance); err != nil {
return ServiceInstanceRecord{}, fmt.Errorf("failed to get service instance: %w", apierrors.FromK8sError(err, ServiceInstanceResourceType))
}

if message.Purge {
err := k8s.PatchResource(ctx, userClient, serviceInstance, func() {
if err = k8s.PatchResource(ctx, userClient, serviceInstance, func() {
controllerutil.RemoveFinalizer(serviceInstance, korifiv1alpha1.CFManagedServiceInstanceFinalizerName)
})
if err != nil {
}); err != nil {
return ServiceInstanceRecord{}, fmt.Errorf("failed to remove finalizer for service instance: %s, %w", message.GUID, apierrors.FromK8sError(err, ServiceInstanceResourceType))
}

if err = r.removeBindingsFinalizer(ctx, userClient, namespace, message.GUID); err != nil {
return ServiceInstanceRecord{}, fmt.Errorf("failed delete related service bindings for instance: %s, %w", message.GUID, apierrors.FromK8sError(err, ServiceBindingResourceType))
}
}

if err := userClient.Delete(ctx, serviceInstance); err != nil {
Expand Down Expand Up @@ -518,6 +521,28 @@ func (r *ServiceInstanceRepo) GetDeletedAt(ctx context.Context, authInfo authori
return serviceInstance.DeletedAt, nil
}

func (r *ServiceInstanceRepo) removeBindingsFinalizer(ctx context.Context, userClient client.WithWatch, namespace, instanceGUID string) error {
serviceBindings := new(korifiv1alpha1.CFServiceBindingList)
if err := userClient.List(ctx, serviceBindings, client.InNamespace(namespace)); err != nil {
return fmt.Errorf("failed to get service bindings: %w", apierrors.FromK8sError(err, ServiceBindingResourceType))
}

filtered := itx.FromSlice(serviceBindings.Items).Filter(func(serviceBinding korifiv1alpha1.CFServiceBinding) bool {
return instanceGUID == serviceBinding.Spec.Service.Name
}).Collect()

for _, binding := range filtered {
err := k8s.PatchResource(ctx, userClient, &binding, func() {
controllerutil.RemoveFinalizer(&binding, korifiv1alpha1.CFServiceBindingFinalizerName)
})
if err != nil {
return err
}
}

return nil
}

func cfServiceInstanceToRecord(cfServiceInstance korifiv1alpha1.CFServiceInstance) ServiceInstanceRecord {
return ServiceInstanceRecord{
Name: cfServiceInstance.Spec.DisplayName,
Expand Down
35 changes: 29 additions & 6 deletions api/repositories/service_instance_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,7 @@ var _ = Describe("ServiceInstanceRepository", func() {
Describe("PurgeServiceInstance", func() {
var (
serviceInstance *korifiv1alpha1.CFServiceInstance
serviceBinding *korifiv1alpha1.CFServiceBinding
deleteMessage repositories.DeleteServiceInstanceMessage
deleteErr error
)
Expand All @@ -1038,6 +1039,27 @@ var _ = Describe("ServiceInstanceRepository", func() {
serviceInstance.Finalizers = append(serviceInstance.Finalizers, korifiv1alpha1.CFManagedServiceInstanceFinalizerName)
Expect(k8sClient.Update(ctx, serviceInstance)).To(Succeed())

serviceBinding = &korifiv1alpha1.CFServiceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: uuid.NewString(),
Namespace: space.Name,
Finalizers: []string{
korifiv1alpha1.CFServiceBindingFinalizerName,
},
},
Spec: korifiv1alpha1.CFServiceBindingSpec{
Service: corev1.ObjectReference{
Kind: "CFServiceInstance",
APIVersion: korifiv1alpha1.SchemeGroupVersion.Identifier(),
Name: serviceInstance.Name,
},
AppRef: corev1.LocalObjectReference{
Name: "some-app-guid",
},
},
}
Expect(k8sClient.Create(ctx, serviceBinding)).To(Succeed())

deleteMessage = repositories.DeleteServiceInstanceMessage{
GUID: serviceInstance.Name,
Purge: true,
Expand All @@ -1052,13 +1074,14 @@ var _ = Describe("ServiceInstanceRepository", func() {
It("purges the service instance", func() {
Expect(deleteErr).ToNot(HaveOccurred())

namespacedName := types.NamespacedName{
Name: serviceInstance.Name,
Namespace: space.Name,
}

err := k8sClient.Get(context.Background(), namespacedName, &korifiv1alpha1.CFServiceInstance{})
err := k8sClient.Get(context.Background(), types.NamespacedName{Name: serviceInstance.Name, Namespace: space.Name}, &korifiv1alpha1.CFServiceInstance{})
Expect(k8serrors.IsNotFound(err)).To(BeTrue(), fmt.Sprintf("error: %+v", err))

binding := new(korifiv1alpha1.CFServiceBinding)
err = k8sClient.Get(context.Background(), types.NamespacedName{Name: serviceBinding.Name, Namespace: space.Name}, binding)

Expect(err).ToNot(HaveOccurred())
Expect(binding.Finalizers).To(BeEmpty())
})
})
})
Expand Down

0 comments on commit c7bfdf1

Please sign in to comment.