Skip to content

Commit

Permalink
e2e: create DynamicClient directly
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerdev committed Dec 23, 2024
1 parent 7e47825 commit 35ab185
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
3 changes: 1 addition & 2 deletions e2e/internal/kubeclient/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,14 @@ 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)
if err != nil {
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 == "" {
Expand Down
10 changes: 6 additions & 4 deletions e2e/internal/kubeclient/kubeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit 35ab185

Please sign in to comment.