diff --git a/cli/commands.go b/cli/commands.go index 51104dc6..7a76fe8d 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -23,7 +23,10 @@ import ( "github.com/urfave/cli" ) -const defaultRegistry = "docker.io" +const ( + defaultRegistry = "docker.io" + defaultServerCount = 1 +) // CheckTools checks if the docker API server is responding func CheckTools(c *cli.Context) error { @@ -100,7 +103,7 @@ func CreateCluster(c *cli.Context) error { } // new port map - portmap, err := mapNodesToPortSpecs(c.StringSlice("publish")) + portmap, err := mapNodesToPortSpecs(c.StringSlice("publish"), GetAllContainerNames(c.String("name"), defaultServerCount, c.Int("workers"))) if err != nil { log.Fatal(err) } diff --git a/cli/config.go b/cli/config.go index d7730495..496c002c 100644 --- a/cli/config.go +++ b/cli/config.go @@ -16,6 +16,10 @@ import ( "github.com/olekukonko/tablewriter" ) +const ( + defaultContainerNamePrefix = "k3d" +) + type cluster struct { name string image string @@ -25,6 +29,26 @@ type cluster struct { workers []types.Container } +// GetContainerName generates the container names +func GetContainerName(role, clusterName string, postfix int) string { + if postfix >= 0 { + return fmt.Sprintf("%s-%s-%s-%d", defaultContainerNamePrefix, clusterName, role, postfix) + } + return fmt.Sprintf("%s-%s-%s", defaultContainerNamePrefix, clusterName, role) +} + +// GetAllContainerNames returns a list of all containernames that will be created +func GetAllContainerNames(clusterName string, serverCount, workerCount int) []string { + names := []string{} + for postfix := 0; postfix < serverCount; postfix++ { + names = append(names, GetContainerName("server", clusterName, postfix)) + } + for postfix := 0; postfix < workerCount; postfix++ { + names = append(names, GetContainerName("worker", clusterName, postfix)) + } + return names +} + // createDirIfNotExists checks for the existence of a directory and creates it along with all required parents if not. // It returns an error if the directory (or parents) couldn't be created and nil if it worked fine or if the path already exists. func createDirIfNotExists(path string) error { diff --git a/cli/container.go b/cli/container.go index a929f7a6..b780508e 100644 --- a/cli/container.go +++ b/cli/container.go @@ -72,7 +72,7 @@ func createServer(verbose bool, image string, apiPort string, args []string, env containerLabels["created"] = time.Now().Format("2006-01-02 15:04:05") containerLabels["cluster"] = name - containerName := fmt.Sprintf("k3d-%s-server", name) + containerName := GetContainerName("server", name, -1) // ports to be assigned to the server belong to roles // all, server or @@ -132,7 +132,7 @@ func createWorker(verbose bool, image string, args []string, env []string, name containerLabels["created"] = time.Now().Format("2006-01-02 15:04:05") containerLabels["cluster"] = name - containerName := fmt.Sprintf("k3d-%s-worker-%d", name, postfix) + containerName := GetContainerName("worker", name, postfix) env = append(env, fmt.Sprintf("K3S_URL=https://k3d-%s-server:%s", name, serverPort)) diff --git a/cli/port.go b/cli/port.go index 4224f8f9..92da019c 100644 --- a/cli/port.go +++ b/cli/port.go @@ -2,6 +2,7 @@ package run import ( "fmt" + "log" "regexp" "strings" @@ -24,26 +25,41 @@ var nodeRuleGroupsMap = map[string][]string{ } // mapNodesToPortSpecs maps nodes to portSpecs -func mapNodesToPortSpecs(specs []string) (map[string][]string, error) { +func mapNodesToPortSpecs(specs []string, createdNodes []string) (map[string][]string, error) { if err := validatePortSpecs(specs); err != nil { return nil, err } + // check node-specifier possibilitites + possibleNodeSpecifiers := []string{"all", "workers", "server", "master"} + possibleNodeSpecifiers = append(possibleNodeSpecifiers, createdNodes...) + nodeToPortSpecMap := make(map[string][]string) for _, spec := range specs { nodes, portSpec := extractNodes(spec) for _, node := range nodes { - nodeToPortSpecMap[node] = append(nodeToPortSpecMap[node], portSpec) + // check if node-specifier is valid (either a role or a name) and append to list if matches + nodeFound := false + for _, name := range possibleNodeSpecifiers { + if node == name { + nodeFound = true + nodeToPortSpecMap[node] = append(nodeToPortSpecMap[node], portSpec) + break + } + } + if !nodeFound { + log.Printf("WARNING: Unknown node-specifier [%s] in port mapping entry [%s]", node, spec) + } } } return nodeToPortSpecMap, nil } -// The factory function for PublishedPorts +// CreatePublishedPorts is the factory function for PublishedPorts func CreatePublishedPorts(specs []string) (*PublishedPorts, error) { if len(specs) == 0 { var newExposedPorts = make(map[nat.Port]struct{}, 1)