Skip to content

Commit

Permalink
fix: check for --image flag in build command
Browse files Browse the repository at this point in the history
If the user provides an `--image` flag on the command line, it should be
used. This commit modifies the image resolving code to check whether or
not the image name was provided on the command line.

Fixes: knative#1125

Signed-off-by: Lance Ball <lball@redhat.com>
  • Loading branch information
lance committed Aug 30, 2022
1 parent 6a0b4a2 commit c6dc817
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func runBuild(cmd *cobra.Command, _ []string, newClient ClientFactory) (err erro
}

// If the function does not yet have an image name and one was not provided on the command line
if function.Image == "" {
if function.Image == "" && config.Image == "" {
// AND a --registry was not provided, then we need to
// prompt for a registry from which we can derive an image name.
if config.Registry == "" {
Expand All @@ -171,6 +171,7 @@ func runBuild(cmd *cobra.Command, _ []string, newClient ClientFactory) (err erro

// We have the registry, so let's use it to derive the function image name
config.Image = deriveImage(config.Image, config.Registry, config.Path)

function.Image = config.Image
}

Expand Down
42 changes: 42 additions & 0 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,48 @@ import (
. "knative.dev/kn-plugin-func/testing"
)

// TestBuild_ImageFlag ensures that the image flag is used when specified.
func TestBuild_ImageFlag(t *testing.T) {
var (
args = []string{"--image", "docker.io/tigerteam/foo"}
builder = mock.NewBuilder()
)

root, cleanup := Mktemp(t)
defer cleanup()

// Write a func.yaml config which does not specify an image
funcYaml := `name: foo
runtime: go
builder: pack
created: 2022-01-01T00:00:00+00:00
`
if err := ioutil.WriteFile("func.yaml", []byte(funcYaml), 0600); err != nil {
t.Fatal(err)
}

// Create build command that will use a mock builder.
cmd := NewBuildCmd(NewClientFactory(func() *fn.Client {
return fn.New(fn.WithBuilder(builder))
}))

// Execute the command
cmd.SetArgs(args)
err := cmd.Execute()
if err != nil {
t.Fatal("Expected error")
}

// Now load the function and ensure that the image is set correctly.
f, err := fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}
if f.Image != "docker.io/tigerteam/foo" {
t.Fatalf("Expected image to be 'docker.io/tigerteam/foo', but got '%v'", f.Image)
}
}

// TestBuild_InvalidRegistry ensures that running build specifying the name of the
// registry explicitly as an argument invokes the registry validation code.
func TestBuild_InvalidRegistry(t *testing.T) {
Expand Down

0 comments on commit c6dc817

Please sign in to comment.