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.
This commit is contained in:
parent
6fe75640bb
commit
f70a8b42f7
@ -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)
|
||||
|
@ -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,
|
||||
}
|
||||
|
4
main.go
4
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(),
|
||||
|
Loading…
Reference in New Issue
Block a user