-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (84 loc) · 2.47 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"flag"
"fmt"
"os"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
//
// Uncomment to load all auth plugins
// _ "k8s.io/client-go/plugin/pkg/client/auth"
//
// Or uncomment to load specific auth plugins
// _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
// _ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
)
func main() {
// parse flags
var namespace *string
namespace = flag.String("namespace", "default", "specify the namespace of the pods")
var selector *string
selector = flag.String("selector", "", "label selector")
var gracePeriod *int64
gracePeriod = flag.Int64("grace-period", 30, "the duration in seconds before the object should be deleted.")
flag.Parse()
// creates the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// list pods with/without selector
var listOptions metav1.ListOptions
var pods *v1.PodList
if *selector == "" {
listOptions = metav1.ListOptions{
Limit: 100,
}
pods, err = clientset.CoreV1().Pods(*namespace).List(listOptions)
if err != nil {
panic(err.Error())
}
fmt.Printf("There are %d pods in the namespace %s\n", len(pods.Items), *namespace)
} else {
listOptions = metav1.ListOptions{
LabelSelector: *selector,
Limit: 100,
}
pods, err = clientset.CoreV1().Pods(*namespace).List(listOptions)
if err != nil {
panic(err.Error())
}
fmt.Printf("There are %d pods match the selector \"%s\" in namespace %s\n", len(pods.Items), *selector, *namespace)
}
// no pods are found
if len(pods.Items) == 0 {
fmt.Printf("No pods are deleted.\n")
os.Exit(0)
}
// delete the pods
deleteOptions := metav1.DeleteOptions{
GracePeriodSeconds: gracePeriod,
}
for _, pod := range pods.Items {
err := clientset.CoreV1().Pods(*namespace).Delete(pod.Name, &deleteOptions)
if errors.IsNotFound(err) {
fmt.Printf("pod %s not found\n", pod.Name)
} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
fmt.Printf("error getting pod %v\n", statusError.ErrStatus.Message)
} else if err != nil {
panic(err.Error())
} else {
fmt.Printf("deleting pod %s\n", pod.Name)
}
}
}