Skip to content

Commit

Permalink
Refactor targeting/bundle controller (#2111)
Browse files Browse the repository at this point in the history
* Rename CLI apply.Apply to apply.CreateBundles
* Refactor target package
* new UpdatePartitions
* new file rollout.go
* Extract BundleDeployment to func
* Remove global fleet args, remove unused args

These arguments were not shared between all commands.
There is one set of commands (apply, cleanup) which use the fleet/client
pkg and another set which uses a controller-runtime client. Same is true
for the debug flag.

* Move fleet apply helm test into apply folder

Both apply/ and helm/ are testing the fleet apply sub command

* Fix typos

---------

Co-authored-by: Corentin Néau <tan.neau@suse.com>
  • Loading branch information
Mario Manno and weyfonk authored Feb 1, 2024
1 parent e400dd1 commit 45a0891
Show file tree
Hide file tree
Showing 18 changed files with 323 additions and 328 deletions.
34 changes: 15 additions & 19 deletions integrationtests/cli/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package apply
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"

"github.com/rancher/fleet/integrationtests/cli"
"github.com/rancher/fleet/internal/cmd/cli/apply"
)
Expand All @@ -24,13 +24,12 @@ var _ = Describe("Fleet apply", Ordered, func() {
When("folder contains simple resources", func() {
BeforeEach(func() {
name = "simple"
options = apply.Options{Output: gbytes.NewBuffer()}
dirs = []string{cli.AssetsPath + "simple"}
})

It("then a Bundle is created with all the resources and keepResources is false", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(options.Output)
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(len(bundle.Spec.Resources)).To(Equal(2))
isSvcPresent, err := cli.IsResourcePresentInBundle(cli.AssetsPath+"simple/svc.yaml", bundle.Spec.Resources)
Expand All @@ -42,16 +41,16 @@ var _ = Describe("Fleet apply", Ordered, func() {
}).Should(BeTrue())
})
})

When("simple resources in a nested folder", func() {
BeforeEach(func() {
name = "nested_simple"
options = apply.Options{Output: gbytes.NewBuffer()}
dirs = []string{cli.AssetsPath + "nested_simple"}
})

It("then a Bundle is created with all the resources", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(options.Output)
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(len(bundle.Spec.Resources)).To(Equal(3))
isSvcPresent, err := cli.IsResourcePresentInBundle(cli.AssetsPath+"nested_simple/simple/svc.yaml", bundle.Spec.Resources)
Expand All @@ -65,16 +64,16 @@ var _ = Describe("Fleet apply", Ordered, func() {
}).Should(BeTrue())
})
})

When("simple resources in a nested folder with two levels", func() {
BeforeEach(func() {
name = "nested_two_levels"
options = apply.Options{Output: gbytes.NewBuffer()}
dirs = []string{cli.AssetsPath + "nested_two_levels"}
})

It("then a Bundle is created with all the resources", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(options.Output)
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(len(bundle.Spec.Resources)).To(Equal(2))
isSvcPresent, err := cli.IsResourcePresentInBundle(cli.AssetsPath+"nested_two_levels/nested/svc/svc.yaml", bundle.Spec.Resources)
Expand All @@ -86,16 +85,16 @@ var _ = Describe("Fleet apply", Ordered, func() {
}).Should(BeTrue())
})
})

When("multiple fleet.yaml in a nested folder", func() {
BeforeEach(func() {
name = "nested_multiple"
options = apply.Options{Output: gbytes.NewBuffer()}
dirs = []string{cli.AssetsPath + "nested_multiple"}
})

It("then 3 Bundles are created with the relevant resources", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleListFromOutput(options.Output)
bundle, err := cli.GetBundleListFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(len(bundle)).To(Equal(3))
deploymentA := bundle[0]
Expand Down Expand Up @@ -123,16 +122,16 @@ var _ = Describe("Fleet apply", Ordered, func() {
}).Should(BeTrue())
})
})

