[FIX] store hostAliases in label to persist them across cluster stop/start (#1029)

pull/1032/head
Thorsten Klein 2 years ago committed by GitHub
parent 71e923d612
commit ff2931b403
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      cmd/cluster/clusterStart.go
  2. 29
      pkg/client/cluster.go
  3. 39
      pkg/types/types.go

@ -58,6 +58,17 @@ func NewCmdClusterStart() *cobra.Command {
l.Log().Fatalf("failed to gather info about cluster environment: %v", err)
}
startClusterOpts.EnvironmentInfo = envInfo
// Get pre-defined clusterStartOpts from cluster
fetchedClusterStartOpts, err := client.GetClusterStartOptsFromLabels(c)
if err != nil {
l.Log().Fatalf("failed to get cluster start opts from cluster labels: %v", err)
}
// override only a few clusterStartOpts from fetched opts
startClusterOpts.HostAliases = fetchedClusterStartOpts.HostAliases
// start the cluster
if err := client.ClusterStart(cmd.Context(), runtimes.SelectedRuntime, c, startClusterOpts); err != nil {
l.Log().Fatalln(err)
}

@ -25,6 +25,7 @@ import (
"bytes"
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"sort"
@ -377,6 +378,18 @@ ClusterCreatOpts:
}
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
*/
@ -762,6 +775,22 @@ func populateClusterFieldsFromLabels(cluster *k3d.Cluster) error {
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

@ -75,25 +75,26 @@ var ClusterExternalNodeRoles = []Role{
// List of k3d technical label name
const (
LabelClusterName string = "k3d.cluster"
LabelClusterURL string = "k3d.cluster.url"
LabelClusterToken string = "k3d.cluster.token"
LabelClusterExternal string = "k3d.cluster.external"
LabelImageVolume string = "k3d.cluster.imageVolume"
LabelNetworkExternal string = "k3d.cluster.network.external"
LabelNetwork string = "k3d.cluster.network"
LabelNetworkID string = "k3d.cluster.network.id"
LabelNetworkIPRange string = "k3d.cluster.network.iprange"
LabelRole string = "k3d.role"
LabelServerAPIPort string = "k3d.server.api.port"
LabelServerAPIHost string = "k3d.server.api.host"
LabelServerAPIHostIP string = "k3d.server.api.hostIP"
LabelServerIsInit string = "k3d.server.init"
LabelRegistryHost string = "k3d.registry.host"
LabelRegistryHostIP string = "k3d.registry.hostIP"
LabelRegistryPortExternal string = "k3s.registry.port.external"
LabelRegistryPortInternal string = "k3s.registry.port.internal"
LabelNodeStaticIP string = "k3d.node.staticIP"
LabelClusterName string = "k3d.cluster"
LabelClusterURL string = "k3d.cluster.url"
LabelClusterToken string = "k3d.cluster.token"
LabelClusterExternal string = "k3d.cluster.external"
LabelImageVolume string = "k3d.cluster.imageVolume"
LabelNetworkExternal string = "k3d.cluster.network.external"
LabelNetwork string = "k3d.cluster.network"
LabelNetworkID string = "k3d.cluster.network.id"
LabelNetworkIPRange string = "k3d.cluster.network.iprange"
LabelClusterStartHostAliases string = "k3d.cluster.start.hostaliases"
LabelRole string = "k3d.role"
LabelServerAPIPort string = "k3d.server.api.port"
LabelServerAPIHost string = "k3d.server.api.host"
LabelServerAPIHostIP string = "k3d.server.api.hostIP"
LabelServerIsInit string = "k3d.server.init"
LabelRegistryHost string = "k3d.registry.host"
LabelRegistryHostIP string = "k3d.registry.hostIP"
LabelRegistryPortExternal string = "k3s.registry.port.external"
LabelRegistryPortInternal string = "k3s.registry.port.internal"
LabelNodeStaticIP string = "k3d.node.staticIP"
)
// DoNotCopyServerFlags defines a list of commands/args that shouldn't be copied from an existing node when adding a similar node to a cluster

Loading…
Cancel
Save