Repository: knative/pkg
Pull request #2697 (2023-02-27 01:26:25)

bump golang version in downstream tests
Fixes the operator downstream test which had their go version bumped to go1.19

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

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/2697#" title="Author self-approved">dprotaso</a>*

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

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/pkg/blob/main/OWNERS)~~ [dprotaso]

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":[]} -->
dprotaso: /assign @houshengbo 
dprotaso: cc @pierDipi looks like your api docs generating tool doesn't work with go1.20  https://github.com/ahmetb/gen-crd-api-reference-docs

We'll want to bump go to 1.20 for the next release 
houshengbo: @dprotaso Do we target to upgrade the go version to 1.20? Or just 1.19 will be enough?
dprotaso: > Do we target to upgrade the go version to 1.20? Or just 1.19 will be enough?

We should use go1.20 in our toolchain. Whether or not your repo (operator) requires go1.19 or go1.20 in your go.mod is up to you.

Patch:
diff --git a/.github/workflows/knative-downstream.yaml b/.github/workflows/knative-downstream.yaml
index 31e1ddc15..e6b144931 100644
--- a/.github/workflows/knative-downstream.yaml
+++ b/.github/workflows/knative-downstream.yaml
@@ -50,10 +50,10 @@ jobs:
     runs-on: ubuntu-latest
 
     steps:
-    - name: Set up Go 1.18.x
+    - name: Set up Go 1.20.x
       uses: actions/setup-go@v3
       with:
-        go-version: 1.18.x
+        go-version: 1.20.x
 
     - name: Install Dependencies
       run: |
diff --git a/webhook/env.go b/webhook/env.go
index cd038b40e..01d52c8b0 100644
--- a/webhook/env.go
+++ b/webhook/env.go
@@ -22,10 +22,14 @@ import (
 	"strconv"
 )
 
-const portEnvKey = "WEBHOOK_PORT"
+const (
+	portEnvKey = "WEBHOOK_PORT"
 
-// Webhook is the name of the override key used inside of the logging config for Webhook Controller.
-const webhookNameEnv = "WEBHOOK_NAME"
+	// Webhook is the name of the override key used inside of the logging config for Webhook Controller.
+	webhookNameEnvKey = "WEBHOOK_NAME"
+
+	secretNameEnvKey = "WEBHOOK_SECRET_NAME"
+)
 
 // PortFromEnv returns the webhook port set by portEnvKey, or default port if env var is not set.
 func PortFromEnv(defaultPort int) int {
@@ -42,7 +46,7 @@ func PortFromEnv(defaultPort int) int {
 }
 
 func NameFromEnv() string {
-	if webhook := os.Getenv(webhookNameEnv); webhook != "" {
+	if webhook := os.Getenv(webhookNameEnvKey); webhook != "" {
 		return webhook
 	}
 
@@ -52,5 +56,13 @@ If this is a process running on Kubernetes, then initialize this variable via:
   env:
   - name: WEBHOOK_NAME
     value: webhook
-`, webhookNameEnv))
+`, webhookNameEnvKey))
+}
+
+func SecretNameFromEnv(defaultSecretName string) string {
+	secret := os.Getenv(secretNameEnvKey)
+	if secret == "" {
+		return defaultSecretName
+	}
+	return secret
 }


Test patch:
diff --git a/webhook/env_test.go b/webhook/env_test.go
index 3f3c705ba..df5a0db9e 100644
--- a/webhook/env_test.go
+++ b/webhook/env_test.go
@@ -21,8 +21,10 @@ import (
 )
 
 const (
-	testDefaultPort      = 8888
-	testMissingInputName = "MissingInput" // portEnvKey is not found.
+	testMissingInputName = "MissingInput"
+
+	testDefaultPort       = 8888
+	testDefaultSecretName = "webhook-certs"
 )
 
 type portTest struct {
@@ -39,6 +41,13 @@ type webhookNameTest struct {
 	wantPanic bool
 }
 
+type secretNameTest struct {
+	name      string
+	in        string
+	want      string
+	wantPanic bool
+}
+
 func TestPort(t *testing.T) {
 	tests := []portTest{{
 		name: testMissingInputName,
@@ -102,7 +111,7 @@ func TestWebhookName(t *testing.T) {
 		t.Run(tc.name, func(t *testing.T) {
 			// webhookNameEnv is unset when testing missing input.
 			if tc.name != testMissingInputName {
-				t.Setenv(webhookNameEnv, tc.in)
+				t.Setenv(webhookNameEnvKey, tc.in)
 			}
 
 			defer func() {
@@ -119,3 +128,39 @@ func TestWebhookName(t *testing.T) {
 		})
 	}
 }
+
+func TestSecretName(t *testing.T) {
+	tests := []secretNameTest{{
+		name: testMissingInputName,
+		want: testDefaultSecretName,
+	}, {
+		name: "EmptyInput",
+		in:   "",
+		want: testDefaultSecretName,
+	}, {
+		name: "ValidInput",
+		in:   "my-webhook-certs",
+		want: "my-webhook-certs",
+	}}
+
+	for _, tc := range tests {
+		t.Run(tc.name, func(t *testing.T) {
+			// secretNameEnvKey is unset when testing missing input.
+			if tc.name != testMissingInputName {
+				t.Setenv(secretNameEnvKey, tc.in)
+			}
+
+			defer func() {
+				if r := recover(); r == nil && tc.wantPanic {
+					t.Error("Did not panic")
+				} else if r != nil && !tc.wantPanic {
+					t.Error("Got unexpected panic")
+				}
+			}()
+
+			if got := SecretNameFromEnv(testDefaultSecretName); got != tc.want {
+				t.Errorf("SecretNameFromEnv = %s, want: %s", got, tc.want)
+			}
+		})
+	}
+}