Skip to content

Commit

Permalink
Add raw label selection to filter all jobs with certain label key
Browse files Browse the repository at this point in the history
Signed-off-by: Burak Sekili <buraksekili@gmail.com>
  • Loading branch information
buraksekili committed Oct 17, 2023
1 parent bc62e6e commit ff4189a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
1 change: 1 addition & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
ReleaseNameEnvVar = "RELEASE_NAME"

TykBootstrapLabel = "tyk.tyk.io/k8s-bootstrap"
TykBootstrapPreDeleteLabel = "tyk-k8s-bootstrap-pre-delete"
TykBootstrapDashboardDeployLabel = "tyk-dashboard"
TykBootstrapDashboardSvcLabel = "tyk-dashboard"
TykBootstrapReleaseLabel = "release"
Expand Down
34 changes: 19 additions & 15 deletions predelete/predelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,36 +103,40 @@ func PreDeleteEnterprisePortalSecret(clientset *kubernetes.Clientset) error {

// PreDeleteBootstrappingJobs deletes all jobs within the release namespace, that has specific label.
func PreDeleteBootstrappingJobs(clientset *kubernetes.Clientset) error {
// Usually, the raw strings in label selectors are not recommended.
jobs, err := clientset.
BatchV1().
Jobs(data.AppConfig.TykPodNamespace).
List(context.TODO(), metav1.ListOptions{})
List(
context.TODO(),
metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s", constants.TykBootstrapLabel),
},
)
if err != nil {
return err
}

found := false
var errCascading error
for _, job := range jobs.Items {
_, exists := job.Labels[constants.TykBootstrapLabel]
if exists {
jobLabel, exists := job.ObjectMeta.Labels[constants.TykBootstrapLabel]
if !exists {
continue
}

// Do not need to delete pre-delete job. It will be deleted by Helm.
if jobLabel != constants.TykBootstrapPreDeleteLabel {
deletePropagationType := metav1.DeletePropagationBackground

err = clientset.
err2 := clientset.
BatchV1().
Jobs(data.AppConfig.TykPodNamespace).
Delete(context.TODO(), job.Name, metav1.DeleteOptions{PropagationPolicy: &deletePropagationType})
if err != nil {
return err
if err2 != nil {
errCascading = err2
}

found = true
}
}

if !found {
fmt.Println("A previously created bootstrapping job has not been identified")
} else {
fmt.Println("A previously created bootstrapping job was identified and deleted")
}
return nil
return errCascading
}

0 comments on commit ff4189a

Please sign in to comment.