Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update kubernetes client creation for single namespace: #536

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading