mirror of
https://github.com/intel/intel-device-plugins-for-kubernetes.git
synced 2025-06-03 03:59:37 +00:00
e2e/utils: Modify overlay customization
Instead creating another overlay, copy the existing overlay and modify it. This helps with multi-level overlays with specific namespace selections. Co-authored-by: Mikko Ylinen <mikko.ylinen@intel.com> Signed-off-by: Tuomas Katila <tuomas.katila@intel.com>
This commit is contained in:
parent
8f047c3c06
commit
d865a1b402
@ -17,7 +17,7 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -25,6 +25,7 @@ import (
|
|||||||
|
|
||||||
"github.com/onsi/gomega"
|
"github.com/onsi/gomega"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
@ -118,17 +119,85 @@ func LocateRepoFile(repopath string) (string, error) {
|
|||||||
return "", errors.New("no file found, try to define PLUGINS_REPO_DIR pointing to the root of the repository")
|
return "", errors.New("no file found, try to define PLUGINS_REPO_DIR pointing to the root of the repository")
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateKustomizationOverlay creates an overlay with overridden namespace.
|
func copyFiles(srcDir, dstDir string) error {
|
||||||
func CreateKustomizationOverlay(namespace, base, overlay string) error {
|
err := filepath.WalkDir(srcDir, func(path string, d os.DirEntry, err error) error {
|
||||||
relPath := ""
|
if d.IsDir() || err != nil {
|
||||||
for range strings.Split(overlay[1:], "/") {
|
return nil
|
||||||
relPath = relPath + "../"
|
}
|
||||||
|
|
||||||
|
n, err := os.ReadFile(path)
|
||||||
|
if err != nil && err != io.EOF || len(n) == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fn := filepath.Join(dstDir, filepath.Base(path))
|
||||||
|
|
||||||
|
if err := os.WriteFile(fn, n, 0600); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateKustomizationOverlay copies the base overlay, and changes the namespace
|
||||||
|
// and relative paths to resources. The deletion of the files is left for the caller.
|
||||||
|
func CreateKustomizationOverlay(namespace, kustomizeYamlFileDir, overlayDir string) error {
|
||||||
|
relPath, err := filepath.Rel(overlayDir, kustomizeYamlFileDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
relPath = relPath + base[1:]
|
// Copy all files under the kustomize path under the temp overlay path.
|
||||||
content := fmt.Sprintf("namespace: %s\nresources:\n - %s", namespace, relPath)
|
err = copyFiles(kustomizeYamlFileDir, overlayDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return os.WriteFile(overlay+"/kustomization.yaml", []byte(content), 0600)
|
kustomizationFile := filepath.Join(overlayDir, "kustomization.yaml")
|
||||||
|
|
||||||
|
bytes, err := os.ReadFile(kustomizationFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
content := make(map[string]interface{})
|
||||||
|
|
||||||
|
err = yaml.Unmarshal(bytes, content)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
content["namespace"] = namespace
|
||||||
|
|
||||||
|
resInterface := content["resources"].([]interface{})
|
||||||
|
resources := make([]string, len(resInterface))
|
||||||
|
|
||||||
|
for i, v := range resInterface {
|
||||||
|
resources[i] = v.(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add relative path for directories. Leave local (.yaml) files as they are.
|
||||||
|
for i, res := range resources {
|
||||||
|
if !strings.HasSuffix(res, ".yaml") {
|
||||||
|
resources[i] = relPath + "/" + res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
content["resources"] = resources
|
||||||
|
|
||||||
|
bytes, err = yaml.Marshal(content)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.WriteFile(kustomizationFile, bytes, 0600); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeployWebhook deploys an admission webhook to a framework-specific namespace.
|
// DeployWebhook deploys an admission webhook to a framework-specific namespace.
|
||||||
@ -145,6 +214,7 @@ func DeployWebhook(ctx context.Context, f *framework.Framework, kustomizationPat
|
|||||||
|
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
|
// The overlay files are deleted by the deferred RemoveAll call above.
|
||||||
err = CreateKustomizationOverlay(f.Namespace.Name, filepath.Dir(kustomizationPath), tmpDir)
|
err = CreateKustomizationOverlay(f.Namespace.Name, filepath.Dir(kustomizationPath), tmpDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
framework.Failf("unable to kustomization overlay: %v", err)
|
framework.Failf("unable to kustomization overlay: %v", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user