-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes.go
473 lines (398 loc) · 13.5 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package kubernetes
import (
"fmt"
"github.com/pkg/errors"
"golang.org/x/exp/slices"
boxModel "github.com/hckops/hckctl/pkg/box/model"
"github.com/hckops/hckctl/pkg/client/kubernetes"
"github.com/hckops/hckctl/pkg/client/terminal"
commonKube "github.com/hckops/hckctl/pkg/common/kubernetes"
commonModel "github.com/hckops/hckctl/pkg/common/model"
"github.com/hckops/hckctl/pkg/schema"
"github.com/hckops/hckctl/pkg/util"
)
func newKubeBoxClient(commonOpts *boxModel.CommonBoxOptions, kubeOpts *commonModel.KubeOptions) (*KubeBoxClient, error) {
kubeCommonClient, err := commonKube.NewKubeCommonClient(kubeOpts, commonOpts.EventBus)
if err != nil {
return nil, errors.Wrap(err, "error kube box client")
}
return &KubeBoxClient{
client: kubeCommonClient.GetClient(),
clientOpts: kubeOpts,
kubeCommon: kubeCommonClient,
eventBus: commonOpts.EventBus,
}, nil
}
func (box *KubeBoxClient) close() error {
return box.kubeCommon.Close()
}
func (box *KubeBoxClient) createBox(opts *boxModel.CreateOptions) (*boxModel.BoxInfo, error) {
namespace := box.clientOpts.Namespace
boxName := opts.Template.GenerateName()
deployment, service, err := kubernetes.BuildResources(newResources(namespace, boxName, opts))
if err != nil {
return nil, err
}
// create namespace
if err := box.client.NamespaceApply(namespace); err != nil {
return nil, err
}
box.eventBus.Publish(newNamespaceApplyKubeEvent(namespace))
// create service
if opts.Template.HasPorts() {
if err := box.client.ServiceCreate(namespace, service); err != nil {
return nil, err
}
box.eventBus.Publish(newServiceCreateKubeEvent(namespace, service.Name))
} else {
box.eventBus.Publish(newServiceCreateIgnoreKubeEvent(namespace, service.Name))
}
// inject sidecar-volume
if opts.CommonInfo.ShareDir != nil {
sidecarOpts := &commonModel.SidecarShareInjectOpts{
MainContainerName: opts.Template.MainContainerName(),
ShareDir: opts.CommonInfo.ShareDir,
}
if err := box.kubeCommon.SidecarShareInject(sidecarOpts, &deployment.Spec.Template.Spec); err != nil {
return nil, err
}
}
// create secret and inject sidecar-vpn
if opts.CommonInfo.NetworkVpn != nil {
sidecarOpts := &commonModel.SidecarVpnInjectOpts{
Name: boxName,
NetworkVpn: opts.CommonInfo.NetworkVpn,
}
if err := box.kubeCommon.SidecarVpnInject(namespace, sidecarOpts, &deployment.Spec.Template.Spec); err != nil {
return nil, err
}
}
// create deployment
box.eventBus.Publish(newResourcesDeployKubeLoaderEvent(namespace, opts.Template.Name))
deploymentOpts := &kubernetes.DeploymentCreateOpts{
Namespace: namespace,
Spec: deployment,
OnStatusEventCallback: func(event string) {
box.eventBus.Publish(newDeploymentCreateStatusKubeEvent(event))
},
}
if err := box.client.DeploymentCreate(deploymentOpts); err != nil {
return nil, err
}
box.eventBus.Publish(newDeploymentCreateKubeEvent(namespace, deployment.Name))
podInfo, err := box.client.PodDescribeFromDeployment(deployment)
if err != nil {
return nil, err
}
box.eventBus.Publish(newPodNameKubeEvent(namespace, podInfo.PodName, podInfo.ContainerName))
// upload shared directory
if opts.CommonInfo.ShareDir != nil {
sidecarOpts := &commonModel.SidecarShareUploadOpts{
Namespace: namespace,
PodName: podInfo.PodName,
ShareDir: opts.CommonInfo.ShareDir,
}
if err := box.kubeCommon.SidecarShareUpload(sidecarOpts); err != nil {
return nil, err
}
}
// TODO always healthy unused? otherwise use DeploymentDescribe instead of PodDescribe
return &boxModel.BoxInfo{Id: podInfo.PodName, Name: boxName, Healthy: true}, nil
}
func newResources(namespace string, name string, opts *boxModel.CreateOptions) *kubernetes.ResourcesOpts {
var envs []kubernetes.KubeEnv
for _, e := range opts.Template.EnvironmentVariables() {
envs = append(envs, kubernetes.KubeEnv{Key: e.Key, Value: e.Value})
}
var ports []kubernetes.KubePort
for _, p := range opts.Template.NetworkPortValues(false) {
ports = append(ports, kubernetes.KubePort{Name: p.Alias, Port: p.Remote})
}
return &kubernetes.ResourcesOpts{
Namespace: namespace,
Name: name,
Annotations: opts.Labels,
Labels: kubernetes.BuildLabels(name, opts.Template.Image.Repository, opts.Template.Image.ResolveVersion(),
map[string]string{commonModel.LabelSchemaKind: util.ToLowerKebabCase(schema.KindBoxV1.String())}),
Ports: ports,
PodInfo: &kubernetes.PodInfo{
Namespace: namespace,
PodName: "INVALID_POD_NAME", // not used, generated suffix by kube
ContainerName: opts.Template.Image.Repository,
ImageName: opts.Template.Image.Name(),
Arguments: []string{},
Env: envs,
Resource: opts.Size.ToKubeResource(),
},
}
}
func (box *KubeBoxClient) connectBox(opts *boxModel.ConnectOptions) error {
if info, err := box.searchBox(opts.Name); err != nil {
return err
} else {
if opts.DisableExec && opts.DisableTunnel {
return errors.New("invalid connection options")
}
namespace := box.clientOpts.Namespace
for _, e := range opts.Template.EnvironmentVariables() {
box.eventBus.Publish(newPodEnvKubeEvent(namespace, info.Id, e))
box.eventBus.Publish(newPodEnvKubeConsoleEvent(namespace, info.Name, e))
}
// tunnel only
if opts.DisableExec {
// tunnel and block to exit, wait until killed
return box.podPortForward(opts.Template, info, true)
}
if !opts.DisableTunnel {
// tunnel and exec after, do not block
if err := box.podPortForward(opts.Template, info, false); err != nil {
return err
}
}
return box.execBox(opts, info)
}
}
func boxNameLabelSelector(name string) string {
return fmt.Sprintf("%s,%s=%s", boxModel.BoxLabelSelector(), kubernetes.LabelKubeName, name)
}
func (box *KubeBoxClient) searchBox(name string) (*boxModel.BoxInfo, error) {
namespace := box.clientOpts.Namespace
box.eventBus.Publish(newDeploymentSearchKubeEvent(namespace, name))
deployments, err := box.client.DeploymentList(namespace, boxModel.BoxPrefixName, boxNameLabelSelector(name))
if err != nil {
return nil, err
}
switch len(deployments) {
case 0:
return nil, errors.New("box not found")
case 1:
info := newBoxInfo(deployments[0])
return &info, nil
default:
// this should never happen
return nil, errors.New("unexpected label selector match")
}
}
func (box *KubeBoxClient) execBox(opts *boxModel.ConnectOptions, info *boxModel.BoxInfo) error {
// TODO if BoxInfo not Healthy attempt scale 1
if opts.Template.Shell == boxModel.BoxShellNone {
// stop loader
box.eventBus.Publish(newPodExecKubeLoaderEvent())
return box.logsBox(opts, info)
}
if opts.DeleteOnExit {
defer box.deleteBox(info.Name)
}
// exec
execOpts := &kubernetes.PodExecOpts{
Namespace: box.clientOpts.Namespace,
PodName: info.Id,
ContainerName: opts.Template.MainContainerName(),
Commands: terminal.DefaultShellCommand(opts.Template.Shell),
InStream: opts.StreamOpts.In,
OutStream: opts.StreamOpts.Out,
ErrStream: opts.StreamOpts.Err,
IsTty: opts.StreamOpts.IsTty,
OnExecCallback: func() {
// stop loader
box.eventBus.Publish(newPodExecKubeLoaderEvent())
},
}
box.eventBus.Publish(newPodExecKubeEvent(opts.Template.Name, box.clientOpts.Namespace, info.Id, opts.Template.Shell))
return box.client.PodExecShell(execOpts)
}
func (box *KubeBoxClient) logsBox(opts *boxModel.ConnectOptions, info *boxModel.BoxInfo) error {
namespace := box.clientOpts.Namespace
if opts.DeleteOnExit {
opts.OnInterruptCallback(func() {
box.eventBus.Publish(newPodLogsExitKubeEvent(namespace, info.Id))
box.eventBus.Publish(newPodLogsExitKubeConsoleEvent())
box.deleteBox(info.Name)
})
}
logsOpts := &kubernetes.PodLogsOpts{
Namespace: namespace,
PodName: info.Id,
ContainerName: opts.Template.MainContainerName(),
OutStream: opts.StreamOpts.Out,
}
box.eventBus.Publish(newPodLogsKubeEvent(namespace, info.Id))
return box.client.PodLogs(logsOpts)
}
func (box *KubeBoxClient) podPortForward(template *boxModel.BoxV1, boxInfo *boxModel.BoxInfo, isWait bool) error {
namespace := box.clientOpts.Namespace
if !template.HasPorts() {
box.eventBus.Publish(newPodPortForwardIgnoreKubeEvent(namespace, boxInfo.Id))
// exit, no service/port available to bind
return nil
}
networkPorts := template.NetworkPortValues(false)
portPadding := boxModel.PortFormatPadding(networkPorts)
ports, err := ToPortBindings(networkPorts, func(port boxModel.BoxPort) {
box.eventBus.Publish(newPodPortForwardBindingKubeEvent(namespace, boxInfo.Id, port))
box.eventBus.Publish(newPodPortForwardBindingKubeConsoleEvent(namespace, boxInfo.Name, port, portPadding))
})
if err != nil {
return err
}
opts := &kubernetes.PodPortForwardOpts{
Namespace: namespace,
PodName: boxInfo.Id,
Ports: ports,
IsWait: isWait,
OnTunnelStartCallback: func() {
// stop loader
box.eventBus.Publish(newPodExecKubeLoaderEvent())
},
OnTunnelErrorCallback: func(err error) {
box.eventBus.Publish(newPodPortForwardErrorKubeEvent(namespace, boxInfo.Id, err))
},
}
if err := box.client.PodPortForward(opts); err != nil {
return err
}
return nil
}
func ToPortBindings(ports []boxModel.BoxPort, onPortBindCallback func(port boxModel.BoxPort)) ([]string, error) {
var portBindings []string
for _, port := range ports {
localPort, err := util.FindOpenPort(port.Local)
if err != nil {
return nil, errors.Wrapf(err, "error kube local port %s", port.Local)
}
// actual bound port
onPortBindCallback(boxModel.BoxPort{
Alias: port.Alias,
Local: localPort,
Remote: port.Remote,
})
portBindings = append(portBindings, fmt.Sprintf("%s:%s", localPort, port.Remote))
}
return portBindings, nil
}
func (box *KubeBoxClient) describeBox(name string) (*boxModel.BoxDetails, error) {
namespace := box.clientOpts.Namespace
boxInfo, err := box.searchBox(name)
if err != nil {
return nil, err
}
box.eventBus.Publish(newDeploymentDescribeKubeEvent(namespace, name))
deployment, err := box.client.DeploymentDescribe(namespace, boxInfo.Name)
if err != nil {
return nil, err
}
box.eventBus.Publish(newServiceDescribeKubeEvent(namespace, name))
service, err := box.client.ServiceDescribe(namespace, boxInfo.Name)
if err != nil {
return nil, err
}
return ToBoxDetails(deployment, service, box.Provider())
}
func ToBoxDetails(deployment *kubernetes.DeploymentDetails, serviceInfo *kubernetes.ServiceInfo, provider boxModel.BoxProvider) (*boxModel.BoxDetails, error) {
labels := commonModel.Labels(deployment.Annotations)
size, err := boxModel.ToBoxSize(labels)
if err != nil {
return nil, err
}
var envs []boxModel.BoxEnv
for _, env := range deployment.Info.PodInfo.Env {
envs = append(envs, boxModel.BoxEnv{
Key: env.Key,
Value: env.Value,
})
}
var ports []boxModel.BoxPort
for _, p := range serviceInfo.Ports {
ports = append(ports, boxModel.BoxPort{
Alias: p.Name,
Local: boxModel.BoxPortNone, // runtime only
Remote: p.Port,
Public: false,
})
}
return &boxModel.BoxDetails{
Info: newBoxInfo(*deployment.Info),
TemplateInfo: &boxModel.BoxTemplateInfo{
CachedTemplate: labels.ToCachedTemplateInfo(),
GitTemplate: labels.ToGitTemplateInfo(),
},
ProviderInfo: &boxModel.BoxProviderInfo{
Provider: provider,
KubeProvider: &commonModel.KubeProviderInfo{
Namespace: deployment.Info.Namespace,
},
},
Size: size,
Env: boxModel.SortEnv(envs),
Ports: boxModel.SortPorts(ports),
Created: deployment.Created,
}, nil
}
func (box *KubeBoxClient) listBoxes() ([]boxModel.BoxInfo, error) {
namespace := box.clientOpts.Namespace
deployments, err := box.client.DeploymentList(namespace, boxModel.BoxPrefixName, boxModel.BoxLabelSelector())
if err != nil {
return nil, err
}
var result []boxModel.BoxInfo
for index, d := range deployments {
result = append(result, newBoxInfo(d))
box.eventBus.Publish(newDeploymentListKubeEvent(index, namespace, d.Name, d.Healthy))
}
return result, nil
}
func newBoxInfo(deployment kubernetes.DeploymentInfo) boxModel.BoxInfo {
return boxModel.BoxInfo{
Id: deployment.PodInfo.PodName,
Name: deployment.Name,
Healthy: deployment.Healthy,
}
}
func (box *KubeBoxClient) deleteBoxes(names []string) ([]string, error) {
namespace := box.clientOpts.Namespace
// optimize delete
if len(names) == 1 {
boxInfo, err := box.searchBox(names[0])
if err != nil {
return nil, err
}
return []string{boxInfo.Name}, box.deleteBox(boxInfo.Name)
}
boxes, err := box.listBoxes()
if err != nil {
return nil, err
}
var deleted []string
for _, boxInfo := range boxes {
// all or filter
if len(names) == 0 || slices.Contains(names, boxInfo.Name) {
if err := box.deleteBox(boxInfo.Name); err == nil {
deleted = append(deleted, boxInfo.Name)
} else {
// silently ignore
box.eventBus.Publish(newResourcesDeleteIgnoreKubeEvent(namespace, boxInfo.Name))
}
}
}
return deleted, nil
}
func (box *KubeBoxClient) deleteBox(name string) error {
namespace := box.clientOpts.Namespace
box.eventBus.Publish(newDeploymentDeleteKubeEvent(namespace, name))
if err := box.client.DeploymentDelete(namespace, name); err != nil {
return err
}
box.eventBus.Publish(newServiceDeleteKubeEvent(namespace, name))
if err := box.client.ServiceDelete(namespace, name); err != nil {
return err
}
if err := box.kubeCommon.SidecarVpnDelete(namespace, name); err != nil {
return err
}
return nil
}
func (box *KubeBoxClient) clean() error {
namespace := box.clientOpts.Namespace
box.eventBus.Publish(newNamespaceDeleteKubeEvent(namespace))
return box.client.NamespaceDelete(namespace)
}