-
Notifications
You must be signed in to change notification settings - Fork 51
/
kubeconfig.go
64 lines (58 loc) · 1.4 KB
/
kubeconfig.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
package kubeconfig
import (
"crypto/rsa"
"crypto/x509"
"strings"
v1 "k8s.io/client-go/tools/clientcmd/api/v1"
"github.com/openshift/openshift-azure/pkg/util/tls"
)
func Make(clientKey *rsa.PrivateKey, clientCert, caCert *x509.Certificate, endpoint, username, namespace string) (*v1.Config, error) {
clustername := strings.Replace(endpoint, ".", "-", -1)
authinfoname := username + "/" + clustername
contextname := namespace + "/" + clustername + "/" + username
caCertBytes, err := tls.CertAsBytes(caCert)
if err != nil {
return nil, err
}
clientCertBytes, err := tls.CertAsBytes(clientCert)
if err != nil {
return nil, err
}
clientKeyBytes, err := tls.PrivateKeyAsBytes(clientKey)
if err != nil {
return nil, err
}
return &v1.Config{
APIVersion: "v1",
Kind: "Config",
Clusters: []v1.NamedCluster{
{
Name: clustername,
Cluster: v1.Cluster{
Server: "https://" + endpoint,
CertificateAuthorityData: caCertBytes,
},
},
},
AuthInfos: []v1.NamedAuthInfo{
{
Name: authinfoname,
AuthInfo: v1.AuthInfo{
ClientCertificateData: clientCertBytes,
ClientKeyData: clientKeyBytes,
},
},
},
Contexts: []v1.NamedContext{
{
Name: contextname,
Context: v1.Context{
Cluster: clustername,
Namespace: namespace,
AuthInfo: authinfoname,
},
},
},
CurrentContext: contextname,
}, nil
}