add --api-port and --port-auto-offset

This commit is contained in:
iwilltry42 2019-05-13 09:08:32 +02:00
parent 7d6ae201e9
commit a0c0c3ff9d
4 changed files with 22 additions and 14 deletions

View File

@ -92,9 +92,9 @@ func CreateCluster(c *cli.Context) error {
// k3s server arguments // k3s server arguments
// TODO: --port will soon be --api-port since we want to re-use --port for arbitrary port mappings // TODO: --port will soon be --api-port since we want to re-use --port for arbitrary port mappings
if c.IsSet("port") { 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") { if c.IsSet("server-arg") || c.IsSet("x") {
k3sServerArgs = append(k3sServerArgs, c.StringSlice("server-arg")...) k3sServerArgs = append(k3sServerArgs, c.StringSlice("server-arg")...)
} }
@ -110,7 +110,7 @@ func CreateCluster(c *cli.Context) error {
dockerID, err := createServer( dockerID, err := createServer(
c.GlobalBool("verbose"), c.GlobalBool("verbose"),
image, image,
c.String("port"), c.String("api-port"),
k3sServerArgs, k3sServerArgs,
env, env,
c.String("name"), c.String("name"),
@ -183,8 +183,9 @@ func CreateCluster(c *cli.Context) error {
c.String("name"), c.String("name"),
strings.Split(c.String("volume"), ","), strings.Split(c.String("volume"), ","),
i, i,
c.String("port"), c.String("api-port"),
portmap, portmap,
c.Int("port-auto-offset"),
) )
if err != nil { if err != nil {
return fmt.Errorf("ERROR: failed to create worker node for cluster %s\n%+v", c.String("name"), err) return fmt.Errorf("ERROR: failed to create worker node for cluster %s\n%+v", c.String("name"), err)

View File

@ -62,7 +62,7 @@ func startContainer(verbose bool, config *container.Config, hostConfig *containe
return resp.ID, nil 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) { name string, volumes []string, nodeToPortSpecMap map[string][]string) (string, error) {
log.Printf("Creating server using %s...\n", image) 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 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) 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 // 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, 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 := make(map[string]string)
containerLabels["app"] = "k3d" containerLabels["app"] = "k3d"
containerLabels["component"] = "worker" 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 // ports to be assigned to the server belong to roles
// all, server or <server-container-name> // all, server or <server-container-name>
workerPorts, err := MergePortSpecs(nodeToPortSpecMap, "worker", containerName) workerPorts, err := MergePortSpecs(nodeToPortSpecMap, "worker", containerName)
fmt.Printf("%s -> ports: %+v\n", containerName, workerPorts)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -147,7 +146,11 @@ func createWorker(verbose bool, image string, args []string, env []string, name
if err != nil { if err != nil {
return "", err 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{ hostConfig := &container.HostConfig{
Tmpfs: map[string]string{ Tmpfs: map[string]string{

View File

@ -40,8 +40,6 @@ func mapNodesToPortSpecs(specs []string) (map[string][]string, error) {
} }
} }
fmt.Printf("nodeToPortSpecMap: %+v\n", nodeToPortSpecMap)
return nodeToPortSpecMap, nil return nodeToPortSpecMap, nil
} }

12
main.go
View File

@ -62,7 +62,12 @@ func main() {
}, },
cli.StringSliceFlag{ cli.StringSliceFlag{
Name: "publish, add-port", 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{ cli.StringFlag{
// TODO: to be deprecated // TODO: to be deprecated
@ -70,9 +75,10 @@ func main() {
Usage: "Choose the k3s image version", Usage: "Choose the k3s image version",
}, },
cli.IntFlag{ 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, 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{ cli.IntFlag{
Name: "timeout, t", Name: "timeout, t",