From 7c80033098c0be950b0392d2bb5e58a1a26e7df6 Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Thu, 15 Jun 2023 17:40:01 +0900 Subject: [PATCH 1/5] Support to set client config via env variable Currently some configuration such as `kube-api-burst` and `kube-api-qps` must be set via command line option. Knative deployments usually do not have the command line option, the command line option is unusual and so knative/operator also does not support the API. Hence This patch allows to set client config via env variable. --- environment/client_config.go | 10 ++++++++- environment/client_config_test.go | 35 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 environment/client_config_test.go diff --git a/environment/client_config.go b/environment/client_config.go index 04d4220b0a..e4d107d54e 100644 --- a/environment/client_config.go +++ b/environment/client_config.go @@ -21,6 +21,7 @@ import ( "fmt" "math" "os" + "strings" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -42,12 +43,19 @@ func (c *ClientConfig) InitFlags(fs *flag.FlagSet) { fs.StringVar(&c.ServerURL, "server", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") - fs.StringVar(&c.Kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"), + fs.StringVar(&c.Kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") fs.IntVar(&c.Burst, "kube-api-burst", 0, "Maximum burst for throttle.") fs.Float64Var(&c.QPS, "kube-api-qps", 0, "Maximum QPS to the server from the client.") + + // Apply flag values from env variable if set. + fs.VisitAll(func(f *flag.Flag) { + if s := os.Getenv(strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))); s != "" { + f.Value.Set(s) + } + }) } func (c *ClientConfig) GetRESTConfig() (*rest.Config, error) { diff --git a/environment/client_config_test.go b/environment/client_config_test.go new file mode 100644 index 0000000000..4d1c9a4837 --- /dev/null +++ b/environment/client_config_test.go @@ -0,0 +1,35 @@ +package environment + +import ( + "flag" + "strconv" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestInitFlag(t *testing.T) { + t.Setenv("KUBE_API_BURST", "50") + t.Setenv("KUBE_API_QPS", "60") + t.Setenv("KUBECONFIG", "myconfig") + + c := new(ClientConfig) + c.InitFlags(flag.CommandLine) + + // Override kube-api-burst via command line option. + flag.CommandLine.Set("kube-api-burst", strconv.Itoa(100)) + + // Call parse() here as InitFlags does not call it. + flag.Parse() + + expect := &ClientConfig{ + Burst: 100, + QPS: 60, + Kubeconfig: "myconfig", + Cluster: "", + } + + if !cmp.Equal(c, expect) { + t.Errorf("ClientConfig mismatch: diff(-want,+got):\n%s", cmp.Diff(expect, c)) + } +} From 334f5498fa41ad06c1a19a6a5b2c360f5eeb9203 Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Thu, 15 Jun 2023 19:01:47 +0900 Subject: [PATCH 2/5] only qps and burst --- environment/client_config.go | 20 ++++++++++---------- environment/client_config_test.go | 1 - 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/environment/client_config.go b/environment/client_config.go index e4d107d54e..f570c80bc9 100644 --- a/environment/client_config.go +++ b/environment/client_config.go @@ -21,7 +21,7 @@ import ( "fmt" "math" "os" - "strings" + "strconv" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -43,19 +43,19 @@ func (c *ClientConfig) InitFlags(fs *flag.FlagSet) { fs.StringVar(&c.ServerURL, "server", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") - fs.StringVar(&c.Kubeconfig, "kubeconfig", "", + fs.StringVar(&c.Kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"), "Path to a kubeconfig. Only required if out-of-cluster.") - fs.IntVar(&c.Burst, "kube-api-burst", 0, "Maximum burst for throttle.") + fs.IntVar(&c.Burst, "kube-api-burst", envVarOrDefault("KUBE_API_BURST", 0), "Maximum burst for throttle.") - fs.Float64Var(&c.QPS, "kube-api-qps", 0, "Maximum QPS to the server from the client.") + fs.Float64Var(&c.QPS, "kube-api-qps", float64(envVarOrDefault("KUBE_API_QPS", 0)), "Maximum QPS to the server from the client.") +} - // Apply flag values from env variable if set. - fs.VisitAll(func(f *flag.Flag) { - if s := os.Getenv(strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))); s != "" { - f.Value.Set(s) - } - }) +func envVarOrDefault(key string, val int) int { + if v := os.Getenv(key); v != "" { + val, _ = strconv.Atoi(v) + } + return val } func (c *ClientConfig) GetRESTConfig() (*rest.Config, error) { diff --git a/environment/client_config_test.go b/environment/client_config_test.go index 4d1c9a4837..8603a0cad6 100644 --- a/environment/client_config_test.go +++ b/environment/client_config_test.go @@ -26,7 +26,6 @@ func TestInitFlag(t *testing.T) { Burst: 100, QPS: 60, Kubeconfig: "myconfig", - Cluster: "", } if !cmp.Equal(c, expect) { From 57185265ff035d46b57cbfa8468ba58c5d7f697f Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Thu, 15 Jun 2023 19:07:52 +0900 Subject: [PATCH 3/5] Add copyright --- environment/client_config_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/environment/client_config_test.go b/environment/client_config_test.go index 8603a0cad6..d6745b7528 100644 --- a/environment/client_config_test.go +++ b/environment/client_config_test.go @@ -1,3 +1,19 @@ +/* +Copyright 2023 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 environment import ( From d19e2b7de89442883f57128d0919cbe718302411 Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Thu, 15 Jun 2023 23:29:57 +0900 Subject: [PATCH 4/5] Handle float --- environment/client_config.go | 8 ++++---- environment/client_config_test.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/environment/client_config.go b/environment/client_config.go index f570c80bc9..fbedffd549 100644 --- a/environment/client_config.go +++ b/environment/client_config.go @@ -46,14 +46,14 @@ func (c *ClientConfig) InitFlags(fs *flag.FlagSet) { fs.StringVar(&c.Kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"), "Path to a kubeconfig. Only required if out-of-cluster.") - fs.IntVar(&c.Burst, "kube-api-burst", envVarOrDefault("KUBE_API_BURST", 0), "Maximum burst for throttle.") + fs.IntVar(&c.Burst, "kube-api-burst", int(envVarOrDefault("KUBE_API_BURST", 0)), "Maximum burst for throttle.") - fs.Float64Var(&c.QPS, "kube-api-qps", float64(envVarOrDefault("KUBE_API_QPS", 0)), "Maximum QPS to the server from the client.") + fs.Float64Var(&c.QPS, "kube-api-qps", envVarOrDefault("KUBE_API_QPS", 0.0), "Maximum QPS to the server from the client.") } -func envVarOrDefault(key string, val int) int { +func envVarOrDefault(key string, val float64) float64 { if v := os.Getenv(key); v != "" { - val, _ = strconv.Atoi(v) + val, _ = strconv.ParseFloat(v, 64) } return val } diff --git a/environment/client_config_test.go b/environment/client_config_test.go index d6745b7528..b5f83b16f2 100644 --- a/environment/client_config_test.go +++ b/environment/client_config_test.go @@ -26,7 +26,7 @@ import ( func TestInitFlag(t *testing.T) { t.Setenv("KUBE_API_BURST", "50") - t.Setenv("KUBE_API_QPS", "60") + t.Setenv("KUBE_API_QPS", "60.1") t.Setenv("KUBECONFIG", "myconfig") c := new(ClientConfig) @@ -40,7 +40,7 @@ func TestInitFlag(t *testing.T) { expect := &ClientConfig{ Burst: 100, - QPS: 60, + QPS: 60.1, Kubeconfig: "myconfig", } From 158646413e8b9918506cf1da46891c5701fdab6c Mon Sep 17 00:00:00 2001 From: Kenjiro Nakayama Date: Fri, 16 Jun 2023 19:30:49 +0900 Subject: [PATCH 5/5] Add error handling --- environment/client_config.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/environment/client_config.go b/environment/client_config.go index fbedffd549..aef33927ef 100644 --- a/environment/client_config.go +++ b/environment/client_config.go @@ -19,6 +19,7 @@ package environment import ( "flag" "fmt" + "log" "math" "os" "strconv" @@ -52,8 +53,11 @@ func (c *ClientConfig) InitFlags(fs *flag.FlagSet) { } func envVarOrDefault(key string, val float64) float64 { + var err error if v := os.Getenv(key); v != "" { - val, _ = strconv.ParseFloat(v, 64) + if val, err = strconv.ParseFloat(v, 64); err != nil { + log.Fatal(err) + } } return val }