From a0ef530c8734c68febc14fcd985d89c49a18f5ba Mon Sep 17 00:00:00 2001 From: WangLe1321 Date: Fri, 28 Jul 2023 11:15:06 +0800 Subject: [PATCH] br: modify to use one kubeconfig for multiple data planes (#5186) --- .../templates/controller-manager-deployment.yaml | 3 +++ charts/br-federation/values.yaml | 5 ++++- cmd/br-federation-manager/main.go | 15 ++++++--------- pkg/controller/br_fed_config.go | 6 +++--- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/charts/br-federation/templates/controller-manager-deployment.yaml b/charts/br-federation/templates/controller-manager-deployment.yaml index fa08cf741d9..b9842d476da 100644 --- a/charts/br-federation/templates/controller-manager-deployment.yaml +++ b/charts/br-federation/templates/controller-manager-deployment.yaml @@ -78,6 +78,9 @@ spec: {{- if .Values.brFederationManager.kubeClientBurst }} - -kube-client-burst={{ .Values.brFederationManager.kubeClientBurst }} {{- end }} + {{- if .Values.brFederationManager.federationKubeconfigSecretKey }} + - -federation-kubeconfig-path=/etc/br-federation/federation-kubeconfig/{{ .Values.brFederationManager.federationKubeconfigSecretKey }} + {{- end }} env: - name: NAMESPACE valueFrom: diff --git a/charts/br-federation/values.yaml b/charts/br-federation/values.yaml index 40a7fe1f7bb..75351ccd088 100644 --- a/charts/br-federation/values.yaml +++ b/charts/br-federation/values.yaml @@ -20,8 +20,11 @@ brFederationManager: serviceAccount: br-federation-manager # Secret name of the kubeconfig for the federation Kubernetes clusters - # The data item key is the cluster name, and the data item value is the base64 encoded kubeconfig federationKubeconfigSecret: br-federation-kubeconfig + # which data item is the kubeconfig file, and the data item value is the base64 encoded kubeconfig + # if you have multiple kubernetes clusters, you should merge them in one kubeconfig + # we use the context name in the kubeconfig as the k8sClusterName in volume backup/restore CR + federationKubeconfigSecretKey: kubeconfig logLevel: 2 replicas: 1 diff --git a/cmd/br-federation-manager/main.go b/cmd/br-federation-manager/main.go index ab62810a4cd..98b169edda6 100644 --- a/cmd/br-federation-manager/main.go +++ b/cmd/br-federation-manager/main.go @@ -20,7 +20,6 @@ import ( _ "net/http/pprof" "os" "os/signal" - "path/filepath" "reflect" "syscall" @@ -219,18 +218,16 @@ func main() { } func initFederationKubeClients(cliCfg *controller.BrFedCLIConfig) (map[string]fedversioned.Interface, error) { - files, err := os.ReadDir(cliCfg.FederationKubeConfigPath) + kubeConfig, err := clientcmd.LoadFromFile(cliCfg.FederationKubeConfigPath) if err != nil { return nil, err } clients := make(map[string]fedversioned.Interface) - for _, f := range files { - if f.IsDir() || f.Name() == "..data" { - continue - } - - cfg, err := clientcmd.BuildConfigFromFlags("", filepath.Join(cliCfg.FederationKubeConfigPath, f.Name())) + for contextName := range kubeConfig.Contexts { + cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + &clientcmd.ClientConfigLoadingRules{ExplicitPath: cliCfg.FederationKubeConfigPath}, + &clientcmd.ConfigOverrides{CurrentContext: contextName}).ClientConfig() if err != nil { return nil, err // return error if any kube client init failed } @@ -243,7 +240,7 @@ func initFederationKubeClients(cliCfg *controller.BrFedCLIConfig) (map[string]fe if err != nil { return nil, err } - clients[f.Name()] = cli + clients[contextName] = cli } return clients, nil diff --git a/pkg/controller/br_fed_config.go b/pkg/controller/br_fed_config.go index 25e2f393610..41556d6f6e7 100644 --- a/pkg/controller/br_fed_config.go +++ b/pkg/controller/br_fed_config.go @@ -19,7 +19,7 @@ import ( ) const ( - defaultFederationKubeConfigPath = "/etc/br-federation/federation-kubeconfig" + defaultFederationKubeConfigPath = "/etc/br-federation/federation-kubeconfig/kubeconfig" ) // BrFedCLIConfig is used to save the configuration of br-federation-manager read from command line. @@ -41,7 +41,7 @@ type BrFedCLIConfig struct { KubeClientQPS float64 KubeClientBurst int - // FederationKubeConfigPath is the path to the directory containing the federation kubeconfig files + // FederationKubeConfigPath is the path to the kubeconfig file of data planes FederationKubeConfigPath string } @@ -73,5 +73,5 @@ func (c *BrFedCLIConfig) AddFlag(_ *flag.FlagSet) { flag.Float64Var(&c.KubeClientQPS, "kube-client-qps", c.KubeClientQPS, "The maximum QPS to the kubenetes API server from client") flag.IntVar(&c.KubeClientBurst, "kube-client-burst", c.KubeClientBurst, "The maximum burst for throttle to the kubenetes API server from client") - flag.StringVar(&c.FederationKubeConfigPath, "federation-kubeconfig-path", c.FederationKubeConfigPath, "The path to the directory containing the federation kubeconfig files") + flag.StringVar(&c.FederationKubeConfigPath, "federation-kubeconfig-path", c.FederationKubeConfigPath, "The path to the kubeconfig file of data planes") }