diff --git a/cmd/smee/backend.go b/cmd/smee/backend.go index 94412fb8..7eac7efb 100644 --- a/cmd/smee/backend.go +++ b/cmd/smee/backend.go @@ -8,9 +8,14 @@ import ( "github.com/tinkerbell/smee/internal/backend/kube" "github.com/tinkerbell/smee/internal/backend/noop" "github.com/tinkerbell/smee/internal/dhcp/handler" + "github.com/tinkerbell/tink/api/v1alpha1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/rest" + "k8s.io/client-go/scale/scheme" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + "sigs.k8s.io/controller-runtime/pkg/cache" + "sigs.k8s.io/controller-runtime/pkg/cluster" ) type Kube struct { @@ -38,26 +43,20 @@ func (n *Noop) backend() handler.BackendReader { } func (k *Kube) getClient() (*rest.Config, error) { - ccfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - &clientcmd.ClientConfigLoadingRules{ - ExplicitPath: k.ConfigFilePath, + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() + loadingRules.ExplicitPath = k.ConfigFilePath + + overrides := &clientcmd.ConfigOverrides{ + ClusterInfo: clientcmdapi.Cluster{ + Server: k.APIURL, }, - &clientcmd.ConfigOverrides{ - ClusterInfo: clientcmdapi.Cluster{ - Server: k.APIURL, - }, - Context: clientcmdapi.Context{ - Namespace: k.Namespace, - }, + Context: clientcmdapi.Context{ + Namespace: k.Namespace, }, - ) - - config, err := ccfg.ClientConfig() - if err != nil { - return nil, err } + loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides) - return config, nil + return loader.ClientConfig() } func (k *Kube) backend(ctx context.Context) (handler.BackendReader, error) { @@ -66,7 +65,24 @@ func (k *Kube) backend(ctx context.Context) (handler.BackendReader, error) { return nil, err } - kb, err := kube.NewBackend(config) + rs := runtime.NewScheme() + + if err := scheme.AddToScheme(rs); err != nil { + return nil, err + } + + if err := v1alpha1.AddToScheme(rs); err != nil { + return nil, err + } + + conf := func(opts *cluster.Options) { + opts.Scheme = rs + if k.Namespace != "" { + opts.Cache.DefaultNamespaces = map[string]cache.Config{k.Namespace: {}} + } + } + + kb, err := kube.NewBackend(config, conf) if err != nil { return nil, err } diff --git a/internal/backend/kube/kube.go b/internal/backend/kube/kube.go index 4c1ceb09..a0ebffba 100644 --- a/internal/backend/kube/kube.go +++ b/internal/backend/kube/kube.go @@ -14,8 +14,6 @@ import ( "github.com/tinkerbell/tink/api/v1alpha1" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/codes" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/cluster" @@ -35,20 +33,7 @@ type Backend struct { // // Callers must instantiate the client-side cache by calling Start() before use. func NewBackend(conf *rest.Config, opts ...cluster.Option) (*Backend, error) { - rs := runtime.NewScheme() - - if err := scheme.AddToScheme(rs); err != nil { - return nil, err - } - - if err := v1alpha1.AddToScheme(rs); err != nil { - return nil, err - } - - opts = append([]cluster.Option{func(o *cluster.Options) { o.Scheme = rs }}, opts...) - o := []cluster.Option{func(o *cluster.Options) { o.Scheme = rs }} - o = append(o, opts...) - c, err := cluster.New(conf, o...) + c, err := cluster.New(conf, opts...) if err != nil { return nil, fmt.Errorf("failed to create new cluster config: %w", err) }