Skip to content

Commit

Permalink
Merge pull request #98 from ethernetdan/nondefault-kubectl-contexts
Browse files Browse the repository at this point in the history
Fixed using non-default kubectl contexts
  • Loading branch information
ethernetdan committed Mar 2, 2016
2 parents ece0043 + 5c3340f commit 39d1e12
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions pkg/deploy/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,32 @@ type KubeCluster struct {
// NewKubeClusterFromContext creates a KubeCluster using a Kubernetes client with the configuration of the given context.
// If the context name is empty, the default context will be used.
func NewKubeClusterFromContext(name string) (*KubeCluster, error) {
rules := defaultLoadingRules()
rulesConfig, err := defaultLoadingRules().Load()
if err != nil {
return nil, fmt.Errorf("could not load rules: %v", err)
}

overrides := &clientcmd.ConfigOverrides{
CurrentContext: name,
clientConfig := clientcmd.NewNonInteractiveClientConfig(*rulesConfig, name, &clientcmd.ConfigOverrides{})

rawConfig, err := clientConfig.RawConfig()
if err != nil || rawConfig.Contexts == nil {
return nil, fmt.Errorf("could not access kubectl config: %v", err)
}

config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides)
if name == DefaultContext {
name = rawConfig.CurrentContext
}

clientConfig, err := config.ClientConfig()
if rawConfig.Contexts[name] == nil {
return nil, fmt.Errorf("context '%s' does not exist", name)
}

restClientConfig, err := clientConfig.ClientConfig()
if err != nil {
if len(name) == 0 {
return nil, fmt.Errorf("could not use default context: %v", err)
}
return nil, fmt.Errorf("could not use context `%s`: %v", name, err)
return nil, fmt.Errorf("could not get ClientConfig: %v", err)
}

client, err := kubecli.New(clientConfig)
client, err := kubecli.New(restClientConfig)
if err != nil {
return nil, fmt.Errorf("could not create Kubernetes client: %v", err)
}
Expand Down

0 comments on commit 39d1e12

Please sign in to comment.