Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Re-use working tunnel for kubectl
Browse files Browse the repository at this point in the history
Signed-off-by: JoshVanL <vleeuwenjoshua@gmail.com>
  • Loading branch information
JoshVanL committed Feb 11, 2019
1 parent b51b1e9 commit 5b49cd9
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions pkg/tarmak/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,6 @@ func (k *Kubectl) ensureWorkingKubeconfig(configPath string, publicAPIEndpoint b
c = conf
}

// If we are using a public endpoint then we don't need to set up a tunnel
// but we need to keep a tunnel var around (in struct) so we can close it
// later on if it is being used. Use k.stopTunnel() to ensure no panics.
if !publicAPIEndpoint {
k.tunnel = k.tarmak.Cluster().APITunnel()
if err := k.tunnel.Start(); err != nil {
k.stopTunnel()
return err
}
}

c, cluster, err := k.setupConfig(c, publicAPIEndpoint)
if err != nil {
return err
Expand Down Expand Up @@ -207,15 +196,6 @@ func (k *Kubectl) ensureWorkingKubeconfig(configPath string, publicAPIEndpoint b
break
}

if !publicAPIEndpoint {
k.stopTunnel()
k.tunnel = k.tarmak.Cluster().APITunnel()
err = k.tunnel.Start()
if err != nil {
break
}
}

// force a new config
c, cluster, err = k.setupConfig(nil, publicAPIEndpoint)
if err != nil {
Expand Down Expand Up @@ -365,12 +345,25 @@ func (k *Kubectl) setupConfig(c *api.Config, publicAPIEndpoint bool) (*api.Confi
k.tarmak.Provider().PublicZone())

} else {
if k.tunnel == nil {
return nil, nil, fmt.Errorf("failed to get tunnel information at it is nil: %v", k.tunnel)

// the following code will attempt to use the current kubeconfig address if
// it exists. If it fails it will create a new tunnel and use this instead

// no server address so we need to create a new tunnel
if cluster.Server == "" {
k.stopTunnel()
k.tunnel = k.tarmak.Cluster().APITunnel()
if err := k.tunnel.Start(); err != nil {
return nil, nil, err
}
}

// tunnel isn't nil so we can try it's address
if k.tunnel != nil {
cluster.Server = fmt.Sprintf("https://%s:%s",
k.tunnel.BindAddress(), k.tunnel.Port())
}

cluster.Server = fmt.Sprintf("https://%s:%s",
k.tunnel.BindAddress(), k.tunnel.Port())
k.log.Warnf("ssh tunnel connecting to Kubernetes API server will close after 10 minutes: %s",
cluster.Server)
}
Expand Down

0 comments on commit 5b49cd9

Please sign in to comment.