forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.go
80 lines (76 loc) · 2.19 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package openshift
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/golang/glog"
"github.com/spf13/cobra"
kclientcmd "k8s.io/client-go/tools/clientcmd"
kclientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/openshift/origin/pkg/oc/cli/cmd/login"
"github.com/openshift/origin/pkg/oc/cli/config"
)
// Login logs into the specified server using given credentials and CA file
func Login(username, password, server, configDir string, f *clientcmd.Factory, c *cobra.Command, out, errOut io.Writer) error {
existingConfig, err := f.OpenShiftClientConfig().RawConfig()
if err != nil {
if !os.IsNotExist(err) {
return err
}
existingConfig = *(kclientcmdapi.NewConfig())
}
adminConfig, err := kclientcmd.LoadFromFile(filepath.Join(configDir, "master", "admin.kubeconfig"))
if err != nil {
return err
}
for k := range adminConfig.AuthInfos {
adminConfig.AuthInfos[k].LocationOfOrigin = ""
}
serverFound := false
for k := range adminConfig.Clusters {
adminConfig.Clusters[k].LocationOfOrigin = ""
if adminConfig.Clusters[k].Server == server {
serverFound = true
}
}
if !serverFound {
// Create a server entry and admin context for
// local cluster
for k := range adminConfig.Clusters {
localCluster := *adminConfig.Clusters[k]
localCluster.Server = server
adminConfig.Clusters["local-cluster"] = &localCluster
for u := range adminConfig.AuthInfos {
if strings.HasPrefix(u, "system:admin") {
context := kclientcmdapi.NewContext()
context.Cluster = "local-cluster"
context.AuthInfo = u
context.Namespace = "default"
adminConfig.Contexts["default/local-cluster/system:admin"] = context
}
}
break
}
}
newConfig, err := config.MergeConfig(existingConfig, *adminConfig)
if err != nil {
return err
}
output := ioutil.Discard
if glog.V(1) {
output = out
}
opts := &login.LoginOptions{
Server: server,
Username: username,
Password: password,
Out: output,
ErrOut: errOut,
StartingKubeConfig: newConfig,
PathOptions: config.NewPathOptions(c),
}
return login.RunLogin(nil, opts)
}