From f0643c64e1c506270862316fbebc8cfdea300e1a Mon Sep 17 00:00:00 2001 From: WangLe1321 Date: Thu, 20 Jul 2023 18:16:51 +0800 Subject: [PATCH] br: modify to use one kubeconfig for multiple data planes Signed-off-by: WangLe1321 --- .../templates/controller-manager-deployment.yaml | 3 +++ charts/br-federation/values.yaml | 1 + cmd/br-federation-manager/main.go | 15 ++++++--------- pkg/controller/br_fed_config.go | 6 +++--- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/charts/br-federation/templates/controller-manager-deployment.yaml b/charts/br-federation/templates/controller-manager-deployment.yaml index fa08cf741d..b9842d476d 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 3234847dc2..e699c4842e 100644 --- a/charts/br-federation/values.yaml +++ b/charts/br-federation/values.yaml @@ -22,6 +22,7 @@ brFederationManager: # 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 + federationKubeconfigSecretKey: kubeconfig logLevel: 2 replicas: 1 diff --git a/cmd/br-federation-manager/main.go b/cmd/br-federation-manager/main.go index ab62810a4c..98b169edda 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 25e2f39361..41556d6f6e 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") }