Skip to content

Commit 02ac738

Browse files
authored
Exclude Fleet config files regardless of their naming (#4208)
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 505915d commit 02ac738

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

integrationtests/cli/apply/apply_test.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,26 @@ var _ = Describe("Fleet apply driven", Ordered, func() {
338338
cli.AssetsPath + "driven/kustomize/overlays/dev/secret.yaml",
339339
cli.AssetsPath + "driven/kustomize/overlays/prod/kustomization.yaml",
340340
cli.AssetsPath + "driven/kustomize/overlays/prod/secret.yaml",
341-
cli.AssetsPath + "driven/kustomize/dev.yaml",
342-
cli.AssetsPath + "driven/kustomize/prod.yaml",
343341
}
344-
Expect(kDevBundle.Spec.Resources).To(HaveLen(8))
345-
Expect(kProdBundle.Spec.Resources).To(HaveLen(8))
346-
for _, r := range kResources {
342+
343+
// Note: the presence of a .fleetignore file, at the same level as both `dev.yaml` and
344+
// `prod.yaml`, excluding only `dev.yaml`, enables us to ensure that that file does not end up in
345+
// any bundle, being excluded from the dev bundle, but _also_ from the prod bundle.
346+
// We deliberately don't exclude both `dev.yaml` and `prod.yaml` from `.fleetignore`, simply to
347+
// validate that `prod.yaml` is excluded from the prod bundle without needing to be excluded from
348+
// `.fleetignore`.
349+
350+
kDevResources := append(slices.Clone(kResources), cli.AssetsPath+"driven/kustomize/prod.yaml")
351+
kProdResources := slices.Clone(kResources)
352+
353+
Expect(kDevBundle.Spec.Resources).To(HaveLen(7))
354+
Expect(kProdBundle.Spec.Resources).To(HaveLen(6))
355+
356+
for _, r := range kDevResources {
347357
Expect(r).To(bePresentInBundleResources(kDevBundle.Spec.Resources))
358+
}
359+
360+
for _, r := range kProdResources {
348361
Expect(r).To(bePresentInBundleResources(kProdBundle.Spec.Resources))
349362
}
350363

@@ -386,13 +399,26 @@ var _ = Describe("Fleet apply driven", Ordered, func() {
386399
cli.AssetsPath + "driven2/kustomize/overlays/dev/secret.yaml",
387400
cli.AssetsPath + "driven2/kustomize/overlays/prod/kustomization.yaml",
388401
cli.AssetsPath + "driven2/kustomize/overlays/prod/secret.yaml",
389-
cli.AssetsPath + "driven2/kustomize/fleetDev.yaml",
390-
cli.AssetsPath + "driven2/kustomize/fleetProd.yaml",
391402
}
392-
Expect(kDevBundle.Spec.Resources).To(HaveLen(8))
393-
Expect(kProdBundle.Spec.Resources).To(HaveLen(8))
394-
for _, r := range kResources {
403+
404+
// Note: the presence of a .fleetignore file, at the same level as both `fleetDev.yaml` and
405+
// `fleetProd.yaml`, excluding only `fleetProd.yaml`, enables us to ensure that that file does
406+
// not end up in any bundle, being excluded from the prod bundle, but _also_ from the dev bundle.
407+
// We deliberately don't exclude both `fleetDev.yaml` and `fleetProd.yaml` from `.fleetignore`,
408+
// simply to validate that `fleetDev.yaml` is excluded from the dev bundle without needing to be
409+
// excluded from `.fleetignore`.
410+
411+
kDevResources := slices.Clone(kResources)
412+
kProdResources := append(slices.Clone(kResources), cli.AssetsPath+"driven2/kustomize/fleetDev.yaml")
413+
414+
Expect(kDevBundle.Spec.Resources).To(HaveLen(6))
415+
Expect(kProdBundle.Spec.Resources).To(HaveLen(7))
416+
417+
for _, r := range kDevResources {
395418
Expect(r).To(bePresentInBundleResources(kDevBundle.Spec.Resources))
419+
}
420+
421+
for _, r := range kProdResources {
396422
Expect(r).To(bePresentInBundleResources(kProdBundle.Spec.Resources))
397423
}
398424

@@ -457,15 +483,15 @@ var _ = Describe("Fleet apply driven", Ordered, func() {
457483
// helm bundle
458484
helmBundle := bundles[0]
459485
Expect(helmBundle.Name).To(Equal("assets-driven-fleet-yaml-subfolder-helm-test-fl-b676f"))
460-
Expect(helmBundle.Spec.Resources).To(HaveLen(4))
486+
Expect(helmBundle.Spec.Resources).To(HaveLen(3))
461487
// as files were unpacked from the downloaded chart we can't just
462488
// list the files in the original folder and compare.
463489
// Files are only located in the bundle resources
464490
Expect("Chart.yaml").To(bePresentOnlyInBundleResources(helmBundle.Spec.Resources))
465491
Expect("values.yaml").To(bePresentOnlyInBundleResources(helmBundle.Spec.Resources))
466492
Expect("templates/configmap.yaml").To(bePresentOnlyInBundleResources(helmBundle.Spec.Resources))
467493
resPath := cli.AssetsPath + "driven_fleet_yaml_subfolder/helm/test/fleet.yaml"
468-
Expect(resPath).To(bePresentInBundleResources(helmBundle.Spec.Resources))
494+
Expect(resPath).NotTo(bePresentInBundleResources(helmBundle.Spec.Resources))
469495
// check for helm options defined in the fleet.yaml file
470496
Expect(helmBundle.Spec.Helm).ToNot(BeNil())
471497
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
@@ -329,6 +329,7 @@ func newBundle(ctx context.Context, name, baseDir string, opts Options) (*fleet.
329329
} else {
330330
var err error
331331
bundle, scans, err = bundlereader.NewBundle(ctx, name, baseDir, opts.BundleFile, &bundlereader.Options{
332+
BundleFile: opts.BundleFile,
332333
Compress: opts.Compress,
333334
Labels: opts.Labels,
334335
ServiceAccount: opts.ServiceAccount,

0 commit comments

Comments
 (0)