From a14854502e746ae7c99f91d8d4c1760cd34f4f58 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Thu, 24 Oct 2019 15:31:29 +0200 Subject: [PATCH] allow setting api-port --- cmd/create/createCluster.go | 18 ++++++++++++++++-- pkg/runtimes/docker/translate.go | 10 ++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cmd/create/createCluster.go b/cmd/create/createCluster.go index 274de6ae..5ecfd84e 100644 --- a/cmd/create/createCluster.go +++ b/cmd/create/createCluster.go @@ -22,6 +22,8 @@ THE SOFTWARE. package create import ( + "fmt" + "github.com/spf13/cobra" k3dCluster "github.com/rancher/k3d/pkg/cluster" @@ -88,6 +90,11 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string) (runtimes.Runtime, log.Fatalln(err) } + // TODO: allow more than one master + if masterCount > 1 { + log.Fatalln("Only one master node supported right now!") + } + // --workers workerCount, err := cmd.Flags().GetInt("workers") if err != nil { @@ -107,10 +114,10 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string) (runtimes.Runtime, } // --api-port // TODO: - /*apiPort, err := cmd.Flags().GetString("api-port") + apiPort, err := cmd.Flags().GetString("api-port") if err != nil { log.Fatalln(err) - }*/ + } /* generate cluster */ cluster := &k3d.Cluster{ @@ -121,14 +128,21 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string) (runtimes.Runtime, // generate list of nodes cluster.Nodes = []k3d.Node{} + + // -> master nodes for i := 0; i < masterCount; i++ { node := k3d.Node{ Role: k3d.MasterRole, Image: image, } + if i == 0 { + node.Ports = append(node.Ports, fmt.Sprintf("0.0.0.0:%s:6443/tcp", apiPort)) // TODO: update (choose interface, enable more than one master) and get '6443' from defaultport variable + node.Labels["k3d.master.apiPort"] = apiPort + } cluster.Nodes = append(cluster.Nodes, node) } + // -> worker nodes for i := 0; i < workerCount; i++ { node := k3d.Node{ Role: k3d.WorkerRole, diff --git a/pkg/runtimes/docker/translate.go b/pkg/runtimes/docker/translate.go index be262786..933b2df7 100644 --- a/pkg/runtimes/docker/translate.go +++ b/pkg/runtimes/docker/translate.go @@ -28,6 +28,7 @@ import ( "github.com/docker/docker/api/types/network" "github.com/docker/go-connections/nat" k3d "github.com/rancher/k3d/pkg/types" + log "github.com/sirupsen/logrus" ) // TranslateNodeToContainer translates a k3d node specification to a docker container representation @@ -78,8 +79,13 @@ func TranslateNodeToContainer(node *k3d.Node) (*NodeInDocker, error) { // containerConfig.Volumes = map[string]struct{}{} // TODO: do we need this? We only used binds before /* Ports */ - containerConfig.ExposedPorts = nat.PortSet{} // TODO: translate from node.Ports to nat.PortSet - hostConfig.PortBindings = nat.PortMap{} // TODO: this and exposedPorts required? + exposedPorts, portBindings, err := nat.ParsePortSpecs(node.Ports) + if err != nil { + log.Errorln("Failed to parse port specs") + return nil, err + } + containerConfig.ExposedPorts = exposedPorts + hostConfig.PortBindings = portBindings /* Network */ networkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{