forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubeclient.go
93 lines (82 loc) · 2.54 KB
/
kubeclient.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
package client
import (
"fmt"
"os"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/client"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
// UpdateKubeconfig updates kubernetes kube config
func UpdateKubeconfig(tc *client.TeleportClient) error {
config, err := LoadKubeConfig()
if err != nil {
return trace.Wrap(err)
}
clusterName := tc.ProxyHost()
if tc.SiteName != "" && tc.SiteName != clusterName {
clusterName = fmt.Sprintf("%v.%v", tc.SiteName, tc.ProxyHost())
}
clusterAddr := fmt.Sprintf("https://%v:%v", clusterName, tc.ProxyKubePort())
creds, err := tc.LocalAgent().GetKey()
if err != nil {
return trace.Wrap(err)
}
certAuthorities, err := tc.LocalAgent().GetCertsPEM()
if err != nil {
return trace.Wrap(err)
}
config.AuthInfos[clusterName] = &clientcmdapi.AuthInfo{
ClientCertificateData: creds.TLSCert,
ClientKeyData: creds.Priv,
}
config.Clusters[clusterName] = &clientcmdapi.Cluster{
Server: clusterAddr,
CertificateAuthorityData: certAuthorities,
}
lastContext := config.Contexts[clusterName]
newContext := &clientcmdapi.Context{
Cluster: clusterName,
AuthInfo: clusterName,
}
if lastContext != nil {
newContext.Namespace = lastContext.Namespace
newContext.Extensions = lastContext.Extensions
}
config.Contexts[clusterName] = newContext
config.CurrentContext = clusterName
return SaveKubeConfig(*config)
}
// LoadKubeconfig tries to read a kubeconfig file and if it can't, returns an error.
// One exception, missing files result in empty configs, not an error.
func LoadKubeConfig() (*clientcmdapi.Config, error) {
filename, err := utils.EnsureLocalPath(
os.Getenv(teleport.EnvKubeConfig), teleport.KubeConfigDir, teleport.KubeConfigFile)
if err != nil {
return nil, trace.Wrap(err)
}
config, err := clientcmd.LoadFromFile(filename)
if err != nil && !os.IsNotExist(err) {
return nil, trace.ConvertSystemError(err)
}
if config == nil {
config = clientcmdapi.NewConfig()
}
return config, nil
}
// SaveKubeConfig saves updated config to location specified by environment variable or
// default location
func SaveKubeConfig(config clientcmdapi.Config) error {
filename, err := utils.EnsureLocalPath(
os.Getenv(teleport.EnvKubeConfig), teleport.KubeConfigDir, teleport.KubeConfigFile)
if err != nil {
return trace.Wrap(err)
}
err = clientcmd.WriteToFile(config, filename)
if err != nil {
return trace.ConvertSystemError(err)
}
return nil
}