-
Notifications
You must be signed in to change notification settings - Fork 119
/
run.go
1112 lines (963 loc) · 33.3 KB
/
run.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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cmd
import (
"bufio"
"bytes"
"fmt"
"os"
"os/signal"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"text/template"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"k8s.io/apimachinery/pkg/api/resource"
"kubevirt.io/kubevirtci/cluster-provision/gocli/cmd/nodesconfig"
"kubevirt.io/kubevirtci/cluster-provision/gocli/cmd/utils"
containers2 "kubevirt.io/kubevirtci/cluster-provision/gocli/containers"
"kubevirt.io/kubevirtci/cluster-provision/gocli/docker"
"kubevirt.io/kubevirtci/cluster-provision/gocli/images"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/aaq"
bindvfio "kubevirt.io/kubevirtci/cluster-provision/gocli/opts/bind-vfio"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/cdi"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/cnao"
dockerproxy "kubevirt.io/kubevirtci/cluster-provision/gocli/opts/docker-proxy"
etcdinmemory "kubevirt.io/kubevirtci/cluster-provision/gocli/opts/etcd"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/istio"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/ksm"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/multus"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/nfscsi"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/node01"
nodesprovision "kubevirt.io/kubevirtci/cluster-provision/gocli/opts/nodes"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/prometheus"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/psa"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/realtime"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/rookceph"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/rootkey"
"kubevirt.io/kubevirtci/cluster-provision/gocli/opts/swap"
k8s "kubevirt.io/kubevirtci/cluster-provision/gocli/pkg/k8s"
"kubevirt.io/kubevirtci/cluster-provision/gocli/pkg/libssh"
"github.com/alessio/shellescape"
)
const (
proxySettings = `
curl {{.Proxy}}/ca.crt > /etc/pki/ca-trust/source/anchors/docker_registry_proxy.crt
update-ca-trust
mkdir -p /etc/systemd/system/crio.service.d
cat <<EOT >/etc/systemd/system/crio.service.d/override.conf
[Service]
Environment="HTTP_PROXY={{.Proxy}}"
Environment="HTTPS_PROXY={{.Proxy}}"
Environment="NO_PROXY=localhost,127.0.0.1,registry,10.96.0.0/12,10.244.0.0/16,192.168.0.0/16,fd00:10:96::/112,fd00:10:244::/112,fd00::/64"
EOT
systemctl daemon-reload
systemctl restart crio.service
while [[ systemctl status crio | grep active | wc -l -eq 0 ]]
do
sleep 2
done
EOF
`
etcdDataDir = "/var/lib/etcd"
nvmeDiskImagePrefix = "/nvme"
scsiDiskImagePrefix = "/scsi"
QEMU_DEVICE_S390X = "virtio-net-ccw"
QEMU_DEVICE_X86_64 = "virtio-net-pci"
)
var soundcardPCIIDs = []string{"8086:2668", "8086:2415"}
var cli *client.Client
var nvmeDisks []string
var scsiDisks []string
var usbDisks []string
var sharedDisks []string
var sshClient libssh.Client
type dockerSetting struct {
Proxy string
}
// NewRunCommand returns command that runs given cluster
func NewRunCommand() *cobra.Command {
run := &cobra.Command{
Use: "run",
Short: "run starts a given cluster",
RunE: run,
Args: cobra.ExactArgs(1),
}
run.Flags().UintP("nodes", "n", 1, "number of cluster nodes to start")
run.Flags().UintP("numa", "u", 1, "number of NUMA nodes per node")
run.Flags().StringP("memory", "m", "3096M", "amount of ram per node")
run.Flags().UintP("cpu", "c", 2, "number of cpu cores per node")
run.Flags().UintP("secondary-nics", "", 0, "number of secondary nics to add")
run.Flags().String("qemu-args", "", "additional qemu args to pass through to the nodes")
run.Flags().String("kernel-args", "", "additional kernel args to pass through to the nodes")
run.Flags().BoolP("background", "b", true, "go to background after nodes are up")
run.Flags().Bool("random-ports", true, "expose all ports on random localhost ports")
run.Flags().Bool("slim", false, "use the slim flavor")
run.Flags().Uint("vnc-port", 0, "port on localhost for vnc")
run.Flags().Uint("http-port", 0, "port on localhost for http")
run.Flags().Uint("https-port", 0, "port on localhost for https")
run.Flags().Uint("registry-port", 0, "port on localhost for the docker registry")
run.Flags().Uint("ocp-port", 0, "port on localhost for the ocp cluster")
run.Flags().Uint("k8s-port", 0, "port on localhost for the k8s cluster")
run.Flags().Uint("ssh-port", 0, "port on localhost for ssh server")
run.Flags().Uint("prometheus-port", 0, "port on localhost for prometheus server")
run.Flags().Uint("grafana-port", 0, "port on localhost for grafana server")
run.Flags().Uint("dns-port", 0, "port on localhost for dns server")
run.Flags().String("nfs-data", "", "path to data which should be exposed via nfs to the nodes")
run.Flags().Bool("enable-ceph", false, "enables dynamic storage provisioning using Ceph")
run.Flags().Bool("enable-istio", false, "deploys Istio service mesh")
run.Flags().Bool("reverse", false, "reverse node setup order")
run.Flags().Bool("enable-cnao", false, "enable network extensions with istio")
run.Flags().Bool("skip-cnao-cr", false, "skip deploying cnao custom resource. if true, only cnao CRDS will be deployed")
run.Flags().Bool("deploy-multus", false, "deploy multus")
run.Flags().Bool("deploy-cdi", false, "deploy cdi")
run.Flags().String("cdi-version", "", "cdi version")
run.Flags().String("aaq-version", "", "aaq version")
run.Flags().Bool("deploy-aaq", false, "deploy aaq")
run.Flags().Bool("enable-nfs-csi", false, "deploys nfs csi dynamic storage")
run.Flags().Bool("enable-prometheus", false, "deploys Prometheus operator")
run.Flags().Bool("enable-prometheus-alertmanager", false, "deploys Prometheus alertmanager")
run.Flags().Bool("enable-grafana", false, "deploys Grafana")
run.Flags().Bool("enable-ksm", false, "enables kernel memory same page merging")
run.Flags().Uint("ksm-page-count", 10, "number of pages to scan per time in ksm")
run.Flags().Uint("ksm-scan-interval", 20, "sleep interval in milliseconds for ksm")
run.Flags().Bool("enable-swap", false, "enable swap")
run.Flags().Bool("unlimited-swap", false, "unlimited swap")
run.Flags().Uint("swap-size", 0, "swap memory size in GB")
run.Flags().Uint("swapiness", 0, "swapiness")
run.Flags().String("docker-proxy", "", "sets network proxy for docker daemon")
run.Flags().String("container-registry", "quay.io", "the registry to pull cluster container from")
run.Flags().String("container-org", "kubevirtci", "the organization at the registry to pull the container from")
run.Flags().String("container-suffix", "", "Override container suffix stored at the cli binary")
run.Flags().String("gpu", "", "pci address of a GPU to assign to a node")
run.Flags().StringArrayVar(&nvmeDisks, "nvme", []string{}, "size of the emulate NVMe disk to pass to the node")
run.Flags().StringArrayVar(&scsiDisks, "scsi", []string{}, "size of the emulate SCSI disk to pass to the node")
run.Flags().Bool("run-etcd-on-memory", false, "configure etcd to run on RAM memory, etcd data will not be persistent")
run.Flags().String("etcd-capacity", "512M", "set etcd data mount size.\nthis flag takes affect only when 'run-etcd-on-memory' is specified")
run.Flags().Uint("hugepages-2m", 64, "number of hugepages of size 2M to allocate")
run.Flags().Uint("hugepages-1g", 0, "number of hugepages of size 1Gi to allocate")
run.Flags().Bool("enable-realtime-scheduler", false, "configures the kernel to allow unlimited runtime for processes that require realtime scheduling")
run.Flags().Bool("enable-fips", false, "enables FIPS")
run.Flags().Bool("enable-psa", false, "Pod Security Admission")
run.Flags().Bool("single-stack", false, "enable single stack IPv6")
run.Flags().Bool("no-etcd-fsync", false, "unsafe: disable fsyncs in etcd")
run.Flags().Bool("enable-audit", false, "enable k8s audit for all metadata events")
run.Flags().StringArrayVar(&usbDisks, "usb", []string{}, "size of the emulate USB disk to pass to the node")
run.Flags().StringArrayVar(&sharedDisks, "shared-block-device", []string{}, "size of block device to share between all nodes")
return run
}
func run(cmd *cobra.Command, args []string) (retErr error) {
prefix, err := cmd.Flags().GetString("prefix")
if err != nil {
return err
}
nodes, err := cmd.Flags().GetUint("nodes")
if err != nil {
return err
}
memory, err := cmd.Flags().GetString("memory")
if err != nil {
return err
}
resource.MustParse(memory)
reverse, err := cmd.Flags().GetBool("reverse")
if err != nil {
return err
}
randomPorts, err := cmd.Flags().GetBool("random-ports")
if err != nil {
return err
}
slim, err := cmd.Flags().GetBool("slim")
if err != nil {
return err
}
portMap := nat.PortMap{}
utils.AppendTCPIfExplicit(portMap, utils.PortSSH, cmd.Flags(), "ssh-port")
utils.AppendTCPIfExplicit(portMap, utils.PortVNC, cmd.Flags(), "vnc-port")
utils.AppendTCPIfExplicit(portMap, utils.PortHTTP, cmd.Flags(), "http-port")
utils.AppendTCPIfExplicit(portMap, utils.PortHTTPS, cmd.Flags(), "https-port")
utils.AppendTCPIfExplicit(portMap, utils.PortAPI, cmd.Flags(), "k8s-port")
utils.AppendTCPIfExplicit(portMap, utils.PortOCP, cmd.Flags(), "ocp-port")
utils.AppendTCPIfExplicit(portMap, utils.PortRegistry, cmd.Flags(), "registry-port")
utils.AppendTCPIfExplicit(portMap, utils.PortPrometheus, cmd.Flags(), "prometheus-port")
utils.AppendTCPIfExplicit(portMap, utils.PortGrafana, cmd.Flags(), "grafana-port")
utils.AppendUDPIfExplicit(portMap, utils.PortDNS, cmd.Flags(), "dns-port")
qemuArgs, err := cmd.Flags().GetString("qemu-args")
if err != nil {
return err
}
kernelArgs, err := cmd.Flags().GetString("kernel-args")
if err != nil {
return err
}
cpu, err := cmd.Flags().GetUint("cpu")
if err != nil {
return err
}
numa, err := cmd.Flags().GetUint("numa")
if err != nil {
return err
}
secondaryNics, err := cmd.Flags().GetUint("secondary-nics")
if err != nil {
return err
}
nfsData, err := cmd.Flags().GetString("nfs-data")
if err != nil {
return err
}
dockerProxy, err := cmd.Flags().GetString("docker-proxy")
if err != nil {
return err
}
cephEnabled, err := cmd.Flags().GetBool("enable-ceph")
if err != nil {
return err
}
nfsCsiEnabled, err := cmd.Flags().GetBool("enable-nfs-csi")
if err != nil {
return err
}
istioEnabled, err := cmd.Flags().GetBool("enable-istio")
if err != nil {
return err
}
prometheusEnabled, err := cmd.Flags().GetBool("enable-prometheus")
if err != nil {
return err
}
prometheusAlertmanagerEnabled, err := cmd.Flags().GetBool("enable-prometheus-alertmanager")
if err != nil {
return err
}
grafanaEnabled, err := cmd.Flags().GetBool("enable-grafana")
if err != nil {
return err
}
cluster := args[0]
background, err := cmd.Flags().GetBool("background")
if err != nil {
return err
}
containerRegistry, err := cmd.Flags().GetString("container-registry")
if err != nil {
return err
}
gpuAddress, err := cmd.Flags().GetString("gpu")
if err != nil {
return err
}
containerOrg, err := cmd.Flags().GetString("container-org")
if err != nil {
return err
}
containerSuffix, err := cmd.Flags().GetString("container-suffix")
if err != nil {
return err
}
runEtcdOnMemory, err := cmd.Flags().GetBool("run-etcd-on-memory")
if err != nil {
return err
}
etcdDataMountSize, err := cmd.Flags().GetString("etcd-capacity")
if err != nil {
return err
}
resource.MustParse(etcdDataMountSize)
hugepages2Mcount, err := cmd.Flags().GetUint("hugepages-2m")
if err != nil {
return err
}
hugepages1Gcount, err := cmd.Flags().GetUint("hugepages-1g")
if err != nil {
return err
}
realtimeSchedulingEnabled, err := cmd.Flags().GetBool("enable-realtime-scheduler")
if err != nil {
return err
}
psaEnabled, err := cmd.Flags().GetBool("enable-psa")
if err != nil {
return err
}
singleStack, err := cmd.Flags().GetBool("single-stack")
if err != nil {
return err
}
noEtcdFsync, err := cmd.Flags().GetBool("no-etcd-fsync")
if err != nil {
return err
}
enableAudit, err := cmd.Flags().GetBool("enable-audit")
if err != nil {
return err
}
fipsEnabled, err := cmd.Flags().GetBool("enable-fips")
if err != nil {
return err
}
cnaoEnabled, err := cmd.Flags().GetBool("enable-cnao")
if err != nil {
return err
}
cnaoSkipCR, err := cmd.Flags().GetBool("skip-cnao-cr")
if err != nil {
return err
}
deployCdi, err := cmd.Flags().GetBool("deploy-cdi")
if err != nil {
return err
}
cdiVersion, err := cmd.Flags().GetString("cdi-version")
if err != nil {
return err
}
deployAaq, err := cmd.Flags().GetBool("deploy-aaq")
if err != nil {
return err
}
aaqVersion, err := cmd.Flags().GetString("aaq-version")
if err != nil {
return err
}
deployMultus, err := cmd.Flags().GetBool("deploy-multus")
if err != nil {
return err
}
enableSwap, err := cmd.Flags().GetBool("enable-swap")
if err != nil {
return err
}
unlimitedSwap, err := cmd.Flags().GetBool("unlimited-swap")
if err != nil {
return err
}
swapiness, err := cmd.Flags().GetUint("swapiness")
if err != nil {
return err
}
swapSize, err := cmd.Flags().GetUint("swap-size")
if err != nil {
return err
}
enableKsm, err := cmd.Flags().GetBool("enable-ksm")
if err != nil {
return err
}
ksmPageCount, err := cmd.Flags().GetUint("ksm-page-count")
if err != nil {
return err
}
ksmScanInterval, err := cmd.Flags().GetUint("ksm-scan-interval")
if err != nil {
return err
}
cli, err = client.NewClientWithOpts(client.FromEnv)
if err != nil {
return err
}
b := context.Background()
ctx, cancel := context.WithCancel(b)
stop := make(chan error, 10)
containers, volumes, done := docker.NewCleanupHandler(cli, stop, cmd.OutOrStderr(), false)
defer func() {
stop <- retErr
<-done
}()
go func() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
<-interrupt
cancel()
stop <- fmt.Errorf("Interrupt received, clean up")
}()
clusterImage := cluster
// Check if cluster container suffix has not being override
// in that case use the default prefix stored at the binary
if containerSuffix == "" {
containerSuffix = images.SUFFIX
}
if containerSuffix != "" {
clusterImage = fmt.Sprintf("%s/%s%s", containerOrg, cluster, containerSuffix)
} else {
clusterImage = path.Join(containerOrg, cluster)
}
if slim {
clusterImage += "-slim"
}
if len(containerRegistry) > 0 {
clusterImage = path.Join(containerRegistry, clusterImage)
fmt.Printf("Download the image %s\n", clusterImage)
err = docker.ImagePull(cli, ctx, clusterImage, types.ImagePullOptions{})
if err != nil {
panic(fmt.Sprintf("Failed to download cluster image %s, %s", clusterImage, err))
}
}
var dnsmasq *container.CreateResponse
for i := 0; i <= 3; i++ {
if i == 3 {
fmt.Printf("dnsmasq container failed to start 3 times")
return err
}
dnsmasq, err = containers2.DNSMasq(cli, ctx, &containers2.DNSMasqOptions{
ClusterImage: clusterImage,
SecondaryNicsCount: secondaryNics,
RandomPorts: randomPorts,
PortMap: portMap,
Prefix: prefix,
NodeCount: nodes,
})
if err != nil {
return err
}
if err := cli.ContainerStart(ctx, dnsmasq.ID, container.StartOptions{}); err != nil {
fmt.Printf("Failed to start dnsmasq container: %s\n", err)
fmt.Printf("Retry creating and starting dnsmasq container\n")
if err := cli.ContainerRemove(ctx, dnsmasq.ID, container.RemoveOptions{}); err != nil {
return err
}
time.Sleep(2 * time.Second)
} else {
containers <- dnsmasq.ID
break
}
}
dm, err := cli.ContainerInspect(context.Background(), dnsmasq.ID)
if err != nil {
return err
}
sshPort, err := utils.GetPublicPort(utils.PortSSH, dm.NetworkSettings.Ports)
apiServerPort, err := utils.GetPublicPort(utils.PortAPI, dm.NetworkSettings.Ports)
// Pull the registry image
err = docker.ImagePull(cli, ctx, utils.DockerRegistryImage, types.ImagePullOptions{})
if err != nil {
panic(err)
}
// Start registry
registry, err := cli.ContainerCreate(ctx, &container.Config{
Image: utils.DockerRegistryImage,
}, &container.HostConfig{
Privileged: true, // fixme we just need proper selinux volume labeling
NetworkMode: container.NetworkMode("container:" + dnsmasq.ID),
}, nil, nil, prefix+"-registry")
if err != nil {
return err
}
containers <- registry.ID
if err := cli.ContainerStart(ctx, registry.ID, container.StartOptions{}); err != nil {
return err
}
if nfsData != "" {
nfsData, err := filepath.Abs(nfsData)
if err != nil {
return err
}
// Pull the ganesha image
err = docker.ImagePull(cli, ctx, utils.NFSGaneshaImage, types.ImagePullOptions{})
if err != nil {
panic(err)
}
// Start the ganesha image
nfsServer, err := cli.ContainerCreate(ctx, &container.Config{
Image: utils.NFSGaneshaImage,
}, &container.HostConfig{
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: nfsData,
Target: "/data/nfs",
},
},
Privileged: true,
NetworkMode: container.NetworkMode("container:" + dnsmasq.ID),
}, nil, nil, prefix+"-nfs-ganesha")
if err != nil {
return err
}
containers <- nfsServer.ID
if err := cli.ContainerStart(ctx, nfsServer.ID, container.StartOptions{}); err != nil {
return err
}
}
sharedVolumeName := prefix + "-shared"
if len(sharedDisks) > 0 {
sharedVolume, err := cli.VolumeCreate(ctx, volume.CreateOptions{Name: sharedVolumeName})
if err != nil {
return err
}
volumes <- sharedVolume.Name
}
// Add serial pty so we can do stuff like 'screen /dev/pts0' to access
// the VM console from the container without ssh
qemuArgs += " -serial pty"
var qemuNetDevice = getNetDeviceByArch()
wg := sync.WaitGroup{}
wg.Add(int(nodes))
// start one vm after each other
macCounter := 0
for x := 0; x < int(nodes); x++ {
nodeQemuArgs := qemuArgs
nodeQemuMonitorArgs := ""
for i := 0; i < int(secondaryNics); i++ {
netSuffix := fmt.Sprintf("%d-%d", x, i)
macSuffix := fmt.Sprintf("%02x", macCounter)
macCounter++
// Secondary network devices are added after VM is started (hot-plug) using qemu monitor to avoid
// primary network interface to be named other than eth0. This is mainly required for s390x, as
// otherwise if primary interface is other than eth0, it can't get the IP from dhcp server.
if qemuNetDevice == QEMU_DEVICE_S390X {
nodeQemuMonitorArgs = fmt.Sprintf("%s netdev_add tap,id=secondarynet%s,ifname=stap%s,script=no,downscript=no; device_add %s,netdev=secondarynet%s,mac=52:55:00:d1:56:%s;", nodeQemuMonitorArgs, netSuffix, netSuffix, qemuNetDevice, netSuffix, macSuffix)
} else { //devices like virtio-net-pci doesn't support hot-plug
nodeQemuArgs = fmt.Sprintf("%s -device %s,netdev=secondarynet%s,mac=52:55:00:d1:56:%s -netdev tap,id=secondarynet%s,ifname=stap%s,script=no,downscript=no", nodeQemuArgs, qemuNetDevice, netSuffix, macSuffix, netSuffix, netSuffix)
}
}
nodeName := nodeNameFromIndex(x + 1)
nodeNum := fmt.Sprintf("%02d", x+1)
sshClient, err = libssh.NewSSHClient(sshPort, x+1, false)
if err != nil {
return err
}
if reverse {
nodeName = nodeNameFromIndex((int(nodes) - x))
nodeNum = fmt.Sprintf("%02d", (int(nodes) - x))
sshClient, err = libssh.NewSSHClient(sshPort, (int(nodes) - x), false)
if err != nil {
return err
}
}
// assign a GPU to one node
var deviceMappings []container.DeviceMapping
if gpuAddress != "" && x == int(nodes)-1 {
iommu_group, err := getPCIDeviceIOMMUGroup(gpuAddress)
if err != nil {
return err
}
vfioDevice := fmt.Sprintf("/dev/vfio/%s", iommu_group)
deviceMappings = []container.DeviceMapping{
{
PathOnHost: "/dev/vfio/vfio",
PathInContainer: "/dev/vfio/vfio",
CgroupPermissions: "mrw",
},
{
PathOnHost: vfioDevice,
PathInContainer: vfioDevice,
CgroupPermissions: "mrw",
},
}
nodeQemuArgs = fmt.Sprintf("%s -device vfio-pci,host=%s", nodeQemuArgs, gpuAddress)
}
var vmArgsNvmeDisks []string
if len(nvmeDisks) > 0 {
for i, size := range nvmeDisks {
resource.MustParse(size)
disk := fmt.Sprintf("%s-%d.img", nvmeDiskImagePrefix, i)
nodeQemuArgs = fmt.Sprintf("%s -drive file=%s,format=raw,id=NVME%d,if=none -device nvme,drive=NVME%d,serial=nvme-%d", nodeQemuArgs, disk, i, i, i)
vmArgsNvmeDisks = append(vmArgsNvmeDisks, fmt.Sprintf("--nvme-device-size %s", size))
}
}
var vmArgsSCSIDisks []string
if len(scsiDisks) > 0 {
nodeQemuArgs = fmt.Sprintf("%s -device virtio-scsi-pci,id=scsi0", nodeQemuArgs)
for i, size := range scsiDisks {
resource.MustParse(size)
disk := fmt.Sprintf("%s-%d.img", scsiDiskImagePrefix, i)
nodeQemuArgs = fmt.Sprintf("%s -drive file=%s,if=none,id=drive%d -device scsi-hd,drive=drive%d,bus=scsi0.0,channel=0,scsi-id=0,lun=%d", nodeQemuArgs, disk, i, i, i)
vmArgsSCSIDisks = append(vmArgsSCSIDisks, fmt.Sprintf("--scsi-device-size %s", size))
}
}
var vmArgsUSBDisks []string
const bus = " -device qemu-xhci,id=bus%d"
const drive = " -drive if=none,id=stick%d,format=raw,file=/usb-%d.img"
const dev = " -device usb-storage,bus=bus%d.0,drive=stick%d"
const usbSizefmt = " --usb-device-size %s"
if len(usbDisks) > 0 {
for i, size := range usbDisks {
resource.MustParse(size)
if i%2 == 0 {
nodeQemuArgs += fmt.Sprintf(bus, i/2)
}
nodeQemuArgs += fmt.Sprintf(drive, i, i)
nodeQemuArgs += fmt.Sprintf(dev, i/2, i)
vmArgsUSBDisks = append(vmArgsUSBDisks, fmt.Sprintf(usbSizefmt, size))
}
}
var vmArgsSharedDisks []string
if len(sharedDisks) > 0 {
for i, size := range sharedDisks {
pciOffset := i + 1
resource.MustParse(size)
disk := fmt.Sprintf("/shared/disk%d.img", i)
blockDev := fmt.Sprintf("-blockdev file,filename=%s,node-name=shared-disk-%d,read-only=off,cache.direct=on,cache.no-flush=off", disk, i)
device1 := fmt.Sprintf("-device pcie-root-port,id=pci.%d,bus=pcie.0", pciOffset)
device2 := fmt.Sprintf("-device virtio-blk-pci,bus=pci.%d,drive=shared-disk-%d,id=shared-disk-%d,share-rw=on,write-cache=on,werror=stop,rerror=stop", pciOffset, i, i)
nodeQemuArgs = fmt.Sprintf("%s %s %s %s", nodeQemuArgs, blockDev, device1, device2)
vmArgsSharedDisks = append(vmArgsSCSIDisks, fmt.Sprintf("--shared-device-size %s", size))
}
}
additionalArgs := []string{}
if len(nodeQemuArgs) > 0 {
additionalArgs = append(additionalArgs, "--qemu-args", shellescape.Quote(nodeQemuArgs))
}
if len(nodeQemuMonitorArgs) > 0 {
additionalArgs = append(additionalArgs, "--qemu-monitor-args", shellescape.Quote(nodeQemuMonitorArgs))
}
if hugepages2Mcount > 0 {
kernelArgs += fmt.Sprintf(" hugepagesz=2M hugepages=%d", hugepages2Mcount)
}
if hugepages1Gcount > 0 {
kernelArgs += fmt.Sprintf(" hugepagesz=1G hugepages=%d", hugepages1Gcount)
}
if fipsEnabled {
kernelArgs += " fips=1"
}
blockDev := ""
if cephEnabled {
blockDev = "--block-device /var/run/disk/blockdev.qcow2 --block-device-size 32212254720"
}
kernelArgs = strings.TrimSpace(kernelArgs)
if kernelArgs != "" {
additionalArgs = append(additionalArgs, "--additional-kernel-args", shellescape.Quote(kernelArgs))
}
vmContainerConfig := &container.Config{
Image: clusterImage,
Env: []string{
fmt.Sprintf("NODE_NUM=%s", nodeNum),
},
Cmd: []string{"/bin/bash", "-c", fmt.Sprintf("/vm.sh -n /var/run/disk/disk.qcow2 --memory %s --cpu %s --numa %s %s %s %s %s %s %s",
memory,
strconv.Itoa(int(cpu)),
strconv.Itoa(int(numa)),
blockDev,
strings.Join(vmArgsSCSIDisks, " "),
strings.Join(vmArgsNvmeDisks, " "),
strings.Join(vmArgsUSBDisks, " "),
strings.Join(vmArgsSharedDisks, " "),
strings.Join(additionalArgs, " "),
)},
}
hostConfig := &container.HostConfig{
Privileged: true,
NetworkMode: container.NetworkMode("container:" + dnsmasq.ID),
Resources: container.Resources{
Devices: deviceMappings,
},
}
if cephEnabled {
vmContainerConfig.Volumes = map[string]struct{}{
"/var/lib/rook": {},
}
}
if len(sharedDisks) > 0 {
if vmContainerConfig.Volumes == nil {
vmContainerConfig.Volumes = map[string]struct{}{}
}
vmContainerConfig.Volumes["/shared"] = struct{}{}
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
Type: mount.TypeVolume,
Source: sharedVolumeName,
Target: "/shared",
})
}
node, err := cli.ContainerCreate(ctx, vmContainerConfig, hostConfig, nil, nil, prefix+"-"+nodeName)
if err != nil {
return err
}
containers <- node.ID
if err := cli.ContainerStart(ctx, node.ID, container.StartOptions{}); err != nil {
return err
}
// Wait for vm start
success, err := docker.Exec(cli, nodeContainer(prefix, nodeName), []string{"/bin/bash", "-c", "while [ ! -f /ssh_ready ] ; do sleep 1; done"}, os.Stdout)
if err != nil {
return err
}
if !success {
return fmt.Errorf("checking for ssh.sh script for node %s failed", nodeName)
}
err = waitForVMToBeUp(cli, prefix, nodeName)
if err != nil {
return err
}
rootkey := rootkey.NewRootKey(sshClient)
if err = rootkey.Exec(); err != nil {
return err
}
sshClient, err = libssh.NewSSHClient(sshPort, x+1, true)
linuxConfigFuncs := []nodesconfig.LinuxConfigFunc{
nodesconfig.WithFipsEnabled(fipsEnabled),
nodesconfig.WithDockerProxy(dockerProxy),
nodesconfig.WithEtcdInMemory(runEtcdOnMemory),
nodesconfig.WithEtcdSize(etcdDataMountSize),
nodesconfig.WithSingleStack(singleStack),
nodesconfig.WithNoEtcdFsync(noEtcdFsync),
nodesconfig.WithEnableAudit(enableAudit),
nodesconfig.WithGpuAddress(gpuAddress),
nodesconfig.WithRealtime(realtimeSchedulingEnabled),
nodesconfig.WithPSA(psaEnabled),
nodesconfig.WithKsm(enableKsm),
nodesconfig.WithKsmPageCount(int(ksmPageCount)),
nodesconfig.WithKsmScanInterval(int(ksmScanInterval)),
nodesconfig.WithSwap(enableSwap),
nodesconfig.WithSwapiness(int(swapiness)),
nodesconfig.WithSwapSize(int(swapSize)),
nodesconfig.WithUnlimitedSwap(unlimitedSwap),
}
n := nodesconfig.NewNodeLinuxConfig(x+1, prefix, linuxConfigFuncs)
if err = provisionNode(sshClient, n); err != nil {
return err
}
go func(id string) {
cli.ContainerWait(ctx, id, container.WaitConditionNotRunning)
wg.Done()
}(node.ID)
}
sshClient, err := libssh.NewSSHClient(sshPort, 1, true)
if err != nil {
return err
}
k8sConfs := []nodesconfig.K8sConfigFunc{
nodesconfig.WithCeph(cephEnabled),
nodesconfig.WithPrometheus(prometheusEnabled),
nodesconfig.WithAlertmanager(prometheusAlertmanagerEnabled),
nodesconfig.WithGrafana(grafanaEnabled),
nodesconfig.WithIstio(istioEnabled),
nodesconfig.WithNfsCsi(nfsCsiEnabled),
nodesconfig.WithCnao(cnaoEnabled),
nodesconfig.WithCNAOSkipCR(cnaoSkipCR),
nodesconfig.WithMultus(deployMultus),
nodesconfig.WithCdi(deployCdi),
nodesconfig.WithCdiVersion(cdiVersion),
nodesconfig.WithAAQ(deployAaq),
nodesconfig.WithAAQVersion(aaqVersion),
}
n := nodesconfig.NewNodeK8sConfig(k8sConfs)
kubeConfFile, err := os.Create(".kubeconfig")
if err != nil {
return err
}
err = sshClient.CopyRemoteFile("/etc/kubernetes/admin.conf", kubeConfFile)
if err != nil {
return err
}
config, err := k8s.NewConfig(".kubeconfig", apiServerPort)
if err != nil {
return err
}
k8sClient, err := k8s.NewDynamicClient(config)
if err != nil {
return err
}
if err = provisionK8sOptions(sshClient, k8sClient, n, prefix); err != nil {
return err
}
// If background flag was specified, we don't want to clean up if we reach that state
if !background {
wg.Wait()
stop <- fmt.Errorf("Done. please clean up")
}
return nil
}
func provisionK8sOptions(sshClient libssh.Client, k8sClient k8s.K8sDynamicClient, n *nodesconfig.NodeK8sConfig, k8sVersion string) error {
opts := []opts.Opt{}
if n.Ceph {
cephOpt := rookceph.NewCephOpt(k8sClient, sshClient)
opts = append(opts, cephOpt)
}
if n.NfsCsi {
nfsCsiOpt := nfscsi.NewNfsCsiOpt(k8sClient)
opts = append(opts, nfsCsiOpt)
}
if n.Multus {
multusOpt := multus.NewMultusOpt(k8sClient, sshClient)
opts = append(opts, multusOpt)
}
if n.CNAO {
cnaoOpt := cnao.NewCnaoOpt(k8sClient, sshClient, n.Multus, n.CNAOSkipCR)
opts = append(opts, cnaoOpt)
}
if n.Istio {
istioOpt := istio.NewIstioOpt(sshClient, k8sClient, n.CNAO)
opts = append(opts, istioOpt)
}
if n.Prometheus {
prometheusOpt := prometheus.NewPrometheusOpt(k8sClient, n.Grafana, n.Alertmanager)
opts = append(opts, prometheusOpt)
}
if n.CDI {
cdi := cdi.NewCdiOpt(k8sClient, sshClient, n.CDIVersion)
opts = append(opts, cdi)
}
if n.AAQ {
if k8sVersion == "k8s-1.30" {
aaq := aaq.NewAaqOpt(k8sClient, sshClient, n.CDIVersion)
opts = append(opts, aaq)
} else {
logrus.Info("AAQ was requested but k8s version is not k8s-1.30, skipping")
}
}
for _, opt := range opts {
if err := opt.Exec(); err != nil {
return err
}
}
return nil
}
func provisionNode(sshClient libssh.Client, n *nodesconfig.NodeLinuxConfig) error {
opts := []opts.Opt{}
nodeName := nodeNameFromIndex(n.NodeIdx)
if n.FipsEnabled {
for _, cmd := range []string{"sudo fips-mode-setup --enable", "sudo reboot"} {
if err := sshClient.Command(cmd); err != nil {
return fmt.Errorf("Starting fips mode failed: %s", err)
}
}
err := waitForVMToBeUp(cli, n.K8sVersion, nodeName)
if err != nil {
return err
}
}
if n.DockerProxy != "" {
//if dockerProxy has value, generate a shell script`/script/docker-proxy.sh` which can be applied to set proxy settings
dp := dockerproxy.NewDockerProxyOpt(sshClient, n.DockerProxy)
opts = append(opts, dp)
}
if n.EtcdInMemory {
logrus.Infof("Creating in-memory mount for etcd data on node %s", nodeName)
etcdinmem := etcdinmemory.NewEtcdInMemOpt(sshClient, n.EtcdSize)
opts = append(opts, etcdinmem)
}
if n.Realtime {
realtimeOpt := realtime.NewRealtimeOpt(sshClient)
opts = append(opts, realtimeOpt)
}
// sound cards are not supported on s390x.
if runtime.GOARCH != "s390x" {
for _, s := range soundcardPCIIDs {
// move the VM sound cards to a vfio-pci driver to prepare for assignment
bvfio := bindvfio.NewBindVfioOpt(sshClient, s)
opts = append(opts, bvfio)
}
}
if n.EnableAudit {
if err := sshClient.Command(fmt.Sprintf("touch /home/%s/enable_audit", libssh.GetUserByArchitecture(runtime.GOARCH))); err != nil {
return fmt.Errorf("provisioning node %d failed (setting enableAudit phase): %s", n.NodeIdx, err)
}
}
if n.PSA {
psaOpt := psa.NewPsaOpt(sshClient)
opts = append(opts, psaOpt)
}
if n.NodeIdx == 1 {
n := node01.NewNode01Provisioner(sshClient, n.SingleStack, n.NoEtcdFsync)
opts = append(opts, n)
} else {