Skip to content

Commit

Permalink
fix(controller): simplify exec of kargo edit set image command (akuit…
Browse files Browse the repository at this point in the history
…y#1481)

Signed-off-by: Kent Rancourt <kent.rancourt@gmail.com>
  • Loading branch information
krancour authored Feb 16, 2024
1 parent 50d238b commit 09a6f6c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
4 changes: 2 additions & 2 deletions internal/controller/promotion/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func selectKustomizeUpdates(updates []kargoapi.GitRepoUpdate) []kargoapi.GitRepo
// kustomizer is a helper struct whose sole purpose is to close over several
// other functions that are used in the implementation of the apply() function.
type kustomizer struct {
setImageFn func(dir, image, fqImageRef string) error
setImageFn func(dir, fqImageRef string) error
}

// apply uses Kustomize to carry out the provided update in the specified
Expand Down Expand Up @@ -70,7 +70,7 @@ func (k *kustomizer) apply(
continue
}
dir := filepath.Join(workingDir, imgUpdate.Path)
if err := k.setImageFn(dir, imgUpdate.Image, fqImageRef); err != nil {
if err := k.setImageFn(dir, fqImageRef); err != nil {
return nil, errors.Wrapf(
err,
"error updating image %q to %q using Kustomize",
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/promotion/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestKustomizerApply(t *testing.T) {
},
},
kustomizer: &kustomizer{
setImageFn: func(string, string, string) error {
setImageFn: func(string, string) error {
return errors.New("something went wrong")
},
},
Expand All @@ -108,7 +108,7 @@ func TestKustomizerApply(t *testing.T) {
},
},
kustomizer: &kustomizer{
setImageFn: func(string, string, string) error {
setImageFn: func(string, string) error {
return nil
},
},
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestKustomizerApply(t *testing.T) {
},
},
kustomizer: &kustomizer{
setImageFn: func(string, string, string) error {
setImageFn: func(string, string) error {
return nil
},
},
Expand Down
9 changes: 4 additions & 5 deletions internal/kustomize/kustomize.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kustomize

import (
"fmt"
"os/exec"

libExec "github.com/akuity/kargo/internal/exec"
Expand All @@ -10,18 +9,18 @@ import (
// SetImage runs `kustomize edit set image ...` in the specified directory.
// The specified directory must already exist and contain a kustomization.yaml
// file.
func SetImage(dir, repo, fqImageRef string) error {
_, err := libExec.Exec(buildSetImageCmd(dir, repo, fqImageRef))
func SetImage(dir, fqImageRef string) error {
_, err := libExec.Exec(buildSetImageCmd(dir, fqImageRef))
return err
}

func buildSetImageCmd(dir, repo, fqImageRef string) *exec.Cmd {
func buildSetImageCmd(dir, fqImageRef string) *exec.Cmd {
cmd := exec.Command( // nolint: gosec
"kustomize",
"edit",
"set",
"image",
fmt.Sprintf("%s=%s", repo, fqImageRef),
fqImageRef,
)
cmd.Dir = dir
return cmd
Expand Down
6 changes: 2 additions & 4 deletions internal/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kustomize

import (
"fmt"
"strings"
"testing"

Expand All @@ -10,9 +9,8 @@ import (

func TestBuildSetImageCmd(t *testing.T) {
const testDir = "/some-dir"
const testImage = "some-image"
const testImageRef = "some-image:some-tag"
cmd := buildSetImageCmd(testDir, testImage, testImageRef)
cmd := buildSetImageCmd(testDir, testImageRef)
require.NotNil(t, cmd)
require.True(t, strings.HasSuffix(cmd.Path, "kustomize"))
require.Equal(
Expand All @@ -22,7 +20,7 @@ func TestBuildSetImageCmd(t *testing.T) {
"edit",
"set",
"image",
fmt.Sprintf("%s=%s", testImage, testImageRef),
testImageRef,
},
cmd.Args,
)
Expand Down

0 comments on commit 09a6f6c

Please sign in to comment.