Fixed odo login
What is the purpose of this change? What does it change?
Fixes `odo login` issues
Was the change discussed in an issue?
Solves
How to test changes?
`odo login`
@@ -4,24 +4,12 @@ import (
"os"
"github.com/openshift/origin/pkg/oc/cli/login"
- "k8s.io/kubernetes/pkg/kubectl/genericclioptions"
-
- restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
- kclientapi "k8s.io/client-go/tools/clientcmd/api"
+ "k8s.io/kubernetes/pkg/kubectl/genericclioptions"
)
// Login takes of authentication part and returns error if there any
func Login(server, username, password, token, caAuth string, skipTLS bool) error {
- var config *restclient.Config
- if server == "" {
-
- // initialize client-go client
- loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
- configOverrides := &clientcmd.ConfigOverrides{}
- kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
- config, _ = kubeConfig.ClientConfig()
- }
a := login.LoginOptions{
Server: server,
@@ -32,17 +20,30 @@ func Login(server, username, password, token, caAuth string, skipTLS bool) error
Password: password,
Project: "",
Token: token,
- StartingKubeConfig: &kclientapi.Config{
- Clusters: map[string]*kclientapi.Cluster{},
- AuthInfos: map[string]*kclientapi.AuthInfo{},
- Contexts: map[string]*kclientapi.Context{},
- },
- Config: config,
PathOptions: &clientcmd.PathOptions{GlobalFile: clientcmd.RecommendedHomeFile, EnvVar: clientcmd.RecommendedConfigPathEnvVar, ExplicitFileFlag: "config", LoadingRules: &clientcmd.ClientConfigLoadingRules{ExplicitPath: ""}},
RequestTimeout: 0,
IOStreams: genericclioptions.IOStreams{Out: os.Stdout, In: os.Stdin},
}
+ // initialize client-go client and read starting kubeconfig file
+
+ loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
+ configOverrides := &clientcmd.ConfigOverrides{}
+ kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
+
+ kubeconfig, _ := kubeConfig.RawConfig()
+
+ a.StartingKubeConfig = &kubeconfig
+
+ // if server URL is not given as argument, we will look for current context from kubeconfig file
+ if len(a.Server) == 0 {
+ if defaultContext, defaultContextExists := a.StartingKubeConfig.Contexts[a.StartingKubeConfig.CurrentContext]; defaultContextExists {
+ if cluster, exists := a.StartingKubeConfig.Clusters[defaultContext.Cluster]; exists {
+ a.Server = cluster.Server
+ }
+ }
+ }
+
err := a.Run()
if err != nil {
return err
