|
| 1 | +package singlecluster_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/rand" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "strings" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + |
| 13 | + "github.com/rancher/fleet/e2e/testenv" |
| 14 | + "github.com/rancher/fleet/e2e/testenv/githelper" |
| 15 | + "github.com/rancher/fleet/e2e/testenv/kubectl" |
| 16 | +) |
| 17 | + |
| 18 | +var _ = Describe("Helm v4 Null Field Drift Detection", Label("infra-setup"), func() { |
| 19 | + var ( |
| 20 | + tmpDir string |
| 21 | + clonedir string |
| 22 | + k kubectl.Command |
| 23 | + gh *githelper.Git |
| 24 | + localRepoName string |
| 25 | + inClusterRepoURL string |
| 26 | + gitrepoName string |
| 27 | + r = rand.New(rand.NewSource(GinkgoRandomSeed())) |
| 28 | + gitServerPort int |
| 29 | + gitProtocol string |
| 30 | + ) |
| 31 | + |
| 32 | + BeforeEach(func() { |
| 33 | + k = env.Kubectl.Namespace(env.Namespace) |
| 34 | + gitServerPort = 8080 |
| 35 | + gitProtocol = "http" |
| 36 | + localRepoName = "repo" |
| 37 | + }) |
| 38 | + |
| 39 | + JustBeforeEach(func() { |
| 40 | + // Build git repo URL reachable _within_ the cluster, for the GitRepo |
| 41 | + host := githelper.BuildGitHostname() |
| 42 | + |
| 43 | + addr, err := githelper.GetExternalRepoAddr(env, gitServerPort, localRepoName) |
| 44 | + Expect(err).ToNot(HaveOccurred()) |
| 45 | + addr = strings.Replace(addr, "http://", fmt.Sprintf("%s://", gitProtocol), 1) |
| 46 | + gh = githelper.NewHTTP(addr) |
| 47 | + |
| 48 | + inClusterRepoURL = gh.GetInClusterURL(host, gitServerPort, localRepoName) |
| 49 | + |
| 50 | + tmpDir, _ = os.MkdirTemp("", "fleet-") |
| 51 | + clonedir = path.Join(tmpDir, localRepoName) |
| 52 | + |
| 53 | + gitrepoName = testenv.RandomFilename("helm-recreate-drift", r) |
| 54 | + }) |
| 55 | + |
| 56 | + AfterEach(func() { |
| 57 | + _ = os.RemoveAll(tmpDir) |
| 58 | + |
| 59 | + _, _ = k.Delete("gitrepo", gitrepoName) |
| 60 | + _, _ = k.Delete("secret", "git-auth") |
| 61 | + _, _ = k.Delete("namespace", "helm-recreate-drift-test") |
| 62 | + |
| 63 | + // Check that the bundle deployment resource has been deleted |
| 64 | + Eventually(func(g Gomega) { |
| 65 | + out, _ := k.Get( |
| 66 | + "bundledeployments", |
| 67 | + "-A", |
| 68 | + "-l", |
| 69 | + fmt.Sprintf("fleet.cattle.io/repo-name=%s", gitrepoName), |
| 70 | + ) |
| 71 | + g.Expect(out).To(ContainSubstring("No resources found")) |
| 72 | + }).Should(Succeed()) |
| 73 | + }) |
| 74 | + |
| 75 | + When("deploying a Helm chart with fields omitted by Helm v4", func() { |
| 76 | + JustBeforeEach(func() { |
| 77 | + // Create git auth secret |
| 78 | + _, err := k.Create( |
| 79 | + "secret", "generic", "git-auth", "--type", "kubernetes.io/basic-auth", |
| 80 | + "--from-literal=username="+os.Getenv("GIT_HTTP_USER"), |
| 81 | + "--from-literal=password="+os.Getenv("GIT_HTTP_PASSWORD"), |
| 82 | + ) |
| 83 | + Expect(err).ToNot(HaveOccurred()) |
| 84 | + |
| 85 | + err = testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/helm-recreate-drift-gitrepo.yaml"), struct { |
| 86 | + Name string |
| 87 | + Repo string |
| 88 | + Branch string |
| 89 | + }{ |
| 90 | + Name: gitrepoName, |
| 91 | + Repo: inClusterRepoURL, |
| 92 | + Branch: gh.Branch, |
| 93 | + }) |
| 94 | + Expect(err).ToNot(HaveOccurred()) |
| 95 | + |
| 96 | + _, err = gh.Create(clonedir, testenv.AssetPath("helm-recreate-drift"), "helm-recreate-drift") |
| 97 | + Expect(err).ToNot(HaveOccurred()) |
| 98 | + }) |
| 99 | + |
| 100 | + It("should not detect drift from Helm v4 rendering", func() { |
| 101 | + By("checking the bundle is ready") |
| 102 | + Eventually(func(g Gomega) { |
| 103 | + out, err := k.Get("bundles", "-A", "-l", fmt.Sprintf("fleet.cattle.io/repo-name=%s", gitrepoName)) |
| 104 | + g.Expect(err).ToNot(HaveOccurred()) |
| 105 | + g.Expect(out).To(ContainSubstring("fleet-local")) |
| 106 | + }, testenv.MediumTimeout, testenv.ShortTimeout).Should(Succeed()) |
| 107 | + |
| 108 | + By("checking the bundle deployment is ready") |
| 109 | + var bundleDeploymentName, bdNamespace string |
| 110 | + Eventually(func(g Gomega) { |
| 111 | + out, err := k.Get("bundledeployments", "-A", "-o", "jsonpath={.items[0]['metadata.name','metadata.namespace']}", "-l", fmt.Sprintf("fleet.cattle.io/repo-name=%s", gitrepoName)) |
| 112 | + g.Expect(err).ToNot(HaveOccurred()) |
| 113 | + g.Expect(out).ToNot(BeEmpty()) |
| 114 | + |
| 115 | + data := strings.Split(out, " ") |
| 116 | + g.Expect(data).To(HaveLen(2)) |
| 117 | + bundleDeploymentName, bdNamespace = data[0], data[1] |
| 118 | + }, testenv.MediumTimeout, testenv.ShortTimeout).Should(Succeed()) |
| 119 | + |
| 120 | + Eventually(func(g Gomega) { |
| 121 | + out, err := k.Namespace(bdNamespace).Get("bundledeployments", bundleDeploymentName, "-o", "jsonpath={.status.ready}") |
| 122 | + g.Expect(err).ToNot(HaveOccurred()) |
| 123 | + g.Expect(out).To(Equal("true")) |
| 124 | + }, testenv.MediumTimeout, testenv.ShortTimeout).Should(Succeed()) |
| 125 | + |
| 126 | + By("checking the deployment has RollingUpdate strategy") |
| 127 | + Eventually(func(g Gomega) { |
| 128 | + out, err := k.Namespace("helm-recreate-drift-test").Get("deployments", "test-deployment", "-o", "jsonpath={.spec.strategy.type}") |
| 129 | + g.Expect(err).ToNot(HaveOccurred()) |
| 130 | + g.Expect(out).To(Equal("RollingUpdate")) |
| 131 | + }, testenv.MediumTimeout, testenv.ShortTimeout).Should(Succeed()) |
| 132 | + |
| 133 | + By("verifying that no drift is detected") |
| 134 | + Eventually(func(g Gomega) { |
| 135 | + out, err := k.Namespace(bdNamespace).Get("bundledeployments", bundleDeploymentName, "-o", "jsonpath={.status.nonModified}") |
| 136 | + g.Expect(err).ToNot(HaveOccurred()) |
| 137 | + g.Expect(out).To(Equal("true"), "BundleDeployment should not show drift from Helm v4 rendering") |
| 138 | + |
| 139 | + // Also check that modifiedStatus is empty |
| 140 | + out, err = k.Namespace(bdNamespace).Get("bundledeployments", bundleDeploymentName, "-o", "jsonpath={.status.modifiedStatus}") |
| 141 | + g.Expect(err).ToNot(HaveOccurred()) |
| 142 | + g.Expect(out).To(BeEmpty(), "modifiedStatus should be empty when no drift is detected") |
| 143 | + }, testenv.MediumTimeout, testenv.ShortTimeout).Should(Succeed()) |
| 144 | + }) |
| 145 | + }) |
| 146 | +}) |
0 commit comments