This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
/
config.go
79 lines (69 loc) · 2.08 KB
/
config.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
package kubernetes
import (
client "k8s.io/client-go/tools/clientcmd"
api "k8s.io/client-go/tools/clientcmd/api"
"os"
)
// KubeConfig holds a kubeconfig and its file name
type KubeConfig struct {
// FileName is the file name of the kubeconfig file
FileName string
config *api.Config
}
// GetDefaultKubeConfigFileName gets the default file name of the kubeconfig
func GetDefaultKubeConfigFileName() string {
return client.RecommendedHomeFile
}
// GetKubeConfig loads a kubeconfig from the given file or a default file
func GetKubeConfig(argument string) (*KubeConfig, error) {
var fileName string
var config *api.Config
var error error
rules := client.NewDefaultClientConfigLoadingRules()
if argument != "" {
fileName = argument
if _, error = os.Stat(argument); os.IsNotExist(error) {
error = nil
config = api.NewConfig()
} else if error == nil {
rules.ExplicitPath = argument
config, error = rules.Load()
}
} else {
fileName = client.RecommendedHomeFile
config, error = rules.Load()
}
return &KubeConfig{
FileName: fileName,
config: config,
}, error
}
// UpdateKubeConfig updates the local kubeconfig (~/.kube/config) with the given user.
func UpdateKubeConfig(kubeconfig *KubeConfig, user UserData, updateContext bool, serverName string) {
authInfo := api.NewAuthInfo()
if current, ok := kubeconfig.config.AuthInfos[user.Name]; ok {
authInfo = current
}
authInfo.AuthProvider = &api.AuthProviderConfig{
Name: "oidc",
Config: map[string]string{
"client-id": user.ClientID,
"client-secret": user.ClientSecret,
"id-token": user.IDToken,
"refresh-token": user.RefreshToken,
"idp-issuer-url": user.IssuerURL,
},
}
kubeconfig.config.AuthInfos[user.Name] = authInfo
if updateContext {
context := api.NewContext()
if current, ok := kubeconfig.config.Contexts[serverName]; ok {
context = current
}
context.Cluster = serverName
context.AuthInfo = user.Name
kubeconfig.config.Contexts[serverName] = context
kubeconfig.config.CurrentContext = serverName
}
client.WriteToFile(*kubeconfig.config, kubeconfig.FileName)
}