Skip to content

Commit

Permalink
fix: correct lookup for the kustomization file when applying patches (a…
Browse files Browse the repository at this point in the history
…rgoproj#22024)

Signed-off-by: nitishfy <justnitish06@gmail.com>
Co-authored-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
  • Loading branch information
nitishfy and crenshaw-dev committed Feb 28, 2025
1 parent 896a461 commit b4f8aaa
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 23 deletions.
45 changes: 31 additions & 14 deletions util/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/argoproj/argo-cd/v2/util/proxy"
)

// represents a Docker image in the format NAME[:TAG].
// Image represents a Docker image in the format NAME[:TAG].
type Image = string

type BuildOpts struct {
Expand Down Expand Up @@ -69,7 +69,29 @@ type kustomize struct {
noProxy string
}

var _ Kustomize = &kustomize{}
var KustomizationNames = []string{"kustomization.yaml", "kustomization.yml", "Kustomization"}

// IsKustomization checks if the given file name matches any known kustomization file names.
func IsKustomization(path string) bool {
for _, kustomization := range KustomizationNames {
if path == kustomization {
return true
}
}
return false
}

// findKustomizeFile looks for any known kustomization file in the path
func findKustomizeFile(dir string) string {
for _, file := range KustomizationNames {
path := filepath.Join(dir, file)
if _, err := os.Stat(path); err == nil {
return file
}
}

return ""
}

func (k *kustomize) getBinaryPath() string {
if k.binaryPath != "" {
Expand Down Expand Up @@ -252,7 +274,13 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
}

if len(opts.Patches) > 0 {
kustomizationPath := filepath.Join(k.path, "kustomization.yaml")
kustFile := findKustomizeFile(k.path)
// If the kustomization file is not found, return early.
// There is no point reading the kustomization path if it doesn't exist.
if kustFile == "" {
return nil, nil, nil, errors.New("kustomization file not found in the path")
}
kustomizationPath := filepath.Join(k.path, kustFile)
b, err := os.ReadFile(kustomizationPath)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to load kustomization.yaml: %w", err)
Expand Down Expand Up @@ -369,17 +397,6 @@ func isHelmEnabled(buildOptions string) bool {
return strings.Contains(buildOptions, "--enable-helm")
}

var KustomizationNames = []string{"kustomization.yaml", "kustomization.yml", "Kustomization"}

func IsKustomization(path string) bool {
for _, kustomization := range KustomizationNames {
if path == kustomization {
return true
}
}
return false
}

// semver/v3 doesn't export the regexp anymore, so shamelessly copied it over to
// here.
// https://github.com/Masterminds/semver/blob/49c09bfed6adcffa16482ddc5e5588cffff9883a/version.go#L42
Expand Down
42 changes: 33 additions & 9 deletions util/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import (
)

const (
kustomization1 = "kustomization_yaml"
kustomization2a = "kustomization_yml"
kustomization2b = "Kustomization"
kustomization3 = "force_common"
kustomization4 = "custom_version"
kustomization5 = "kustomization_yaml_patches"
kustomization6 = "kustomization_yaml_components"
kustomization7 = "label_without_selector"
kustomization1 = "kustomization_yaml"
kustomization3 = "force_common"
kustomization4 = "custom_version"
kustomization5 = "kustomization_yaml_patches"
kustomization6 = "kustomization_yaml_components"
kustomization7 = "label_without_selector"
kustomization8 = "kustomization_yaml_patches_empty"
)

func testDataDir(tb testing.TB, testData string) (string, error) {
Expand Down Expand Up @@ -65,7 +64,7 @@ func TestKustomizeBuild(t *testing.T) {
Replicas: []v1alpha1.KustomizeReplica{
{
Name: "nginx-deployment",
Count: intstr.FromInt(2),
Count: intstr.FromInt32(2),
},
{
Name: "web",
Expand Down Expand Up @@ -512,3 +511,28 @@ func TestKustomizeBuildPatches(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "test", name)
}

func TestFailKustomizeBuildPatches(t *testing.T) {
appPath, err := testDataDir(t, kustomization8)
require.NoError(t, err)
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")

kustomizeSource := v1alpha1.ApplicationSourceKustomize{
Patches: []v1alpha1.KustomizePatch{
{
Patch: `[ { "op": "replace", "path": "/spec/template/spec/containers/0/ports/0/containerPort", "value": 443 }, { "op": "replace", "path": "/spec/template/spec/containers/0/name", "value": "test" }]`,
Target: &v1alpha1.KustomizeSelector{
KustomizeResId: v1alpha1.KustomizeResId{
KustomizeGvk: v1alpha1.KustomizeGvk{
Kind: "Deployment",
},
Name: "nginx-deployment",
},
},
},
},
}

_, _, _, err = kustomize.Build(&kustomizeSource, nil, nil, nil)
require.EqualError(t, err, "kustomization file not found in the path")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is an invalid kustomization file
resources:
- ./deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.4
ports:
- containerPort: 80

0 comments on commit b4f8aaa

Please sign in to comment.