Skip to content

Commit

Permalink
Introduce LoadRESTConfig function pointer
Browse files Browse the repository at this point in the history
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 <twieczorek@mirantis.com>
  • Loading branch information
twz123 committed Sep 21, 2024
1 parent e03c794 commit ed0bc7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
16 changes: 15 additions & 1 deletion cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}

Expand Down
19 changes: 3 additions & 16 deletions pkg/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package kubernetes

import (
"fmt"
"sync"

k0sclientset "github.com/k0sproject/k0s/pkg/client/clientset"
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit ed0bc7b

Please sign in to comment.