-
Notifications
You must be signed in to change notification settings - Fork 0
/
podController.go
141 lines (112 loc) · 3.08 KB
/
podController.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package kubernetes
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
type podData struct {
Name string `json:"name"`
StartTime int64 `json:"startTime"`
PodIP string `json:"podIP"`
Phase v1.PodPhase `json:"phase"`
}
func createClient() *kubernetes.Clientset {
// creates the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
return clientset
}
func getNameSpace(podID string) string {
clientset := createClient()
var podNameSpace string
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
log.Panic(err.Error())
}
for i := range pods.Items {
if pods.Items[i].Name == podID {
podNameSpace = pods.Items[i].Namespace
}
}
log.Printf("The pod %s is in the %s\n", podID, podNameSpace)
return podNameSpace
}
// ListPods returns a list of pods running within the cluster.
func ListPods(w http.ResponseWriter) string {
clientset := createClient()
var podList []podData
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
log.Panic(err.Error())
}
log.Printf("There are %d pods in the cluster\n", len(pods.Items))
var podData podData
for i := range pods.Items {
podData.Name = pods.Items[i].Name
podData.StartTime = pods.Items[i].Status.StartTime.Unix()
podData.PodIP = pods.Items[i].Status.PodIP
podData.Phase = pods.Items[i].Status.Phase
podList = append(podList, podData)
}
json, err := json.Marshal(podList)
if err != nil {
log.Panic(err.Error())
}
result := string(json)
fmt.Println(result)
return result
}
// ListMolePods returns a list of test mole pods running within the cluster.
func ListMolePods(w http.ResponseWriter) string {
clientset := createClient()
var podList []podData
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
log.Panic(err.Error())
}
log.Printf("There are %d pods in the cluster\n", len(pods.Items))
var podData podData
for i := range pods.Items {
if strings.Contains(pods.Items[i].Name, "test-moles") {
podData.Name = pods.Items[i].Name
podData.StartTime = pods.Items[i].Status.StartTime.Unix()
podData.PodIP = pods.Items[i].Status.PodIP
podData.Phase = pods.Items[i].Status.Phase
podList = append(podList, podData)
}
}
json, err := json.Marshal(podList)
if err != nil {
log.Panic(err.Error())
}
result := string(json)
fmt.Println(result)
return result
}
// DeletePod deletes a specified pod within the cluster.
func DeletePod(w http.ResponseWriter, podID string) {
clientset := createClient()
podNameSpace := getNameSpace(podID)
if len(podNameSpace) == 0 {
log.Printf("Pod not found\n")
} else {
err := clientset.CoreV1().Pods(podNameSpace).Delete(podID, &metav1.DeleteOptions{})
if err != nil {
log.Panic(err.Error())
}
log.Printf("Pod %v was deleted\n", podID)
}
}