Skip to content

Commit

Permalink
Merge pull request #718 from fluxcd/restore-emptydir-build
Browse files Browse the repository at this point in the history
kustomize: Restore empty dir build
  • Loading branch information
stefanprodan authored Jan 19, 2024
2 parents c2c9efe + e4ca429 commit 87f5fa0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 28 deletions.
7 changes: 2 additions & 5 deletions kustomize/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ require (
github.com/drone/envsubst v1.0.3
github.com/fluxcd/pkg/apis/kustomize v1.2.0
github.com/fluxcd/pkg/sourceignore v0.4.0
github.com/go-git/go-git/v5 v5.10.1
github.com/hashicorp/go-multierror v1.1.1
github.com/go-git/go-git/v5 v5.11.0
github.com/onsi/gomega v1.30.0
github.com/otiai10/copy v1.14.0
k8s.io/api v0.28.4
k8s.io/apiextensions-apiserver v0.28.4
k8s.io/apimachinery v0.28.4
k8s.io/client-go v0.28.4
sigs.k8s.io/controller-runtime v0.16.3
sigs.k8s.io/kustomize/api v0.15.0
sigs.k8s.io/kustomize/api v0.16.0
sigs.k8s.io/kustomize/kyaml v0.16.0
sigs.k8s.io/yaml v1.4.0
)
Expand Down Expand Up @@ -52,7 +51,6 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand All @@ -69,7 +67,6 @@ require (
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
golang.org/x/net v0.19.0 // indirect
Expand Down
10 changes: 2 additions & 8 deletions kustomize/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66D
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=
github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow=
github.com/go-git/go-git/v5 v5.10.1 h1:tu8/D8i+TWxgKpzQ3Vc43e+kkhXqtsZCKI/egajKnxk=
github.com/go-git/go-git/v5 v5.10.1/go.mod h1:uEuHjxkHap8kAl//V5F/nNWwqIYtP/402ddd05mp0wg=
github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4=
github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
Expand Down Expand Up @@ -76,11 +76,6 @@ github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaU
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
Expand Down Expand Up @@ -144,7 +139,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
29 changes: 18 additions & 11 deletions kustomize/kustomize_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package kustomize

import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand All @@ -26,7 +27,6 @@ import (
"strings"
"sync"

securefs "github.com/fluxcd/pkg/kustomize/filesys"
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 @@ -274,12 +274,12 @@ func (g *Generator) getPatches() ([]kustomize.Patch, error) {
patch, ok := p.(map[string]interface{})
if !ok {
err := fmt.Errorf("unable to convert patch %d to map[string]interface{}", k)
resultErr = multierror.Append(resultErr, err)
resultErr = errors.Join(resultErr, err)
}
var kpatch kustomize.Patch
err = runtime.DefaultUnstructuredConverter.FromUnstructured(patch, &kpatch)
if err != nil {
resultErr = multierror.Append(resultErr, err)
resultErr = errors.Join(resultErr, err)
}
res = append(res, kpatch)
}
Expand All @@ -303,12 +303,12 @@ func (g *Generator) getPatchesStrategicMerge() ([]apiextensionsv1.JSON, error) {
patch, ok := p.(map[string]interface{})
if !ok {
err := fmt.Errorf("unable to convert patch %d to map[string]interface{}", k)
resultErr = multierror.Append(resultErr, err)
resultErr = errors.Join(resultErr, err)
}
var kpatch apiextensionsv1.JSON
err = runtime.DefaultUnstructuredConverter.FromUnstructured(patch, &kpatch)
if err != nil {
resultErr = multierror.Append(resultErr, err)
resultErr = errors.Join(resultErr, err)
}
res = append(res, kpatch)
}
Expand All @@ -332,12 +332,12 @@ func (g *Generator) getPatchesJson6902() ([]kustomize.JSON6902Patch, error) {
patch, ok := p.(map[string]interface{})
if !ok {
err := fmt.Errorf("unable to convert patch %d to map[string]interface{}", k)
resultErr = multierror.Append(resultErr, err)
resultErr = errors.Join(resultErr, err)
}
var kpatch kustomize.JSON6902Patch
err = runtime.DefaultUnstructuredConverter.FromUnstructured(patch, &kpatch)
if err != nil {
resultErr = multierror.Append(resultErr, err)
resultErr = errors.Join(resultErr, err)
}
res = append(res, kpatch)
}
Expand All @@ -361,12 +361,12 @@ func (g *Generator) getImages() ([]kustomize.Image, error) {
im, ok := i.(map[string]interface{})
if !ok {
err := fmt.Errorf("unable to convert patch %d to map[string]interface{}", k)
resultErr = multierror.Append(resultErr, err)
resultErr = errors.Join(resultErr, err)
}
var image kustomize.Image
err = runtime.DefaultUnstructuredConverter.FromUnstructured(im, &image)
if err != nil {
resultErr = multierror.Append(resultErr, err)
resultErr = errors.Join(resultErr, err)
}
res = append(res, image)
}
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
6 changes: 3 additions & 3 deletions kustomize/kustomize_varsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package kustomize

import (
"context"
"errors"
"fmt"
"regexp"
"strings"

"github.com/drone/envsubst"
"github.com/hashicorp/go-multierror"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -181,12 +181,12 @@ func getSubstituteFrom(kustomization unstructured.Unstructured) ([]SubstituteRef
sub, ok := s.(map[string]interface{})
if !ok {
err := fmt.Errorf("unable to convert patch %d to map[string]interface{}", k)
resultErr = multierror.Append(resultErr, err)
resultErr = errors.Join(resultErr, err)
}
var substitute SubstituteReference
err = runtime.DefaultUnstructuredConverter.FromUnstructured(sub, &substitute)
if err != nil {
resultErr = multierror.Append(resultErr, err)
resultErr = errors.Join(resultErr, err)
}
res = append(res, substitute)
}
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 87f5fa0

Please sign in to comment.