fix: kustomize edit add component check (#24100)

Signed-off-by: Blake Pettersson <blake.pettersson@gmail.com>
This commit is contained in:
Blake Pettersson
2025-08-11 11:04:17 +02:00
committed by GitHub
parent 04d1ca4733
commit 45a7a18256
2 changed files with 32 additions and 9 deletions

View File

@@ -365,15 +365,18 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
foundComponents = append(foundComponents, c)
}
}
args := []string{"edit", "add", "component"}
args = append(args, foundComponents...)
cmd := exec.Command(k.getBinaryPath(), args...)
cmd.Dir = k.path
cmd.Env = env
commands = append(commands, executil.GetCommandArgsToLog(cmd))
_, err := executil.Run(cmd)
if err != nil {
return nil, nil, nil, err
if len(foundComponents) > 0 {
args := []string{"edit", "add", "component"}
args = append(args, foundComponents...)
cmd := exec.Command(k.getBinaryPath(), args...)
cmd.Dir = k.path
cmd.Env = env
commands = append(commands, executil.GetCommandArgsToLog(cmd))
_, err := executil.Run(cmd)
if err != nil {
return nil, nil, nil, err
}
}
}
}

View File

@@ -615,6 +615,26 @@ func TestFailKustomizeBuildPatches(t *testing.T) {
require.EqualError(t, err, "kustomization file not found in the path")
}
func TestKustomizeBuildComponentsNoFoundComponents(t *testing.T) {
appPath, err := testDataDir(t, kustomization6)
require.NoError(t, err)
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
// Test with non-existent components and IgnoreMissingComponents = true
// This should result in foundComponents being empty, so no "edit add component" command should be executed
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
Components: []string{"./non-existent-component1", "./non-existent-component2"},
IgnoreMissingComponents: true,
}
_, _, commands, err := kustomize.Build(&kustomizeSource, nil, nil, nil)
require.NoError(t, err)
// Verify that no "edit add component" command was executed
for _, cmd := range commands {
assert.NotContains(t, cmd, "edit add component", "kustomize edit add component should not be invoked when foundComponents is empty")
}
}
func Test_getImageParameters_sorted(t *testing.T) {
apps := []*unstructured.Unstructured{
{