When("multiple fleet.yaml mixed with simple resources in a nested folder", func() {
BeforeEach(func() {
name = "nested_mixed_two_levels"
options = apply.Options{Output: gbytes.NewBuffer()}
dirs = []string{cli.AssetsPath + "nested_mixed_two_levels"}
})

It("then Bundles are created with all the resources", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleListFromOutput(options.Output)
bundle, err := cli.GetBundleListFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(len(bundle)).To(Equal(3))
root := bundle[0]
Expand Down Expand Up @@ -164,16 +163,16 @@ var _ = Describe("Fleet apply", Ordered, func() {
}).Should(BeTrue())
})
})

When("containing keepResources in the fleet.yaml", func() {
BeforeEach(func() {
name = "keep_resources"
options = apply.Options{Output: gbytes.NewBuffer()}
dirs = []string{cli.AssetsPath + "keep_resources"}
})

It("then a Bundle is created with keepResources", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(options.Output)
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
return bundle.Spec.KeepResources
}).Should(BeTrue())
Expand All @@ -184,13 +183,12 @@ var _ = Describe("Fleet apply", Ordered, func() {
When("passes along enabled helm options", func() {
BeforeEach(func() {
name = "helm_options_enabled"
options = apply.Options{Output: gbytes.NewBuffer()}
dirs = []string{cli.AssetsPath + name}
})

It("publishes the flag in the bundle options", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(options.Output)
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
return bundle.Spec.Helm.TakeOwnership &&
bundle.Spec.Helm.Atomic &&
Expand All @@ -205,13 +203,12 @@ var _ = Describe("Fleet apply", Ordered, func() {
When("passes along disabled helm options", func() {
BeforeEach(func() {
name = "helm_options_disabled"
options = apply.Options{Output: gbytes.NewBuffer()}
dirs = []string{cli.AssetsPath + name}
})

It("publishes the flag in the bundle options", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(options.Output)
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
return bundle.Spec.Helm.TakeOwnership == false &&
bundle.Spec.Helm.Atomic == false &&
Expand All @@ -226,13 +223,12 @@ var _ = Describe("Fleet apply", Ordered, func() {
When("passes along helm options with a kustomize bundle", func() {
BeforeEach(func() {
name = "helm_options_kustomize"
options = apply.Options{Output: gbytes.NewBuffer()}
dirs = []string{cli.AssetsPath + name}
})

It("publishes the flag in the bundle options", func() {
Eventually(func() bool {
bundle, err := cli.GetBundleFromOutput(options.Output)
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
return bundle.Spec.Helm.TakeOwnership &&
bundle.Spec.Helm.ReleaseName == "kustomize"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helm
package apply

import (
"crypto/subtle"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helm
package apply

import (
"io/fs"
Expand Down Expand Up @@ -29,28 +29,32 @@ var _ = Describe("Fleet apply helm release", Serial, func() {
var repo = repository{
port: port,
}

BeforeEach(func() {
repo.startRepository(true)
})

AfterEach(func() {
err := repo.stopRepository()
Expect(err).NotTo(HaveOccurred())
})

When("path credentials are provided just for root folder", func() {
It("fleet apply fails for sub folder", func() {
Eventually(func() string {
err := fleetApply([]string{cli.AssetsPath + "helm_path_credentials"}, apply.Options{
err := fleetApply("helm", []string{cli.AssetsPath + "helm_path_credentials"}, apply.Options{
AuthByPath: map[string]bundlereader.Auth{cli.AssetsPath + "helm_path_credentials": {Username: username, Password: password}},
})
Expect(err).To(HaveOccurred())
return err.Error()
}).Should(ContainSubstring("401"))
})
})

When("path credentials are provided for both root and sub folder", func() {
It("fleet apply works fine", func() {
Eventually(func() error {
return fleetApply([]string{cli.AssetsPath + "helm_path_credentials"}, apply.Options{
return fleetApply("helm", []string{cli.AssetsPath + "helm_path_credentials"}, apply.Options{
AuthByPath: map[string]bundlereader.Auth{
cli.AssetsPath + "helm_path_credentials": {Username: username, Password: password},
cli.AssetsPath + "helm_path_credentials/subfolder": {Username: username, Password: password},
Expand Down Expand Up @@ -86,7 +90,7 @@ func testHelmRepo(path, port string) {
})
It("fleet apply success", func() {
Eventually(func() error {
return fleetApply([]string{cli.AssetsPath + path}, apply.Options{})
return fleetApply("helm", []string{cli.AssetsPath + path}, apply.Options{})
}).Should(Not(HaveOccurred()))
By("verifying Bundle is created with all the resources inside of the helm release", func() {
Eventually(verifyResourcesArePresent).Should(BeTrue())
Expand All @@ -100,7 +104,7 @@ func testHelmRepo(path, port string) {
})
It("fleet apply fails when no auth provided", func() {
Eventually(func() string {
err := fleetApply([]string{cli.AssetsPath + path}, apply.Options{})
err := fleetApply("helm", []string{cli.AssetsPath + path}, apply.Options{})
Expect(err).To(HaveOccurred())
return err.Error()
}).Should(ContainSubstring("401"))
Expand All @@ -113,7 +117,7 @@ func testHelmRepo(path, port string) {
})
It("fleet apply success", func() {
Eventually(func() error {
return fleetApply([]string{cli.AssetsPath + path}, apply.Options{Auth: bundlereader.Auth{Username: username, Password: password}})
return fleetApply("helm", []string{cli.AssetsPath + path}, apply.Options{Auth: bundlereader.Auth{Username: username, Password: password}})
}).Should(Not(HaveOccurred()))
By("verifying Bundle is created with all the resources inside of the helm release", func() {
Eventually(verifyResourcesArePresent).Should(BeTrue())
Expand All @@ -127,7 +131,7 @@ func testHelmRepo(path, port string) {
})
It("fleet apply success", func() {
Eventually(func() error {
return fleetApply([]string{cli.AssetsPath + path}, apply.Options{
return fleetApply("helm", []string{cli.AssetsPath + path}, apply.Options{
Auth: bundlereader.Auth{Username: username, Password: password},
HelmRepoURLRegex: "http://localhost/*",
})
Expand All @@ -144,7 +148,7 @@ func testHelmRepo(path, port string) {
})
It("fleet apply fails when --helm-repo-url-regex doesn't match the helm repo url", func() {
Eventually(func() string {
err := fleetApply([]string{cli.AssetsPath + path}, apply.Options{
err := fleetApply("helm", []string{cli.AssetsPath + path}, apply.Options{
Auth: bundlereader.Auth{Username: username, Password: password},
HelmRepoURLRegex: "nomatch",
})
Expand All @@ -160,7 +164,7 @@ func testHelmRepo(path, port string) {
})
It("fleet apply fails when --helm-repo-url-regex is not valid", func() {
Eventually(func() string {
err := fleetApply([]string{cli.AssetsPath + path}, apply.Options{
err := fleetApply("helm", []string{cli.AssetsPath + path}, apply.Options{
Auth: bundlereader.Auth{Username: username, Password: password},
HelmRepoURLRegex: "a(b",
})
Expand All @@ -176,7 +180,7 @@ func testHelmRepo(path, port string) {
})
It("fleet apply uses credentials from HelmSecretNameForPaths", func() {
Eventually(func() error {
return fleetApply([]string{cli.AssetsPath + path}, apply.Options{AuthByPath: map[string]bundlereader.Auth{cli.AssetsPath + path: {Username: username, Password: password}}})
return fleetApply("helm", []string{cli.AssetsPath + path}, apply.Options{AuthByPath: map[string]bundlereader.Auth{cli.AssetsPath + path: {Username: username, Password: password}}})
}).Should(Not(HaveOccurred()))
By("verify Bundle is created with all the resources inside of the helm release", func() {
Eventually(verifyResourcesArePresent).Should(BeTrue())
Expand All @@ -190,7 +194,7 @@ func testHelmRepo(path, port string) {
})
It("fleet apply uses credentials from HelmSecretNameForPaths", func() {
Eventually(func() error {
return fleetApply([]string{cli.AssetsPath + path}, apply.Options{Auth: bundlereader.Auth{Username: "wrong", Password: "wrong"}, AuthByPath: map[string]bundlereader.Auth{cli.AssetsPath + path: {Username: username, Password: password}}})
return fleetApply("helm", []string{cli.AssetsPath + path}, apply.Options{Auth: bundlereader.Auth{Username: "wrong", Password: "wrong"}, AuthByPath: map[string]bundlereader.Auth{cli.AssetsPath + path: {Username: username, Password: password}}})
}).Should(Not(HaveOccurred()))
By("verify Bundle is created with all the resources inside of the helm release", func() {
Eventually(verifyResourcesArePresent).Should(BeTrue())
Expand Down
12 changes: 8 additions & 4 deletions integrationtests/cli/apply/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ import (

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

func TestFleet(t *testing.T) {
var buf *gbytes.Buffer

func TestFleetApply(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Fleet Suite")
RunSpecs(t, "Fleet CLI Apply Suite")
}

// simulates fleet cli execution
func fleetApply(name string, dirs []string, options apply.Options) error {

return apply.Apply(context.Background(), client.NewGetter("", "", "fleet-local"), name, dirs, options)
buf = gbytes.NewBuffer()
options.Output = buf
return apply.CreateBundles(context.Background(), client.NewGetter("", "", "fleet-local"), name, dirs, options)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

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

var _ = Describe("Fleet apply targets", func() {
Expand All @@ -26,12 +25,11 @@ var _ = Describe("Fleet apply targets", func() {

When("Targets file is empty, and overrideTargets is not provided", func() {
BeforeEach(func() {
options = apply.Options{Output: gbytes.NewBuffer()}
dirs = []string{cli.AssetsPath + "targets/simple"}
})

It("Bundle contains the default target", func() {
bundle, err := cli.GetBundleFromOutput(options.Output)
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(len(bundle.Spec.Targets)).To(Equal(1))
Expect(bundle.Spec.Targets[0].ClusterGroup).To(Equal("default"))
Expand All @@ -41,12 +39,11 @@ var _ = Describe("Fleet apply targets", func() {

When("Targets file is empty, and overrideTargets is provided", func() {
BeforeEach(func() {
options = apply.Options{Output: gbytes.NewBuffer()}
dirs = []string{cli.AssetsPath + "targets/override"}
})

It("Bundle contains targets and targetRestrictions from override", func() {
bundle, err := cli.GetBundleFromOutput(options.Output)
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(len(bundle.Spec.Targets)).To(Equal(1))
Expect(bundle.Spec.Targets[0].ClusterName).To(Equal("overridden"))
Expand All @@ -65,12 +62,12 @@ var _ = Describe("Fleet apply targets", func() {
targets = []fleet.BundleTarget{{Name: "target1", ClusterName: "test1"}}
targetRestrictions = []fleet.BundleTargetRestriction{{Name: "target1", ClusterName: "test1"}}
file := createTargetsFile(targets, targetRestrictions)
options = apply.Options{Output: gbytes.NewBuffer(), TargetsFile: file.Name()}
options = apply.Options{TargetsFile: file.Name()}
dirs = []string{cli.AssetsPath + "targets/simple"}
})

It("Bundle contains targets and targetRestrictions from the targets file", func() {
bundle, err := cli.GetBundleFromOutput(options.Output)
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(bundle.Spec.Targets).To(Equal(targets))
Expect(bundle.Spec.TargetRestrictions).To(Equal(targetRestrictions))
Expand All @@ -87,12 +84,12 @@ var _ = Describe("Fleet apply targets", func() {
targets = []fleet.BundleTarget{{Name: "target1", ClusterName: "test1"}}
targetRestrictions = []fleet.BundleTargetRestriction{{Name: "target1", ClusterName: "test1"}}
file := createTargetsFile(targets, targetRestrictions)
options = apply.Options{Output: gbytes.NewBuffer(), TargetsFile: file.Name()}
options = apply.Options{TargetsFile: file.Name()}
dirs = []string{cli.AssetsPath + "targets/override"}
})

It("Bundle contains targets and targetRestrictions from override", func() {
bundle, err := cli.GetBundleFromOutput(options.Output)
bundle, err := cli.GetBundleFromOutput(buf)
Expect(err).NotTo(HaveOccurred())
Expect(bundle.Spec.Targets).To(Not(Equal(targets)))
Expect(bundle.Spec.TargetRestrictions).To(Not(Equal(targetRestrictions)))
Expand Down
Loading

0 comments on commit 45a0891

Please sign in to comment.