Ask Your Question
0

What is the method for removing k8s Jobs and their associated pods using client-go?

asked 2023-06-19 14:42:49 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-19 15:06:01 +0000

devzero gravatar image

You can use the following client-go method to remove k8s Jobs and their associated pods:

  1. First, create a clientset instance:
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())
}
  1. Then, create a Job object using the clientset:
jobClient := clientset.BatchV1().Jobs(namespace)

err = jobClient.Delete(jobName, &metav1.DeleteOptions{})
if err != nil {
    panic(err.Error())
}

  1. Finally, delete the associated pods using the clientset:
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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-19 14:42:49 +0000

Seen: 8 times

Last updated: Jun 19 '23