Skip to content

Commit 1c1c176

Browse files
fix: kustomize edit add component check (#24100) (cherry-pick 3.1) (#24102)
Signed-off-by: Blake Pettersson <[email protected]>
1 parent 61d2a05 commit 1c1c176

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

util/kustomize/kustomize.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,18 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
365365
foundComponents = append(foundComponents, c)
366366
}
367367
}
368-
args := []string{"edit", "add", "component"}
369-
args = append(args, foundComponents...)
370-
cmd := exec.Command(k.getBinaryPath(), args...)
371-
cmd.Dir = k.path
372-
cmd.Env = env
373-
commands = append(commands, executil.GetCommandArgsToLog(cmd))
374-
_, err := executil.Run(cmd)
375-
if err != nil {
376-
return nil, nil, nil, err
368+
369+
if len(foundComponents) > 0 {
370+
args := []string{"edit", "add", "component"}
371+
args = append(args, foundComponents...)
372+
cmd := exec.Command(k.getBinaryPath(), args...)
373+
cmd.Dir = k.path
374+
cmd.Env = env
375+
commands = append(commands, executil.GetCommandArgsToLog(cmd))
376+
_, err := executil.Run(cmd)
377+
if err != nil {
378+
return nil, nil, nil, err
379+
}
377380
}
378381
}
379382
}

util/kustomize/kustomize_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,26 @@ func TestFailKustomizeBuildPatches(t *testing.T) {
615615
require.EqualError(t, err, "kustomization file not found in the path")
616616
}
617617

618+
func TestKustomizeBuildComponentsNoFoundComponents(t *testing.T) {
619+
appPath, err := testDataDir(t, kustomization6)
620+
require.NoError(t, err)
621+
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
622+
623+
// Test with non-existent components and IgnoreMissingComponents = true
624+
// This should result in foundComponents being empty, so no "edit add component" command should be executed
625+
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
626+
Components: []string{"./non-existent-component1", "./non-existent-component2"},
627+
IgnoreMissingComponents: true,
628+
}
629+
_, _, commands, err := kustomize.Build(&kustomizeSource, nil, nil, nil)
630+
require.NoError(t, err)
631+
632+
// Verify that no "edit add component" command was executed
633+
for _, cmd := range commands {
634+
assert.NotContains(t, cmd, "edit add component", "kustomize edit add component should not be invoked when foundComponents is empty")
635+
}
636+
}
637+
618638
func Test_getImageParameters_sorted(t *testing.T) {
619639
apps := []*unstructured.Unstructured{
620640
{

0 commit comments

Comments
 (0)