Skip to content

Commit

Permalink
Update kubernetes client creation for single namespace:
Browse files Browse the repository at this point in the history
Single namespace look ups weren't functioning properly.
They function properly now.

Signed-off-by: Jacob Weinstock <jakobweinstock@gmail.com>
  • Loading branch information
jacobweinstock committed Oct 15, 2024
1 parent 2a58e52 commit 6f17387
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
50 changes: 33 additions & 17 deletions cmd/smee/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down
17 changes: 1 addition & 16 deletions internal/backend/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down

0 comments on commit 6f17387

Please sign in to comment.