diff --git a/pkg/deploy/cluster.go b/pkg/deploy/cluster.go index 69f1166..86aa166 100644 --- a/pkg/deploy/cluster.go +++ b/pkg/deploy/cluster.go @@ -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) }