Skip to content

Commit

Permalink
Merge pull request #708 from allenporter/flux-generator-remote
Browse files Browse the repository at this point in the history
Fix a silently dropping Kustomization remote resources
  • Loading branch information
hiddeco authored Dec 18, 2023
2 parents e4b557e + 99853c5 commit acee675
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
24 changes: 13 additions & 11 deletions kustomize/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,26 @@ func filterKsWithIgnoreFiles(ks *kustypes.Kustomization, dirPath string, ignore
return nil
}

func isUrl(s string) bool {
u, err := url.ParseRequestURI(s)
return err == nil && u.Scheme != ""
}

func filterSlice(ks *kustypes.Kustomization, path string, s *[]string, t string, filter filter) error {
start := 0
for _, res := range *s {
// check if we have a url and skip it
// 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 {
if u, err := url.ParseRequestURI(res); err == nil && u.Scheme != "" {
if t == crds || !isUrl(res) {
f := filepath.Join(path, res)
info, err := os.Lstat(f)
if err != nil {
return err
}
if filter(f, info) {
continue
}
}
f := filepath.Join(path, res)
info, err := os.Lstat(f)
if err != nil {
return err
}
if filter(f, info) {
continue
}
(*s)[start] = res
start++
}
Expand Down
59 changes: 59 additions & 0 deletions kustomize/kustomize_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"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"
)
Expand Down Expand Up @@ -290,3 +291,61 @@ func TestKustomizationGenerator_WithSourceIgnore(t *testing.T) {
})
}
}

func TestKustomizationGenerator_WithRemoteResource(t *testing.T) {
tests := []struct {
name string
path string
ignore string
expected []string
}{
{
name: "without ignore",
expected: []string{
"configmap.yaml",
"https://raw.githubusercontent.com/fluxcd/flux2/main/manifests/rbac/controller.yaml",
},
},
{
name: "with ignore",
ignore: "configmap.yaml",
expected: []string{
"https://raw.githubusercontent.com/fluxcd/flux2/main/manifests/rbac/controller.yaml",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
// Create a kustomization file with varsub
yamlKus, err := os.ReadFile("./testdata/kustomization.yaml")
g.Expect(err).NotTo(HaveOccurred())

clientObjects, err := readYamlObjects(strings.NewReader(string(yamlKus)))
g.Expect(err).NotTo(HaveOccurred())

// Get a generator
gen := kustomize.NewGeneratorWithIgnore("./testdata/remote", tt.ignore, clientObjects[0])
action, err := gen.WriteFile("./testdata/remote", kustomize.WithSaveOriginalKustomization())
g.Expect(err).NotTo(HaveOccurred())
defer kustomize.CleanDirectory("./testdata/remote", action)

// Read updated Kustomization contents
updatedContent, err := os.ReadFile("./testdata/remote/kustomization.yaml")
g.Expect(err).NotTo(HaveOccurred())

kus := kustypes.Kustomization{
TypeMeta: kustypes.TypeMeta{
APIVersion: kustypes.KustomizationVersion,
Kind: kustypes.KustomizationKind,
},
}
err = yaml.Unmarshal(updatedContent, &kus)
g.Expect(err).NotTo(HaveOccurred())

// Check that the resources are passed through
g.Expect(kus.Resources).To(Equal(tt.expected))
})
}
}
9 changes: 9 additions & 0 deletions kustomize/testdata/remote/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: app-vars
namespace: apps
data:
config: key

9 changes: 9 additions & 0 deletions kustomize/testdata/remote/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: apps
resources:
- configmap.yaml
# Arbitrary remote resource. The specific resource doesn't matter. We don't actually
# build this in the test.
- https://raw.githubusercontent.com/fluxcd/flux2/main/manifests/rbac/controller.yaml

0 comments on commit acee675

Please sign in to comment.