Path: blob/main/dev/preview/previewctl/pkg/k8s/context.go
2501 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package k8s56import (7"fmt"89"github.com/imdario/mergo"10"k8s.io/client-go/tools/clientcmd"11"k8s.io/client-go/tools/clientcmd/api"12)1314func RenameConfig(config *api.Config, oldName, newName string) (*api.Config, error) {15// TODO: https://github.com/gitpod-io/ops/issues/652416kubeCtx, exists := config.Contexts[oldName]17if !exists {18return nil, fmt.Errorf("cannot rename %q, it's not in the provided context", oldName)19}2021if _, newExists := config.Contexts[newName]; newExists {22return nil, fmt.Errorf("cannot rename %q, it already exists in the provided context", oldName)23}2425kubeCtx.Cluster = newName26kubeCtx.AuthInfo = newName27config.Contexts[newName] = kubeCtx28delete(config.Contexts, oldName)2930if config.CurrentContext == oldName {31config.CurrentContext = newName32}3334// we need to overwrite the cluster name and auth info35// as otherwise another context might use the wrong cluster/auth (e.g. if they are called default)36cluster, exists := config.Clusters[oldName]37if !exists {38return nil, fmt.Errorf("cannot rename %q, it's not in the provided context", oldName)39}4041if _, newExists := config.Clusters[newName]; newExists {42return nil, fmt.Errorf("cannot rename %q, it already exists in the provided context", oldName)43}4445config.Clusters[newName] = cluster46delete(config.Clusters, oldName)4748auth, exists := config.AuthInfos[oldName]49if !exists {50return nil, fmt.Errorf("cannot rename %q, it's not in the provided context", oldName)51}5253if _, newExists := config.AuthInfos[newName]; newExists {54return nil, fmt.Errorf("cannot rename %q, it already exists in the provided context", oldName)55}5657config.AuthInfos[newName] = auth58delete(config.AuthInfos, oldName)5960return config, nil61}6263func MergeContextsWithDefault(configs ...*api.Config) (*api.Config, error) {64// TODO: https://github.com/gitpod-io/ops/issues/652465defaultConfig, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()66if err != nil {67return nil, err68}6970mapConfig := api.NewConfig()71err = mergo.Merge(mapConfig, defaultConfig, mergo.WithOverride)72if err != nil {73return nil, err74}7576// If the same contexts exist in the default config, we'll override them with the configs we merge77for _, config := range configs {78err = mergo.Merge(mapConfig, config, mergo.WithOverride)79if err != nil {80return nil, err81}82}8384return mapConfig, nil85}8687func OutputContext(kubeConfigSavePath string, config *api.Config) error {88if kubeConfigSavePath != "" {89return clientcmd.WriteToFile(*config, kubeConfigSavePath)90}9192bytes, err := clientcmd.Write(*config)93if err != nil {94return err95}9697fmt.Println(string(bytes))9899return err100}101102func DeleteContext(config *api.Config, name string) {103_, exists := config.Contexts[name]104if !exists {105return106}107108delete(config.Contexts, name)109delete(config.Clusters, name)110}111112113