Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't ignore config map reference in valuesFrom list element #1431

Merged
merged 3 commits into from
Mar 29, 2023
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/fleet-upgrade-in-rancher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ jobs:
-
name: Create example workload
run: |
kubectl apply -n fleet-local -f e2e/assets/gitrepo-simple.yaml
kubectl apply -n fleet-default -f e2e/assets/gitrepo-simple.yaml
kubectl apply -n fleet-local -f e2e/assets/fleet-upgrade/gitrepo-simple.yaml
kubectl apply -n fleet-default -f e2e/assets/fleet-upgrade/gitrepo-simple.yaml
-
name: Deploy latest fleet
env:
Expand Down
14 changes: 14 additions & 0 deletions e2e/assets/gitrepo-template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
kind: GitRepo
apiVersion: fleet.cattle.io/v1alpha1
metadata:
name: {{ .Name }}

spec:
repo: https://github.com/rancher/fleet-test-data
branch: {{ .Branch }}
paths:
{{- range .Paths}}
- {{.}}
{{- end}}

targetNamespace: {{ .TargetNamespace }}
File renamed without changes.
6 changes: 6 additions & 0 deletions e2e/assets/single-cluster/values-from-configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: configmap overrides values from fleet.yaml
config: config option
options:
onlyconfigmap: configmap option
english:
name: configmap override
6 changes: 6 additions & 0 deletions e2e/assets/single-cluster/values-from-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: secret overrides values from fleet.yaml
secret: xyz secret
options:
onlysecret: secret option
english:
name: secret override
2 changes: 1 addition & 1 deletion e2e/installation/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var _ = Describe("Fleet Installation", func() {
When("Deploying another bundle still works", func() {
var tmpdir string
BeforeEach(func() {
asset = "simple/gitrepo.yaml"
asset = "installation/verify.yaml"
})

JustBeforeEach(func() {
Expand Down
13 changes: 2 additions & 11 deletions e2e/multi-cluster/bundle_namespace_mapping_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package mc_examples_test
package multicluster_test

import (
"os"
"path"
"time"

"github.com/rancher/fleet/e2e/testenv"
Expand All @@ -20,7 +18,6 @@ var _ = Describe("Bundle Namespace Mapping", Label("difficult"), func() {
asset string
namespace string
data any
tmpdir string

interval = 2 * time.Second
duration = 30 * time.Second
Expand All @@ -40,19 +37,13 @@ var _ = Describe("Bundle Namespace Mapping", Label("difficult"), func() {
})

JustBeforeEach(func() {
tmpdir, _ = os.MkdirTemp("", "fleet-")
output := path.Join(tmpdir, testenv.RandomFilename("manifests.yaml"))
err := testenv.Template(output, testenv.AssetPath(asset), data)
err := testenv.ApplyTemplate(k.Namespace(namespace), testenv.AssetPath(asset), data)
Expect(err).ToNot(HaveOccurred())

out, err := k.Namespace(namespace).Apply("-f", output)
Expect(err).ToNot(HaveOccurred(), out)
})

AfterEach(func() {
out, err := k.Delete("ns", namespace)
Expect(err).ToNot(HaveOccurred(), out)
os.RemoveAll(tmpdir)
})

Context("with bundlenamespacemapping", func() {
Expand Down
3 changes: 2 additions & 1 deletion e2e/multi-cluster/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package mc_examples_test
// Package multicluster contains e2e tests deploying to multiple clusters. The tests use kubectl to apply manifests. Expectations are verified by checking cluster resources. Assets refer to the https://github.com/rancher/fleet-test-data git repo.
package multicluster_test

import (
"testing"
Expand Down
15 changes: 6 additions & 9 deletions e2e/require-secrets/gitrepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,20 @@ var _ = Describe("Git Repo", func() {
)
Expect(err).ToNot(HaveOccurred(), out)

tmpdir, _ = os.MkdirTemp("", "fleet-")
repodir = path.Join(tmpdir, "repo")
repo, err = gh.Create(repodir, testenv.AssetPath("gitrepo/sleeper-chart"), "examples")
Expect(err).ToNot(HaveOccurred())

gitrepo := path.Join(tmpdir, "gitrepo.yaml")
err = testenv.Template(gitrepo, testenv.AssetPath("gitrepo/gitrepo.yaml"), struct {
err = testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), struct {
Repo string
Branch string
}{
gh.URL,
gh.Branch,
})
Expect(err).ToNot(HaveOccurred(), out)

tmpdir, _ = os.MkdirTemp("", "fleet-")
repodir = path.Join(tmpdir, "repo")
repo, err = gh.Create(repodir, testenv.AssetPath("gitrepo/sleeper-chart"), "examples")
Expect(err).ToNot(HaveOccurred())

out, err = k.Apply("-f", gitrepo)
Expect(err).ToNot(HaveOccurred(), out)
})

AfterEach(func() {
Expand Down
4 changes: 2 additions & 2 deletions e2e/single-cluster/bundle_diffs_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package examples_test
package singlecluster_test

import (
"encoding/json"
Expand Down Expand Up @@ -62,7 +62,7 @@ var _ = Describe("BundleDiffs", func() {
}

return false
}).Should(BeTrue(), "bundledepoloyment should be deleted")
}).Should(BeTrue(), "bundledeployment should be deleted")
})
})

Expand Down
2 changes: 1 addition & 1 deletion e2e/single-cluster/helm_verify_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package examples_test
package singlecluster_test

import (
"github.com/rancher/fleet/e2e/testenv"
Expand Down
4 changes: 2 additions & 2 deletions e2e/single-cluster/release_names_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package examples_test
package singlecluster_test

import (
"strings"
Expand All @@ -10,7 +10,7 @@ import (
. "github.com/onsi/gomega"
)

var _ = Describe("ReleaseName", func() {
var _ = Describe("Release Name", func() {
var (
asset string
k kubectl.Command
Expand Down
5 changes: 2 additions & 3 deletions e2e/single-cluster/single_cluster_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package examples_test
package singlecluster_test

import (
"strings"
Expand All @@ -10,8 +10,7 @@ import (
. "github.com/onsi/gomega"
)

// These tests use the examples from https://github.com/rancher/fleet-examples/tree/master/single-cluster
var _ = Describe("Single Cluster Examples", func() {
var _ = Describe("Single Cluster Deployments", func() {
var (
asset string
k kubectl.Command
Expand Down
3 changes: 2 additions & 1 deletion e2e/single-cluster/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package examples_test
// Package singlecluster contains e2e tests deploying to a single cluster. The tests use kubectl to apply manifests. Expectations are verified by checking cluster resources. Assets refer to the https://github.com/rancher/fleet-test-data git repo.
package singlecluster_test

import (
"testing"
Expand Down
75 changes: 75 additions & 0 deletions e2e/single-cluster/values_from_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package singlecluster_test

import (
"encoding/json"

"github.com/rancher/fleet/e2e/testenv"
"github.com/rancher/fleet/e2e/testenv/kubectl"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("ValuesFrom", func() {
var (
// k is the kubectl command for the cluster registration namespace
k kubectl.Command
// kw is the kubectl command for namespace the workload is deployed to
kw kubectl.Command
namespace string
)

BeforeEach(func() {
k = env.Kubectl.Namespace(env.Namespace)
namespace = testenv.NewNamespaceName("values-from")
kw = k.Namespace(namespace)

out, err := k.Create("ns", namespace)
Expect(err).ToNot(HaveOccurred(), out)

out, err = k.Namespace(namespace).Create("secret", "generic", "secret-values",
"--from-file=values.yaml="+testenv.AssetPath("single-cluster/values-from-secret.yaml"))
Expect(err).ToNot(HaveOccurred(), out)

out, err = k.Namespace("default").Create("configmap", "configmap-values",
"--from-file=values.yaml="+testenv.AssetPath("single-cluster/values-from-configmap.yaml"))
Expect(err).ToNot(HaveOccurred(), out)

err = testenv.CreateGitRepo(k, namespace, "values-from", "master", "helm-values-from")
Expect(err).ToNot(HaveOccurred())

DeferCleanup(func() {
out, err := k.Delete("gitrepo", "values-from")
Expect(err).ToNot(HaveOccurred(), out)
out, err = k.Delete("ns", namespace)
Expect(err).ToNot(HaveOccurred(), out)
out, err = k.Namespace("default").Delete("configmap", "configmap-values")
Expect(err).ToNot(HaveOccurred(), out)
})
})

When("fleet.yaml makes use of valuesFrom", func() {
Context("referencing a secret as well as a configmap", func() {
It("all referenced resources are available as values to the chart", func() {
Eventually(func() bool {
_, err := kw.Get("configmap", "result")
return err == nil
}).Should(BeTrue())

out, err := kw.Get("configmap", "result", "-o", "jsonpath={.data}")
Expect(err).ToNot(HaveOccurred(), out)
result := map[string]string{}
err = json.Unmarshal([]byte(out), &result)
Expect(err).ToNot(HaveOccurred())

Expect(result).To(HaveKeyWithValue("name", "secret overrides values from fleet.yaml"))
Expect(result).To(HaveKeyWithValue("secret", "xyz secret"))
Expect(result).To(HaveKeyWithValue("config", "config option"))
Expect(result).To(HaveKeyWithValue("fleetyaml", "from fleet.yaml"))
Expect(result).To(HaveKeyWithValue("englishname", "secret override"))
Expect(result).To(HaveKeyWithValue("optionconfigmap", "configmap option"))
Expect(result).To(HaveKeyWithValue("optionsecret", "secret option"))
})
})
})
})
9 changes: 7 additions & 2 deletions e2e/testenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const Timeout = 5 * time.Minute
type Env struct {
Kubectl kubectl.Command
// Upstream context for cluster containing the fleet controller and local agent
Upstream string
Upstream string
// Downstream context for fleet-agent cluster
Downstream string
Namespace string
// Namespace which contains the cluster resource (cluster registration namespace)
Namespace string
}

func New() *Env {
Expand All @@ -43,6 +45,9 @@ func (e *Env) getShellEnv() {
e.Namespace = val
}
}

// NewNamespaceName returns a name for a namespace that is unique to the test
// run. e.g. as a targetNamespace for workloads
func NewNamespaceName(name string) string {
rand.Seed(time.Now().UnixNano())
p := make([]byte, 12)
Expand Down
44 changes: 43 additions & 1 deletion e2e/testenv/template.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,55 @@
package testenv

import (
"fmt"
"html/template"
"math/rand"
"os"
"path"
"strconv"
"strings"

"github.com/rancher/fleet/e2e/testenv/kubectl"
"github.com/rancher/gitjob/e2e/testenv"
)

const gitrepoTemplate = "gitrepo-template.yaml"

// GitRepoData can be used with the gitrepo-template.yaml asset when no custom
// GitRepo properties are required. All fields are required.
type GitRepoData struct {
Name string
Branch string
Paths []string
TargetNamespace string
}

// CreateGitRepo uses the template to create a gitrepo resource. The namespace is the TargetNamespace for the workloads.
func CreateGitRepo(k kubectl.Command, namespace string, name string, branch string, paths ...string) error {
return ApplyTemplate(k, AssetPath(gitrepoTemplate), GitRepoData{
TargetNamespace: namespace,
Name: name,
Branch: branch,
Paths: paths,
})
}

// ApplyTemplate templates a file and applies it to the cluster.
func ApplyTemplate(k kubectl.Command, asset string, data interface{}) error {
tmpdir, _ := os.MkdirTemp("", "fleet-")
defer os.RemoveAll(tmpdir)

output := path.Join(tmpdir, RandomFilename(asset))
if err := Template(output, testenv.AssetPath(asset), data); err != nil {
return err
}
out, err := k.Apply("-f", output)
if err != nil {
return fmt.Errorf("%w: %s", err, out)
}
return nil
}

// Template loads a gotemplate from a file and writes templated output to
// another file, preferably in a temp dir
func Template(output string, tmplPath string, data any) error {
Expand All @@ -33,6 +74,7 @@ func Template(output string, tmplPath string, data any) error {
// RandomName returns a slightly random name, so temporary assets don't conflict
func RandomFilename(filename string) string {
ext := path.Ext(filename)
filename = path.Base(filename)
r := strconv.Itoa(rand.Intn(99999)) // nolint:gosec // Non-crypto use
return strings.TrimSuffix(filename, ext) + r + ext
return strings.TrimSuffix(filename, ext) + r + "." + ext
}
Loading