-
Notifications
You must be signed in to change notification settings - Fork 9
/
clusters_get_credentials.go
52 lines (43 loc) · 1.48 KB
/
clusters_get_credentials.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
package cd
import (
"fmt"
gqlclient "github.com/pluralsh/console-client-go"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
func SaveClusterKubeconfig(cluster *gqlclient.ClusterFragment, token string) error {
configAccess := clientcmd.NewDefaultPathOptions()
config, err := configAccess.GetStartingConfig()
if err != nil {
return fmt.Errorf("cannot read kubeconfig: %w", err)
}
if config == nil {
config = &clientcmdapi.Config{}
}
// TODO: We should additionally set CertificateAuthority for Cluster.
configCluster := clientcmdapi.NewCluster()
configCluster.Server = *cluster.KasURL
if config.Clusters == nil {
config.Clusters = make(map[string]*clientcmdapi.Cluster)
}
config.Clusters[cluster.Name] = configCluster
configAuthInfo := clientcmdapi.NewAuthInfo()
configAuthInfo.Token = fmt.Sprintf("plrl:%s:%s", cluster.ID, token)
if config.AuthInfos == nil {
config.AuthInfos = make(map[string]*clientcmdapi.AuthInfo)
}
config.AuthInfos[cluster.Name] = configAuthInfo
configContext := clientcmdapi.NewContext()
configContext.Cluster = cluster.Name
configContext.AuthInfo = cluster.Name
if config.Contexts == nil {
config.Contexts = make(map[string]*clientcmdapi.Context)
}
config.Contexts[cluster.Name] = configContext
config.CurrentContext = cluster.Name
if err := clientcmd.ModifyConfig(configAccess, *config, true); err != nil {
return err
}
fmt.Printf("set your kubectl context to %s\n", cluster.Name)
return nil
}