Add certs secret name read from env (#2685)

* Add certs secret name read from env

* Rename webhookNameEnv to webhookNameEnvKey

* Add test code for certs secret name from env

* Add missing input case to test CertsSecretNameFromEnv

* Change CertsSecretName to SecretName
This commit is contained in:
Hyeonki Hong 2023-03-07 00:41:24 +09:00 committed by GitHub
parent 645afb2d5d
commit fd00e037d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 8 deletions

View File

@ -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
}

View File

@ -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)
}
})
}
}