forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_merge.go
206 lines (171 loc) · 6.75 KB
/
smart_merge.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package config
import (
"crypto/x509"
"net/url"
"reflect"
"strings"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/third_party/forked/golang/netutil"
x509request "k8s.io/apiserver/pkg/authentication/request/x509"
restclient "k8s.io/client-go/rest"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
userclient "github.com/openshift/origin/pkg/user/generated/internalclientset"
)
// GetClusterNicknameFromConfig returns host:port of the clientConfig.Host, with .'s replaced by -'s
func GetClusterNicknameFromConfig(clientCfg *restclient.Config) (string, error) {
return GetClusterNicknameFromURL(clientCfg.Host)
}
// GetClusterNicknameFromURL returns host:port of the apiServerLocation, with .'s replaced by -'s
func GetClusterNicknameFromURL(apiServerLocation string) (string, error) {
u, err := url.Parse(apiServerLocation)
if err != nil {
return "", err
}
hostPort := netutil.CanonicalAddr(u)
// we need a character other than "." to avoid conflicts with. replace with '-'
return strings.Replace(hostPort, ".", "-", -1), nil
}
// GetUserNicknameFromConfig returns "username(as known by the server)/GetClusterNicknameFromConfig". This allows tab completion for switching users to
// work easily and obviously.
func GetUserNicknameFromConfig(clientCfg *restclient.Config) (string, error) {
userPartOfNick, err := getUserPartOfNickname(clientCfg)
if err != nil {
return "", err
}
clusterNick, err := GetClusterNicknameFromConfig(clientCfg)
if err != nil {
return "", err
}
return userPartOfNick + "/" + clusterNick, nil
}
func GetUserNicknameFromCert(clusterNick string, chain ...*x509.Certificate) (string, error) {
userInfo, _, err := x509request.CommonNameUserConversion(chain)
if err != nil {
return "", err
}
return userInfo.GetName() + "/" + clusterNick, nil
}
func getUserPartOfNickname(clientCfg *restclient.Config) (string, error) {
userClient, err := userclient.NewForConfig(clientCfg)
if err != nil {
return "", err
}
userInfo, err := userClient.User().Users().Get("~", metav1.GetOptions{})
if kerrors.IsNotFound(err) || kerrors.IsForbidden(err) {
// if we're talking to kube (or likely talking to kube), take a best guess consistent with login
switch {
case len(clientCfg.BearerToken) > 0:
userInfo.Name = clientCfg.BearerToken
case len(clientCfg.Username) > 0:
userInfo.Name = clientCfg.Username
}
} else if err != nil {
return "", err
}
return userInfo.Name, nil
}
// GetContextNicknameFromConfig returns "namespace/GetClusterNicknameFromConfig/username(as known by the server)". This allows tab completion for switching projects/context
// to work easily. First tab is the most selective on project. Second stanza in the next most selective on cluster name. The chances of a user trying having
// one projects on a single server that they want to operate against with two identities is low, so username is last.
func GetContextNicknameFromConfig(namespace string, clientCfg *restclient.Config) (string, error) {
userPartOfNick, err := getUserPartOfNickname(clientCfg)
if err != nil {
return "", err
}
clusterNick, err := GetClusterNicknameFromConfig(clientCfg)
if err != nil {
return "", err
}
return namespace + "/" + clusterNick + "/" + userPartOfNick, nil
}
func GetContextNickname(namespace, clusterNick, userNick string) string {
tokens := strings.SplitN(userNick, "/", 2)
return namespace + "/" + clusterNick + "/" + tokens[0]
}
// CreateConfig takes a clientCfg and builds a config (kubeconfig style) from it.
func CreateConfig(namespace string, clientCfg *restclient.Config) (*clientcmdapi.Config, error) {
clusterNick, err := GetClusterNicknameFromConfig(clientCfg)
if err != nil {
return nil, err
}
userNick, err := GetUserNicknameFromConfig(clientCfg)
if err != nil {
return nil, err
}
contextNick, err := GetContextNicknameFromConfig(namespace, clientCfg)
if err != nil {
return nil, err
}
config := clientcmdapi.NewConfig()
credentials := clientcmdapi.NewAuthInfo()
credentials.Token = clientCfg.BearerToken
credentials.ClientCertificate = clientCfg.TLSClientConfig.CertFile
if len(credentials.ClientCertificate) == 0 {
credentials.ClientCertificateData = clientCfg.TLSClientConfig.CertData
}
credentials.ClientKey = clientCfg.TLSClientConfig.KeyFile
if len(credentials.ClientKey) == 0 {
credentials.ClientKeyData = clientCfg.TLSClientConfig.KeyData
}
config.AuthInfos[userNick] = credentials
cluster := clientcmdapi.NewCluster()
cluster.Server = clientCfg.Host
cluster.CertificateAuthority = clientCfg.CAFile
if len(cluster.CertificateAuthority) == 0 {
cluster.CertificateAuthorityData = clientCfg.CAData
}
cluster.InsecureSkipTLSVerify = clientCfg.Insecure
config.Clusters[clusterNick] = cluster
context := clientcmdapi.NewContext()
context.Cluster = clusterNick
context.AuthInfo = userNick
context.Namespace = namespace
config.Contexts[contextNick] = context
config.CurrentContext = contextNick
return config, nil
}
// MergeConfig adds the additional Config stanzas to the startingConfig. It blindly stomps clusters and users, but
// it searches for a matching context before writing a new one.
func MergeConfig(startingConfig, addition clientcmdapi.Config) (*clientcmdapi.Config, error) {
ret := startingConfig
for requestedKey, value := range addition.Clusters {
ret.Clusters[requestedKey] = value
}
for requestedKey, value := range addition.AuthInfos {
ret.AuthInfos[requestedKey] = value
}
requestedContextNamesToActualContextNames := map[string]string{}
for requestedKey, newContext := range addition.Contexts {
actualContext := clientcmdapi.NewContext()
actualContext.AuthInfo = newContext.AuthInfo
actualContext.Cluster = newContext.Cluster
actualContext.Namespace = newContext.Namespace
actualContext.Extensions = newContext.Extensions
if existingName := FindExistingContextName(startingConfig, *actualContext); len(existingName) > 0 {
// if this already exists, just move to the next, our job is done
requestedContextNamesToActualContextNames[requestedKey] = existingName
continue
}
requestedContextNamesToActualContextNames[requestedKey] = requestedKey
ret.Contexts[requestedKey] = actualContext
}
if len(addition.CurrentContext) > 0 {
if newCurrentContext, exists := requestedContextNamesToActualContextNames[addition.CurrentContext]; exists {
ret.CurrentContext = newCurrentContext
} else {
ret.CurrentContext = addition.CurrentContext
}
}
return &ret, nil
}
// FindExistingContextName finds the nickname for the passed context
func FindExistingContextName(haystack clientcmdapi.Config, needle clientcmdapi.Context) string {
for key, context := range haystack.Contexts {
context.LocationOfOrigin = ""
if reflect.DeepEqual(context, needle) {
return key
}
}
return ""
}