forked from redhat-developer/odo
/
login.go
53 lines (42 loc) · 1.66 KB
/
login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package auth
import (
"os"
"github.com/openshift/origin/pkg/oc/cli/login"
"k8s.io/client-go/tools/clientcmd"
"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 {
a := login.LoginOptions{
Server: server,
CommandName: "odo",
CAFile: caAuth,
InsecureTLS: skipTLS,
Username: username,
Password: password,
Project: "",
Token: token,
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
}
return nil
}