-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleter.go
109 lines (97 loc) · 2.36 KB
/
deleter.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
100
101
102
103
104
105
106
107
108
109
package main
import (
"context"
"errors"
"math/rand"
"github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
type PodDeleter struct {
clientset kubernetes.Interface
ctx context.Context
}
func NewPodDeleter() PodDeleter {
log.Debug().
Msg("Initializing client")
config, err := rest.InClusterConfig()
if err != nil {
log.Fatal().
Err(err).
Msg("Error reading in cluster configuration")
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal().
Err(err).
Msg("Error creating the client")
}
return PodDeleter{
clientset: clientset,
ctx: context.TODO(),
}
}
func (p PodDeleter) DeleteRandomPod(namespace, labelSelector string) {
log.Info().Str("namespace", namespace).Msg("Deleting a random pod")
pods, err := p.listPods(namespace, labelSelector)
if err != nil {
log.Fatal().
Err(err).
Msg("Error listing pods")
}
pod, err := pickRandomPod(pods)
if err != nil {
log.Fatal().
Err(err).
Msg("Error picking pod")
}
err = p.deletePod(pod.Name, namespace)
if err != nil {
log.Fatal().
Err(err).
Msg("Error deleting pod")
}
log.Info().Str("pod", pod.Name).Msg("Pod deleted")
}
func (p PodDeleter) listPods(namespace, labelSelector string) ([]v1.Pod, error) {
log.Debug().
Str("labelSelector", labelSelector).
Msg("Listing pods")
podList, err := p.clientset.CoreV1().
Pods(namespace).
List(p.ctx, metav1.ListOptions{
LabelSelector: labelSelector,
})
log.Debug().
Strs("pods", prettySlicePods(podList.Items)).
Msgf("There are %d pods\n", len(podList.Items))
if podList.Items == nil {
return []v1.Pod{}, nil
}
return podList.Items, err
}
func (p PodDeleter) deletePod(pod, namespace string) error {
log.Debug().
Str("pod", pod).
Msg("Deleting pod")
return p.clientset.CoreV1().
Pods(namespace).
Delete(p.ctx, pod, metav1.DeleteOptions{})
}
func pickRandomPod(in []v1.Pod) (v1.Pod, error) {
if len(in) == 0 {
return v1.Pod{}, errors.New("no pods found")
}
randomIndex := rand.Intn(len(in))
return in[randomIndex], nil
}
// prettySlicePods converts []v1.Pod into []string using each Pod.Name. This is useful for logging and debugging
func prettySlicePods(pods []v1.Pod) []string {
out := []string{}
for _, pod := range pods {
out = append(out, pod.Name)
}
return out
}