-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathdeploy.go
705 lines (660 loc) · 18.7 KB
/
deploy.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
package deploy
import (
"bytes"
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"time"
dtypes "github.com/docker/docker/api/types"
dclient "github.com/docker/docker/client"
kexec "github.com/google/kne/os/exec"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/kind/pkg/cluster"
)
const (
dockerConfigEnvVar = "DOCKER_CONFIG"
kubeletConfigPathTemplate = "%s:/var/lib/kubelet/config.json"
dockerConfigTemplateContents = `{
"auths": {
{{range $val := .}} "{{$val}}": {}
{{end}} }
}
`
ixiaTGConfigMapHeader = `apiVersion: v1
kind: ConfigMap
metadata:
name: ixiatg-release-config
namespace: ixiatg-op-system
data:
versions: |
`
)
var (
dockerConfigTemplate = template.Must(template.New("dockerConfig").Parse(dockerConfigTemplateContents))
logOut = log.StandardLogger().Out
healthTimeout = time.Minute
// execer handles all execs on host.
execer execerInterface = kexec.NewExecer(logOut, logOut)
// Stubs for testing.
newProvider = defaultProvider
execLookPath = exec.LookPath
osStat = os.Stat
)
//go:generate mockgen -source=specs.go -destination=mocks/mock_provider.go -package=mocks provider
type provider interface {
List() ([]string, error)
Create(name string, options ...cluster.CreateOption) error
}
func defaultProvider() provider {
return cluster.NewProvider(cluster.ProviderWithLogger(&logAdapter{log.StandardLogger()}))
}
type execerInterface interface {
Exec(string, ...string) error
SetStdout(io.Writer)
SetStderr(io.Writer)
}
type Cluster interface {
Deploy(context.Context) error
Delete() error
Healthy() error
GetName() string
}
type Ingress interface {
Deploy(context.Context) error
SetKClient(kubernetes.Interface)
Healthy(context.Context) error
}
type CNI interface {
Deploy(context.Context) error
SetKClient(kubernetes.Interface)
Healthy(context.Context) error
}
type Controller interface {
Deploy(context.Context) error
SetKClient(kubernetes.Interface)
Healthy(context.Context) error
}
type Deployment struct {
Cluster Cluster
Ingress Ingress
CNI CNI
Controllers []Controller
}
func (d *Deployment) String() string {
b, _ := json.MarshalIndent(d, "", "\t")
return string(b)
}
func (d *Deployment) Deploy(ctx context.Context, kubecfg string) error {
log.Infof("Deploying cluster...")
if err := d.Cluster.Deploy(ctx); err != nil {
return err
}
log.Infof("Cluster deployed")
// Once cluster is up set kClient
rCfg, err := clientcmd.BuildConfigFromFlags("", kubecfg)
if err != nil {
return err
}
kClient, err := kubernetes.NewForConfig(rCfg)
if err != nil {
return err
}
d.Ingress.SetKClient(kClient)
log.Infof("Deploying ingress...")
if err := d.Ingress.Deploy(ctx); err != nil {
return err
}
tCtx, cancel := context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := d.Ingress.Healthy(tCtx); err != nil {
return err
}
log.Infof("Ingress healthy")
log.Infof("Deploying CNI...")
if err := d.CNI.Deploy(ctx); err != nil {
return err
}
d.CNI.SetKClient(kClient)
tCtx, cancel = context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := d.CNI.Healthy(tCtx); err != nil {
return err
}
log.Infof("CNI healthy")
for _, c := range d.Controllers {
log.Infof("Deploying controller...")
if err := c.Deploy(ctx); err != nil {
return err
}
c.SetKClient(kClient)
tCtx, cancel = context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := c.Healthy(tCtx); err != nil {
return err
}
}
log.Infof("Controllers deployed and healthy")
return nil
}
func (d *Deployment) Delete() error {
log.Infof("Deleting cluster...")
if err := d.Cluster.Delete(); err != nil {
return err
}
log.Infof("Cluster deleted")
return nil
}
func (d *Deployment) Healthy(ctx context.Context) error {
if err := d.Cluster.Healthy(); err != nil {
return err
}
log.Infof("Cluster healthy")
tCtx, cancel := context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := d.Ingress.Healthy(tCtx); err != nil {
return err
}
log.Infof("Ingress healthy")
tCtx, cancel = context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := d.CNI.Healthy(tCtx); err != nil {
return err
}
log.Infof("CNI healthy")
for _, c := range d.Controllers {
tCtx, cancel = context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := c.Healthy(tCtx); err != nil {
return err
}
}
log.Infof("Controllers healthy")
return nil
}
type KindSpec struct {
Name string `yaml:"name"`
Recycle bool `yaml:"recycle"`
Version string `yaml:"version"`
Image string `yaml:"image"`
Retain bool `yaml:"retain"`
Wait time.Duration `yaml:"wait"`
Kubecfg string `yaml:"kubecfg"`
DeployWithClient bool `yaml:"deployWithClient"`
GoogleArtifactRegistries []string `yaml:"googleArtifactRegistries"`
ContainerImages map[string]string `yaml:"containerImages"`
}
func (k *KindSpec) Deploy(ctx context.Context) error {
provider := newProvider()
if k.Recycle {
clusters, err := provider.List()
if err != nil {
return err
}
for _, v := range clusters {
if k.Name == v {
log.Infof("Recycling existing cluster: %s", v)
return nil
}
}
}
if k.DeployWithClient {
if len(k.GoogleArtifactRegistries) != 0 {
return fmt.Errorf("setting up access to artifact registries %v requires unsetting the deployWithClient field", k.GoogleArtifactRegistries)
}
if len(k.ContainerImages) != 0 {
return fmt.Errorf("loading container images requires unsetting the deployWithClient field")
}
if err := provider.Create(
k.Name,
cluster.CreateWithNodeImage(k.Image),
cluster.CreateWithRetain(k.Retain),
cluster.CreateWithWaitForReady(k.Wait),
cluster.CreateWithKubeconfigPath(k.Kubecfg),
cluster.CreateWithDisplayUsage(true),
cluster.CreateWithDisplaySalutation(true),
); err != nil {
return errors.Wrap(err, "failed to create cluster using kind client")
}
log.Infof("Deployed kind cluster using kind client: %s", k.Name)
return nil
}
if _, err := execLookPath("kind"); err != nil {
return errors.Wrap(err, "install kind cli to deploy, or set the deployWithClient field")
}
args := []string{"create", "cluster"}
if k.Name != "" {
args = append(args, "--name", k.Name)
}
if k.Image != "" {
args = append(args, "--image", k.Image)
}
if k.Retain {
args = append(args, "--retain")
}
if k.Wait != 0 {
args = append(args, "--wait", k.Wait.String())
}
if k.Kubecfg != "" {
args = append(args, "--kubeconfig", k.Kubecfg)
}
if err := execer.Exec("kind", args...); err != nil {
return errors.Wrap(err, "failed to create cluster using cli")
}
log.Infof("Deployed kind cluster: %s", k.Name)
if len(k.GoogleArtifactRegistries) != 0 {
log.Infof("Setting up Google Artifact Registry access for %v", k.GoogleArtifactRegistries)
if err := k.setupGoogleArtifactRegistryAccess(); err != nil {
return errors.Wrap(err, "setting up google artifact registry access")
}
}
if len(k.ContainerImages) != 0 {
log.Infof("Loading container images")
if err := k.loadContainerImages(); err != nil {
return errors.Wrap(err, "loading container images")
}
}
return nil
}
func (k *KindSpec) Delete() error {
if _, err := execLookPath("kind"); err != nil {
return errors.Wrap(err, "install kind cli to delete")
}
args := []string{"delete", "cluster"}
if k.Name != "" {
args = append(args, "--name", k.Name)
}
if err := execer.Exec("kind", args...); err != nil {
return errors.Wrap(err, "failed to delete cluster using cli")
}
return nil
}
func (k *KindSpec) Healthy() error {
if _, err := exec.LookPath("kubectl"); err != nil {
return errors.Wrap(err, "install kubectl to check health")
}
if err := execer.Exec("kubectl", "cluster-info", "--context", fmt.Sprintf("kind-%s", k.GetName())); err != nil {
return errors.Wrap(err, "cluster not healthy")
}
return nil
}
func (k *KindSpec) GetName() string {
if k.Name != "" {
return k.Name
}
return "kind"
}
func (k *KindSpec) setupGoogleArtifactRegistryAccess() error {
if _, err := execLookPath("gcloud"); err != nil {
return errors.Wrap(err, "install gcloud cli to setup Google Artifact Registry access")
}
if _, err := execLookPath("docker"); err != nil {
return errors.Wrap(err, "install docker cli to setup Google Artifact Registry access")
}
// Create a temporary dir to hold a new docker config that lacks credsStore.
// Then use `docker login` to store the generated credentials directly in
// the temporary docker config.
// See https://kind.sigs.k8s.io/docs/user/private-registries/#use-an-access-token
// for more information.
tempDockerDir, err := os.MkdirTemp("", "kne_kind_docker")
if err != nil {
return err
}
defer os.RemoveAll(tempDockerDir)
originalConfig := os.Getenv(dockerConfigEnvVar)
defer os.Setenv(dockerConfigEnvVar, originalConfig)
if err := os.Setenv(dockerConfigEnvVar, tempDockerDir); err != nil {
return err
}
configPath := filepath.Join(tempDockerDir, "config.json")
if err := writeDockerConfig(configPath, k.GoogleArtifactRegistries); err != nil {
return err
}
var token bytes.Buffer
execer.SetStdout(&token)
if err := execer.Exec("gcloud", "auth", "print-access-token"); err != nil {
return err
}
execer.SetStdout(log.StandardLogger().Out)
for _, r := range k.GoogleArtifactRegistries {
s := fmt.Sprintf("https://%s", r)
if err := execer.Exec("docker", "login", "-u", "oauth2accesstoken", "-p", token.String(), s); err != nil {
return err
}
}
args := []string{"get", "nodes"}
if k.Name != "" {
args = append(args, "--name", k.Name)
}
var nodes bytes.Buffer
execer.SetStdout(&nodes)
if err := execer.Exec("kind", args...); err != nil {
return err
}
execer.SetStdout(log.StandardLogger().Out)
// Copy the new docker config to each node and restart kubelet so it
// picks up the new config that contains the embedded credentials.
for _, node := range strings.Split(nodes.String(), " ") {
node = strings.TrimSuffix(node, "\n")
if err := execer.Exec("docker", "cp", configPath, fmt.Sprintf(kubeletConfigPathTemplate, node)); err != nil {
return err
}
if err := execer.Exec("docker", "exec", node, "systemctl", "restart", "kubelet.service"); err != nil {
return err
}
}
log.Infof("Setup credentials for accessing GAR locations %v in kind cluster", k.GoogleArtifactRegistries)
return nil
}
func (k *KindSpec) loadContainerImages() error {
if _, err := execLookPath("docker"); err != nil {
return errors.Wrap(err, "install docker cli to load container images")
}
for s, d := range k.ContainerImages {
log.Infof("Loading %q as %q", s, d)
if err := execer.Exec("docker", "pull", s); err != nil {
return errors.Wrapf(err, "pulling %q", s)
}
if err := execer.Exec("docker", "tag", s, d); err != nil {
return errors.Wrapf(err, "tagging %q with %q", s, d)
}
args := []string{"load", "docker-image", d}
if k.Name != "" {
args = append(args, "--name", k.Name)
}
if err := execer.Exec("kind", args...); err != nil {
return errors.Wrapf(err, "loading %q", d)
}
}
log.Infof("Loaded all container images")
return nil
}
func writeDockerConfig(path string, registries []string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return dockerConfigTemplate.Execute(f, registries)
}
type MetalLBSpec struct {
Version string `yaml:"version"`
IPCount int `yaml:"ip_count"`
ManifestDir string `yaml:"manifests"`
kClient kubernetes.Interface
dClient dclient.NetworkAPIClient
}
func (m *MetalLBSpec) SetKClient(c kubernetes.Interface) {
m.kClient = c
}
func inc(ip net.IP, cnt int) {
for cnt > 0 {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
cnt--
}
}
type pool struct {
Name string `yaml:"name"`
Protocol string `yaml:"protocol"`
Addresses []string `yaml:"addresses"`
}
type metalLBConfig struct {
AddressPools []pool `yaml:"address-pools"`
}
func makeConfig(n *net.IPNet, count int) metalLBConfig {
start := make(net.IP, len(n.IP))
copy(start, n.IP)
inc(start, 50)
end := make(net.IP, len(start))
copy(end, start)
inc(end, count)
return metalLBConfig{
AddressPools: []pool{{
Name: "default",
Protocol: "layer2",
Addresses: []string{fmt.Sprintf("%s - %s", start, end)},
}},
}
}
func (m *MetalLBSpec) Deploy(ctx context.Context) error {
if m.dClient == nil {
var err error
m.dClient, err = dclient.NewClientWithOpts(dclient.FromEnv)
if err != nil {
return err
}
}
log.Infof("Creating metallb namespace")
if err := execer.Exec("kubectl", "apply", "-f", filepath.Join(m.ManifestDir, "namespace.yaml")); err != nil {
return err
}
_, err := m.kClient.CoreV1().Secrets("metallb-system").Get(ctx, "memberlist", metav1.GetOptions{})
if err != nil {
log.Infof("Creating metallb secret")
d := make([]byte, 16)
rand.Read(d)
s := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "memberlist",
},
StringData: map[string]string{
"secretkey": base64.StdEncoding.EncodeToString(d),
},
}
_, err := m.kClient.CoreV1().Secrets("metallb-system").Create(ctx, s, metav1.CreateOptions{})
if err != nil {
return err
}
}
log.Infof("Applying metallb pods")
if err := execer.Exec("kubectl", "apply", "-f", filepath.Join(m.ManifestDir, "metallb.yaml")); err != nil {
return err
}
_, err = m.kClient.CoreV1().ConfigMaps("metallb-system").Get(ctx, "config", metav1.GetOptions{})
if err != nil {
log.Infof("Applying metallb ingress config")
// Get Network information from docker.
nr, err := m.dClient.NetworkList(ctx, dtypes.NetworkListOptions{})
if err != nil {
return err
}
var network dtypes.NetworkResource
for _, v := range nr {
if v.Name == "kind" {
network = v
break
}
}
var n *net.IPNet
for _, ipRange := range network.IPAM.Config {
_, ipNet, err := net.ParseCIDR(ipRange.Subnet)
if err != nil {
return err
}
if ipNet.IP.To4() != nil {
n = ipNet
break
}
}
if n == nil {
return fmt.Errorf("failed to find kind ipv4 docker net")
}
config := makeConfig(n, m.IPCount)
b, err := yaml.Marshal(config)
if err != nil {
return err
}
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config",
},
Data: map[string]string{
"config": string(b),
},
}
_, err = m.kClient.CoreV1().ConfigMaps("metallb-system").Create(ctx, cm, metav1.CreateOptions{})
if err != nil {
return err
}
}
return nil
}
func (m *MetalLBSpec) Healthy(ctx context.Context) error {
return deploymentHealthy(ctx, m.kClient, "metallb-system")
}
type MeshnetSpec struct {
Image string `yaml:"image"`
ManifestDir string `yaml:"manifests"`
kClient kubernetes.Interface
}
func (m *MeshnetSpec) SetKClient(c kubernetes.Interface) {
m.kClient = c
}
func (m *MeshnetSpec) Deploy(ctx context.Context) error {
log.Infof("Deploying Meshnet from: %s", m.ManifestDir)
if err := execer.Exec("kubectl", "apply", "-k", m.ManifestDir); err != nil {
return err
}
log.Infof("Meshnet Deployed")
return nil
}
func (m *MeshnetSpec) Healthy(ctx context.Context) error {
log.Infof("Waiting on Meshnet to be Healthy")
w, err := m.kClient.AppsV1().DaemonSets("meshnet").Watch(ctx, metav1.ListOptions{
FieldSelector: fields.SelectorFromSet(fields.Set{metav1.ObjectNameField: "meshnet"}).String(),
})
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
return fmt.Errorf("context canceled before healthy")
case e, ok := <-w.ResultChan():
if !ok {
return fmt.Errorf("watch channel closed before healthy")
}
d, ok := e.Object.(*appsv1.DaemonSet)
if !ok {
return fmt.Errorf("invalid object type: %T", d)
}
if d.Status.NumberReady == d.Status.DesiredNumberScheduled &&
d.Status.NumberUnavailable == 0 {
log.Infof("Meshnet Healthy")
return nil
}
}
}
}
type IxiaTGSpec struct {
ManifestDir string `yaml:"manifests"`
ConfigMap *IxiaTGConfigMap `yaml:"configMap"`
kClient kubernetes.Interface
}
type IxiaTGConfigMap struct {
Release string `yaml:"release" json:"release"`
Images []*IxiaTGImage `yaml:"images" json:"images"`
}
type IxiaTGImage struct {
Name string `yaml:"name" json:"name"`
Path string `yaml:"path" json:"path"`
Tag string `yaml:"tag" json:"tag"`
}
func (i *IxiaTGSpec) SetKClient(c kubernetes.Interface) {
i.kClient = c
}
func (i *IxiaTGSpec) Deploy(ctx context.Context) error {
log.Infof("Deploying IxiaTG controller from: %s", i.ManifestDir)
if err := execer.Exec("kubectl", "apply", "-f", filepath.Join(i.ManifestDir, "ixiatg-operator.yaml")); err != nil {
return err
}
if i.ConfigMap == nil {
path := filepath.Join(i.ManifestDir, "ixia-configmap.yaml")
if _, err := osStat(path); err != nil {
return fmt.Errorf("ixia configmap not found: %v", err)
}
log.Infof("Deploying IxiaTG configmap from: %s", path)
if err := execer.Exec("kubectl", "apply", "-f", path); err != nil {
return err
}
log.Infof("IxiaTG controller Deployed")
return nil
}
b, err := json.MarshalIndent(i.ConfigMap, " ", " ")
if err != nil {
return err
}
b = append([]byte(ixiaTGConfigMapHeader), b...)
f, err := os.CreateTemp("", "ixiatg-configmap-*.yaml")
if err != nil {
return err
}
defer os.Remove(f.Name())
if _, err := f.Write(b); err != nil {
return err
}
log.Infof("Deploying IxiaTG configmap from: %s", f.Name())
if err := execer.Exec("kubectl", "apply", "-f", f.Name()); err != nil {
return err
}
log.Infof("IxiaTG controller Deployed")
return nil
}
func (i *IxiaTGSpec) Healthy(ctx context.Context) error {
return deploymentHealthy(ctx, i.kClient, "ixiatg-op-system")
}
func deploymentHealthy(ctx context.Context, c kubernetes.Interface, name string) error {
log.Infof("Waiting on deployment %q to be healthy", name)
w, err := c.AppsV1().Deployments(name).Watch(ctx, metav1.ListOptions{})
if err != nil {
return err
}
ch := w.ResultChan()
for {
select {
case <-ctx.Done():
return fmt.Errorf("context canceled before healthy")
case e, ok := <-ch:
if !ok {
return fmt.Errorf("watch channel closed before healthy")
}
d, ok := e.Object.(*appsv1.Deployment)
if !ok {
return fmt.Errorf("invalid object type: %T", d)
}
var r int32 = 1
if d.Spec.Replicas != nil {
r = *d.Spec.Replicas
}
if d.Status.AvailableReplicas == r &&
d.Status.ReadyReplicas == r &&
d.Status.UnavailableReplicas == 0 &&
d.Status.Replicas == r &&
d.Status.UpdatedReplicas == r {
log.Infof("Deployment %q healthy", name)
return nil
}
}
}
}