Skip to content
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
21 changes: 21 additions & 0 deletions e2e/assets/multi-cluster/bundle-deployment-labels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
kind: GitRepo
apiVersion: fleet.cattle.io/v1alpha1
metadata:
name: simpleapplabels
namespace: {{.ProjectNamespace}}
labels:
team: one

spec:
repo: https://github.com/rancher/fleet-test-data
branch: master
paths:
- simple

targetNamespace: {{.TargetNamespace}}

targets:
- name: test
clusterSelector:
matchLabels:
envlabels: test
94 changes: 94 additions & 0 deletions e2e/multi-cluster/not_matching_targets_delete_bd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package multicluster_test

import (
"math/rand"

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

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

// This test uses labels in clusters to demonstrate that applying or
// deleting the label installs or uninstalls the bundle deployment
var _ = Describe("Target clusters by label", func() {
var (
k kubectl.Command
kd kubectl.Command

asset string
namespace string
data any

r = rand.New(rand.NewSource(GinkgoRandomSeed()))
)

type TemplateData struct {
ProjectNamespace string
TargetNamespace string
}

BeforeEach(func() {
k = env.Kubectl.Context(env.Upstream)
kd = env.Kubectl.Context(env.ManagedDownstream)
})

JustBeforeEach(func() {
err := testenv.ApplyTemplate(k.Namespace(env.ClusterRegistrationNamespace), testenv.AssetPath(asset), data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this could most likely be made more lightweight by testing direct creation of a bundle, skipping GitRepo.

Expect(err).ToNot(HaveOccurred())
})

AfterEach(func() {
out, err := k.Namespace(env.ClusterRegistrationNamespace).Delete("gitrepo", "simpleapplabels", "--wait=false")
Expect(err).ToNot(HaveOccurred(), out)
})

Context("if cluster has the expected label, bundle is deployed", func() {
BeforeEach(func() {
namespace = testenv.NewNamespaceName("label-not-set", r)
asset = "multi-cluster/bundle-deployment-labels.yaml"
data = TemplateData{env.ClusterRegistrationNamespace, namespace}
// set the expected label
out, err := k.Namespace(env.ClusterRegistrationNamespace).Label("clusters", "second", "envlabels=test")
Expect(err).ToNot(HaveOccurred(), out)
})

It("deploys to the mapped downstream cluster and when label is deleted it removes the deployment", func() {
Eventually(func() string {
out, _ := k.Get("bundledeployments", "-A", "-l", "fleet.cattle.io/bundle-name=simpleapplabels-simple")
return out
}).Should(ContainSubstring("simpleapplabels-simple"))
Eventually(func() string {
out, _ := kd.Get("configmaps", "-A")
return out
}).Should(ContainSubstring("simple-config"))

// delete the label (bundledeployment should be deleted and resources in cluster deleted)
out, err := k.Namespace(env.ClusterRegistrationNamespace).Label("clusters", "second", "envlabels-")
Expect(err).ToNot(HaveOccurred(), out)

Eventually(func() string {
out, _ := k.Get("bundledeployments", "-A", "-l", "fleet.cattle.io/bundle-name=simpleapplabels-simple")
return out
}).ShouldNot(ContainSubstring("simpleapplabels-simple"))
Eventually(func() string {
out, _ := kd.Namespace(namespace).Get("configmaps")
return out
}).ShouldNot(ContainSubstring("simple-config"))

// re-apply the label (bundledeployment should be created and applied again)
out, err = k.Namespace(env.ClusterRegistrationNamespace).Label("clusters", "second", "envlabels=test")
Expect(err).ToNot(HaveOccurred(), out)

Eventually(func() string {
out, _ := k.Get("bundledeployments", "-A", "-l", "fleet.cattle.io/bundle-name=simpleapplabels-simple")
return out
}).Should(ContainSubstring("simpleapplabels-simple"))
Eventually(func() string {
out, _ := kd.Namespace(namespace).Get("configmaps")
return out
}).Should(ContainSubstring("simple-config"))
})
})
})
3 changes: 3 additions & 0 deletions e2e/testenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Env struct {
Upstream string
// Downstream context for fleet-agent cluster
Downstream string
// Managed downstream cluster
ManagedDownstream string
// Namespace which contains the cluster resource for most E2E tests
// (cluster registration namespace)
Namespace string
Expand All @@ -32,6 +34,7 @@ func New() *Env {
Kubectl: kubectl.New("", "default"),
Upstream: "k3d-upstream",
Downstream: "k3d-downstream",
ManagedDownstream: "k3d-managed-downstream",
Namespace: "fleet-local",
ClusterRegistrationNamespace: "fleet-default",
}
Expand Down
4 changes: 4 additions & 0 deletions e2e/testenv/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (c Command) Patch(args ...string) (string, error) {
return c.Run(append([]string{"patch"}, args...)...)
}

func (c Command) Label(args ...string) (string, error) {
return c.Run(append([]string{"label"}, args...)...)
}

func (c Command) Run(args ...string) (string, error) {
if c.cnt != "" {
args = append([]string{"--context", c.cnt}, args...)
Expand Down
Loading