Last active
August 9, 2018 20:15
-
-
Save dkerwin/981a52c91a020faa17b4bd916bb84282 to your computer and use it in GitHub Desktop.
Delete job via api client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for _, job := range jobs { | |
// get uid and created jobs | |
uid := job.GetObjectMeta().GetUID() | |
// Delete job first. See https://github.com/kubernetes/kubernetes/issues/43168 | |
err := clientset.BatchV1().Jobs(job.GetNamespace()).Delete(job.GetName(), &metav1.DeleteOptions{}) | |
if err != nil { | |
return i, fmt.Errorf("failed to delete job %s/%s: %s", job.GetNamespace(), job.GetName(), err) | |
} else { | |
i++ | |
log.Infof("successfully deleted job %s/%s", job.GetNamespace(), job.GetName()) | |
} | |
// Delete associated pods | |
childPods, err := GetPodsWithUidLabel(clientset, uid, job.GetNamespace()) | |
log.Infof("Deleting %d associated pods for job %s/%s", len(childPods.Items), job.GetNamespace(), job.GetName()) | |
if err != nil { | |
return i, fmt.Errorf("failed to get child pods: %s", err) | |
} | |
for _, pod := range childPods.Items { | |
err := clientset.CoreV1().Pods(job.GetNamespace()).Delete(pod.GetName(), nil) | |
if err != nil { | |
return i, fmt.Errorf("failed to delete associated pod %s: %s", pod.GetName(), err) | |
} | |
log.Infof("Deleted associated pod %s", pod.GetName()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment