Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new flag "--ports" to expose ports for docker & podman drivers #9404

Merged
merged 11 commits into from
Oct 7, 2020
9 changes: 9 additions & 0 deletions cmd/minikube/cmd/start_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const (
forceSystemd = "force-systemd"
kicBaseImage = "base-image"
startOutput = "output"
ports = "ports"
)

// initMinikubeFlags includes commandline flags for minikube.
Expand Down Expand Up @@ -197,6 +198,9 @@ func initDriverFlags() {
startCmd.Flags().String(hypervVirtualSwitch, "", "The hyperv virtual switch name. Defaults to first found. (hyperv driver only)")
startCmd.Flags().Bool(hypervUseExternalSwitch, false, "Whether to use external switch over Default Switch if virtual switch not explicitly specified. (hyperv driver only)")
startCmd.Flags().String(hypervExternalAdapter, "", "External Adapter on which external switch will be created if no external switch is found. (hyperv driver only)")

// docker & podman
startCmd.Flags().StringSlice(ports, []string{}, "List of ports that should be exposed (docker and podman driver only)")
}

// initNetworkingFlags inits the commandline flags for connectivity related flags for start
Expand Down Expand Up @@ -311,6 +315,7 @@ func generateClusterConfig(cmd *cobra.Command, existing *config.ClusterConfig, k
HostOnlyNicType: viper.GetString(hostOnlyNicType),
NatNicType: viper.GetString(natNicType),
StartHostTimeout: viper.GetDuration(waitTimeout),
ExposedPorts: viper.GetStringSlice(ports),
KubernetesConfig: config.KubernetesConfig{
KubernetesVersion: k8sVersion,
ClusterName: ClusterFlagValue(),
Expand Down Expand Up @@ -547,6 +552,10 @@ func updateExistingConfigFromFlags(cmd *cobra.Command, existing *config.ClusterC
cc.KubernetesConfig.NodePort = viper.GetInt(apiServerPort)
}

if cmd.Flags().Changed(vsockPorts) {
cc.ExposedPorts = viper.GetStringSlice(ports)
}

// pre minikube 1.9.2 cc.KubernetesConfig.NodePort was not populated.
// in minikube config there were two fields for api server port.
// one in cc.KubernetesConfig.NodePort and one in cc.Nodes.Port
Expand Down
2 changes: 1 addition & 1 deletion pkg/drivers/kic/kic.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (d *Driver) Create() error {
CPUs: strconv.Itoa(d.NodeConfig.CPU),
Memory: strconv.Itoa(d.NodeConfig.Memory) + "mb",
Envs: d.NodeConfig.Envs,
ExtraArgs: []string{"--expose", fmt.Sprintf("%d", d.NodeConfig.APIServerPort)},
ExtraArgs: append([]string{"--expose", fmt.Sprintf("%d", d.NodeConfig.APIServerPort)}, d.NodeConfig.ExtraArgs...),
OCIBinary: d.NodeConfig.OCIBinary,
APIServerPort: d.NodeConfig.APIServerPort,
}
Expand Down
1 change: 1 addition & 0 deletions pkg/drivers/kic/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ type Config struct {
Envs map[string]string // key,value of environment variables passed to the node
KubernetesVersion string // Kubernetes version to install
ContainerRuntime string // container runtime kic is running
ExtraArgs []string // a list of any extra option to pass to oci binary during creation time, for example --expose 8080...
}
1 change: 1 addition & 0 deletions pkg/minikube/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type ClusterConfig struct {
Addons map[string]bool
VerifyComponents map[string]bool // map of components to verify and wait for after start.
StartHostTimeout time.Duration
ExposedPorts []string // Only used by the docker and podman driver
}

// KubernetesConfig contains the parameters used to configure the VM Kubernetes.
Expand Down
7 changes: 7 additions & 0 deletions pkg/minikube/registry/drvs/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) {
}
}

extraArgs := []string{}

for _, port := range cc.ExposedPorts {
extraArgs = append(extraArgs, "-p", port)
}

return kic.NewDriver(kic.Config{
ClusterName: cc.Name,
MachineName: driver.MachineName(cc, n),
Expand All @@ -72,6 +78,7 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) {
APIServerPort: cc.Nodes[0].Port,
KubernetesVersion: cc.KubernetesConfig.KubernetesVersion,
ContainerRuntime: cc.KubernetesConfig.ContainerRuntime,
ExtraArgs: extraArgs,
}), nil
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/minikube/registry/drvs/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) {
}
}

extraArgs := []string{}

for _, port := range cc.ExposedPorts {
extraArgs = append(extraArgs, "-p", port)
}

return kic.NewDriver(kic.Config{
ClusterName: cc.Name,
MachineName: driver.MachineName(cc, n),
Expand All @@ -84,6 +90,7 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) {
APIServerPort: cc.Nodes[0].Port,
KubernetesVersion: cc.KubernetesConfig.KubernetesVersion,
ContainerRuntime: cc.KubernetesConfig.ContainerRuntime,
ExtraArgs: extraArgs,
}), nil
}

Expand Down
1 change: 1 addition & 0 deletions site/content/en/docs/commands/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ minikube start [flags]
--no-vtx-check Disable checking for the availability of hardware virtualization before the vm is started (virtualbox driver only)
-n, --nodes int The number of nodes to spin up. Defaults to 1. (default 1)
-o, --output string Format to print stdout in. Options include: [text,json] (default "text")
--ports strings List of ports that should be exposed (docker and podman driver only)
--preload If set, download tarball of preloaded images if available to improve start time. Defaults to true. (default true)
--registry-mirror strings Registry mirrors to pass to the Docker daemon
--service-cluster-ip-range string The CIDR to be used for service cluster IPs. (default "10.96.0.0/12")
Expand Down