Skip to content

Commit

Permalink
clean: purge jobs if their pods are not going away
Browse files Browse the repository at this point in the history
During the destroy-cluster, if there are any osd-prepare
jobs they will be purged, similar to deployments that are
not being purged automatically.

Signed-off-by: Travis Nielsen <[email protected]>
  • Loading branch information
travisn committed Apr 5, 2024
1 parent a9dad50 commit 0f3dfde
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion pkg/crds/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"time"

"github.com/pkg/errors"
"github.com/rook/kubectl-rook-ceph/pkg/k8sutil"
"github.com/rook/kubectl-rook-ceph/pkg/logging"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -201,7 +202,11 @@ func ensureClusterIsEmpty(ctx context.Context, k8sClientSet kubernetes.Interface
appLabel := getPodLabel(ctx, pod, "app")
err := pruneDeployments(ctx, k8sClientSet, clusterNamespace, appLabel)
if err != nil {
logging.Fatal(err)
logging.Warning("failed to prune deployments. %v", err)
}
err = pruneJobs(ctx, k8sClientSet, clusterNamespace, appLabel)
if err != nil {
logging.Warning("failed to prune jobs. %v", err)
}
}

Expand Down Expand Up @@ -240,3 +245,26 @@ func pruneDeployments(ctx context.Context, k8sClientSet kubernetes.Interface, cl
}
return nil
}

func pruneJobs(ctx context.Context, k8sClientSet kubernetes.Interface, clusterNamespace, labelApp string) error {
selector := "rook_cluster=" + clusterNamespace + ",app=" + labelApp
jobs, err := k8sClientSet.BatchV1().Jobs(clusterNamespace).List(ctx, v1.ListOptions{
LabelSelector: selector,
})
if err != nil {
if !k8sErrors.IsNotFound(err) {
return err
}
return nil
}

for _, job := range jobs.Items {
logging.Info("job %s exists removing....", job.Name)
if err := k8sClientSet.BatchV1().Jobs(clusterNamespace).Delete(ctx, job.Name, v1.DeleteOptions{}); err != nil {
if !k8sErrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to delete job %q", job.Name)
}
}
}
return nil
}

0 comments on commit 0f3dfde

Please sign in to comment.