Skip to content

Commit

Permalink
Use global kubernetes client for IPAM plugin (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
damyan authored Sep 25, 2024
1 parent 39628e6 commit 30885e4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
6 changes: 5 additions & 1 deletion internal/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package kubernetes

import (
"fmt"
"k8s.io/client-go/rest"

ipamv1alpha1 "github.com/ironcore-dev/ipam/api/ipam/v1alpha1"
metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1"
Expand All @@ -17,6 +18,7 @@ import (
var (
scheme = runtime.NewScheme()
kubeClient client.Client
cfg *rest.Config
)

func init() {
Expand All @@ -25,7 +27,7 @@ func init() {
}

func InitClient() error {
cfg := config.GetConfigOrDie()
cfg = config.GetConfigOrDie()
var err error
kubeClient, err = client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
Expand All @@ -40,3 +42,5 @@ func SetClient(client *client.Client) {
}

func GetClient() client.Client { return kubeClient }

func GetConfig() *rest.Config { return cfg }
14 changes: 3 additions & 11 deletions plugins/ipam/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/ironcore-dev/fedhcp/internal/kubernetes"
"net"
"os"
"reflect"
Expand All @@ -24,7 +25,6 @@ import (
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

const (
Expand All @@ -41,16 +41,8 @@ type K8sClient struct {
}

func NewK8sClient(namespace string, subnetNames []string) (*K8sClient, error) {

if err := ipamv1alpha1.AddToScheme(scheme.Scheme); err != nil {
return nil, fmt.Errorf("unable to add registered types ipam to client scheme: %w", err)
}

cfg := config.GetConfigOrDie()
cl, err := client.New(cfg, client.Options{})
if err != nil {
return nil, fmt.Errorf("failed to create controller runtime client: %w", err)
}
cfg := kubernetes.GetConfig()
cl := kubernetes.GetClient()

clientset, err := ipam.NewForConfig(cfg)
if err != nil {
Expand Down

0 comments on commit 30885e4

Please sign in to comment.