Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ jobs:
- name: Golang Test Helper
if: ${{ needs.analyze-changes.outputs.go_helper_any_changed == 'true' }}
run: |
go test -v ./test
go test -v ./test -timeout 30m

- name: Upload KinD logs
if: always()
Expand Down
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
kubeconfig
hosts.yml
vendor/*
tls.crt
tls.key
public-key-cert.pem
**/kind-logs
*.crt
*.key
*.pem
**/kind-logs
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module test
module e2eutils

go 1.24.0

Expand Down
2 changes: 1 addition & 1 deletion kubernetes-services/templates/argo-rollouts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ spec:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
- CreateNamespace=true
30 changes: 0 additions & 30 deletions kubernetes-services/templates/kargo.yaml

This file was deleted.

8 changes: 4 additions & 4 deletions test/pkg/api/api.go → pkg/api.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package api
package e2eutils

import (
"context"
"errors"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -12,7 +13,6 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/e2e-framework/klient/k8s"
"test/test/pkg/test"
)

func Apply(clientset kubernetes.Clientset, object runtime.Object) error {
Expand All @@ -33,7 +33,7 @@ func Apply(clientset kubernetes.Clientset, object runtime.Object) error {
_, err := createPersistentVolumeClaim(clientset, *object.(*corev1.PersistentVolumeClaim))
return err
case *unstructured.Unstructured:
dynClient, err := test.GetDynClient()
dynClient, err := GetDynClient()
if err != nil {
return err
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func createPersistentVolumeClaim(clientset kubernetes.Clientset, object corev1.P
}

func getResourceName(object unstructured.Unstructured) (string, error) {
discoveryClient, err := test.GetDiscoveryClient()
discoveryClient, err := GetDiscoveryClient()
if err != nil {
return "", errors.New("Failed to get discovery client " + err.Error())
}
Expand Down
File renamed without changes.
31 changes: 28 additions & 3 deletions test/pkg/argo/argo.go → pkg/argo/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
"io"
"net/http"
"os"
"sigs.k8s.io/yaml"
"strconv"
"strings"

"sigs.k8s.io/yaml"
)

func GetArgoApplication(applicationYaml string) (Application, error) {
func GetArgoApplication(applicationYaml string) (
Application,
error,
) {
yamlFile, err := os.ReadFile(applicationYaml)
if err != nil {
return Application{}, errors.New("Failed to open application yaml file " + applicationYaml + ". " + err.Error())
Expand All @@ -25,7 +29,10 @@ func GetArgoApplication(applicationYaml string) (Application, error) {
return *argoApplication, nil
}

func GetArgoApplicationFromGit(gitRepository string, applicationYaml string) (Application, error) {
func GetArgoApplicationFromGit(gitRepository string, applicationYaml string) (
Application,
error,
) {
baseUrl := gitRepository + strings.TrimPrefix(applicationYaml, "../")
response, err := http.Get(baseUrl)
if err != nil {
Expand All @@ -49,3 +56,21 @@ func GetArgoApplicationFromGit(gitRepository string, applicationYaml string) (Ap

return *argoApplication, nil
}

func GatherArgoAppPaths(app Application) (
pathCollection []string,
) {
if app.Spec.Sources != nil {
for _, source := range app.Spec.Sources {
if source.Path != "" {
pathCollection = append(pathCollection, source.Path)
}
}
}

if app.Spec.Source != nil && app.Spec.Source.Path != "" {
pathCollection = append(pathCollection, app.Spec.Source.Path)
}

return pathCollection
}
40 changes: 40 additions & 0 deletions pkg/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package e2eutils

import (
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/e2e-framework/klient"
"sigs.k8s.io/e2e-framework/pkg/envconf"
)

func GetClient() klient.Client {
cfg := envconf.Config{}
return cfg.Client()
}

func GetRestConfig() *rest.Config {
client := GetClient()
return client.RESTConfig()
}

func GetClientSet() (*kubernetes.Clientset, error) {
kubeConfig := GetRestConfig()
clientSet, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return nil, err
}

return clientSet, nil
}

func GetDynClient() (*dynamic.DynamicClient, error) {
kubeConfig := GetRestConfig()
return dynamic.NewForConfig(kubeConfig)
}

func GetDiscoveryClient() (*discovery.DiscoveryClient, error) {
kubeConfig := GetRestConfig()
return discovery.NewDiscoveryClientForConfig(kubeConfig)
}
2 changes: 1 addition & 1 deletion test/pkg/git/git.go → pkg/git.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package git
package e2eutils

import (
"os/exec"
Expand Down
72 changes: 72 additions & 0 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package helm

import (
"fmt"
"os"
"path/filepath"
"strings"

"e2eutils/pkg/argo"

"sigs.k8s.io/e2e-framework/third_party/helm"
)

func NewHelmManager(kubeConfigFile string) *helm.Manager {
return helm.New(kubeConfigFile)
}

func AddHelmRepository(helmMgr *helm.Manager, repoURL, chartName string) error {
return helmMgr.RunRepo(helm.WithArgs("add", "--force-update", chartName, repoURL))
}

func DeployHelmChart(helmMgr *helm.Manager, src argo.ApplicationSource, namespace string) error {
opts, err := buildHelmOptions(src, namespace)
if err != nil {
return fmt.Errorf("building helm options: %w", err)
}

if err := helmMgr.RunUpgrade(opts...); err != nil {
return fmt.Errorf("helm upgrade/install failed: %w", err)
}

return nil
}

func buildHelmOptions(src argo.ApplicationSource, namespace string) ([]helm.Option, error) {
chart := ""
if strings.HasPrefix(src.RepoURL, "oci://") {
base := strings.TrimSuffix(src.RepoURL, "/")
chart = fmt.Sprintf("%s/%s", base, src.Chart)
} else {
chart = fmt.Sprintf("%s/%s", src.Chart, src.Chart)
}

options := []helm.Option{
helm.WithName(src.Chart),
helm.WithNamespace(namespace),
helm.WithChart(chart),
helm.WithVersion(src.TargetRevision),
helm.WithArgs("--install", "--create-namespace"),
}

if src.Helm != nil && src.Helm.Values != "" {
valuesFilePath, err := writeValuesFile(src.Chart, src.TargetRevision, src.Helm.Values)
if err != nil {
return nil, fmt.Errorf("writing values file: %w", err)
}

options = append(options, helm.WithArgs("-f", valuesFilePath))
}

return options, nil
}

func writeValuesFile(chart, revision, valuesContent string) (string, error) {
valuesFilePath := fmt.Sprintf("values-%s-%s.yaml", chart, revision)
filePath := filepath.Join(os.TempDir(), valuesFilePath)
if err := os.WriteFile(filePath, []byte(valuesContent), 0o600); err != nil {
return "", err
}

return filePath, nil
}
55 changes: 55 additions & 0 deletions pkg/kustomize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package e2eutils

import (
"bytes"
"errors"
"fmt"
"io"
"strings"

"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

func BuildKustomization(path string) ([]*unstructured.Unstructured, error) {
if strings.TrimSpace(path) == "" {
return nil, errors.New("kustomization path must not be empty")
}

fs := filesys.MakeFsOnDisk()
kustomizer := krusty.MakeKustomizer(krusty.MakeDefaultOptions())

resMap, err := kustomizer.Run(fs, path)
if err != nil {
return nil, fmt.Errorf("failed to run kustomize: %w", err)
}

yamlData, err := resMap.AsYaml()
if err != nil {
return nil, fmt.Errorf("failed to convert resource map to YAML: %w", err)
}

decoder := yaml.NewDecoder(bytes.NewReader(yamlData))

var objects []*unstructured.Unstructured
for {
var obj unstructured.Unstructured
if err := decoder.Decode(&obj.Object); err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, fmt.Errorf("failed to decode YAML: %w", err)
}

// skip empty documents
if len(obj.Object) == 0 {
continue
}

objects = append(objects, &obj)
}

return objects, nil
}
44 changes: 44 additions & 0 deletions pkg/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package e2eutils

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"

"sigs.k8s.io/e2e-framework/klient/decoder"
"sigs.k8s.io/e2e-framework/klient/k8s"
)

func GetKubernetesManifests(ctx context.Context, pathCollection []string) ([]k8s.Object, error) {
if pathCollection == nil {
return nil, errors.New("kustomization pathCollection must not be empty")
}

var objects []k8s.Object
for _, source := range pathCollection {
if source == "" {
continue
}

o, err := prepareKubernetesManifests(ctx, source)
if err != nil {
return nil, fmt.Errorf("failed to prepare manifests from source pathCollection %q: %w", source, err)
}
objects = append(objects, o...)
}

return objects, nil
}

func prepareKubernetesManifests(ctx context.Context, path string) ([]k8s.Object, error) {
manifestPath := filepath.Join("..", path)
manifestFS := os.DirFS(manifestPath)

objects, err := decoder.DecodeAllFiles(ctx, manifestFS, "*.yaml")
if err != nil {
return nil, fmt.Errorf("failed to decode YAML files from path %q: %w", manifestPath, err)
}
return objects, nil
}
Loading