From ed0bc7bc32f2141c002a9200c50c7ab4bd02095a Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Tue, 10 Sep 2024 07:09:01 +0200 Subject: [PATCH] Introduce LoadRESTConfig function pointer This way, the way the REST config is loaded is up to the client factory users. The special "admin" settings are now part of the controller command, freeing the client factory from any particular way of loading and setting up the config. Signed-off-by: Tom Wieczorek --- cmd/controller/controller.go | 16 +++++++++++++++- pkg/kubernetes/client.go | 19 +++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/cmd/controller/controller.go b/cmd/controller/controller.go index efe9853fbeee..fd477b83b535 100644 --- a/cmd/controller/controller.go +++ b/cmd/controller/controller.go @@ -58,6 +58,7 @@ import ( "github.com/k0sproject/k0s/pkg/token" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "k8s.io/client-go/rest" ) type command config.CLIOptions @@ -162,7 +163,20 @@ func (c *command) start(ctx context.Context) error { }() // common factory to get the admin kube client that's needed in many components - adminClientFactory := kubernetes.NewAdminClientFactory(c.K0sVars.AdminKubeConfigPath) + adminClientFactory := &kubernetes.ClientFactory{LoadRESTConfig: func() (*rest.Config, error) { + config, err := kubernetes.ClientConfig(kubernetes.KubeconfigFromFile(c.K0sVars.AdminKubeConfigPath)) + if err != nil { + return nil, err + } + + // We're always running the client on the same host as the API, no need to compress + config.DisableCompression = true + // To mitigate stack applier bursts in startup + config.QPS = 40.0 + config.Burst = 400.0 + + return config, nil + }} certificateManager := certificate.Manager{K0sVars: c.K0sVars} diff --git a/pkg/kubernetes/client.go b/pkg/kubernetes/client.go index c6379c480896..bee49d19b23f 100644 --- a/pkg/kubernetes/client.go +++ b/pkg/kubernetes/client.go @@ -17,7 +17,6 @@ limitations under the License. package kubernetes import ( - "fmt" "sync" k0sclientset "github.com/k0sproject/k0s/pkg/client/clientset" @@ -44,18 +43,11 @@ type ClientFactoryInterface interface { GetEtcdMemberClient() (etcdMemberClient.EtcdMemberInterface, error) // Deprecated: Use [ClientFactoryInterface.GetK0sClient] instead. } -// NewAdminClientFactory creates a new factory that loads the admin kubeconfig based client -func NewAdminClientFactory(kubeconfigPath string) ClientFactoryInterface { - return &ClientFactory{ - configPath: kubeconfigPath, - } -} - // ClientFactory implements a cached and lazy-loading ClientFactory for all the different types of kube clients we use // It's imoplemented as lazy-loading so we can create the factory itself before we have the api, etcd and other components up so we can pass // the factory itself to components needing kube clients and creation time. type ClientFactory struct { - configPath string + LoadRESTConfig func() (*rest.Config, error) client kubernetes.Interface dynamicClient dynamic.Interface @@ -190,15 +182,10 @@ func (c *ClientFactory) getRESTConfig() (*rest.Config, error) { return c.restConfig, nil } - config, err := clientcmd.BuildConfigFromFlags("", c.configPath) + config, err := c.LoadRESTConfig() if err != nil { - return nil, fmt.Errorf("failed to load kubeconfig: %w", err) + return nil, err } - // We're always running the client on the same host as the API, no need to compress - config.DisableCompression = true - // To mitigate stack applier bursts in startup - config.QPS = 40.0 - config.Burst = 400.0 c.restConfig = config