github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/infrastructure/tasks/kubeconfig.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package tasks
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"time"
    26  
    27  	"k8s.io/client-go/tools/clientcmd"
    28  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    29  
    30  	"github.com/1aal/kubeblocks/pkg/cli/util"
    31  	cfgcore "github.com/1aal/kubeblocks/pkg/configuration/core"
    32  )
    33  
    34  func kubeconfigMerge(newKubeConfig *clientcmdapi.Config, existingKubeConfig *clientcmdapi.Config, outPath string) error {
    35  	// merge clusters
    36  	for k, v := range newKubeConfig.Clusters {
    37  		if _, ok := existingKubeConfig.Clusters[k]; ok {
    38  			return cfgcore.MakeError("Cluster '%s' already exists in target KubeConfig", k)
    39  		}
    40  		existingKubeConfig.Clusters[k] = v
    41  	}
    42  
    43  	// merge auth
    44  	for k, v := range newKubeConfig.AuthInfos {
    45  		if _, ok := existingKubeConfig.AuthInfos[k]; ok {
    46  			return cfgcore.MakeError("AuthInfo '%s' already exists in target KubeConfig", k)
    47  		}
    48  		existingKubeConfig.AuthInfos[k] = v
    49  	}
    50  
    51  	// merge contexts
    52  	for k, v := range newKubeConfig.Contexts {
    53  		if _, ok := existingKubeConfig.Contexts[k]; ok {
    54  			return cfgcore.MakeError("Context '%s' already exists in target KubeConfig", k)
    55  		}
    56  		existingKubeConfig.Contexts[k] = v
    57  	}
    58  
    59  	existingKubeConfig.CurrentContext = newKubeConfig.CurrentContext
    60  	return kubeconfigWrite(existingKubeConfig, outPath)
    61  }
    62  
    63  func kubeconfigWrite(config *clientcmdapi.Config, path string) error {
    64  	tempPath := fmt.Sprintf("%s.kb_%s", path, time.Now().Format("20060102_150405.000000"))
    65  	if err := clientcmd.WriteToFile(*config, tempPath); err != nil {
    66  		return cfgcore.WrapError(err, "failed to write merged kubeconfig to temporary file '%s'", tempPath)
    67  	}
    68  
    69  	// Move temporary file over existing KubeConfig
    70  	if err := os.Rename(tempPath, path); err != nil {
    71  		return cfgcore.WrapError(err, "failed to overwrite existing KubeConfig '%s' with new kubeconfig '%s'", path, tempPath)
    72  	}
    73  	return nil
    74  }
    75  
    76  func GetDefaultConfig() string {
    77  	defaultKubeConfigLoadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
    78  	kcFile := defaultKubeConfigLoadingRules.GetDefaultFilename()
    79  	if kcFile == "" {
    80  		kcFile = util.GetKubeconfigDir()
    81  	}
    82  	return kcFile
    83  }
    84  
    85  func kubeconfigLoad(kcFile string) (*clientcmdapi.Config, error) {
    86  	if _, err := os.Stat(kcFile); err == nil {
    87  		return clientcmd.LoadFromFile(kcFile)
    88  	}
    89  	return nil, nil
    90  }