Repository: knative-sandbox/kn-plugin-func
Pull request #1168 (2022-08-15 21:13:01)

fix: ensure registry in func.yaml is respected
# Changes

:bug: If the user has a value specified in func.yaml for the image registry, that value should be used by the build command, if not otherwise specified as a flag or via the environment.

Fixes: https://github.com/knative-sandbox/kn-plugin-func/issues/1159

Signed-off-by: Lance Ball <lball@redhat.com>

/kind bug

**Release Note**

```release-note
Fixes function builds when a registry exists in func.yaml but an image has not yet been built
```



knative-prow[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative-sandbox/kn-plugin-func/pull/1168#" title="Author self-approved">lance</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative-sandbox%2Fkn-plugin-func).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/knative-sandbox/kn-plugin-func/blob/main/OWNERS)~~ [lance]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
codecov[bot]: # [Codecov](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/1168?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) Report
> Merging [#1168](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/1168?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) (bde796a) into [main](https://codecov.io/gh/knative-sandbox/kn-plugin-func/commit/133e4dc3a926105701e8fc009fdf18ca0883dde0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) (133e4dc) will **increase** coverage by `0.03%`.
> The diff coverage is `70.00%`.

```diff
@@            Coverage Diff             @@
##             main    #1168      +/-   ##
==========================================
+ Coverage   46.63%   46.66%   +0.03%     
==========================================
  Files          67       67              
  Lines        8843     8848       +5     
==========================================
+ Hits         4124     4129       +5     
  Misses       4324     4324              
  Partials      395      395              
```


| [Impacted Files](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/1168?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) | Coverage Δ | |
|---|---|---|
| [cmd/build.go](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/1168/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox#diff-Y21kL2J1aWxkLmdv) | `65.77% <70.00%> (+0.94%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox)

knative-prow[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative-sandbox/kn-plugin-func/pull/1168#" title="Author self-approved">lance</a>*, *<a href="https://github.com/knative-sandbox/kn-plugin-func/pull/1168#pullrequestreview-1073358112" title="LGTM">lkingland</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative-sandbox%2Fkn-plugin-func).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/knative-sandbox/kn-plugin-func/blob/main/OWNERS)~~ [lance,lkingland]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/cmd/build.go b/cmd/build.go
index fc7cd2ae..6fb971b7 100644
--- a/cmd/build.go
+++ b/cmd/build.go
@@ -23,8 +23,8 @@ func NewBuildCmd(newClient ClientFactory) *cobra.Command {
 
 This command builds the function project in the current directory or in the directory
 specified by --path. The result will be a container image that is pushed to a registry.
-The func.yaml file is read to determine the image name and registry. 
-If the project has not already been built, either --registry or --image must be provided 
+The func.yaml file is read to determine the image name and registry.
+If the project has not already been built, either --registry or --image must be provided
 and the image name is stored in the configuration file.
 `,
 		Example: `
@@ -117,6 +117,14 @@ func runBuild(cmd *cobra.Command, _ []string, newClient ClientFactory) (err erro
 		if err != nil {
 			return
 		}
+	} else if function.Registry != "" {
+		// Otherwise, if a registry name was provided in func.config, set that value
+		// for the command configuration and validate it
+		config.Registry = function.Registry
+		err = ValidNamespaceAndRegistry(config.Path)(config.Registry)
+		if err != nil {
+			return
+		}
 	}
 
 	// If the function does not yet have an image name and one was not provided on the command line
@@ -169,13 +177,6 @@ func runBuild(cmd *cobra.Command, _ []string, newClient ClientFactory) (err erro
 		return
 	}
 
-	// if registry was not changed via command line flag meaning it's empty
-	// keep the same registry by setting the config.registry to empty otherwise
-	// trust viper to override the env variable with the given flag if both are specified
-	if regFlag, _ := cmd.Flags().GetString("registry"); regFlag == "" {
-		config.Registry = ""
-	}
-
 	// Use the user-provided builder image, if supplied
 	if config.BuilderImage != "" {
 		function.BuilderImages[config.Builder] = config.BuilderImage


Test patch:
diff --git a/cmd/build_test.go b/cmd/build_test.go
index 57f34b6e..34025467 100644
--- a/cmd/build_test.go
+++ b/cmd/build_test.go
@@ -54,6 +54,40 @@ created: 2021-01-01T00:00:00+00:00
 	}
 }
 
+// TestBuild_registryConfigurationInYaml tests that a build will execute successfully
+// when there is no registry provided on the command line, but one exists in func.yaml
+func TestBuild_registryConfigurationInYaml(t *testing.T) {
+	var (
+		builder = mock.NewBuilder() // with a mock builder
+	)
+
+	// Run this test in a temporary directory
+	defer Fromtemp(t)()
+	// Write a func.yaml config which does not specify an image
+	// but does specify a registry
+	funcYaml := `name: registrytest
+namespace: ""
+runtime: go
+image: ""
+registry: quay.io/boson/foo
+created: 2021-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), fn.WithRegistry("quay.io/boson/foo"))
+	}))
+
+	// Execute the command
+	err := cmd.Execute()
+	if err != nil {
+		t.Fatal(err)
+	}
+}
+
 func TestBuild_runBuild(t *testing.T) {
 	tests := []struct {
 		name         string