This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kubernetes.go
140 lines (111 loc) · 2.6 KB
/
kubernetes.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
package main
import (
"fmt"
"io"
"os"
"strconv"
"time"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Get Kubernetes client set
func getClientSet() *kubernetes.Clientset {
c := getConfig()
// Use the current context in kubeconfig
cc, err := clientcmd.BuildConfigFromFlags("", *c.kubeConfig)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
// Create the client set
cs, err := kubernetes.NewForConfig(cc)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return cs
}
// Get pods (use namespace)
func getPods() (*v1.PodList, error) {
cs := getClientSet()
return cs.CoreV1().Pods(NAMESPACE).List(metav1.ListOptions{})
}
// Get namespaces
func getNamespaces() (*v1.NamespaceList, error) {
cs := getClientSet()
return cs.CoreV1().Namespaces().List(metav1.ListOptions{})
}
// Get the pod containers
func getPodContainers(p string) []string {
var pc []string
cs := getClientSet()
pod, _ := cs.CoreV1().Pods(NAMESPACE).Get(p, metav1.GetOptions{})
for _, c := range pod.Spec.Containers {
pc = append(pc, c.Name)
}
return pc
}
// Delete pod
func deletePod(p string) error {
cs := getClientSet()
return cs.CoreV1().Pods(NAMESPACE).Delete(p, &metav1.DeleteOptions{})
}
// Get pod container logs
func getPodContainerLogs(p string, c string, o io.Writer) error {
tl := int64(50)
cs := getClientSet()
opts := &v1.PodLogOptions{
Container: c,
TailLines: &tl,
}
req := cs.CoreV1().Pods(NAMESPACE).GetLogs(p, opts)
readCloser, err := req.Stream()
if err != nil {
return err
}
_, err = io.Copy(o, readCloser)
readCloser.Close()
return err
}
// Column helper: Restarts
func columnHelperRestarts(cs []v1.ContainerStatus) string {
r := 0
for _, c := range cs {
r = r + int(c.RestartCount)
}
return strconv.Itoa(r)
}
// Column helper: Age
func columnHelperAge(t metav1.Time) string {
d := time.Now().Sub(t.Time)
if d.Hours() > 1 {
if d.Hours() > 24 {
ds := float64(d.Hours() / 24)
return fmt.Sprintf("%.0fd", ds)
} else {
return fmt.Sprintf("%.0fh", d.Hours())
}
} else if d.Minutes() > 1 {
return fmt.Sprintf("%.0fm", d.Minutes())
} else if d.Seconds() > 1 {
return fmt.Sprintf("%.0fs", d.Seconds())
}
return "?"
}
// Column helper: Status
func columnHelperStatus(s v1.PodStatus) string {
return fmt.Sprintf("%s", s.Phase)
}
// Column helper: Ready
func columnHelperReady(cs []v1.ContainerStatus) string {
cr := 0
for _, c := range cs {
if c.Ready {
cr = cr + 1
}
}
return fmt.Sprintf("%d/%d", cr, len(cs))
}