2022-05-04 16:05:03 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
|
2022-09-20 13:35:18 +00:00
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
2022-05-04 16:05:03 +00:00
|
|
|
rest "k8s.io/client-go/rest"
|
2022-08-29 06:21:42 +00:00
|
|
|
clientcmd "k8s.io/client-go/tools/clientcmd"
|
2022-05-04 16:05:03 +00:00
|
|
|
)
|
|
|
|
|
2022-08-29 06:21:42 +00:00
|
|
|
// CreateClientConfig creates client config and applies rate limit QPS and burst
|
|
|
|
func CreateClientConfig(kubeconfig string, qps float64, burst int) (*rest.Config, error) {
|
|
|
|
clientConfig, err := createClientConfig(kubeconfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-04 16:05:03 +00:00
|
|
|
if qps > math.MaxFloat32 {
|
2022-08-29 06:21:42 +00:00
|
|
|
return nil, fmt.Errorf("client rate limit QPS must not be higher than %e", math.MaxFloat32)
|
2022-05-04 16:05:03 +00:00
|
|
|
}
|
|
|
|
clientConfig.Burst = burst
|
|
|
|
clientConfig.QPS = float32(qps)
|
2022-08-29 06:21:42 +00:00
|
|
|
return clientConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createClientConfig creates client config
|
|
|
|
func createClientConfig(kubeconfig string) (*rest.Config, error) {
|
|
|
|
if kubeconfig == "" {
|
|
|
|
return rest.InClusterConfig()
|
|
|
|
}
|
|
|
|
return clientcmd.BuildConfigFromFlags("", kubeconfig)
|
2022-05-04 16:05:03 +00:00
|
|
|
}
|
2022-09-20 13:35:18 +00:00
|
|
|
|
|
|
|
// CreateClientConfigWithContext creates client config from custom kubeconfig file and context
|
|
|
|
// Used for cli commands
|
|
|
|
func CreateClientConfigWithContext(kubeconfig string, context string) (*rest.Config, error) {
|
|
|
|
kubernetesConfig := genericclioptions.NewConfigFlags(true)
|
|
|
|
kubernetesConfig.KubeConfig = &kubeconfig
|
|
|
|
kubernetesConfig.Context = &context
|
|
|
|
return kubernetesConfig.ToRESTConfig()
|
|
|
|
}
|