Skip to content

Commit

Permalink
Merge pull request #757 from fluxcd/build-ks-no-resources
Browse files Browse the repository at this point in the history
kustomize: Allow empty resources in `kustomization.yaml`
  • Loading branch information
stefanprodan authored Apr 8, 2024
2 parents cfa07c7 + f5498f9 commit d8ff5fa
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
15 changes: 5 additions & 10 deletions kustomize/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ import (
"path/filepath"
"strings"

"github.com/fluxcd/pkg/sourceignore"
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
kustypes "sigs.k8s.io/kustomize/api/types"
)

const (
crds = "crds"
resources = "resources"
components = "components"
"github.com/fluxcd/pkg/sourceignore"
)

// filter must return true if a file should not be included in the archive after inspecting the given path
Expand Down Expand Up @@ -61,19 +56,19 @@ func filterKsWithIgnoreFiles(ks *kustypes.Kustomization, dirPath string, ignore
}

// filter resources first
err = filterSlice(ks, path, &ks.Resources, resources, ignoreFileFilter(ps, ignoreDomain))
err = filterSlice(ks, path, &ks.Resources, resourcesField, ignoreFileFilter(ps, ignoreDomain))
if err != nil {
return err
}

// filter components second
err = filterSlice(ks, path, &ks.Components, components, ignoreFileFilter(ps, ignoreDomain))
err = filterSlice(ks, path, &ks.Components, componentsField, ignoreFileFilter(ps, ignoreDomain))
if err != nil {
return err
}

// filter crds third
err = filterSlice(ks, path, &ks.Crds, crds, ignoreFileFilter(ps, ignoreDomain))
err = filterSlice(ks, path, &ks.Crds, crdsField, ignoreFileFilter(ps, ignoreDomain))
if err != nil {
return err
}
Expand All @@ -91,7 +86,7 @@ func filterSlice(ks *kustypes.Kustomization, path string, s *[]string, t string,
for _, res := range *s {
// check if we have a url and skip the source file filters
// this is not needed for crds as they are not allowed to be urls
if t == crds || !isUrl(res) {
if t == crdsField || !isUrl(res) {
f := filepath.Join(path, res)
info, err := os.Lstat(f)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions kustomize/kustomize_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ import (
const (
specField = "spec"
targetNSField = "targetNamespace"
resourcesField = "resources"
patchesField = "patches"
componentsField = "components"
crdsField = "crds"
patchesSMField = "patchesStrategicMerge"
patchesJson6902Field = "patchesJson6902"
imagesField = "images"
Expand Down Expand Up @@ -147,6 +149,12 @@ func (g *Generator) WriteFile(dirPath string, opts ...SavingOptions) (Action, er
return action, fmt.Errorf("%v %v", err, errf)
}

if action == UnchangedAction && len(kus.Resources) == 0 {
// if there are no resources, set the build metadata
// to avoid "kustomization.yaml is empty" build error
kus.BuildMetadata = []string{"originAnnotations"}
}

// apply filters if any
if g.filter {
err = filterKsWithIgnoreFiles(&kus, dirPath, g.ignore)
Expand Down
23 changes: 23 additions & 0 deletions kustomize/kustomize_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ func TestGenerator_EmptyDir(t *testing.T) {
g.Expect(resMap.Resources()).To(HaveLen(0))
}

func TestGenerator_NoResources(t *testing.T) {
g := NewWithT(t)
dataKS, err := os.ReadFile("./testdata/noResources/ks.yaml")
g.Expect(err).NotTo(HaveOccurred())

ks, err := readYamlObjects(strings.NewReader(string(dataKS)))
g.Expect(err).NotTo(HaveOccurred())

tmpDir, err := testTempDir(t)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(copy.Copy("testdata/noResources", tmpDir)).To(Succeed())
_, err = kustomize.NewGenerator(tmpDir, ks[0]).WriteFile(tmpDir)
g.Expect(err).NotTo(HaveOccurred())

resMap, err := kustomize.SecureBuild(tmpDir, tmpDir, false)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(resMap.Resources()).To(HaveLen(0))

data, err := os.ReadFile(filepath.Join(tmpDir, "kustomization.yaml"))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(string(data)).To(ContainSubstring("originAnnotations"))
}

func TestKustomizationGenerator(t *testing.T) {
tests := []struct {
name string
Expand Down
13 changes: 13 additions & 0 deletions kustomize/testdata/noResources/ks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: app
namespace: apps
spec:
interval: 4m0s
path: ./
prune: true
sourceRef:
kind: GitRepository
name: app
4 changes: 4 additions & 0 deletions kustomize/testdata/noResources/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: []

0 comments on commit d8ff5fa

Please sign in to comment.