github.phpd.cn/cilium/cilium@v1.6.12/pkg/k8s/config.go (about) 1 // Copyright 2017 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package k8s abstracts all Kubernetes specific behaviour 16 package k8s 17 18 import ( 19 "os" 20 "strings" 21 ) 22 23 var ( 24 // config is the configuration of Kubernetes related settings 25 config configuration 26 ) 27 28 type configuration struct { 29 // APIServer is the address of the API server 30 APIServer string 31 32 // KubeconfigPath is the local path to the kubeconfig configuration 33 // file on the filesystem 34 KubeconfigPath string 35 36 // QPS is the QPS to pass to the kubernetes client configuration. 37 QPS float32 38 39 // Burst is the burst to pass to the kubernetes client configuration. 40 Burst int 41 } 42 43 // GetAPIServer returns the configured API server address 44 func GetAPIServer() string { 45 return config.APIServer 46 } 47 48 // GetKubeconfigPath returns the configured path to the kubeconfig 49 // configuration file 50 func GetKubeconfigPath() string { 51 return config.KubeconfigPath 52 } 53 54 // GetQPS gets the QPS of the K8s configuration. 55 func GetQPS() float32 { 56 return config.QPS 57 } 58 59 // GetBurst gets the burst limit of the K8s configuration. 60 func GetBurst() int { 61 return config.Burst 62 } 63 64 // Configure sets the parameters of the Kubernetes package 65 func Configure(apiServer, kubeconfigPath string, qps float32, burst int) { 66 config.APIServer = apiServer 67 config.KubeconfigPath = kubeconfigPath 68 config.QPS = qps 69 config.Burst = burst 70 71 if IsEnabled() && 72 config.APIServer != "" && 73 !strings.HasPrefix(apiServer, "http") { 74 config.APIServer = "http://" + apiServer 75 } 76 } 77 78 // IsEnabled checks if Cilium is being used in tandem with Kubernetes. 79 func IsEnabled() bool { 80 return config.APIServer != "" || 81 config.KubeconfigPath != "" || 82 (os.Getenv("KUBERNETES_SERVICE_HOST") != "" && 83 os.Getenv("KUBERNETES_SERVICE_PORT") != "") || 84 os.Getenv("K8S_NODE_NAME") != "" 85 }