Add helper function to read webhook port number from env var (#1162)

* Add helper function to read port number from env var

* Add check for port == 0

* Add the unit test

* Update error message

Co-Authored-By: savitaashture <sashture@redhat.com>

* Panic when env var is set but invalid

* Rename the func to be PortFromEnv.

* Apply suggestions from code review on error message

Co-authored-by: Victor Agababov <vagababov@gmail.com>

* Join defers

Co-authored-by: savitaashture <sashture@redhat.com>
Co-authored-by: Victor Agababov <vagababov@gmail.com>
This commit is contained in:
Jihui Nie 2020-05-16 18:43:02 -07:00 committed by GitHub
parent 148827986b
commit 3c29a1ce89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 122 additions and 0 deletions

39
webhook/env.go Normal file
View File

@ -0,0 +1,39 @@
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package webhook
import (
"fmt"
"os"
"strconv"
)
const portEnvKey = "WEBHOOK_PORT"
// PortFromEnv returns the webhook port set by portEnvKey, or default port if env var is not set.
func PortFromEnv(defaultPort int) int {
if os.Getenv(portEnvKey) == "" {
return defaultPort
}
port, err := strconv.Atoi(os.Getenv(portEnvKey))
if err != nil {
panic(fmt.Sprintf("failed to convert the environment variable %q : %v", portEnvKey, err))
} else if port == 0 {
panic(fmt.Sprintf("the environment variable %q can't be zero", portEnvKey))
}
return port
}

83
webhook/env_test.go Normal file
View File

@ -0,0 +1,83 @@
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package webhook
import (
"os"
"testing"
)
const (
testDefaultPort = 8888
testMissingInputName = "MissingInput" // portEnvKey is not found.
)
type portTest struct {
name string
in string
want int
wantPanic bool
}
func TestPort(t *testing.T) {
tests := []portTest{{
name: testMissingInputName,
want: testDefaultPort,
}, {
name: "EmptyInput",
in: "",
want: testDefaultPort,
}, {
name: "InvalidInputNonNumeric",
in: "invalid",
wantPanic: true,
}, {
name: "InvalidInputTrailingSpace",
in: "8443 ",
wantPanic: true,
}, {
name: "InvalidInputZero",
in: "0",
wantPanic: true,
}, {
name: "ValidInput",
in: "443",
want: 443,
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// portEnvKey is unset when testing missing input.
if tc.name != testMissingInputName {
os.Setenv(portEnvKey, 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")
}
os.Unsetenv(portEnvKey)
}()
if got := PortFromEnv(testDefaultPort); got != tc.want {
t.Errorf("PortFromEnv = %d, want: %d", got, tc.want)
}
})
}
}