Skip to content

Commit

Permalink
suppress warnings about k8s env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Jul 6, 2023
1 parent 6772a17 commit 5a82e21
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/tools/unknown_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,32 @@ func checkEnvironmentConfigKeys(defaults map[string]interface{}) {
if !strings.HasPrefix(envKey, envPrefix) {
continue
}
// Kubernetes automatically adds some variables which are not used by Centrifugo
// itself. We skip warnings about them.
if isKubernetesEnvVar(envKey) {
continue
}
if !isKnownEnvKey(defaults, envPrefix, envKey) {
log.Warn().Str("key", envKey).Msg("unknown key found in the environment")
}
}
}
}

var k8sPrefixes = []string{
"CENTRIFUGO_PORT_",
"CENTRIFUGO_SERVICE_",
}

func isKubernetesEnvVar(envKey string) bool {
for _, k8sPrefix := range k8sPrefixes {
if strings.HasPrefix(envKey, k8sPrefix) {
return true
}
}
return false
}

func isKnownEnvKey(defaults map[string]interface{}, envPrefix string, envKey string) bool {
for defKey := range defaults {
defKeyEnvFormat := envPrefix + strings.ToUpper(strings.ReplaceAll(defKey, ".", "_"))
Expand Down
23 changes: 23 additions & 0 deletions internal/tools/unknown_keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tools

import (
"strconv"
"testing"
)

func Test_isKubernetesEnvVar(t *testing.T) {
tests := []struct {
envKey string
want bool
}{
{"CENTRIFUGO_SERVICE_PORT_GRPC", true},
{"CENTRIFUGO_PORT_8000_TCP", true},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if got := isKubernetesEnvVar(tt.envKey); got != tt.want {
t.Errorf("isKubernetesEnvVar() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 5a82e21

Please sign in to comment.