From d7e71e07d24c990cde2f72b615d2985e054bd334 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Thu, 23 Apr 2020 11:40:13 +0200 Subject: [PATCH] fix/loadImage: extract labels from master/worker nodes only - imageVolume couldn't be determined before in some cases, because it tried to extract the name from labels on the masterlb which doesn't have them. - now we're only trying get the label from master/worker nodes and this also adds a failover solution - also, we ignore non-worker/master nodes upon image import --- pkg/tools/tools.go | 35 ++++++++++++++++++++++++----------- tests/test_full_lifecycle.sh | 6 ++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go index 62f180c8..2ad393f0 100644 --- a/pkg/tools/tools.go +++ b/pkg/tools/tools.go @@ -45,10 +45,20 @@ func LoadImagesIntoCluster(runtime runtimes.Runtime, images []string, cluster *k return fmt.Errorf("Failed to get network for cluster '%s'", cluster.Name) } - if _, ok := cluster.Nodes[0].Labels["k3d.cluster.imageVolume"]; !ok { // TODO: add failover solution + var imageVolume string + var ok bool + for _, node := range cluster.Nodes { + if node.Role == k3d.MasterRole || node.Role == k3d.WorkerRole { + if imageVolume, ok = node.Labels["k3d.cluster.imageVolume"]; ok { + break + } + } + } + if imageVolume == "" { return fmt.Errorf("Failed to find image volume for cluster '%s'", cluster.Name) } - imageVolume := cluster.Nodes[0].Labels["k3d.cluster.imageVolume"] + + log.Debugf("Attaching to cluster's image volume '%s'", imageVolume) // create tools node to export images log.Infoln("Starting k3d-tools node...") @@ -76,15 +86,18 @@ func LoadImagesIntoCluster(runtime runtimes.Runtime, images []string, cluster *k log.Infoln("Importing images into nodes...") var importWaitgroup sync.WaitGroup for _, node := range cluster.Nodes { - importWaitgroup.Add(1) - go func(node *k3d.Node, wg *sync.WaitGroup) { - log.Infof("Importing images into node '%s'...", node.Name) - if err := runtime.ExecInNode(node, []string{"ctr", "image", "import", tarName}); err != nil { - log.Errorf("Failed to import images in node '%s'", node.Name) - log.Errorln(err) - } - wg.Done() - }(node, &importWaitgroup) + // only import image in master and worker nodes (i.e. ignoring auxiliary nodes like the master loadbalancer) + if node.Role == k3d.MasterRole || node.Role == k3d.WorkerRole { + importWaitgroup.Add(1) + go func(node *k3d.Node, wg *sync.WaitGroup) { + log.Infof("Importing images into node '%s'...", node.Name) + if err := runtime.ExecInNode(node, []string{"ctr", "image", "import", tarName}); err != nil { + log.Errorf("Failed to import images in node '%s'", node.Name) + log.Errorln(err) + } + wg.Done() + }(node, &importWaitgroup) + } } importWaitgroup.Wait() diff --git a/tests/test_full_lifecycle.sh b/tests/test_full_lifecycle.sh index 623ad682..ee424122 100755 --- a/tests/test_full_lifecycle.sh +++ b/tests/test_full_lifecycle.sh @@ -41,6 +41,12 @@ check_clusters "$clustername" || failed "error checking cluster" info "Checking that we have 2 nodes online..." check_multi_node "$clustername" 2 || failed "failed to verify number of nodes" +# 4. load an image into the cluster +info "Loading an image into the cluster..." +docker pull nginx:latest +docker tag nginx:latest nginx:local +$EXE load image nginx:local -c $clustername + # Cleanup info "Deleting cluster $clustername..."