knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/environment/client_config.go (about) 1 /* 2 Copyright 2021 The Knative Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package environment 18 19 import ( 20 "flag" 21 "fmt" 22 "log" 23 "math" 24 "os" 25 "strconv" 26 27 "k8s.io/client-go/rest" 28 "k8s.io/client-go/tools/clientcmd" 29 clientcmdapi "k8s.io/client-go/tools/clientcmd/api" 30 ) 31 32 // ClientConfig holds the information about the environment and can be configured with flags 33 type ClientConfig struct { 34 Cluster string // K8s cluster (defaults to cluster in kubeconfig) 35 ServerURL string // ServerURL - The address of the Kubernetes API server. Overrides any value in kubeconfig. 36 Burst int // Burst - Maximum burst for throttle. 37 QPS float64 // QPS - Maximum QPS to the server from the client. 38 Kubeconfig string // Kubeconfig - Path to a kubeconfig. Current casing is present for backwards compatibility 39 } 40 41 func (c *ClientConfig) InitFlags(fs *flag.FlagSet) { 42 fs.StringVar(&c.Cluster, "cluster", "", "Defaults to the current cluster in kubeconfig.") 43 44 fs.StringVar(&c.ServerURL, "server", "", 45 "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") 46 47 fs.StringVar(&c.Kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"), 48 "Path to a kubeconfig. Only required if out-of-cluster.") 49 50 fs.IntVar(&c.Burst, "kube-api-burst", int(envVarOrDefault("KUBE_API_BURST", 0)), "Maximum burst for throttle.") 51 52 fs.Float64Var(&c.QPS, "kube-api-qps", envVarOrDefault("KUBE_API_QPS", 0.0), "Maximum QPS to the server from the client.") 53 } 54 55 func envVarOrDefault(key string, val float64) float64 { 56 var err error 57 if v := os.Getenv(key); v != "" { 58 if val, err = strconv.ParseFloat(v, 64); err != nil { 59 log.Fatal(err) 60 } 61 } 62 return val 63 } 64 65 func (c *ClientConfig) GetRESTConfig() (*rest.Config, error) { 66 if c.Burst < 0 { 67 return nil, fmt.Errorf("provided burst value %d must be > 0", c.Burst) 68 } 69 if c.QPS < 0 || c.QPS > math.MaxFloat32 { 70 return nil, fmt.Errorf("provided QPS value %f must be >0 and <3.4+e38", c.QPS) 71 } 72 73 loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() 74 overrides := &clientcmd.ConfigOverrides{} 75 76 if c.Kubeconfig != "" { 77 loadingRules.ExplicitPath = c.Kubeconfig 78 } 79 if c.Cluster != "" { 80 overrides.Context = clientcmdapi.Context{Cluster: c.Cluster} 81 } else if c.ServerURL != "" { 82 overrides.ClusterInfo = clientcmdapi.Cluster{Server: c.ServerURL} 83 } 84 85 config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( 86 loadingRules, 87 overrides, 88 ).ClientConfig() 89 if err != nil { 90 return nil, fmt.Errorf("failed to create client config: %w", err) 91 } 92 93 config.QPS = float32(c.QPS) 94 config.Burst = c.Burst 95 96 return config, nil 97 }