Skip to content

Commit e253e0b

Browse files
committed
Exclude Fleet config files regardless of their naming
While Fleet config files are typically named `fleet.yaml`, the recent introduction of user-driven bundle scanning enables a config file to be named arbitrarily, in which case it must still be excluded from the corresponding bundle. Fleet now takes care of this without any action needed from the user. Integration tests also demonstrate that `.fleetignore` files can be leveraged to exclude config files living in the same directory as the config file referenced explicitly through user-driven bundle scanning. Fleet would otherwise not exclude those files from the bundle.
1 parent 4a7bce4 commit e253e0b

6 files changed

Lines changed: 48 additions & 18 deletions

File tree

integrationtests/cli/apply/apply_test.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,26 @@ var _ = Describe("Fleet apply driven", Ordered, func() {
355355
cli.AssetsPath + "driven/kustomize/overlays/dev/secret.yaml",
356356
cli.AssetsPath + "driven/kustomize/overlays/prod/kustomization.yaml",
357357
cli.AssetsPath + "driven/kustomize/overlays/prod/secret.yaml",
358-
cli.AssetsPath + "driven/kustomize/dev.yaml",
359-
cli.AssetsPath + "driven/kustomize/prod.yaml",
360358
}
361-
Expect(kDevBundle.Spec.Resources).To(HaveLen(8))
362-
Expect(kProdBundle.Spec.Resources).To(HaveLen(8))
363-
for _, r := range kResources {
359+
360+
// Note: the presence of a .fleetignore file, at the same level as both `dev.yaml` and
361+
// `prod.yaml`, excluding only `dev.yaml`, enables us to ensure that that file does not end up in
362+
// any bundle, being excluded from the dev bundle, but _also_ from the prod bundle.
363+
// We deliberately don't exclude both `dev.yaml` and `prod.yaml` from `.fleetignore`, simply to
364+
// validate that `prod.yaml` is excluded from the prod bundle without needing to be excluded from
365+
// `.fleetignore`.
366+
367+
kDevResources := append(slices.Clone(kResources), cli.AssetsPath+"driven/kustomize/prod.yaml")
368+
kProdResources := slices.Clone(kResources)
369+
370+
Expect(kDevBundle.Spec.Resources).To(HaveLen(7))
371+
Expect(kProdBundle.Spec.Resources).To(HaveLen(6))
372+
373+
for _, r := range kDevResources {
364374
Expect(r).To(bePresentInBundleResources(kDevBundle.Spec.Resources))
375+
}
376+
377+
for _, r := range kProdResources {
365378
Expect(r).To(bePresentInBundleResources(kProdBundle.Spec.Resources))
366379
}
367380

@@ -403,13 +416,26 @@ var _ = Describe("Fleet apply driven", Ordered, func() {
403416
cli.AssetsPath + "driven2/kustomize/overlays/dev/secret.yaml",
404417
cli.AssetsPath + "driven2/kustomize/overlays/prod/kustomization.yaml",
405418
cli.AssetsPath + "driven2/kustomize/overlays/prod/secret.yaml",
406-
cli.AssetsPath + "driven2/kustomize/fleetDev.yaml",
407-
cli.AssetsPath + "driven2/kustomize/fleetProd.yaml",
408419
}
409-
Expect(kDevBundle.Spec.Resources).To(HaveLen(8))
410-
Expect(kProdBundle.Spec.Resources).To(HaveLen(8))
411-
for _, r := range kResources {
420+
421+
// Note: the presence of a .fleetignore file, at the same level as both `fleetDev.yaml` and
422+
// `fleetProd.yaml`, excluding only `fleetProd.yaml`, enables us to ensure that that file does
423+
// not end up in any bundle, being excluded from the prod bundle, but _also_ from the dev bundle.
424+
// We deliberately don't exclude both `fleetDev.yaml` and `fleetProd.yaml` from `.fleetignore`,
425+
// simply to validate that `fleetDev.yaml` is excluded from the dev bundle without needing to be
426+
// excluded from `.fleetignore`.
427+
428+
kDevResources := slices.Clone(kResources)
429+
kProdResources := append(slices.Clone(kResources), cli.AssetsPath+"driven2/kustomize/fleetDev.yaml")
430+
431+
Expect(kDevBundle.Spec.Resources).To(HaveLen(6))
432+
Expect(kProdBundle.Spec.Resources).To(HaveLen(7))
433+
434+
for _, r := range kDevResources {
412435
Expect(r).To(bePresentInBundleResources(kDevBundle.Spec.Resources))
436+
}
437+
438+
for _, r := range kProdResources {
413439
Expect(r).To(bePresentInBundleResources(kProdBundle.Spec.Resources))
414440
}
415441

@@ -474,15 +500,15 @@ var _ = Describe("Fleet apply driven", Ordered, func() {
474500
// helm bundle
475501
helmBundle := bundles[0]
476502
Expect(helmBundle.Name).To(Equal("assets-driven-fleet-yaml-subfolder-helm-test-fl-b676f"))
477-
Expect(helmBundle.Spec.Resources).To(HaveLen(4))
503+
Expect(helmBundle.Spec.Resources).To(HaveLen(3))
478504
// as files were unpacked from the downloaded chart we can't just
479505
// list the files in the original folder and compare.
480506
// Files are only located in the bundle resources
481507
Expect("Chart.yaml").To(bePresentOnlyInBundleResources(helmBundle.Spec.Resources))
482508
Expect("values.yaml").To(bePresentOnlyInBundleResources(helmBundle.Spec.Resources))
483509
Expect("templates/configmap.yaml").To(bePresentOnlyInBundleResources(helmBundle.Spec.Resources))
484510
resPath := cli.AssetsPath + "driven_fleet_yaml_subfolder/helm/test/fleet.yaml"
485-
Expect(resPath).To(bePresentInBundleResources(helmBundle.Spec.Resources))
511+
Expect(resPath).NotTo(bePresentInBundleResources(helmBundle.Spec.Resources))
486512
// check for helm options defined in the fleet.yaml file
487513
Expect(helmBundle.Spec.Helm).ToNot(BeNil())
488514
Expect(helmBundle.Spec.Helm.ReleaseName).To(Equal("config-chart"))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dev.yaml
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fleetProd.yaml

internal/bundlereader/read.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
// Options include the GitRepo overrides, which are passed via command line args
2626
type Options struct {
27+
BundleFile string
2728
Compress bool
2829
Labels map[string]string
2930
ServiceAccount string
@@ -189,7 +190,7 @@ func bundleFromDir(ctx context.Context, name, baseDir string, bundleData []byte,
189190

190191
propagateHelmChartProperties(&fy.BundleSpec)
191192

192-
resources, err := readResources(ctx, &fy.BundleSpec, opts.Compress, baseDir, opts.Auth, opts.HelmRepoURLRegex)
193+
resources, err := readResources(ctx, &fy.BundleSpec, opts.Compress, baseDir, opts.Auth, opts.HelmRepoURLRegex, opts.BundleFile)
193194
if err != nil {
194195
return nil, nil, fmt.Errorf("failed reading resources for %q: %w", baseDir, err)
195196
}

internal/bundlereader/resources.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var hasOCIURL = regexp.MustCompile(`^oci:\/\/`)
2323

2424
// readResources reads and downloads all resources from the bundle. Resources
2525
// can be downloaded and are spread across multiple directories.
26-
func readResources(ctx context.Context, spec *fleet.BundleSpec, compress bool, base string, auth Auth, helmRepoURLRegex string) ([]fleet.BundleResource, error) {
26+
func readResources(ctx context.Context, spec *fleet.BundleSpec, compress bool, base string, auth Auth, helmRepoURLRegex, bundleFile string) ([]fleet.BundleResource, error) {
2727
directories, err := addDirectory(base, ".", ".")
2828
if err != nil {
2929
return nil, err
@@ -64,7 +64,7 @@ func readResources(ctx context.Context, spec *fleet.BundleSpec, compress bool, b
6464
loadOpts := loadOpts{
6565
compress: compress,
6666
disableDepsUpdate: disableDepsUpdate,
67-
ignoreApplyConfigs: ignoreApplyConfigs(spec.Helm, spec.Targets...),
67+
ignoreApplyConfigs: ignoreApplyConfigs(bundleFile, spec.Helm, spec.Targets...),
6868
}
6969
resources, err := loadDirectories(ctx, loadOpts, directories...)
7070
if err != nil {
@@ -93,11 +93,11 @@ type loadOpts struct {
9393
// ignoreApplyConfigs returns a list of config files that should not be added to the
9494
// bundle's resources. Their contents are converted into deployment options.
9595
// This includes:
96-
// * fleet.yaml
96+
// * bundle file (typically named fleet.yaml, but may be arbitrarily named when user-driven bundle scan is used)
9797
// * spec.Helm.ValuesFiles
9898
// * spec.Targets[].Helm.ValuesFiles
99-
func ignoreApplyConfigs(spec *fleet.HelmOptions, targets ...fleet.BundleTarget) []string {
100-
ignore := []string{"fleet.yaml"}
99+
func ignoreApplyConfigs(bundleFile string, spec *fleet.HelmOptions, targets ...fleet.BundleTarget) []string {
100+
ignore := []string{"fleet.yaml", bundleFile}
101101

102102
// Values files may be referenced from `fleet.yaml` files either with their file name
103103
// alone, or with a directory prefix, for instance for a chart directory.

internal/cmd/cli/apply/apply.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ func newBundle(ctx context.Context, name, baseDir string, opts Options) (*fleet.
332332
} else {
333333
var err error
334334
bundle, scans, err = bundlereader.NewBundle(ctx, name, baseDir, opts.BundleFile, &bundlereader.Options{
335+
BundleFile: opts.BundleFile,
335336
Compress: opts.Compress,
336337
Labels: opts.Labels,
337338
ServiceAccount: opts.ServiceAccount,

0 commit comments

Comments
 (0)