-
Notifications
You must be signed in to change notification settings - Fork 173
/
kube_config.go
122 lines (101 loc) · 3.58 KB
/
kube_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
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
package kubectl
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"gopkg.in/yaml.v3"
)
// Manages k8s configurations available to the k8s CLI
type KubeConfigManager struct {
cli KubectlCli
configPath string
}
// Creates a new instance of the KubeConfigManager
func NewKubeConfigManager(cli KubectlCli) (*KubeConfigManager, error) {
kubeConfigDir, err := getKubeConfigDir()
if err != nil {
return nil, err
}
return &KubeConfigManager{
cli: cli,
configPath: kubeConfigDir,
}, nil
}
// Parses the raw bytes into a KubeConfig instance
func ParseKubeConfig(ctx context.Context, raw []byte) (*KubeConfig, error) {
var existing KubeConfig
if err := yaml.Unmarshal(raw, &existing); err != nil {
return nil, fmt.Errorf("failed unmarshalling Kube Config YAML: %w", err)
}
return &existing, nil
}
// Saves the KubeConfig to the kube configuration folder with the specified name
func (kcm *KubeConfigManager) SaveKubeConfig(ctx context.Context, configName string, config *KubeConfig) (string, error) {
kubeConfigRaw, err := yaml.Marshal(config)
if err != nil {
return "", fmt.Errorf("failed marshalling KubeConfig to yaml: %w", err)
}
// Create .kube config folder if it doesn't already exist
if err := os.MkdirAll(kcm.configPath, osutil.PermissionDirectory); err != nil {
return "", fmt.Errorf("failed creating .kube config directory, %w", err)
}
outFilePath := filepath.Join(kcm.configPath, configName)
err = os.WriteFile(outFilePath, kubeConfigRaw, osutil.PermissionFile)
if err != nil {
return "", fmt.Errorf("failed writing kube config file: %w", err)
}
return outFilePath, nil
}
// Deletes the KubeConfig with the specified name
func (kcm *KubeConfigManager) DeleteKubeConfig(ctx context.Context, configName string) error {
kubeConfigPath := filepath.Join(kcm.configPath, configName)
err := os.Remove(kubeConfigPath)
if err != nil {
return fmt.Errorf("failed deleting kube config file: %w", err)
}
return nil
}
// Merges the specified kube configs into the kube config
// This powers the use of the kubectl config set of commands that allow developers to switch between different
// k8s cluster contexts
func (kcm *KubeConfigManager) MergeConfigs(ctx context.Context, newConfigName string, path ...string) (string, error) {
fullConfigPaths := []string{}
for _, kubeConfigName := range path {
fullConfigPaths = append(fullConfigPaths, filepath.Join(kcm.configPath, kubeConfigName))
}
kubeConfig := strings.Join(fullConfigPaths, string(os.PathListSeparator))
kcm.cli.SetKubeConfig(kubeConfig)
res, err := kcm.cli.ConfigView(ctx, true, true, nil)
if err != nil {
return "", fmt.Errorf("kubectl config view failed: %w", err)
}
kubeConfigRaw := []byte(res.Stdout)
outFilePath := filepath.Join(kcm.configPath, newConfigName)
err = os.WriteFile(outFilePath, kubeConfigRaw, osutil.PermissionFile)
if err != nil {
return "", fmt.Errorf("failed writing new kube config: %w", err)
}
return outFilePath, nil
}
// Adds a new or updates an existing KubeConfig in the main kube config
func (kcm *KubeConfigManager) AddOrUpdateContext(
ctx context.Context,
contextName string,
newKubeConfig *KubeConfig,
) (string, error) {
configPath, err := kcm.SaveKubeConfig(ctx, contextName, newKubeConfig)
if err != nil {
return "", fmt.Errorf("failed write new kube context file: %w", err)
}
return configPath, nil
}
func getKubeConfigDir() (string, error) {
userHomeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("cannot get user home directory: %w", err)
}
return filepath.Join(userHomeDir, ".kube"), nil
}