Skip to content

Commit

Permalink
Merge pull request #164 from uselagoon/pod-deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon authored Aug 15, 2022
2 parents a520c57 + 0567c73 commit fac3c2b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
43 changes: 43 additions & 0 deletions controllers/messenger/deletions.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,49 @@ func (h *Messaging) DeleteJobs(ctx context.Context, opLog logr.Logger, ns, proje
return true
}

// DeletePods will delete any pods from the namespace.
func (h *Messaging) DeletePods(ctx context.Context, opLog logr.Logger, ns, project, branch string) bool {
pods := &corev1.PodList{}
listOption := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
client.InNamespace(ns),
})
if err := h.Client.List(ctx, pods, 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 pods.Items {
if err := h.Client.Delete(ctx, &ds); err != nil {
opLog.Error(err,
fmt.Sprintf(
"Unable to delete pods %s in %s for project %s, branch %s",
ds.ObjectMeta.Name,
ns,
project,
branch,
),
)
return false
}
opLog.Info(
fmt.Sprintf(
"Deleted pods %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 @@ -295,6 +295,10 @@ func (h *Messaging) Consumer(targetName string) { //error {
message.Ack(false) // ack to remove from queue
return
}
if del := h.DeletePods(ctx, opLog.WithName("DeletePods"), 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 fac3c2b

Please sign in to comment.