Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/preview/previewctl/pkg/k8s/context.go
2501 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package k8s
6
7
import (
8
"fmt"
9
10
"github.com/imdario/mergo"
11
"k8s.io/client-go/tools/clientcmd"
12
"k8s.io/client-go/tools/clientcmd/api"
13
)
14
15
func RenameConfig(config *api.Config, oldName, newName string) (*api.Config, error) {
16
// TODO: https://github.com/gitpod-io/ops/issues/6524
17
kubeCtx, exists := config.Contexts[oldName]
18
if !exists {
19
return nil, fmt.Errorf("cannot rename %q, it's not in the provided context", oldName)
20
}
21
22
if _, newExists := config.Contexts[newName]; newExists {
23
return nil, fmt.Errorf("cannot rename %q, it already exists in the provided context", oldName)
24
}
25
26
kubeCtx.Cluster = newName
27
kubeCtx.AuthInfo = newName
28
config.Contexts[newName] = kubeCtx
29
delete(config.Contexts, oldName)
30
31
if config.CurrentContext == oldName {
32
config.CurrentContext = newName
33
}
34
35
// we need to overwrite the cluster name and auth info
36
// as otherwise another context might use the wrong cluster/auth (e.g. if they are called default)
37
cluster, exists := config.Clusters[oldName]
38
if !exists {
39
return nil, fmt.Errorf("cannot rename %q, it's not in the provided context", oldName)
40
}
41
42
if _, newExists := config.Clusters[newName]; newExists {
43
return nil, fmt.Errorf("cannot rename %q, it already exists in the provided context", oldName)
44
}
45
46
config.Clusters[newName] = cluster
47
delete(config.Clusters, oldName)
48
49
auth, exists := config.AuthInfos[oldName]
50
if !exists {
51
return nil, fmt.Errorf("cannot rename %q, it's not in the provided context", oldName)
52
}
53
54
if _, newExists := config.AuthInfos[newName]; newExists {
55
return nil, fmt.Errorf("cannot rename %q, it already exists in the provided context", oldName)
56
}
57
58
config.AuthInfos[newName] = auth
59
delete(config.AuthInfos, oldName)
60
61
return config, nil
62
}
63
64
func MergeContextsWithDefault(configs ...*api.Config) (*api.Config, error) {
65
// TODO: https://github.com/gitpod-io/ops/issues/6524
66
defaultConfig, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
67
if err != nil {
68
return nil, err
69
}
70
71
mapConfig := api.NewConfig()
72
err = mergo.Merge(mapConfig, defaultConfig, mergo.WithOverride)
73
if err != nil {
74
return nil, err
75
}
76
77
// If the same contexts exist in the default config, we'll override them with the configs we merge
78
for _, config := range configs {
79
err = mergo.Merge(mapConfig, config, mergo.WithOverride)
80
if err != nil {
81
return nil, err
82
}
83
}
84
85
return mapConfig, nil
86
}
87
88
func OutputContext(kubeConfigSavePath string, config *api.Config) error {
89
if kubeConfigSavePath != "" {
90
return clientcmd.WriteToFile(*config, kubeConfigSavePath)
91
}
92
93
bytes, err := clientcmd.Write(*config)
94
if err != nil {
95
return err
96
}
97
98
fmt.Println(string(bytes))
99
100
return err
101
}
102
103
func DeleteContext(config *api.Config, name string) {
104
_, exists := config.Contexts[name]
105
if !exists {
106
return
107
}
108
109
delete(config.Contexts, name)
110
delete(config.Clusters, name)
111
}
112
113