From a0c0c3ff9d2bfbb66d61b94a24425423c5153def Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Mon, 13 May 2019 09:08:32 +0200 Subject: [PATCH] add --api-port and --port-auto-offset --- cli/commands.go | 9 +++++---- cli/container.go | 13 ++++++++----- cli/port.go | 2 -- main.go | 12 +++++++++--- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index afe61a40..51104dc6 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -92,9 +92,9 @@ func CreateCluster(c *cli.Context) error { // k3s server arguments // TODO: --port will soon be --api-port since we want to re-use --port for arbitrary port mappings if c.IsSet("port") { - log.Println("WARNING: --port will soon be used for arbitrary port-mappings. It's original functionality can then be used via --api-port.") + log.Println("WARNING: As of v2.0.0 --port will be used for arbitrary port-mappings. It's original functionality can then be used via --api-port.") } - k3sServerArgs := []string{"--https-listen-port", c.String("port")} + k3sServerArgs := []string{"--https-listen-port", c.String("api-port")} if c.IsSet("server-arg") || c.IsSet("x") { k3sServerArgs = append(k3sServerArgs, c.StringSlice("server-arg")...) } @@ -110,7 +110,7 @@ func CreateCluster(c *cli.Context) error { dockerID, err := createServer( c.GlobalBool("verbose"), image, - c.String("port"), + c.String("api-port"), k3sServerArgs, env, c.String("name"), @@ -183,8 +183,9 @@ func CreateCluster(c *cli.Context) error { c.String("name"), strings.Split(c.String("volume"), ","), i, - c.String("port"), + c.String("api-port"), portmap, + c.Int("port-auto-offset"), ) if err != nil { return fmt.Errorf("ERROR: failed to create worker node for cluster %s\n%+v", c.String("name"), err) diff --git a/cli/container.go b/cli/container.go index 899ec2bf..a929f7a6 100644 --- a/cli/container.go +++ b/cli/container.go @@ -62,7 +62,7 @@ func startContainer(verbose bool, config *container.Config, hostConfig *containe return resp.ID, nil } -func createServer(verbose bool, image string, port string, args []string, env []string, +func createServer(verbose bool, image string, apiPort string, args []string, env []string, name string, volumes []string, nodeToPortSpecMap map[string][]string) (string, error) { log.Printf("Creating server using %s...\n", image) @@ -81,7 +81,7 @@ func createServer(verbose bool, image string, port string, args []string, env [] return "", err } - apiPortSpec := fmt.Sprintf("0.0.0.0:%s:%s/tcp", port, port) + apiPortSpec := fmt.Sprintf("0.0.0.0:%s:%s/tcp", apiPort, apiPort) serverPorts = append(serverPorts, apiPortSpec) @@ -125,7 +125,7 @@ func createServer(verbose bool, image string, port string, args []string, env [] // createWorker creates/starts a k3s agent node that connects to the server func createWorker(verbose bool, image string, args []string, env []string, name string, volumes []string, - postfix int, serverPort string, nodeToPortSpecMap map[string][]string) (string, error) { + postfix int, serverPort string, nodeToPortSpecMap map[string][]string, portAutoOffset int) (string, error) { containerLabels := make(map[string]string) containerLabels["app"] = "k3d" containerLabels["component"] = "worker" @@ -139,7 +139,6 @@ func createWorker(verbose bool, image string, args []string, env []string, name // ports to be assigned to the server belong to roles // all, server or workerPorts, err := MergePortSpecs(nodeToPortSpecMap, "worker", containerName) - fmt.Printf("%s -> ports: %+v\n", containerName, workerPorts) if err != nil { return "", err } @@ -147,7 +146,11 @@ func createWorker(verbose bool, image string, args []string, env []string, name if err != nil { return "", err } - workerPublishedPorts = workerPublishedPorts.Offset(postfix + 1) + if portAutoOffset > 0 { + // TODO: add some checks before to print a meaningful log message saying that we cannot map multiple container ports + // to the same host port without a offset + workerPublishedPorts = workerPublishedPorts.Offset(postfix + portAutoOffset) + } hostConfig := &container.HostConfig{ Tmpfs: map[string]string{ diff --git a/cli/port.go b/cli/port.go index e760f13e..4224f8f9 100644 --- a/cli/port.go +++ b/cli/port.go @@ -40,8 +40,6 @@ func mapNodesToPortSpecs(specs []string) (map[string][]string, error) { } } - fmt.Printf("nodeToPortSpecMap: %+v\n", nodeToPortSpecMap) - return nodeToPortSpecMap, nil } diff --git a/main.go b/main.go index 222e7a77..6d3b930e 100644 --- a/main.go +++ b/main.go @@ -62,7 +62,12 @@ func main() { }, cli.StringSliceFlag{ Name: "publish, add-port", - Usage: "publish k3s node ports to the host (Format: `[ip:][host-port:]container-port[/protocol]@node-specifier`, use multiple options to expose more ports)", + Usage: "Publish k3s node ports to the host (Format: `[ip:][host-port:]container-port[/protocol]@node-specifier`, use multiple options to expose more ports)", + }, + cli.IntFlag{ + Name: "port-auto-offset", + Value: 0, + Usage: "Automatically add an offset (+ worker number) to the chosen host port when using `--publish` to map the same container-port from multiple k3d workers to the host", }, cli.StringFlag{ // TODO: to be deprecated @@ -70,9 +75,10 @@ func main() { Usage: "Choose the k3s image version", }, cli.IntFlag{ - Name: "port, p", + // TODO: only --api-port, -a soon since we want to use --port, -p for the --publish/--add-port functionality + Name: "api-port, a, port, p", Value: 6443, - Usage: "Map the Kubernetes ApiServer port to a local port", + Usage: "Map the Kubernetes ApiServer port to a local port (Note: --port/-p will have different functionality as of v2.0.0)", }, cli.IntFlag{ Name: "timeout, t",