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 // --role
// TODO: createNode: for --role=master, update the nginx config and add TLS-SAN and server connection, etc.
roleStr, err := cmd.Flags().GetString("role") roleStr, err := cmd.Flags().GetString("role")
if err != nil { if err != nil {
log.Errorln("No node role specified") 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), Name: fmt.Sprintf("%s-%s-%d", k3d.DefaultObjectNamePrefix, args[0], i),
Role: role, Role: role,
Image: image, Image: image,
Labels: map[string]string{
"k3d.role": roleStr,
},
} }
nodes = append(nodes, node) nodes = append(nodes, node)
} }

View File

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

View File

@ -67,6 +67,12 @@ var DefaultObjectLabels = map[string]string{
"app": "k3d", "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 // DefaultTmpfsMounts specifies tmpfs mounts that are required for all k3d nodes
var DefaultTmpfsMounts = []string{ var DefaultTmpfsMounts = []string{
"/run", "/run",