Skip to content

Commit

Permalink
Merge pull request #151 from jsturtevant/fix-qps-burst
Browse files Browse the repository at this point in the history
Fix logic for parsing values
  • Loading branch information
k8s-ci-robot authored Sep 30, 2024
2 parents 2173faf + a9eb562 commit 981824b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
8 changes: 4 additions & 4 deletions admission-webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,21 @@ func createKubeClient() (*kubeClient, error) {

func env_float(key string, defaultFloat float32) float32 {
if v, found := os.LookupEnv(key); found {
if i, err := strconv.ParseFloat(v, 32); err != nil {
if i, err := strconv.ParseFloat(v, 32); err == nil {
return float32(i)
}
logrus.Warningf("unable to parse environment variable %s; using default value %f", key, defaultFloat)
logrus.Warningf("unable to parse environment variable %s with value %s; using default value %f", key, v, defaultFloat)
}

return defaultFloat
}

func env_int(key string, defaultInt int) int {
if v, found := os.LookupEnv(key); found {
if i, err := strconv.Atoi(v); err != nil {
if i, err := strconv.Atoi(v); err == nil {
return i
}
logrus.Warningf("unable to parse environment variable %s; using default value %d", key, defaultInt)
logrus.Warningf("unable to parse environment variable %s with value %s; using default value %d", key, v, defaultInt)
}

return defaultInt
Expand Down
88 changes: 88 additions & 0 deletions admission-webhook/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"os"
"testing"
)

func Test_env_float(t *testing.T) {
defaultFloat := float32(5.0)
tests := []struct {
name string
envkey string
envval string
want float32
}{
{
name: "Environment variable set to valid float",
envkey: "TEST_ENV_FLOAT",
envval: "3.14",
want: 3.14,
},
{
name: "Environment variable set to invalid float",
envkey: "TEST_ENV_FLOAT",
envval: "invalid",
want: float32(defaultFloat),
},
{
name: "Environment variable not set",
envkey: "TEST_ENV_FLOAT",
envval: "",
want: float32(defaultFloat),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envval != "" {
os.Setenv(tt.envkey, tt.envval)
} else {
os.Unsetenv(tt.envkey)
}
if got := env_float(tt.envkey, defaultFloat); got != tt.want {
t.Errorf("env_float() = %v, want %v", got, tt.want)
}
})
}
}

func Test_env_int(t *testing.T) {
defaultInt := 5
tests := []struct {
name string
envkey string
envval string
want int
}{
{
name: "Environment variable set to valid int",
envkey: "TEST_ENV_INT",
envval: "10",
want: 10,
},
{
name: "Environment variable set to invalid int",
envkey: "TEST_ENV_INT",
envval: "invalid",
want: defaultInt,
},
{
name: "Environment variable not set",
envkey: "TEST_ENV_INT",
envval: "",
want: defaultInt,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envval != "" {
os.Setenv(tt.envkey, tt.envval)
} else {
os.Unsetenv(tt.envkey)
}
if got := env_int(tt.envkey, defaultInt); got != tt.want {
t.Errorf("env_int() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion charts/gmsa/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ podSecurityContext: {}
replicaCount: 2
securityContext: {}
tolerations: []
qps: 30
qps: 30.0
burst: 50

0 comments on commit 981824b

Please sign in to comment.