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

pull/43/head
iwilltry42 5 years ago
parent 7d6ae201e9
commit a0c0c3ff9d
  1. 9
      cli/commands.go
  2. 13
      cli/container.go
  3. 2
      cli/port.go
  4. 12
      main.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)

@ -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 <server-container-name>
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{

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

@ -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",

Loading…
Cancel
Save