-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s.go
146 lines (125 loc) · 3.47 KB
/
k8s.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
142
143
144
145
146
package main
import (
"context"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/deprecated/scheme"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// K8sContext contains data about the path the user took through the troubleshooting
type K8sContext struct {
kubeConfigPath string
k8sClient *kubernetes.Clientset
namespace string
config *rest.Config
pods []corev1.Pod
svc corev1.Service
svcPort corev1.ServicePort
controller *appsv1.Deployment
containerPort corev1.ContainerPort
//podList []corev1.Pod
podPort corev1.ContainerPort
}
func NewK8sContext(config string) *K8sContext {
return &K8sContext{
kubeConfigPath: config,
}
}
func (k *K8sContext) InitClient() error {
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", k.kubeConfigPath)
if err != nil {
return err
}
k.config = config
// TODO this hack matches that in k8s.io/kubectl/pkg/cmd/util/kubectl_match_version.go
if nil == k.config.GroupVersion {
k.config.GroupVersion = &schema.GroupVersion{Group: "", Version: "v1"}
}
if k.config.APIPath == "" {
k.config.APIPath = "/api"
}
if k.config.NegotiatedSerializer == nil {
// This codec factory ensures the resources are not converted. Therefore, resources
// will not be round-tripped through internal versions. Defaulting does not happen
// on the client.
k.config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
}
// TODO end hack
// create the clientset
k.k8sClient, err = kubernetes.NewForConfig(config)
if err != nil {
return err
}
return nil
}
func (k *K8sContext) getNamespaces() ([]string, error) {
nms, err := k.k8sClient.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return []string{}, err
}
result := []string{}
for _, nm := range nms.Items {
result = append(result, nm.GetName())
}
return result, nil
}
func (k *K8sContext) GetPods() ([]corev1.Pod, error) {
podList, err := k.k8sClient.CoreV1().Pods(k.namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return []corev1.Pod{}, err
}
pods := []corev1.Pod{}
for _, p := range podList.Items {
pods = append(pods, p)
}
k.pods = pods
return pods, nil
}
func (k *K8sContext) GetPendingPods() ([]string, error) {
result := []string{}
for _, pod := range k.pods {
status := pod.Status
if status.Phase == corev1.PodPending {
result = append(result, pod.GetName())
}
}
return result, nil
}
func (k *K8sContext) GetNonrunningPods() ([]string, error) {
result := []string{}
for _, pod := range k.pods {
status := pod.Status
if status.Phase != corev1.PodRunning {
result = append(result, pod.GetName())
}
}
return result, nil
}
func (k *K8sContext) GetNotReadyPods() ([]string, error) {
result := []string{}
for _, pod := range k.pods {
status := pod.Status
for _, c := range status.Conditions {
if c.Type == corev1.PodReady && c.Status != corev1.ConditionTrue {
result = append(result, pod.GetName())
}
}
}
return result, nil
}
func (k *K8sContext) GetServices() ([]string, error) {
result := []string{}
svcs, err := k.k8sClient.CoreV1().Services(k.namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return result, err
}
for _, s := range svcs.Items {
result = append(result, s.GetName())
}
return result, nil
}