Skip to content

Commit cb1df5d

Browse files
nitishfycrenshaw-dev
andauthoredFeb 28, 2025··
fix: correct lookup for the kustomization file when applying patches (cherry-pick #22024) (#22086)
Signed-off-by: nitishfy <justnitish06@gmail.com> Co-authored-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>

File tree

4 files changed

+88
-23
lines changed

4 files changed

+88
-23
lines changed
 

‎util/kustomize/kustomize.go

+31-14
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/argoproj/argo-cd/v2/util/proxy"
2626
)
2727

28-
// represents a Docker image in the format NAME[:TAG].
28+
// Image represents a Docker image in the format NAME[:TAG].
2929
type Image = string
3030

3131
type BuildOpts struct {
@@ -69,7 +69,29 @@ type kustomize struct {
6969
noProxy string
7070
}
7171

72-
var _ Kustomize = &kustomize{}
72+
var KustomizationNames = []string{"kustomization.yaml", "kustomization.yml", "Kustomization"}
73+
74+
// IsKustomization checks if the given file name matches any known kustomization file names.
75+
func IsKustomization(path string) bool {
76+
for _, kustomization := range KustomizationNames {
77+
if path == kustomization {
78+
return true
79+
}
80+
}
81+
return false
82+
}
83+
84+
// findKustomizeFile looks for any known kustomization file in the path
85+
func findKustomizeFile(dir string) string {
86+
for _, file := range KustomizationNames {
87+
path := filepath.Join(dir, file)
88+
if _, err := os.Stat(path); err == nil {
89+
return file
90+
}
91+
}
92+
93+
return ""
94+
}
7395

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

254276
if len(opts.Patches) > 0 {
255-
kustomizationPath := filepath.Join(k.path, "kustomization.yaml")
277+
kustFile := findKustomizeFile(k.path)
278+
// If the kustomization file is not found, return early.
279+
// There is no point reading the kustomization path if it doesn't exist.
280+
if kustFile == "" {
281+
return nil, nil, nil, fmt.Errorf("kustomization file not found in the path")
282+
}
283+
kustomizationPath := filepath.Join(k.path, kustFile)
256284
b, err := os.ReadFile(kustomizationPath)
257285
if err != nil {
258286
return nil, nil, nil, fmt.Errorf("failed to load kustomization.yaml: %w", err)
@@ -369,17 +397,6 @@ func isHelmEnabled(buildOptions string) bool {
369397
return strings.Contains(buildOptions, "--enable-helm")
370398
}
371399

372-
var KustomizationNames = []string{"kustomization.yaml", "kustomization.yml", "Kustomization"}
373-
374-
func IsKustomization(path string) bool {
375-
for _, kustomization := range KustomizationNames {
376-
if path == kustomization {
377-
return true
378-
}
379-
}
380-
return false
381-
}
382-
383400
// semver/v3 doesn't export the regexp anymore, so shamelessly copied it over to
384401
// here.
385402
// https://github.com/Masterminds/semver/blob/49c09bfed6adcffa16482ddc5e5588cffff9883a/version.go#L42

‎util/kustomize/kustomize_test.go

+33-9
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ import (
1818
)
1919

2020
const (
21-
kustomization1 = "kustomization_yaml"
22-
kustomization2a = "kustomization_yml"
23-
kustomization2b = "Kustomization"
24-
kustomization3 = "force_common"
25-
kustomization4 = "custom_version"
26-
kustomization5 = "kustomization_yaml_patches"
27-
kustomization6 = "kustomization_yaml_components"
28-
kustomization7 = "label_without_selector"
21+
kustomization1 = "kustomization_yaml"
22+
kustomization3 = "force_common"
23+
kustomization4 = "custom_version"
24+
kustomization5 = "kustomization_yaml_patches"
25+
kustomization6 = "kustomization_yaml_components"
26+
kustomization7 = "label_without_selector"
27+
kustomization8 = "kustomization_yaml_patches_empty"
2928
)
3029

3130
func testDataDir(tb testing.TB, testData string) (string, error) {
@@ -65,7 +64,7 @@ func TestKustomizeBuild(t *testing.T) {
6564
Replicas: []v1alpha1.KustomizeReplica{
6665
{
6766
Name: "nginx-deployment",
68-
Count: intstr.FromInt(2),
67+
Count: intstr.FromInt32(2),
6968
},
7069
{
7170
Name: "web",
@@ -512,3 +511,28 @@ func TestKustomizeBuildPatches(t *testing.T) {
512511
require.NoError(t, err)
513512
assert.Equal(t, "test", name)
514513
}
514+
515+
func TestFailKustomizeBuildPatches(t *testing.T) {
516+
appPath, err := testDataDir(t, kustomization8)
517+
require.NoError(t, err)
518+
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
519+
520+
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
521+
Patches: []v1alpha1.KustomizePatch{
522+
{
523+
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" }]`,
524+
Target: &v1alpha1.KustomizeSelector{
525+
KustomizeResId: v1alpha1.KustomizeResId{
526+
KustomizeGvk: v1alpha1.KustomizeGvk{
527+
Kind: "Deployment",
528+
},
529+
Name: "nginx-deployment",
530+
},
531+
},
532+
},
533+
},
534+
}
535+
536+
_, _, _, err = kustomize.Build(&kustomizeSource, nil, nil, nil)
537+
require.EqualError(t, err, "kustomization file not found in the path")
538+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This is an invalid kustomization file
2+
resources:
3+
- ./deployment.yaml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: nginx-deployment
5+
labels:
6+
app: nginx
7+
spec:
8+
replicas: 3
9+
selector:
10+
matchLabels:
11+
app: nginx
12+
template:
13+
metadata:
14+
labels:
15+
app: nginx
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.15.4
20+
ports:
21+
- containerPort: 80

0 commit comments

Comments
 (0)
Please sign in to comment.