/
kubernetes.go
131 lines (112 loc) · 3.29 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
package utils
import (
"bytes"
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
core_v1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
)
type K8sClient struct {
config *rest.Config
clientSet *kubernetes.Clientset
}
func NewK8sClient() (*K8sClient, error) {
log.Printf("Creating k8s client...")
var config *rest.Config
var err error
userHome, err := os.UserHomeDir()
if err != nil {
log.Printf("failed to get home: %v\n", err)
return nil, fmt.Errorf(fmt.Sprintf("failed to get home: %v\n", err))
}
kubeCfgLoc := filepath.Join(userHome, ".kube", "config")
log.Printf("Using .kube/config...")
config, err = clientcmd.BuildConfigFromFlags("", kubeCfgLoc)
if err != nil {
return nil, fmt.Errorf("error in building config from %s: %s", kubeCfgLoc, err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("error in creating clientset: %s", err.Error())
}
log.Println("Successfully created k8s client !")
return &K8sClient{
config: config,
clientSet: clientset,
}, nil
}
func (c *K8sClient) AllPodsAreReady(namespace string) (bool, error) {
pods, err := c.clientSet.CoreV1().Pods(namespace).List(context.TODO(), v1.ListOptions{})
if err != nil {
return false, err
}
if len(pods.Items) == 0 {
return false, nil
}
for _, pod := range pods.Items {
for _, condition := range pod.Status.Conditions {
log.Printf("waiting for pod %s in namespace %s to be ready....", pod.Name, namespace)
if condition.Status != "True" {
return false, nil
}
log.Printf("pod %s in namespace %s: condition: %s, is ready", pod.Name, namespace, condition.Type)
}
}
return true, nil
}
func (c *K8sClient) GetPod(namespace string, podname string) (p *core_v1.Pod, err error) {
pod, err := c.clientSet.CoreV1().Pods(namespace).Get(context.TODO(), podname, v1.GetOptions{})
if err != nil {
fmt.Printf("Pod %s in namespace %s not found\n", pod, namespace)
} else {
fmt.Printf("Found pod %s in namespace %s\n", podname, namespace)
}
return pod, nil
}
func (c *K8sClient) Exec(p *core_v1.Pod, command []string) (string, error) {
attachOptions := &core_v1.PodExecOptions{
Container: p.Spec.Containers[0].Name,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
Command: command,
}
request := c.clientSet.CoreV1().RESTClient().Post().
Resource("pods").
Name(p.Name).
Namespace(p.Namespace).
SubResource("exec").
VersionedParams(attachOptions, scheme.ParameterCodec)
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
streamOptions := remotecommand.StreamOptions{
Stdout: stdout,
Stderr: stderr,
}
exec, err := remotecommand.NewSPDYExecutor(c.config, "POST", request.URL())
if err != nil {
fmt.Println(exec)
return "", err
}
err = exec.Stream(streamOptions)
if err != nil {
result := strings.TrimSpace(stdout.String()) + "\n" + strings.TrimSpace(stderr.String())
result = strings.TrimSpace(result)
log.Print(result)
return "", err
}
result := strings.TrimSpace(stdout.String()) + "\n" + strings.TrimSpace(stderr.String())
result = strings.TrimSpace(result)
log.Print(result)
return result, nil
}