-
-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathcluster.go
1273 lines (1108 loc) · 44.2 KB
/
cluster.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
/*
Copyright © 2020-2023 The k3d Author(s)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package client
import (
"bytes"
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"sort"
"strconv"
"strings"
"time"
"github.com/docker/go-connections/nat"
"github.com/imdario/mergo"
copystruct "github.com/mitchellh/copystructure"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"k8s.io/utils/strings/slices"
"sigs.k8s.io/yaml"
wharfie "github.com/rancher/wharfie/pkg/registries"
"github.com/k3d-io/k3d/v5/pkg/actions"
config "github.com/k3d-io/k3d/v5/pkg/config/v1alpha5"
l "github.com/k3d-io/k3d/v5/pkg/logger"
k3drt "github.com/k3d-io/k3d/v5/pkg/runtimes"
runtimeErr "github.com/k3d-io/k3d/v5/pkg/runtimes/errors"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"github.com/k3d-io/k3d/v5/pkg/types/k3s"
"github.com/k3d-io/k3d/v5/pkg/util"
goyaml "gopkg.in/yaml.v2"
)
// ClusterRun orchestrates the steps of cluster creation, configuration and starting
func ClusterRun(ctx context.Context, runtime k3drt.Runtime, clusterConfig *config.ClusterConfig) error {
/*
* Step 0: (Infrastructure) Preparation
*/
if err := ClusterPrep(ctx, runtime, clusterConfig); err != nil {
return fmt.Errorf("Failed Cluster Preparation: %+v", err)
}
// Create tools-node for later steps
g, _ := errgroup.WithContext(ctx)
g.Go(func() error {
_, err := EnsureToolsNode(ctx, runtime, &clusterConfig.Cluster)
return err
})
/*
* Step 1: Create Containers
*/
if err := ClusterCreate(ctx, runtime, &clusterConfig.Cluster, &clusterConfig.ClusterCreateOpts); err != nil {
return fmt.Errorf("failed Cluster Creation: %+v", err)
}
// Wait for tools node to be available
if err := g.Wait(); err != nil {
return fmt.Errorf("failed to ensure tools node: %w", err)
}
/*
* Step 2: Pre-Start Configuration
*/
// Gather Environment information, e.g. the host gateway address
envInfo, err := GatherEnvironmentInfo(ctx, runtime, &clusterConfig.Cluster)
if err != nil {
return fmt.Errorf("failed to gather environment information used for cluster creation: %w", err)
}
/*
* Step 3: Start Containers
*/
if err := ClusterStart(ctx, runtime, &clusterConfig.Cluster, k3d.ClusterStartOpts{
WaitForServer: clusterConfig.ClusterCreateOpts.WaitForServer,
Timeout: clusterConfig.ClusterCreateOpts.Timeout, // TODO: here we should consider the time used so far
NodeHooks: clusterConfig.ClusterCreateOpts.NodeHooks,
EnvironmentInfo: envInfo,
Intent: k3d.IntentClusterCreate,
HostAliases: clusterConfig.ClusterCreateOpts.HostAliases,
}); err != nil {
return fmt.Errorf("Failed Cluster Start: %+v", err)
}
/*
* Post-Start Configuration
*/
/**********************************
* Additional Cluster Preparation *
**********************************/
// create the registry hosting configmap
if len(clusterConfig.ClusterCreateOpts.Registries.Use) > 0 {
if err := prepCreateLocalRegistryHostingConfigMap(ctx, runtime, &clusterConfig.Cluster); err != nil {
l.Log().Warnf("Failed to create LocalRegistryHosting ConfigMap: %+v", err)
}
}
return nil
}
// ClusterPrep takes care of the steps required before creating/starting the cluster containers
func ClusterPrep(ctx context.Context, runtime k3drt.Runtime, clusterConfig *config.ClusterConfig) error {
/*
* Set up contexts
* Used for (early) termination (across API boundaries)
*/
clusterPrepCtx := ctx
if clusterConfig.ClusterCreateOpts.Timeout > 0*time.Second {
var cancelClusterPrepCtx context.CancelFunc
clusterPrepCtx, cancelClusterPrepCtx = context.WithTimeout(ctx, clusterConfig.ClusterCreateOpts.Timeout)
defer cancelClusterPrepCtx()
}
/*
* Step 0: Pre-Pull Images
*/
// TODO: ClusterPrep: add image pre-pulling step
/*
* Step 1: Network
*/
if err := ClusterPrepNetwork(clusterPrepCtx, runtime, &clusterConfig.Cluster, &clusterConfig.ClusterCreateOpts); err != nil {
return fmt.Errorf("Failed Network Preparation: %+v", err)
}
/*
* Step 2: Volume(s)
*/
if !clusterConfig.ClusterCreateOpts.DisableImageVolume {
if err := ClusterPrepImageVolume(ctx, runtime, &clusterConfig.Cluster, &clusterConfig.ClusterCreateOpts); err != nil {
return fmt.Errorf("Failed Image Volume Preparation: %+v", err)
}
}
/*
* Step 3: Registries
*/
// Ensure referenced registries
for _, reg := range clusterConfig.ClusterCreateOpts.Registries.Use {
l.Log().Debugf("Trying to find registry %s", reg.Host)
regNode, err := runtime.GetNode(ctx, &k3d.Node{Name: reg.Host})
if err != nil {
return fmt.Errorf("Failed to find registry node '%s': %+v", reg.Host, err)
}
regFromNode, err := RegistryFromNode(regNode)
if err != nil {
return fmt.Errorf("failed to translate node to registry spec: %w", err)
}
*reg = *regFromNode
}
// Create managed registry bound to this cluster
if clusterConfig.ClusterCreateOpts.Registries.Create != nil {
registryNode, err := RegistryCreate(ctx, runtime, clusterConfig.ClusterCreateOpts.Registries.Create)
if err != nil {
return fmt.Errorf("Failed to create registry: %+v", err)
}
clusterConfig.Cluster.Nodes = append(clusterConfig.Cluster.Nodes, registryNode)
clusterConfig.ClusterCreateOpts.Registries.Use = append(clusterConfig.ClusterCreateOpts.Registries.Use, clusterConfig.ClusterCreateOpts.Registries.Create)
}
// Use existing registries (including the new one, if created)
l.Log().Tracef("Using Registries: %+v", clusterConfig.ClusterCreateOpts.Registries.Use)
var registryConfig *wharfie.Registry
if len(clusterConfig.ClusterCreateOpts.Registries.Use) > 0 {
// ensure that all selected registries exist and connect them to the cluster network
for _, externalReg := range clusterConfig.ClusterCreateOpts.Registries.Use {
regNode, err := runtime.GetNode(ctx, &k3d.Node{Name: externalReg.Host})
if err != nil {
return fmt.Errorf("Failed to find registry node '%s': %+v", externalReg.Host, err)
}
if err := RegistryConnectNetworks(ctx, runtime, regNode, []string{clusterConfig.Cluster.Network.Name}); err != nil {
return fmt.Errorf("Failed to connect registry node '%s' to cluster network: %+v", regNode.Name, err)
}
}
// generate the registries.yaml
regConf, err := RegistryGenerateK3sConfig(ctx, clusterConfig.ClusterCreateOpts.Registries.Use)
if err != nil {
return fmt.Errorf("Failed to generate registry config file for k3s: %+v", err)
}
// generate the LocalRegistryHosting configmap
regCm, err := RegistryGenerateLocalRegistryHostingConfigMapYAML(ctx, runtime, clusterConfig.ClusterCreateOpts.Registries.Use)
if err != nil {
return fmt.Errorf("Failed to generate LocalRegistryHosting configmap: %+v", err)
}
l.Log().Tracef("Writing LocalRegistryHosting YAML:\n%s", string(regCm))
clusterConfig.ClusterCreateOpts.NodeHooks = append(clusterConfig.ClusterCreateOpts.NodeHooks, k3d.NodeHook{
Stage: k3d.LifecycleStagePreStart,
Action: actions.WriteFileAction{
Runtime: runtime,
Content: regCm,
Dest: k3d.DefaultLocalRegistryHostingConfigmapTempPath,
Mode: 0644,
Description: "Write LocalRegistryHosting Configmap",
},
})
registryConfig = regConf
}
// merge with pre-existing, referenced registries.yaml
if clusterConfig.ClusterCreateOpts.Registries.Config != nil {
if registryConfig != nil {
if err := RegistryMergeConfig(ctx, registryConfig, clusterConfig.ClusterCreateOpts.Registries.Config); err != nil {
return err
}
l.Log().Tracef("Merged registry config: %+v", registryConfig)
} else {
registryConfig = clusterConfig.ClusterCreateOpts.Registries.Config
}
}
if registryConfig != nil {
regConfBytes, err := goyaml.Marshal(®istryConfig)
if err != nil {
return fmt.Errorf("Failed to marshal registry configuration: %+v", err)
}
clusterConfig.ClusterCreateOpts.NodeHooks = append(clusterConfig.ClusterCreateOpts.NodeHooks, k3d.NodeHook{
Stage: k3d.LifecycleStagePreStart,
Action: actions.WriteFileAction{
Runtime: runtime,
Content: regConfBytes,
Dest: k3d.DefaultRegistriesFilePath,
Mode: 0644,
Description: "Write Registry Configuration",
},
})
}
/*
* Step 4: Files
*/
for id, node := range clusterConfig.Nodes {
for _, nodefile := range node.Files {
clusterConfig.Nodes[id].HookActions = append(clusterConfig.Nodes[id].HookActions, k3d.NodeHook{
Stage: k3d.LifecycleStagePreStart,
Action: actions.WriteFileAction{
Runtime: runtime,
Content: nodefile.Content,
Dest: nodefile.Destination,
Mode: 0644,
Description: nodefile.Description,
},
})
}
}
return nil
}
// ClusterPrepNetwork creates a new cluster network, if needed or sets everything up to re-use an existing network
func ClusterPrepNetwork(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Cluster, clusterCreateOpts *k3d.ClusterCreateOpts) error {
l.Log().Infoln("Prep: Network")
// error out if external cluster network should be used but no name was set
if cluster.Network.Name == "" && cluster.Network.External {
return fmt.Errorf("Failed to use external network because no name was specified")
}
if cluster.Network.Name != "" && cluster.Network.External && cluster.Network.IPAM.IPPrefix.IsValid() {
return fmt.Errorf("cannot specify subnet for exiting network")
}
// generate cluster network name, if not set
if cluster.Network.Name == "" && !cluster.Network.External {
cluster.Network.Name = fmt.Sprintf("%s-%s", k3d.DefaultObjectNamePrefix, cluster.Name)
}
// handle hostnetwork
if cluster.Network.Name == "host" {
if len(cluster.Nodes) > 1 {
return fmt.Errorf("only one server node supported when using host network")
}
}
// create cluster network or use an existing one
network, networkExists, err := runtime.CreateNetworkIfNotPresent(ctx, &cluster.Network)
if err != nil {
return fmt.Errorf("failed to create cluster network: %w", err)
}
cluster.Network = *network
clusterCreateOpts.GlobalLabels[k3d.LabelNetworkID] = network.ID
clusterCreateOpts.GlobalLabels[k3d.LabelNetwork] = cluster.Network.Name
clusterCreateOpts.GlobalLabels[k3d.LabelNetworkIPRange] = cluster.Network.IPAM.IPPrefix.String()
clusterCreateOpts.GlobalLabels[k3d.LabelNetworkExternal] = strconv.FormatBool(cluster.Network.External)
if networkExists {
l.Log().Infof("Re-using existing network '%s' (%s)", network.Name, network.ID)
clusterCreateOpts.GlobalLabels[k3d.LabelNetworkExternal] = "true" // if the network wasn't created, we say that it's managed externally (important for cluster deletion)
}
// just reserve some IPs for k3d (e.g. k3d-tools container), so we don't try to use them again
if cluster.Network.IPAM.Managed {
reservedIP, err := GetIP(ctx, runtime, &cluster.Network)
if err != nil {
return fmt.Errorf("error reserving IP in new cluster network %s", network.Name)
}
cluster.Network.IPAM.IPsUsed = append(cluster.Network.IPAM.IPsUsed, reservedIP)
}
return nil
}
func ClusterPrepImageVolume(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Cluster, clusterCreateOpts *k3d.ClusterCreateOpts) error {
/*
* Cluster-Wide volumes
* - image volume (for importing images)
*/
imageVolumeName := fmt.Sprintf("%s-%s-images", k3d.DefaultObjectNamePrefix, cluster.Name)
if err := runtime.CreateVolume(ctx, imageVolumeName, map[string]string{k3d.LabelClusterName: cluster.Name}); err != nil {
return fmt.Errorf("failed to create image volume '%s' for cluster '%s': %w", imageVolumeName, cluster.Name, err)
}
l.Log().Infof("Created image volume %s", imageVolumeName)
clusterCreateOpts.GlobalLabels[k3d.LabelImageVolume] = imageVolumeName
cluster.ImageVolume = imageVolumeName
cluster.Volumes = append(cluster.Volumes, imageVolumeName)
// attach volume to nodes
for _, node := range cluster.Nodes {
node.Volumes = append(node.Volumes, fmt.Sprintf("%s:%s", imageVolumeName, k3d.DefaultImageVolumeMountPath))
}
return nil
}
// ClusterCreate creates a new cluster consisting of
// - some containerized k3s nodes
// - a docker network
func ClusterCreate(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Cluster, clusterCreateOpts *k3d.ClusterCreateOpts) error {
l.Log().Tracef(`
===== Creating Cluster =====
Runtime:
%+v
Cluster:
%+v
ClusterCreatOpts:
%+v
============================
`, runtime, cluster, clusterCreateOpts)
/*
* Set up contexts
* Used for (early) termination (across API boundaries)
*/
clusterCreateCtx := ctx
if clusterCreateOpts.Timeout > 0*time.Second {
var cancelClusterCreateCtx context.CancelFunc
clusterCreateCtx, cancelClusterCreateCtx = context.WithTimeout(ctx, clusterCreateOpts.Timeout)
defer cancelClusterCreateCtx()
}
/*
* Docker Machine Special Configuration
*/
if cluster.KubeAPI.Host == k3d.DefaultAPIHost && runtime == k3drt.Docker {
// If the runtime is docker, attempt to use the docker host
if runtime == k3drt.Docker {
dockerHost := runtime.GetHost()
if dockerHost != "" {
dockerHost = strings.Split(dockerHost, ":")[0] // remove the port
l.Log().Tracef("Using docker host %s", dockerHost)
cluster.KubeAPI.Host = dockerHost
}
}
}
/*
* Cluster Token
*/
if cluster.Token == "" {
cluster.Token = GenerateClusterToken()
}
clusterCreateOpts.GlobalLabels[k3d.LabelClusterToken] = cluster.Token
/*
* Extra Labels
*/
if len(clusterCreateOpts.HostAliases) > 0 {
hostAliasesJSON, err := json.Marshal(clusterCreateOpts.HostAliases)
if err != nil {
return fmt.Errorf("error marshalling hostaliases: %w", err)
}
clusterCreateOpts.GlobalLabels[k3d.LabelClusterStartHostAliases] = string(hostAliasesJSON)
}
/*
* Nodes
*/
clusterCreateOpts.GlobalLabels[k3d.LabelClusterName] = cluster.Name
// Add serverlb url to be used as tls-san value
// This is used to avoid a fatal error on registering server nodes
// using loadbalancer
if clusterCreateOpts.DisableLoadBalancer {
clusterCreateOpts.GlobalLabels[k3d.LabelServerLoadBalancer] = ""
} else {
clusterCreateOpts.GlobalLabels[k3d.LabelServerLoadBalancer] = fmt.Sprintf("%s-%s-serverlb", k3d.DefaultObjectNamePrefix, cluster.Name)
}
// agent defaults (per cluster)
// connection url is always the name of the first server node (index 0) // TODO: change this to the server loadbalancer
connectionURL := fmt.Sprintf("https://%s:%s", GenerateNodeName(cluster.Name, k3d.ServerRole, 0), k3d.DefaultAPIPort)
clusterCreateOpts.GlobalLabels[k3d.LabelClusterURL] = connectionURL
clusterCreateOpts.GlobalEnv = append(clusterCreateOpts.GlobalEnv, fmt.Sprintf("%s=%s", k3s.EnvClusterToken, cluster.Token))
nodeSetup := func(node *k3d.Node) error {
// cluster specific settings
if node.RuntimeLabels == nil {
node.RuntimeLabels = make(map[string]string) // TODO: maybe create an init function?
}
// ensure global labels
for k, v := range clusterCreateOpts.GlobalLabels {
node.RuntimeLabels[k] = v
}
// ensure global env
node.Env = append(node.Env, clusterCreateOpts.GlobalEnv...)
// node role specific settings
if node.Role == k3d.ServerRole {
if cluster.Network.IPAM.Managed {
ip, err := GetIP(ctx, runtime, &cluster.Network)
if err != nil {
return fmt.Errorf("failed to find free IP in network %s: %w", cluster.Network.Name, err)
}
cluster.Network.IPAM.IPsUsed = append(cluster.Network.IPAM.IPsUsed, ip) // make sure that we're not reusing the same IP next time
node.IP.Static = true
node.IP.IP = ip
node.RuntimeLabels[k3d.LabelNodeStaticIP] = ip.String()
}
node.ServerOpts.KubeAPI = cluster.KubeAPI
// the cluster has an init server node, but its not this one, so connect it to the init node
if cluster.InitNode != nil && !node.ServerOpts.IsInit {
node.Env = append(node.Env, fmt.Sprintf("%s=%s", k3s.EnvClusterConnectURL, connectionURL))
node.RuntimeLabels[k3d.LabelServerIsInit] = "false" // set label, that this server node is not the init server
}
} else if node.Role == k3d.AgentRole {
node.Env = append(node.Env, fmt.Sprintf("%s=%s", k3s.EnvClusterConnectURL, connectionURL))
}
node.Networks = []string{cluster.Network.Name}
node.Restart = true
node.GPURequest = clusterCreateOpts.GPURequest
// create node
l.Log().Infof("Creating node '%s'", node.Name)
if err := NodeCreate(clusterCreateCtx, runtime, node, k3d.NodeCreateOpts{}); err != nil {
return fmt.Errorf("failed to create node: %w", err)
}
l.Log().Debugf("Created node '%s'", node.Name)
return nil
}
// used for node suffices
serverCount := 0
// create init node first
if cluster.InitNode != nil {
l.Log().Infoln("Creating initializing server node")
cluster.InitNode.Args = append(cluster.InitNode.Args, "--cluster-init")
if cluster.InitNode.RuntimeLabels == nil {
cluster.InitNode.RuntimeLabels = map[string]string{}
}
cluster.InitNode.RuntimeLabels[k3d.LabelServerIsInit] = "true" // set label, that this server node is the init server
// in case the LoadBalancer was disabled, expose the API Port on the initializing server node
if clusterCreateOpts.DisableLoadBalancer {
if cluster.InitNode.Ports == nil {
cluster.InitNode.Ports = nat.PortMap{}
}
cluster.InitNode.Ports[k3d.DefaultAPIPort] = []nat.PortBinding{cluster.KubeAPI.Binding}
}
if err := nodeSetup(cluster.InitNode); err != nil {
return fmt.Errorf("failed init node setup: %w", err)
}
serverCount++
}
// create all other nodes, but skip the init node
for _, node := range cluster.Nodes {
if node.Role == k3d.ServerRole {
// skip the init node here
if node == cluster.InitNode {
continue
} else if serverCount == 0 && clusterCreateOpts.DisableLoadBalancer {
// if this is the first server node and the server loadbalancer is disabled, expose the API Port on this server node
if node.Ports == nil {
node.Ports = nat.PortMap{}
}
node.Ports[k3d.DefaultAPIPort] = []nat.PortBinding{cluster.KubeAPI.Binding}
}
time.Sleep(1 * time.Second) // FIXME: arbitrary wait for one second to avoid race conditions of servers registering
serverCount++
}
if node.Role == k3d.ServerRole || node.Role == k3d.AgentRole {
if err := nodeSetup(node); err != nil {
return fmt.Errorf("failed setup of server/agent node %s: %w", node.Name, err)
}
}
}
// WARN, if there are exactly two server nodes: that means we're using etcd, but don't have fault tolerance
if serverCount == 2 {
l.Log().Warnln("You're creating 2 server nodes: Please consider creating at least 3 to achieve etcd quorum & fault tolerance")
}
/*
* Auxiliary Containers
*/
// *** ServerLoadBalancer ***
if !clusterCreateOpts.DisableLoadBalancer {
if cluster.ServerLoadBalancer == nil {
l.Log().Infof("No loadbalancer specified, creating a default one...")
cluster.ServerLoadBalancer = k3d.NewLoadbalancer()
var err error
cluster.ServerLoadBalancer.Node, err = LoadbalancerPrepare(ctx, runtime, cluster, &k3d.LoadbalancerCreateOpts{Labels: clusterCreateOpts.GlobalLabels})
if err != nil {
return fmt.Errorf("failed to prepare loadbalancer: %w", err)
}
cluster.Nodes = append(cluster.Nodes, cluster.ServerLoadBalancer.Node) // append lbNode to list of cluster nodes, so it will be considered during rollback
}
if len(cluster.ServerLoadBalancer.Config.Ports) == 0 {
lbConfig, err := LoadbalancerGenerateConfig(cluster)
if err != nil {
return fmt.Errorf("error generating loadbalancer config: %v", err)
}
cluster.ServerLoadBalancer.Config = &lbConfig
}
// ensure labels
cluster.ServerLoadBalancer.Node.FillRuntimeLabels()
for k, v := range clusterCreateOpts.GlobalLabels {
cluster.ServerLoadBalancer.Node.RuntimeLabels[k] = v
}
// prepare to write config to lb container
configyaml, err := yaml.Marshal(cluster.ServerLoadBalancer.Config)
if err != nil {
return fmt.Errorf("failed to marshal loadbalancer config: %w", err)
}
writeLbConfigAction := k3d.NodeHook{
Stage: k3d.LifecycleStagePreStart,
Action: actions.WriteFileAction{
Runtime: runtime,
Dest: k3d.DefaultLoadbalancerConfigPath,
Mode: 0744,
Content: configyaml,
Description: "Write Loadbalancer Configuration",
},
}
cluster.ServerLoadBalancer.Node.HookActions = append(cluster.ServerLoadBalancer.Node.HookActions, writeLbConfigAction)
cluster.ServerLoadBalancer.Node.Restart = true
l.Log().Infof("Creating LoadBalancer '%s'", cluster.ServerLoadBalancer.Node.Name)
if err := NodeCreate(ctx, runtime, cluster.ServerLoadBalancer.Node, k3d.NodeCreateOpts{}); err != nil {
return fmt.Errorf("error creating loadbalancer: %v", err)
}
l.Log().Debugf("Created loadbalancer '%s'", cluster.ServerLoadBalancer.Node.Name)
}
return nil
}
// ClusterDelete deletes an existing cluster
func ClusterDelete(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Cluster, opts k3d.ClusterDeleteOpts) error {
l.Log().Infof("Deleting cluster '%s'", cluster.Name)
cluster, err := ClusterGet(ctx, runtime, cluster)
if err != nil {
return fmt.Errorf("failed to get cluster: %w", err)
}
l.Log().Debugf("Cluster Details: %+v", cluster)
failed := 0
for _, node := range cluster.Nodes {
// registry: only delete, if not connected to other networks
if node.Role == k3d.RegistryRole && !opts.SkipRegistryCheck {
l.Log().Tracef("Registry Node has %d networks: %+v", len(node.Networks), node)
// check if node is connected to other networks, that are not
// - the cluster network
// - default docker networks
// -> if so, disconnect it from the cluster network and continue
connectedToOtherNet := false
for _, net := range node.Networks {
if net == cluster.Network.Name {
continue
}
if net == k3d.DefaultRuntimeNetwork || net == "host" {
continue
}
l.Log().Tracef("net: %s", net)
connectedToOtherNet = true
break
}
if connectedToOtherNet {
l.Log().Infof("Registry %s is also connected to other (non-default) networks (%+v), not deleting it...", node.Name, node.Networks)
if err := runtime.DisconnectNodeFromNetwork(ctx, node, cluster.Network.Name); err != nil {
l.Log().Warnf("Failed to disconnect registry %s from cluster network %s", node.Name, cluster.Network.Name)
}
continue
}
}
if err := NodeDelete(ctx, runtime, node, k3d.NodeDeleteOpts{SkipLBUpdate: true}); err != nil {
l.Log().Warningf("Failed to delete node '%s': Try to delete it manually", node.Name)
failed++
continue
}
}
// Delete the cluster network, if it was created for/by this cluster (and if it's not in use anymore)
if cluster.Network.Name != "" {
if !cluster.Network.External {
l.Log().Infof("Deleting cluster network '%s'", cluster.Network.Name)
if err := runtime.DeleteNetwork(ctx, cluster.Network.Name); err != nil {
if errors.Is(err, runtimeErr.ErrRuntimeNetworkNotEmpty) { // there are still containers connected to that network
connectedNodes, err := runtime.GetNodesInNetwork(ctx, cluster.Network.Name) // check, if there are any k3d nodes connected to the cluster
if err != nil {
l.Log().Warningf("Failed to check cluster network for connected nodes: %+v", err)
}
if len(connectedNodes) > 0 { // there are still k3d-managed containers (aka nodes) connected to the network
connectedRegistryNodes := util.FilterNodesByRole(connectedNodes, k3d.RegistryRole)
if len(connectedRegistryNodes) == len(connectedNodes) { // only registry node(s) left in the network
for _, node := range connectedRegistryNodes {
l.Log().Debugf("Disconnecting registry node %s from the network...", node.Name)
if err := runtime.DisconnectNodeFromNetwork(ctx, node, cluster.Network.Name); err != nil {
l.Log().Warnf("Failed to disconnect registry %s from network %s", node.Name, cluster.Network.Name)
} else {
if err := runtime.DeleteNetwork(ctx, cluster.Network.Name); err != nil {
l.Log().Warningf("Failed to delete cluster network, even after disconnecting registry node(s): %+v", err)
}
}
}
} else { // besides the registry node(s), there are still other nodes... maybe they still need a registry
l.Log().Debugf("There are some non-registry nodes left in the network")
}
} else {
l.Log().Warningf("Failed to delete cluster network '%s' because it's still in use: is there another cluster using it?", cluster.Network.Name)
}
} else {
l.Log().Warningf("Failed to delete cluster network '%s': '%+v'", cluster.Network.Name, err)
}
}
} else if cluster.Network.External {
l.Log().Debugf("Skip deletion of cluster network '%s' because it's managed externally", cluster.Network.Name)
}
}
// delete managed volumes attached to this cluster
l.Log().Infof("Deleting %d attached volumes...", len(cluster.Volumes))
for _, vol := range cluster.Volumes {
l.Log().Debugf("Deleting volume %s...", vol)
if err := runtime.DeleteVolume(ctx, vol); err != nil {
l.Log().Warningf("Failed to delete volume '%s' of cluster '%s': %v -> Try to delete it manually", cluster.ImageVolume, cluster.Name, err)
}
}
// return error if we failed to delete a node
if failed > 0 {
return fmt.Errorf("Failed to delete %d nodes: Try to delete them manually", failed)
}
return nil
}
// ClusterList returns a list of all existing clusters
func ClusterList(ctx context.Context, runtime k3drt.Runtime) ([]*k3d.Cluster, error) {
l.Log().Traceln("Listing Clusters...")
nodes, err := runtime.GetNodesByLabel(ctx, k3d.DefaultRuntimeLabels)
if err != nil {
return nil, fmt.Errorf("runtime failed to list nodes: %w", err)
}
l.Log().Debugf("Found %d nodes", len(nodes))
if l.Log().GetLevel() == logrus.TraceLevel {
for _, node := range nodes {
l.Log().Tracef("Found node %s of role %s", node.Name, node.Role)
}
}
nodes = NodeFilterByRoles(nodes, k3d.ClusterInternalNodeRoles, k3d.ClusterExternalNodeRoles)
l.Log().Tracef("Found %d cluster-internal nodes", len(nodes))
if l.Log().GetLevel() == logrus.TraceLevel {
for _, node := range nodes {
l.Log().Tracef("Found cluster-internal node %s of role %s belonging to cluster %s", node.Name, node.Role, node.RuntimeLabels[k3d.LabelClusterName])
}
}
clusters := []*k3d.Cluster{}
// for each node, check, if we can add it to a cluster or add the cluster if it doesn't exist yet
for _, node := range nodes {
clusterExists := false
for _, cluster := range clusters {
if node.RuntimeLabels[k3d.LabelClusterName] == cluster.Name { // TODO: handle case, where this label doesn't exist
cluster.Nodes = append(cluster.Nodes, node)
clusterExists = true
break
}
}
// cluster is not in the list yet, so we add it with the current node as its first member
if !clusterExists {
clusters = append(clusters, &k3d.Cluster{
Name: node.RuntimeLabels[k3d.LabelClusterName],
Nodes: []*k3d.Node{node},
})
}
}
// enrich cluster structs with label values
for _, cluster := range clusters {
if err := populateClusterFieldsFromLabels(cluster); err != nil {
l.Log().Warnf("Failed to populate cluster fields from node label values for cluster '%s'", cluster.Name)
l.Log().Warnln(err)
}
}
l.Log().Debugf("Found %d clusters", len(clusters))
return clusters, nil
}
// populateClusterFieldsFromLabels inspects labels attached to nodes and translates them to struct fields
func populateClusterFieldsFromLabels(cluster *k3d.Cluster) error {
networkExternalSet := false
for _, node := range cluster.Nodes {
// get the name of the cluster network
if cluster.Network.Name == "" {
if networkName, ok := node.RuntimeLabels[k3d.LabelNetwork]; ok {
cluster.Network.Name = networkName
}
}
// check if the network is external
// since the struct value is a bool, initialized as false, we cannot check if it's unset
if !cluster.Network.External && !networkExternalSet {
if networkExternalString, ok := node.RuntimeLabels[k3d.LabelNetworkExternal]; ok {
if networkExternal, err := strconv.ParseBool(networkExternalString); err == nil {
cluster.Network.External = networkExternal
networkExternalSet = true
}
}
}
// get image volume // TODO: enable external image volumes the same way we do it with networks
if cluster.ImageVolume == "" {
if imageVolumeName, ok := node.RuntimeLabels[k3d.LabelImageVolume]; ok {
cluster.ImageVolume = imageVolumeName
}
}
// get k3s cluster's token
if cluster.Token == "" {
if token, ok := node.RuntimeLabels[k3d.LabelClusterToken]; ok {
cluster.Token = token
}
}
}
return nil
}
func GetClusterStartOptsFromLabels(cluster *k3d.Cluster) (k3d.ClusterStartOpts, error) {
clusterStartOpts := k3d.ClusterStartOpts{
HostAliases: []k3d.HostAlias{},
}
for _, node := range cluster.Nodes {
if len(clusterStartOpts.HostAliases) == 0 {
if hostAliasesJSON, ok := node.RuntimeLabels[k3d.LabelClusterStartHostAliases]; ok {
if err := json.Unmarshal([]byte(hostAliasesJSON), &clusterStartOpts.HostAliases); err != nil {
return clusterStartOpts, fmt.Errorf("error unmarshalling hostaliases JSON from node %s label: %w", node.Name, err)
}
}
}
}
return clusterStartOpts, nil
}
var ClusterGetNoNodesFoundError = errors.New("No nodes found for given cluster")
// ClusterGet returns an existing cluster with all fields and node lists populated
func ClusterGet(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Cluster) (*k3d.Cluster, error) {
// get nodes that belong to the selected cluster
nodes, err := runtime.GetNodesByLabel(ctx, map[string]string{k3d.LabelClusterName: cluster.Name})
if err != nil {
l.Log().Errorf("Failed to get nodes for cluster '%s': %v", cluster.Name, err)
}
if len(nodes) == 0 {
return nil, ClusterGetNoNodesFoundError
}
// append nodes
for _, node := range nodes {
// check if there's already a node in the struct
overwroteExisting := false
for _, existingNode := range cluster.Nodes {
// overwrite existing node
if existingNode.Name == node.Name {
if err := mergo.Merge(existingNode, node, mergo.WithOverride); err != nil {
return nil, fmt.Errorf("failed to merge node %s into cluster: %v", node.Name, err)
}
overwroteExisting = true
}
}
// no existing node overwritten: append new node
if !overwroteExisting {
cluster.Nodes = append(cluster.Nodes, node)
}
}
// Loadbalancer
if cluster.ServerLoadBalancer == nil {
for _, node := range cluster.Nodes {
if node.Role == k3d.LoadBalancerRole {
cluster.ServerLoadBalancer = &k3d.Loadbalancer{
Node: node,
}
}
}
if cluster.ServerLoadBalancer != nil && cluster.ServerLoadBalancer.Node != nil {
lbcfg, err := GetLoadbalancerConfig(ctx, runtime, cluster)
if err != nil {
l.Log().Errorf("error getting loadbalancer config from %s: %v", cluster.ServerLoadBalancer.Node.Name, err)
}
cluster.ServerLoadBalancer.Config = &lbcfg
}
}
vols, err := runtime.GetVolumesByLabel(ctx, map[string]string{k3d.LabelClusterName: cluster.Name})
if err != nil {
return nil, err
}
for _, vol := range vols {
if !slices.Contains(cluster.Volumes, vol) {
cluster.Volumes = append(cluster.Volumes, vol)
}
}
if err := populateClusterFieldsFromLabels(cluster); err != nil {
l.Log().Warnf("Failed to populate cluster fields from node labels: %v", err)
}
return cluster, nil
}
// GenerateClusterToken generates a random 20 character string
func GenerateClusterToken() string {
return util.GenerateRandomString(20)
}
func GenerateNodeName(cluster string, role k3d.Role, suffix int) string {
return fmt.Sprintf("%s-%s-%s-%d", k3d.DefaultObjectNamePrefix, cluster, role, suffix)
}
// ClusterStart starts a whole cluster (i.e. all nodes of the cluster)
func ClusterStart(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Cluster, clusterStartOpts k3d.ClusterStartOpts) error {
l.Log().Infof("Starting cluster '%s'", cluster.Name)
if clusterStartOpts.Intent == "" {
clusterStartOpts.Intent = k3d.IntentClusterStart
}
if clusterStartOpts.Timeout > 0*time.Second {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, clusterStartOpts.Timeout)
defer cancel()
}
// sort the nodes into categories
var initNode *k3d.Node
var servers []*k3d.Node
var agents []*k3d.Node
var aux []*k3d.Node
for _, n := range cluster.Nodes {
if !n.State.Running {
if n.Role == k3d.ServerRole {
if n.ServerOpts.IsInit {
initNode = n
continue
}
servers = append(servers, n)
} else if n.Role == k3d.AgentRole {
agents = append(agents, n)
} else {
aux = append(aux, n)
}
} else {
l.Log().Tracef("Node %s already running.", n.Name)
}
}
// sort list of servers for properly ordered sequential start
sort.Slice(servers, func(i, j int) bool {
return servers[i].Name < servers[j].Name
})
/*
* Init Node
*/
if initNode != nil {
l.Log().Infoln("Starting the initializing server...")
if err := NodeStart(ctx, runtime, initNode, &k3d.NodeStartOpts{
Wait: true, // always wait for the init node
NodeHooks: clusterStartOpts.NodeHooks,
ReadyLogMessage: k3d.GetReadyLogMessage(initNode, clusterStartOpts.Intent), // initNode means, that we're using etcd -> this will need quorum, so "k3s is up and running" won't happen right now
EnvironmentInfo: clusterStartOpts.EnvironmentInfo,
}); err != nil {
return fmt.Errorf("Failed to start initializing server node: %+v", err)
}
}
/*
* Server Nodes
*/
if len(servers) > 0 {
l.Log().Infoln("Starting servers...")
for _, serverNode := range servers {
if err := NodeStart(ctx, runtime, serverNode, &k3d.NodeStartOpts{
Wait: true,
NodeHooks: append(clusterStartOpts.NodeHooks, serverNode.HookActions...),
EnvironmentInfo: clusterStartOpts.EnvironmentInfo,
}); err != nil {
return fmt.Errorf("Failed to start server %s: %+v", serverNode.Name, err)
}
}
} else {
l.Log().Infoln("All servers already running.")
}
/*
* Agent Nodes
*/
if len(agents) > 0 {
agentWG, aCtx := errgroup.WithContext(ctx)
l.Log().Infoln("Starting agents...")
for _, agentNode := range agents {
currentAgentNode := agentNode
agentWG.Go(func() error {
return NodeStart(aCtx, runtime, currentAgentNode, &k3d.NodeStartOpts{
Wait: true,
NodeHooks: append(clusterStartOpts.NodeHooks, agentNode.HookActions...),
EnvironmentInfo: clusterStartOpts.EnvironmentInfo,
})
})
}
if err := agentWG.Wait(); err != nil {
return fmt.Errorf("Failed to add one or more agents: %w", err)
}
} else {
l.Log().Infoln("All agents already running.")
}