Skip to content

Commit

Permalink
Merge pull request #536 from jacobweinstock/k8s-client
Browse files Browse the repository at this point in the history
Update kubernetes client creation for single namespace:

## Description

<!--- Please describe what this PR is going to change -->
Single namespace look ups weren't functioning properly. They function properly now.

## Why is this needed

<!--- Link to issue you have raised -->

Fixes: #

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->


## How are existing users impacted? What migration steps/scripts do we need?

<!--- Fixes a bug, unblocks installation, removes a component of the stack etc -->
<!--- Requires a DB migration script, etc. -->


## Checklist:

I have:

- [ ] updated the documentation and/or roadmap (if required)
- [ ] added unit or e2e tests
- [ ] provided instructions on how to upgrade
  • Loading branch information
jacobweinstock authored Oct 15, 2024
2 parents 2a58e52 + 6f17387 commit 87fde25
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 87fde25

Please sign in to comment.