From fe87520df554db9ed29a2f7c8f1760f9d831d86c Mon Sep 17 00:00:00 2001 From: Markus Rudy Date: Fri, 20 Dec 2024 17:17:27 +0100 Subject: [PATCH] e2e: create DynamicClient directly --- e2e/internal/kubeclient/deploy.go | 3 +-- e2e/internal/kubeclient/kubeclient.go | 10 ++++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/e2e/internal/kubeclient/deploy.go b/e2e/internal/kubeclient/deploy.go index 028d328f5e..3e226d0b39 100644 --- a/e2e/internal/kubeclient/deploy.go +++ b/e2e/internal/kubeclient/deploy.go @@ -491,7 +491,6 @@ func isPodReady(pod *corev1.Pod) bool { } func (c *Kubeclient) resourceInterfaceFor(obj *unstructured.Unstructured) (dynamic.ResourceInterface, error) { - dyn := dynamic.New(c.Client.RESTClient()) gvk := obj.GroupVersionKind() mapping, err := c.restMapper.RESTMapping(gvk.GroupKind(), gvk.Version) @@ -499,7 +498,7 @@ func (c *Kubeclient) resourceInterfaceFor(obj *unstructured.Unstructured) (dynam return nil, fmt.Errorf("getting resource for %#v: %w", gvk, err) } c.log.Info("found mapping", "resource", mapping.Resource) - ri := dyn.Resource(mapping.Resource) + ri := c.dyn.Resource(mapping.Resource) if mapping.Scope.Name() == "namespace" { namespace := obj.GetNamespace() if namespace == "" { diff --git a/e2e/internal/kubeclient/kubeclient.go b/e2e/internal/kubeclient/kubeclient.go index 36e8a4c5f0..213e5a8425 100644 --- a/e2e/internal/kubeclient/kubeclient.go +++ b/e2e/internal/kubeclient/kubeclient.go @@ -19,6 +19,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" @@ -37,14 +38,14 @@ type Kubeclient struct { restMapper meta.RESTMapper // config is the "Kubeconfig" for the client config *rest.Config + // dyn is a dynamic API client that can work with unstructured resources + dyn *dynamic.DynamicClient } // New creates a new Kubeclient from a given Kubeconfig. func New(config *rest.Config, log *slog.Logger) (*Kubeclient, error) { - client, err := kubernetes.NewForConfig(config) - if err != nil { - return nil, fmt.Errorf("creating kubernetes client: %w", err) - } + client := kubernetes.NewForConfigOrDie(config) + dyn := dynamic.NewForConfigOrDie(config) resources, err := restmapper.GetAPIGroupResources(client.Discovery()) if err != nil { @@ -56,6 +57,7 @@ func New(config *rest.Config, log *slog.Logger) (*Kubeclient, error) { Client: client, config: config, restMapper: restmapper.NewDiscoveryRESTMapper(resources), + dyn: dyn, }, nil }