Skip to content

Commit

Permalink
feat: delete jobs in the namespace too
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jun 28, 2022
1 parent 1992e2b commit 2536969
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
44 changes: 44 additions & 0 deletions controllers/messenger/deletions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/go-logr/logr"
"gopkg.in/matryer/try.v1"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -230,6 +231,49 @@ func (h *Messaging) DeleteDaemonSets(ctx context.Context, opLog logr.Logger, ns,
return true
}

// DeleteJobs will delete any jobs from the namespace.
func (h *Messaging) DeleteJobs(ctx context.Context, opLog logr.Logger, ns, project, branch string) bool {
jobs := &batchv1.JobList{}
listOption := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
client.InNamespace(ns),
})
if err := h.Client.List(ctx, jobs, listOption); err != nil {
opLog.Error(err,
fmt.Sprintf(
"Unable to list jobs in namespace %s for project %s, branch %s",
ns,
project,
branch,
),
)
return false
}
for _, ds := range jobs.Items {
if err := h.Client.Delete(ctx, &ds); err != nil {
opLog.Error(err,
fmt.Sprintf(
"Unable to delete jobs %s in %s for project %s, branch %s",
ds.ObjectMeta.Name,
ns,
project,
branch,
),
)
return false
}
opLog.Info(
fmt.Sprintf(
"Deleted jobs %s in %s for project %s, branch %s",
ds.ObjectMeta.Name,
ns,
project,
branch,
),
)
}
return true
}

// DeletePVCs will delete any PVCs from the namespace.
func (h *Messaging) DeletePVCs(ctx context.Context, opLog logr.Logger, ns, project, branch string) bool {
pvcs := &corev1.PersistentVolumeClaimList{}
Expand Down
4 changes: 4 additions & 0 deletions controllers/messenger/queues.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ func (h *Messaging) Consumer(targetName string) { //error {
message.Ack(false) // ack to remove from queue
return
}
if del := h.DeleteJobs(ctx, opLog.WithName("DeleteJobs"), ns, project, branch); del == false {
message.Ack(false) // ack to remove from queue
return
}
if del := h.DeletePVCs(ctx, opLog.WithName("DeletePVCs"), ns, project, branch); del == false {
message.Ack(false) // ack to remove from queue
return
Expand Down

0 comments on commit 2536969

Please sign in to comment.