Skip to content

Commit

Permalink
Fixup: refactor k8s client methods to reuse config
Browse files Browse the repository at this point in the history
Reduce duplication by extracting k8s cluster config struct to separate
method.

Signed-off-by: Angel Misevski <amisevsk@redhat.com>
  • Loading branch information
amisevsk committed May 22, 2020
1 parent 17e678c commit 1999ace
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions pkg/terminal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,9 @@ import (

// createDynamicClient create dynamic client with the configured token to be used
func (p *Proxy) createDynamicClient(token string) (dynamic.Interface, error) {
var tlsClientConfig rest.TLSClientConfig
if p.TLSClientConfig.InsecureSkipVerify {
// off-cluster mode
tlsClientConfig.Insecure = true
} else {
inCluster, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
tlsClientConfig = inCluster.TLSClientConfig
}

config := &rest.Config{
Host: p.ClusterEndpoint.Host,
TLSClientConfig: tlsClientConfig,
BearerToken: token,
config, err := p.getConfig(token)
if err != nil {
return nil, err
}

client, err := dynamic.NewForConfig(dynamic.ConfigFor(config))
Expand All @@ -34,6 +21,15 @@ func (p *Proxy) createDynamicClient(token string) (dynamic.Interface, error) {
}

func (p *Proxy) createTypedClient(token string) (*kubernetes.Clientset, error) {
config, err := p.getConfig(token)
if err != nil {
return nil, err
}

return kubernetes.NewForConfig(config)
}

func (p *Proxy) getConfig(token string) (*rest.Config, error) {
var tlsClientConfig rest.TLSClientConfig
if p.TLSClientConfig.InsecureSkipVerify {
// off-cluster mode
Expand All @@ -46,11 +42,9 @@ func (p *Proxy) createTypedClient(token string) (*kubernetes.Clientset, error) {
tlsClientConfig = inCluster.TLSClientConfig
}

config := &rest.Config{
return &rest.Config{
Host: p.ClusterEndpoint.Host,
TLSClientConfig: tlsClientConfig,
BearerToken: token,
}

return kubernetes.NewForConfig(config)
}, nil
}

0 comments on commit 1999ace

Please sign in to comment.