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

Use global kubernetes client for IPAM plugin #189

Merged
merged 1 commit into from
Sep 25, 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
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
Loading