Skip to content

Commit

Permalink
kustomize: Restore empty dir build
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Jan 18, 2024
1 parent c2c9efe commit a542b49
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
13 changes: 10 additions & 3 deletions kustomize/kustomize_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strings"
"sync"

securefs "github.com/fluxcd/pkg/kustomize/filesys"
"github.com/hashicorp/go-multierror"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -39,7 +39,7 @@ import (
"sigs.k8s.io/yaml"

"github.com/fluxcd/pkg/apis/kustomize"
"github.com/hashicorp/go-multierror"
securefs "github.com/fluxcd/pkg/kustomize/filesys"
)

const (
Expand Down Expand Up @@ -467,7 +467,14 @@ func (g *Generator) generateKustomization(dirPath string) (Action, string, error
resources = append(resources, strings.Replace(file, abs, ".", 1))
}

kus.Resources = resources
if len(resources) == 0 {
// if there are no resources, set a placeholder namespace
// to avoid "kustomization.yaml is empty" build error
kus.Namespace = "_placeholder"
} else {
kus.Resources = resources
}

kd, err := yaml.Marshal(kus)
if err != nil {
// delete the kustomization file
Expand Down
26 changes: 25 additions & 1 deletion kustomize/kustomize_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,42 @@ import (
"strings"
"testing"

"github.com/fluxcd/pkg/kustomize"
. "github.com/onsi/gomega"
"github.com/otiai10/copy"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/kustomize/api/resmap"
kustypes "sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/yaml"

"github.com/fluxcd/pkg/kustomize"
)

const resourcePath = "./testdata/resources/"

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

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

emptyDir, err := testTempDir(t)
g.Expect(err).NotTo(HaveOccurred())

_, err = kustomize.NewGenerator("", ks[0]).WriteFile(emptyDir)
g.Expect(err).NotTo(HaveOccurred())

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

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

func TestKustomizationGenerator(t *testing.T) {
tests := []struct {
name string
Expand Down
13 changes: 13 additions & 0 deletions kustomize/testdata/empty/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: ./empty
prune: true
sourceRef:
kind: GitRepository
name: app

0 comments on commit a542b49

Please sign in to comment.