createCluster: fix loadblancer consideration

- issue #248: nil pointer dereference because we're trying to access
non-existent loadblancer node
This commit is contained in:
iwilltry42 2020-05-27 18:23:39 +02:00
parent 2a0b425f67
commit dabec2f091
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
2 changed files with 8 additions and 6 deletions

View File

@ -374,15 +374,17 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string, createClusterOpts
} }
// append ports // append ports
lbCount := 1 nodeCount := masterCount + workerCount
if createClusterOpts.DisableLoadBalancer { nodeList := cluster.Nodes
lbCount = 0 if !createClusterOpts.DisableLoadBalancer {
nodeCount++
nodeList = append(nodeList, cluster.MasterLoadBalancer)
} }
for portmap, filters := range portFilterMap { for portmap, filters := range portFilterMap {
if len(filters) == 0 && (masterCount+workerCount+lbCount) > 1 { if len(filters) == 0 && (nodeCount) > 1 {
log.Fatalf("Malformed portmapping '%s' lacks a node filter, but there is more than one node (including the loadbalancer, if there is any).", portmap) log.Fatalf("Malformed portmapping '%s' lacks a node filter, but there is more than one node (including the loadbalancer, if there is any).", portmap)
} }
nodes, err := cliutil.FilterNodes(append(cluster.Nodes, cluster.MasterLoadBalancer), filters) nodes, err := cliutil.FilterNodes(nodeList, filters)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }

View File

@ -67,7 +67,7 @@ func SplitFiltersFromFlag(flag string) (string, []string, error) {
func FilterNodes(nodes []*k3d.Node, filters []string) ([]*k3d.Node, error) { func FilterNodes(nodes []*k3d.Node, filters []string) ([]*k3d.Node, error) {
if len(filters) == 0 || len(filters[0]) == 0 { if len(filters) == 0 || len(filters[0]) == 0 {
log.Warnln("No filter specified") log.Warnln("No node filter specified")
return nodes, nil return nodes, nil
} }