Skip to content

Commit

Permalink
feat: delete workerclusterResources Work resource when deleting the p…
Browse files Browse the repository at this point in the history
…romise

Co-authored-by: Abby Bangser <abby@syntasso.io>
  • Loading branch information
aclevername and abangser committed Nov 15, 2022
1 parent 2486344 commit 97eb10a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ var _ = Context("Promise Reconciler", func() {
"kratix.io/resource-request-cleanup",
"kratix.io/dynamic-controller-dependant-resources-cleanup",
"kratix.io/crd-cleanup",
"kratix.io/worker-cluster-resources-cleanup",
),
"Promise should have finalizers set")

Expand Down Expand Up @@ -238,6 +239,12 @@ var _ = Context("Promise Reconciler", func() {
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue(), "Expected CRD to not be found")

By("also deleting the Work")
Eventually(func() bool {
err := k8sClient.Get(context.Background(), workNamespacedName, &v1alpha1.Work{})
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue(), "Expected Work to not be found")

By("finally deleting the Promise itself")
Eventually(func() bool {
err := k8sClient.Get(context.Background(), expectedPromise, &v1alpha1.Promise{})
Expand Down
36 changes: 35 additions & 1 deletion controllers/promise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ const (
resourceRequestCleanupFinalizer = finalizerPrefix + "resource-request-cleanup"
dynamicControllerDependantResourcesCleaupFinalizer = finalizerPrefix + "dynamic-controller-dependant-resources-cleanup"
crdCleanupFinalizer = finalizerPrefix + "crd-cleanup"
workerClusterResourcesCleanupFinalizer = finalizerPrefix + "worker-cluster-resources-cleanup"
)

var promiseFinalizers = []string{
clusterSelectorsConfigMapCleanupFinalizer,
resourceRequestCleanupFinalizer,
dynamicControllerDependantResourcesCleaupFinalizer,
crdCleanupFinalizer,
workerClusterResourcesCleanupFinalizer,
}

//+kubebuilder:rbac:groups=platform.kratix.io,resources=promises,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -152,6 +154,7 @@ func (r *PromiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
workToCreate.Spec.Replicas = v1alpha1.WorkerResourceReplicas
workToCreate.Name = promise.GetIdentifier()
workToCreate.Namespace = "default"
workToCreate.Labels = resourceLabels
workToCreate.Spec.ClusterSelector = promise.Spec.ClusterSelector
for _, u := range promise.Spec.WorkerClusterResources {
workToCreate.Spec.Workload.Manifests = append(workToCreate.Spec.Workload.Manifests, v1alpha1.Manifest{Unstructured: u.Unstructured})
Expand Down Expand Up @@ -371,7 +374,14 @@ func (r *PromiseReconciler) deletePromise(ctx context.Context, promise *v1alpha1
return ctrl.Result{RequeueAfter: fastRequeue}, err
}

//delete work for workerClusterResources
if controllerutil.ContainsFinalizer(promise, workerClusterResourcesCleanupFinalizer) {
logger.Info("deleting Work associated with finalizer", "finalizer", workerClusterResourcesCleanupFinalizer)
err := r.deleteWork(ctx, promise, resourceLabels, logger)
if err != nil {
return ctrl.Result{RequeueAfter: defaultRequeue}, err
}
return ctrl.Result{RequeueAfter: fastRequeue}, err
}

return ctrl.Result{RequeueAfter: fastRequeue}, nil
}
Expand Down Expand Up @@ -519,6 +529,30 @@ func (r *PromiseReconciler) deleteCRDs(ctx context.Context, promise *v1alpha1.Pr
return nil
}

func (r *PromiseReconciler) deleteWork(ctx context.Context, promise *v1alpha1.Promise, resourceLabels map[string]string, logger logr.Logger) error {
workGVK := schema.GroupVersionKind{
Group: v1alpha1.GroupVersion.Group,
Version: v1alpha1.GroupVersion.Version,
Kind: "Work",
}

resourcesRemaining, err := r.deleteAllResourcesWithKindMatchingLabel(ctx, workGVK, resourceLabels, logger)
if err != nil {
return err
}

if resourcesRemaining {
return nil
}

controllerutil.RemoveFinalizer(promise, workerClusterResourcesCleanupFinalizer)
if err := r.Client.Update(ctx, promise); err != nil {
return err
}

return nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *PromiseReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Expand Down
29 changes: 20 additions & 9 deletions test/integration/kratix_platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,25 @@ var _ = Describe("kratix Platform Integration Test", func() {
})

Describe("Deleting the Promise", func() {
workName := types.NamespacedName{
wcrWorkName := types.NamespacedName{
Name: redisPromiseID,
Namespace: "default",
}
rrWorkName := types.NamespacedName{
Name: redisDefaultRRName,
Namespace: "default",
}

BeforeEach(func() {
// ensure resource request files exist in repo
Eventually(func(g Gomega) {
fileExists, _ := workerHasResource(workName, "opstree-redis", "Redis", DevWorkerCluster2)
g.Expect(fileExists).To(BeTrue(), "minio file should exist")
}, timeout, interval).Should(Succeed(), "minio files do not exist")
wcrFileExists, _ := workerHasResource(wcrWorkName, "a-non-crd-resource", "Namespace", DevWorkerCluster2)
g.Expect(wcrFileExists).To(BeTrue(), "workerClusterResource files should exist in minio")
}, timeout, interval).Should(Succeed(), "workerClusterResource files do not exist in minio")

Eventually(func(g Gomega) {
rrFileExists, _ := workerHasResource(rrWorkName, "opstree-redis", "Redis", DevWorkerCluster2)
g.Expect(rrFileExists).To(BeTrue(), "resourceRequest files should exist in minio")
}, timeout, interval).Should(Succeed(), "resourceRequest files do not exist in minio")
})

It("deletes the associated resources", func() {
Expand All @@ -279,9 +287,13 @@ var _ = Describe("kratix Platform Integration Test", func() {

By("deleting the files in the repo")
Eventually(func(g Gomega) {
fileExists, _ := workerHasResource(workName, "opstree-redis", "Redis", DevWorkerCluster2)
g.Expect(fileExists).To(BeFalse(), "minio file should have been deleted")
}, timeout, interval).Should(Succeed(), "minio files were not deleted")
wcrFileExists, _ := workerHasResource(wcrWorkName, "a-non-crd-resource", "Namespace", DevWorkerCluster2)
g.Expect(wcrFileExists).To(BeFalse(), "workerClusterResource files should not exist in minio")
}, timeout, interval).Should(Succeed(), "workerClusterResource files still exist in minio")
Eventually(func(g Gomega) {
rrFileExists, _ := workerHasResource(rrWorkName, "opstree-redis", "Redis", DevWorkerCluster2)
g.Expect(rrFileExists).To(BeFalse(), "resourceRequest files should not exist in minio")
}, timeout, interval).Should(Succeed(), "resourceRequest files still exist in minio")

By("deleting the request pipeline pods")
Eventually(func() int {
Expand All @@ -308,7 +320,6 @@ var _ = Describe("kratix Platform Integration Test", func() {
}
crd := &apiextensionsv1.CustomResourceDefinition{}
err := k8sClient.Get(context.Background(), expectedCRDName, crd)
fmt.Printf("\nerr: %v\n crd: %v\n", err, crd)
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue(), "expected CRD %v to have been deleted:", redis_gvk)

Expand Down

0 comments on commit 97eb10a

Please sign in to comment.