-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
116 lines (101 loc) · 4.1 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"fmt"
v1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
typedappsv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
"k8s.io/client-go/rest"
)
type bin int
func (b bin) String() string {
return fmt.Sprintf("%b", b)
}
type DeleteFunc func(deploymentInterface typedappsv1.DeploymentInterface, deployment *v1.Deployment, restarts int, restartThreshold int) (bool, error)
func DeleteCrash(deploymentInterface typedappsv1.DeploymentInterface, deployment *v1.Deployment, restarts int, restartThreshold int) (bool, error) {
// pod is in a CrashLoopBackOff state
// check if restart number meets or exceeds the restart threshold
// the restart threshold will be 0 if not specified in the config, so have to handle that case
if restartThreshold > 0 && restarts >= restartThreshold {
return DeleteGeneric(deploymentInterface, deployment, restarts, restartThreshold)
} else {
fmt.Printf("%s/%s is in a CrashLoopBackOff state, but doesn't meet the restart threshold. " +
"Restarts/Threshold = %v/%v\n", deployment.Namespace, deployment.Name, restarts, restartThreshold)
return false, nil
}
}
func DeleteGeneric(deploymentInterface typedappsv1.DeploymentInterface, deployment *v1.Deployment, restarts int, restartThreshold int) (bool, error) {
// pod is in a state defined in config.yaml
policy := metav1.DeletePropagationForeground
gracePeriodSeconds := int64(0)
fmt.Printf("About to delete %s/%s and its associated resources.\n", deployment.Namespace, deployment.Name)
err := deploymentInterface.Delete(deployment.Name, &metav1.DeleteOptions{PropagationPolicy: &policy, GracePeriodSeconds: &gracePeriodSeconds})
if err != nil {
fmt.Printf("%s/%s, Error: %s \n", deployment.Namespace, deployment.Name, err.Error())
return false, err
}
fmt.Printf("Deleted %s/%s and its associated resources.\n", deployment.Namespace, deployment.Name)
return true, nil
}
func main() {
// initialize the config, from yaml or environment variables
var kleanerConfig = ConfigObj
// create the map that will hold the reasons and the config object
var waitingReasons = make(map[string]SweeperConfigDetails)
// fill the map
for _, conf := range kleanerConfig.Reasons {
waitingReasons[conf.Reason] = conf
}
// fail fast if not in the cluster
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
fmt.Println("Beginning the sweep.")
// get a list of pods for all namespaces
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
fmt.Println("Got list of pods.")
for _, pod := range pods.Items {
StatusLoop:
for _, status := range pod.Status.ContainerStatuses {
waiting := status.State.Waiting
if waiting != nil {
reason := waiting.Reason
if SweeperConfigDetails, ok := waitingReasons[reason]; ok {
fmt.Printf("Waiting reason match. %s/%s has a waiting reason of: %s\n", pod.Namespace,
pod.OwnerReferences[0].Name, reason)
rs, err := clientset.AppsV1().ReplicaSets(pod.Namespace).Get(pod.OwnerReferences[0].Name, metav1.GetOptions{})
if err != nil {
fmt.Printf("Error retrieving ReplicaSets. Error: %s\n", err.Error())
continue StatusLoop
}
if rs.OwnerReferences != nil {
deploy, err := clientset.AppsV1().Deployments(pod.Namespace).Get(rs.OwnerReferences[0].Name, metav1.GetOptions{})
if err != nil {
fmt.Printf("Error retrieving Deployments. Error: %s\n", err.Error())
continue StatusLoop
}
if deploy != nil && deploy.Name != "" { // indicates something to be deleted
_, err = SweeperConfigDetails.DeleteFunction(clientset.AppsV1().Deployments(pod.Namespace), deploy, int(status.RestartCount), SweeperConfigDetails.RestartThreshold)
if err != nil {
fmt.Printf("Error deleting Deployment. Error: %s\n", err.Error())
continue StatusLoop
}
} else {
fmt.Println("No deployment found.")
}
} else {
fmt.Println("No replica set owner reference.")
}
}
}
}
}
}