You can use the following client-go method to remove k8s Jobs and their associated pods:
import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
jobClient := clientset.BatchV1().Jobs(namespace)
err = jobClient.Delete(jobName, &metav1.DeleteOptions{})
if err != nil {
panic(err.Error())
}
podClient := clientset.CoreV1().Pods(namespace)
selector := fmt.Sprintf("job-name=%s", jobName)
err = podClient.DeleteCollection(&metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: selector,
})
if err != nil {
panic(err.Error())
}
With the above client-go methods, you can easily remove k8s Jobs and their associated pods.
Asked: 2023-06-19 14:42:49 +0000
Seen: 8 times
Last updated: Jun 19 '23