Skip to content

Commit

Permalink
Testing INSTALL-EBPF-XDP#12
Browse files Browse the repository at this point in the history
  • Loading branch information
vpidatala94 committed Feb 17, 2025
1 parent 0608d49 commit f8cbbc9
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions test/e2e/framework/kubernetes/apply-yaml-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
)

Expand All @@ -34,16 +35,23 @@ func (a *ApplyYamlConfig) Run() error {
return fmt.Errorf("error building kubeconfig: %w", err)
}

clientset, err := kubernetes.NewForConfig(config)
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return fmt.Errorf("error creating Kubernetes client: %w", err)
return fmt.Errorf("error creating dynamic client: %w", err)
}

dynamicClient, err := dynamic.NewForConfig(config)
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return fmt.Errorf("error creating dynamic client: %w", err)
return fmt.Errorf("error creating discovery client: %w", err)
}

resources, err := restmapper.GetAPIGroupResources(discoveryClient)
if err != nil {
return fmt.Errorf("error getting API group resources: %w", err)
}

mapper := restmapper.NewDiscoveryRESTMapper(resources)

yamlFile, err := os.ReadFile(a.YamlFilePath)
if err != nil {
return fmt.Errorf("error reading YAML file: %w", err)
Expand All @@ -56,20 +64,26 @@ func (a *ApplyYamlConfig) Run() error {
return fmt.Errorf("error decoding YAML file: %w", err)
}

// Get GroupVersionResource to invoke the dynamic client
gvk := rawObj.GroupVersionKind()
mapping, err := clientset.RESTMapper().RESTMapping(gvk.GroupKind(), gvk.Version)
restMapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return fmt.Errorf("error getting REST mapping: %w", err)
}
gvr := restMapping.Resource

resourceInterface := dynamicClient.Resource(mapping.Resource).Namespace(rawObj.GetNamespace())
_, err = resourceInterface.Create(ctx, &rawObj, metav1.CreateOptions{})
// Apply the YAML document
namespace := rawObj.GetNamespace()
if len(namespace) == 0 {
namespace = "default"
}
applyOpts := metav1.ApplyOptions{FieldManager: "kube-apply"}
_, err = dynamicClient.Resource(gvr).Namespace(namespace).Apply(ctx, rawObj.GetName(), &rawObj, applyOpts)
if err != nil {
return fmt.Errorf("error applying YAML file: %w", err)
return fmt.Errorf("apply error: %w", err)
}

log.Printf("applied YAML file: %s\n", a.YamlFilePath)

return nil
}

Expand Down

0 comments on commit f8cbbc9

Please sign in to comment.