createNode: use default role label and cmd...

...if existing node does not match target role
Fixes #254
This commit is contained in:
iwilltry42 2020-05-29 14:03:34 +02:00
parent 16f01f5fc9
commit cf2c839b27
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
3 changed files with 17 additions and 6 deletions

View File

@ -78,7 +78,6 @@ func parseCreateNodeCmd(cmd *cobra.Command, args []string) ([]*k3d.Node, *k3d.Cl
}
// --role
// TODO: createNode: for --role=master, update the nginx config and add TLS-SAN and server connection, etc.
roleStr, err := cmd.Flags().GetString("role")
if err != nil {
log.Errorln("No node role specified")
@ -112,6 +111,9 @@ func parseCreateNodeCmd(cmd *cobra.Command, args []string) ([]*k3d.Node, *k3d.Cl
Name: fmt.Sprintf("%s-%s-%d", k3d.DefaultObjectNamePrefix, args[0], i),
Role: role,
Image: image,
Labels: map[string]string{
"k3d.role": roleStr,
},
}
nodes = append(nodes, node)
}

View File

@ -43,13 +43,15 @@ func AddNodeToCluster(runtime runtimes.Runtime, node *k3d.Node, cluster *k3d.Clu
return err
}
log.Debugf("Adding node to cluster %+v", cluster)
// network
node.Network = cluster.Network.Name
// skeleton
node.Labels = map[string]string{}
if node.Labels == nil {
node.Labels = map[string]string{
"k3d.role": string(node.Role),
}
}
node.Env = []string{}
// copy labels and env vars from a similar node in the selected cluster
@ -62,7 +64,8 @@ func AddNodeToCluster(runtime runtimes.Runtime, node *k3d.Node, cluster *k3d.Clu
}
// if we didn't find a node with the same role in the cluster, just choose any other node
if chosenNode == nil {
log.Debugf("Didn't find node with role '%s' in cluster '%s'. Choosing any other node...", node.Role, cluster.Name)
log.Debugf("Didn't find node with role '%s' in cluster '%s'. Choosing any other node (and using defaults)...", node.Role, cluster.Name)
node.Cmd = k3d.DefaultRoleCmds[node.Role]
for _, existingNode := range cluster.Nodes {
if existingNode.Role != k3d.LoadBalancerRole { // any role except for the LoadBalancer role
chosenNode = existingNode
@ -77,7 +80,7 @@ func AddNodeToCluster(runtime runtimes.Runtime, node *k3d.Node, cluster *k3d.Clu
return err
}
log.Debugf("Copying configuration from existing node %+v", chosenNode)
log.Debugf("Adding node %+v \n>>> to cluster %+v\n>>> based on existing node %+v", node, cluster, chosenNode)
// merge node config of new node into existing node config
if err := mergo.MergeWithOverwrite(chosenNode, *node); err != nil {

View File

@ -67,6 +67,12 @@ var DefaultObjectLabels = map[string]string{
"app": "k3d",
}
// DefaultRoleCmds maps the node roles to their respective default commands
var DefaultRoleCmds = map[Role][]string{
MasterRole: {"server"},
WorkerRole: {"agent"},
}
// DefaultTmpfsMounts specifies tmpfs mounts that are required for all k3d nodes
var DefaultTmpfsMounts = []string{
"/run",