From f70a8b42f7b03bece845a5bf49193b0e16b4b7ee Mon Sep 17 00:00:00 2001 From: Andy Zhou Date: Tue, 7 May 2019 09:07:06 -0700 Subject: [PATCH] Add support for the --publish option for create subcommand Inspired by the docker CLI, --publish take same input as docker CLI and provides similar functions. For the k3s cluster server node, it behaves the same as docker cli; it exports the k3d server ports to the host ports. Handling for worker nodes will be added in the subsequent patches. This option can be used mutiple times for exposing more ports. --add-port is an alias to this option. --- cli/commands.go | 6 ++++++ cli/container.go | 22 +++++++++------------- main.go | 4 ++++ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index 8735fcb3..16fdf79c 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -71,6 +71,11 @@ func CreateCluster(c *cli.Context) error { k3sServerArgs = append(k3sServerArgs, c.StringSlice("server-arg")...) } + publishedPorts, err := createPublishedPorts(c.StringSlice("publish")) + if (err != nil) { + log.Fatalf("ERROR: failed to parse the publish parameter.\n%+v", err) + } + // create the server log.Printf("Creating cluster [%s]", c.String("name")) dockerID, err := createServer( @@ -81,6 +86,7 @@ func CreateCluster(c *cli.Context) error { env, c.String("name"), strings.Split(c.String("volume"), ","), + publishedPorts, ) if err != nil { log.Fatalf("ERROR: failed to create cluster\n%+v", err) diff --git a/cli/container.go b/cli/container.go index 89947c1d..752ca552 100644 --- a/cli/container.go +++ b/cli/container.go @@ -138,7 +138,8 @@ 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, name string, volumes []string) (string, error) { +func createServer(verbose bool, image string, port string, args []string, env []string, + name string, volumes []string, pPorts *PublishedPorts) (string, error) { log.Printf("Creating server using %s...\n", image) containerLabels := make(map[string]string) @@ -149,17 +150,14 @@ func createServer(verbose bool, image string, port string, args []string, env [] containerName := fmt.Sprintf("k3d-%s-server", name) - containerPort := nat.Port(fmt.Sprintf("%s/tcp", port)) + apiPortSpec := fmt.Sprintf("0.0.0.0:%s:%s/tcp", port, port) + serverPublishedPorts, err := pPorts.AddPort(apiPortSpec) + if (err != nil) { + log.Fatalf("Error: failed to parse API port spec %s \n%+v", apiPortSpec, err) + } hostConfig := &container.HostConfig{ - PortBindings: nat.PortMap{ - containerPort: []nat.PortBinding{ - { - HostIP: "0.0.0.0", - HostPort: port, - }, - }, - }, + PortBindings: serverPublishedPorts.PortBindings, Privileged: true, } @@ -179,9 +177,7 @@ func createServer(verbose bool, image string, port string, args []string, env [] Hostname: containerName, Image: image, Cmd: append([]string{"server"}, args...), - ExposedPorts: nat.PortSet{ - containerPort: struct{}{}, - }, + ExposedPorts: serverPublishedPorts.ExposedPorts, Env: env, Labels: containerLabels, } diff --git a/main.go b/main.go index ec8b28de..aa3e40a9 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,10 @@ func main() { Name: "volume, v", Usage: "Mount one or more volumes into every node of the cluster (Docker notation: `source:destination[,source:destination]`)", }, + cli.StringSliceFlag{ + Name: "publish, add-port", + Usage: "publish k3s node ports to the host (Docker notation: `ip:public:private/proto`, use multiple options to expose more ports)", + }, cli.StringFlag{ Name: "version", Value: version.GetK3sVersion(),