forked from maorfr/skbn
/
kube.go
286 lines (248 loc) · 6.64 KB
/
kube.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package skbn
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/maorfr/skbn/pkg/utils"
core_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
)
// K8sClient holds a clientset and a config
type K8sClient struct {
ClientSet *kubernetes.Clientset
Config *rest.Config
}
// GetClientToK8s returns a k8sClient
func GetClientToK8s() (*K8sClient, error) {
var kubeconfig string
if kubeConfigPath := os.Getenv("KUBECONFIG"); kubeConfigPath != "" {
kubeconfig = kubeConfigPath // CI process
} else {
kubeconfig = filepath.Join(os.Getenv("HOME"), ".kube", "config") // Development environment
}
var config *rest.Config
_, err := os.Stat(kubeconfig)
if err != nil {
// In cluster configuration
config, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
} else {
// Out of cluster configuration
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
var client = &K8sClient{ClientSet: clientset, Config: config}
return client, nil
}
// GetListOfFilesFromK8s gets list of files in path from Kubernetes (recursive)
func GetListOfFilesFromK8s(iClient interface{}, path, findType, findName string) ([]string, error) {
client := *iClient.(*K8sClient)
pSplit := strings.Split(path, "/")
if err := validateK8sPath(pSplit); err != nil {
return nil, err
}
namespace, podName, containerName, findPath := initK8sVariables(pSplit)
command := []string{"find", findPath, "-type", findType, "-name", findName}
attempts := 3
attempt := 0
for attempt < attempts {
attempt++
output := new(bytes.Buffer)
stderr, err := Exec(client, namespace, podName, containerName, command, nil, output)
if len(stderr) != 0 {
if attempt == attempts {
return nil, fmt.Errorf("STDERR: " + (string)(stderr))
}
utils.Sleep(attempt)
continue
}
if err != nil {
if attempt == attempts {
return nil, err
}
utils.Sleep(attempt)
continue
}
lines := strings.Split(output.String(), "\n")
var outLines []string
for _, line := range lines {
if line != "" {
outLines = append(outLines, strings.Replace(line, findPath, "", 1))
}
}
return outLines, nil
}
return nil, nil
}
// DownloadFromK8s downloads a single file from Kubernetes
func DownloadFromK8s(iClient interface{}, path string, writer io.Writer) error {
client := *iClient.(*K8sClient)
pSplit := strings.Split(path, "/")
if err := validateK8sPath(pSplit); err != nil {
return err
}
namespace, podName, containerName, pathToCopy := initK8sVariables(pSplit)
command := []string{"cat", pathToCopy}
attempts := 3
attempt := 0
for attempt < attempts {
attempt++
stderr, err := Exec(client, namespace, podName, containerName, command, nil, writer)
if attempt == attempts {
if len(stderr) != 0 {
return fmt.Errorf("STDERR: " + (string)(stderr))
}
if err != nil {
return err
}
}
if err == nil {
return nil
}
utils.Sleep(attempt)
}
return nil
}
// UploadToK8s uploads a single file to Kubernetes
func UploadToK8s(iClient interface{}, toPath, fromPath string, reader io.Reader) error {
client := *iClient.(*K8sClient)
pSplit := strings.Split(toPath, "/")
if err := validateK8sPath(pSplit); err != nil {
return err
}
if len(pSplit) == 3 {
_, fileName := filepath.Split(fromPath)
pSplit = append(pSplit, fileName)
}
namespace, podName, containerName, pathToCopy := initK8sVariables(pSplit)
attempts := 3
attempt := 0
for attempt < attempts {
attempt++
dir, _ := filepath.Split(pathToCopy)
command := []string{"mkdir", "-p", dir}
stderr, err := Exec(client, namespace, podName, containerName, command, nil, nil)
if len(stderr) != 0 {
if attempt == attempts {
return fmt.Errorf("STDERR: " + (string)(stderr))
}
utils.Sleep(attempt)
continue
}
if err != nil {
if attempt == attempts {
return err
}
utils.Sleep(attempt)
continue
}
command = []string{"touch", pathToCopy}
stderr, err = Exec(client, namespace, podName, containerName, command, nil, nil)
if len(stderr) != 0 {
if attempt == attempts {
return fmt.Errorf("STDERR: " + (string)(stderr))
}
utils.Sleep(attempt)
continue
}
if err != nil {
if attempt == attempts {
return err
}
utils.Sleep(attempt)
continue
}
command = []string{"cp", "/dev/stdin", pathToCopy}
stderr, err = Exec(client, namespace, podName, containerName, command, readerWrapper{reader}, nil)
if len(stderr) != 0 {
if attempt == attempts {
return fmt.Errorf("STDERR: " + (string)(stderr))
}
utils.Sleep(attempt)
continue
}
if err != nil {
if attempt == attempts {
return err
}
utils.Sleep(attempt)
continue
}
return nil
}
return nil
}
type readerWrapper struct {
reader io.Reader
}
func (r readerWrapper) Read(p []byte) (int, error) {
return r.reader.Read(p)
}
// Exec executes a command in a given container
func Exec(client K8sClient, namespace, podName, containerName string, command []string, stdin io.Reader, stdout io.Writer) ([]byte, error) {
clientset, config := client.ClientSet, client.Config
req := clientset.CoreV1().RESTClient().Post().
Resource("pods").
Name(podName).
Namespace(namespace).
SubResource("exec")
scheme := runtime.NewScheme()
if err := core_v1.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf("error adding to scheme: %v", err)
}
parameterCodec := runtime.NewParameterCodec(scheme)
req.VersionedParams(&core_v1.PodExecOptions{
Command: command,
Container: containerName,
Stdin: stdin != nil,
Stdout: stdout != nil,
Stderr: true,
TTY: false,
}, parameterCodec)
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
return nil, fmt.Errorf("error while creating Executor: %v", err)
}
var stderr bytes.Buffer
err = exec.Stream(remotecommand.StreamOptions{
Stdin: stdin,
Stdout: stdout,
Stderr: &stderr,
Tty: false,
})
if err != nil {
return nil, fmt.Errorf("error in Stream: %v", err)
}
return stderr.Bytes(), nil
}
func validateK8sPath(pathSplit []string) error {
if len(pathSplit) >= 3 {
return nil
}
return fmt.Errorf("illegal path: %s", filepath.Join(pathSplit...))
}
func initK8sVariables(split []string) (string, string, string, string) {
namespace := split[0]
pod := split[1]
container := split[2]
path := getAbsPath(split[3:]...)
return namespace, pod, container, path
}
func getAbsPath(path ...string) string {
return filepath.Join("/", filepath.Join(path...))
}