From dc1bbd6965699945256fa75a4a2ebdae8ad55e28 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Tue, 14 Apr 2020 17:30:12 +0200 Subject: [PATCH] manage imageVolume lifecycle --- pkg/cluster/cluster.go | 38 ++++++++++++++++++++++++-------------- pkg/tools/tools.go | 4 ++-- pkg/types/types.go | 1 + 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 209259ac..7d225627 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -88,7 +88,7 @@ func CreateCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error { return err } - extraLabels["k3d.cluster.volumes.imagevolume"] = imageVolumeName + extraLabels["k3d.cluster.imageVolume"] = imageVolumeName // attach volume to nodes for _, node := range cluster.Nodes { @@ -253,6 +253,7 @@ initNodeFinished: func DeleteCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error { log.Infof("Deleting cluster '%s'", cluster.Name) + log.Debugf("%+v", cluster) failed := 0 for _, node := range cluster.Nodes { @@ -263,27 +264,27 @@ func DeleteCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error { } } - // Delete the cluster network, if it was created for/by this cluster (and if it's not in use anymore) // TODO: does this make sense or should we always try to delete it? (Will fail anyway, if it's still in use) - if network, ok := cluster.Nodes[0].Labels["k3d.cluster.network"]; ok { - if !cluster.Network.External || cluster.Nodes[0].Labels["k3d.cluster.network.external"] == "false" { - log.Infof("Deleting cluster network '%s'", network) - if err := runtime.DeleteNetwork(network); err != nil { + // 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 { + log.Infof("Deleting cluster network '%s'", cluster.Network.Name) + if err := runtime.DeleteNetwork(cluster.Network.Name); err != nil { if strings.HasSuffix(err.Error(), "active endpoints") { - log.Warningf("Failed to delete cluster network '%s' because it's still in use: is there another cluster using it?", network) + log.Warningf("Failed to delete cluster network '%s' because it's still in use: is there another cluster using it?", cluster.Network.Name) } else { - log.Warningf("Failed to delete cluster network '%s': '%+v'", network, err) + log.Warningf("Failed to delete cluster network '%s': '%+v'", cluster.Network.Name, err) } } - } else if cluster.Network.External || cluster.Nodes[0].Labels["k3d.cluster.network.external"] == "true" { - log.Debugf("Skip deletion of cluster network '%s' because it's managed externally", network) + } else if cluster.Network.External { + log.Debugf("Skip deletion of cluster network '%s' because it's managed externally", cluster.Network.Name) } } // delete image volume - if imagevolume, ok := cluster.Nodes[0].Labels["k3d.cluster.volumes.imagevolume"]; ok { - log.Infof("Deleting image volume '%s'", imagevolume) - if err := runtime.DeleteVolume(imagevolume); err != nil { - log.Warningf("Failed to delete image volume '%s' of cluster '%s': Try to delete it manually", cluster.Name, imagevolume) + if cluster.ImageVolume != "" { + log.Infof("Deleting image volume '%s'", cluster.ImageVolume) + if err := runtime.DeleteVolume(cluster.ImageVolume); err != nil { + log.Warningf("Failed to delete image volume '%s' of cluster '%s': Try to delete it manually", cluster.ImageVolume, cluster.Name) } } @@ -355,7 +356,16 @@ func populateClusterFieldsFromLabels(cluster *k3d.Cluster) error { } } } + + // get image volume // TODO: enable external image volumes the same way we do it with networks + if cluster.ImageVolume == "" { + if imageVolumeName, ok := node.Labels["k3d.cluster.imageVolume"]; ok { + cluster.ImageVolume = imageVolumeName + } + } + } + return nil } diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go index 427e67b1..62f180c8 100644 --- a/pkg/tools/tools.go +++ b/pkg/tools/tools.go @@ -45,10 +45,10 @@ 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.volumes.imagevolume"]; !ok { // TODO: add failover solution + if _, ok := cluster.Nodes[0].Labels["k3d.cluster.imageVolume"]; !ok { // TODO: add failover solution return fmt.Errorf("Failed to find image volume for cluster '%s'", cluster.Name) } - imageVolume := cluster.Nodes[0].Labels["k3d.cluster.volumes.imagevolume"] + imageVolume := cluster.Nodes[0].Labels["k3d.cluster.imageVolume"] // create tools node to export images log.Infoln("Starting k3d-tools node...") diff --git a/pkg/types/types.go b/pkg/types/types.go index 7296de3f..1b8e4146 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -112,6 +112,7 @@ type Cluster struct { MasterLoadBalancer *ClusterLoadbalancer `yaml:"master_loadbalancer" json:"masterLoadBalancer,omitempty"` ExternalDatastore ExternalDatastore `yaml:"external_datastore" json:"externalDatastore,omitempty"` CreateClusterOpts *CreateClusterOpts `yaml:"options" json:"options,omitempty"` + ImageVolume string `yaml:"image_volume" json:"imageVolume,omitempty"` } // Node describes a k3d node