From 1cad5af73a9c998d34f6e3531014bc6ae84271da Mon Sep 17 00:00:00 2001 From: Janis Bebritis Date: Tue, 12 Dec 2023 15:44:11 +0200 Subject: [PATCH 1/2] remove postinstall jobs based on release label instead of hardcoded name --- cmd/ciReleaseDelete.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/cmd/ciReleaseDelete.go b/cmd/ciReleaseDelete.go index f936984..75d63a5 100644 --- a/cmd/ciReleaseDelete.go +++ b/cmd/ciReleaseDelete.go @@ -7,7 +7,6 @@ import ( helmclient "github.com/mittwald/go-helm-client" "github.com/spf13/cobra" - errs "k8s.io/apimachinery/pkg/api/errors" // k8s errors and handling v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // gcp auth provider @@ -67,13 +66,23 @@ var ciReleaseDeleteCmd = &cobra.Command{ log.Fatalf("Error removing a release:%s", uninstallErr) } - propagationPolicy := v1.DeletePropagationBackground - deleteErr := clientset.BatchV1().Jobs(namespace).Delete(context.TODO(), releaseName+"-post-release", v1.DeleteOptions{PropagationPolicy: &propagationPolicy}) - if deleteErr != nil { - if errs.IsNotFound(deleteErr) { - //Resource doesnt exist, lets skip printing a message - } else { - log.Println("Cannot delete post-release job: %s", deleteErr) + //Delete pre-release jobs + selectorLabels := []string{ + "release", + "app.kubernetes.io/instance", + } + + for _, l := range selectorLabels { + selector := l + "=" + releaseName + list, err := clientset.BatchV1().Jobs(namespace).List(context.TODO(), v1.ListOptions{ + LabelSelector: selector, + }) + if err != nil { + log.Fatalf("Error getting the list of jobs: %s", err) + } + for _, v := range list.Items { + log.Printf("Deleting job: %s", v.Name) + clientset.BatchV1().Jobs(namespace).Delete(context.TODO(), v.Name, v1.DeleteOptions{}) } } From 01bb0cd9236a97d80f4748ac3de1190b077111b5 Mon Sep 17 00:00:00 2001 From: Janis Bebritis Date: Wed, 13 Dec 2023 12:20:16 +0200 Subject: [PATCH 2/2] propagationPolicy for job pod removal --- cmd/ciReleaseDelete.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/ciReleaseDelete.go b/cmd/ciReleaseDelete.go index 75d63a5..8e21a4b 100644 --- a/cmd/ciReleaseDelete.go +++ b/cmd/ciReleaseDelete.go @@ -82,7 +82,8 @@ var ciReleaseDeleteCmd = &cobra.Command{ } for _, v := range list.Items { log.Printf("Deleting job: %s", v.Name) - clientset.BatchV1().Jobs(namespace).Delete(context.TODO(), v.Name, v1.DeleteOptions{}) + propagationPolicy := v1.DeletePropagationBackground + clientset.BatchV1().Jobs(namespace).Delete(context.TODO(), v.Name, v1.DeleteOptions{PropagationPolicy: &propagationPolicy}) } }