Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct lookup for the kustomization file when applying patches (cherry-pick #22024) #22087

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, fmt.Errorf("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 @@ -64,7 +63,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 @@ -511,3 +510,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
Loading