Skip to content

Commit

Permalink
Add tunneling of apiserver port for qemu driver
Browse files Browse the repository at this point in the history
  • Loading branch information
afbjorklund committed Apr 4, 2022
1 parent 6965307 commit 191ff5b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
11 changes: 6 additions & 5 deletions pkg/minikube/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ type ClusterConfig struct {
HypervVirtualSwitch string
HypervUseExternalSwitch bool
HypervExternalAdapter string
KVMNetwork string // Only used by the KVM2 driver
KVMQemuURI string // Only used by the KVM2 driver
KVMGPU bool // Only used by the KVM2 driver
KVMHidden bool // Only used by the KVM2 driver
KVMNUMACount int // Only used by the KVM2 driver
KVMNetwork string // Only used by the KVM2 driver
KVMQemuURI string // Only used by the KVM2 driver
KVMGPU bool // Only used by the KVM2 driver
KVMHidden bool // Only used by the KVM2 driver
KVMNUMACount int // Only used by the KVM2 driver
APIServerPort int
DockerOpt []string // Each entry is formatted as KEY=VALUE.
DisableDriverMounts bool // Only used by virtualbox
NFSShare []string
Expand Down
4 changes: 3 additions & 1 deletion pkg/minikube/driver/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

// ControlPlaneEndpoint returns the location where callers can reach this cluster
func ControlPlaneEndpoint(cc *config.ClusterConfig, cp *config.Node, driverName string) (string, net.IP, int, error) {
if NeedsPortForward(driverName) {
if NeedsPortForward(driverName) && IsKIC(driverName) {
port, err := oci.ForwardedPort(cc.Driver, cc.Name, cp.Port)
if err != nil {
klog.Warningf("failed to get forwarded control plane port %v", err)
Expand All @@ -45,6 +45,8 @@ func ControlPlaneEndpoint(cc *config.ClusterConfig, cp *config.Node, driverName
hostname = cc.KubernetesConfig.APIServerName
}
return hostname, ips[0], port, err
} else if NeedsPortForward(driverName) && IsQEMU(driverName) {
return "localhost", net.IPv4(127, 0, 0, 1), cc.APIServerPort, nil
}

// https://github.com/kubernetes/minikube/issues/3878
Expand Down
32 changes: 32 additions & 0 deletions pkg/minikube/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ func handleAPIServer(starter Starter, cr cruntime.Manager, hostIP net.IP) (*kube
return nil, bs, err
}

// Tunnel apiserver to guest, if needed
if starter.Cfg.APIServerPort != 0 {
args := []string{"-f", "-NTL", fmt.Sprintf("%d:localhost:8443", starter.Cfg.APIServerPort)}
err := machine.CreateSSHShell(starter.MachineAPI, *starter.Cfg, *starter.Node, args, false)
if err != nil {
klog.Warningf("apiserver tunnel failed: %v", err)
}
}

// Write the kubeconfig to the file system after everything required (like certs) are created by the bootstrapper.
if err := kubeconfig.Update(kcs); err != nil {
return nil, bs, errors.Wrap(err, "Failed kubeconfig update")
Expand Down Expand Up @@ -552,6 +561,14 @@ func startMachine(cfg *config.ClusterConfig, node *config.Node, delOnFail bool)
return runner, preExists, m, host, errors.Wrap(err, "Failed to validate network")
}

if driver.IsQEMU(host.Driver.DriverName()) {
apiServerPort, err := getPort()
if err != nil {
return runner, preExists, m, host, errors.Wrap(err, "Failed to find apiserver port")
}
cfg.APIServerPort = apiServerPort
}

// Bypass proxy for minikube's vm host ip
err = proxy.ExcludeIP(ip)
if err != nil {
Expand All @@ -561,6 +578,21 @@ func startMachine(cfg *config.ClusterConfig, node *config.Node, delOnFail bool)
return runner, preExists, m, host, err
}

// getPort asks the kernel for a free open port that is ready to use
func getPort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
panic(err)
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
return -1, errors.Errorf("Error accessing port %d", addr.Port)
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}

// startHostInternal starts a new minikube host using a VM or None
func startHostInternal(api libmachine.API, cc *config.ClusterConfig, n *config.Node, delOnFail bool) (*host.Host, bool, error) {
host, exists, err := machine.StartHost(api, cc, n)
Expand Down

0 comments on commit 191ff5b

Please sign in to comment.