1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-10 01:46:55 +00:00
kyverno/pkg/config/client.go
Sandesh More 01b1ece704
added kubeconfig and context flag to kyverno apply (#4524)
Signed-off-by: Sandesh More <sandesh.more@infracloud.io>
2022-09-20 19:05:18 +05:30

44 lines
1.4 KiB
Go

package config
import (
"fmt"
"math"
"k8s.io/cli-runtime/pkg/genericclioptions"
rest "k8s.io/client-go/rest"
clientcmd "k8s.io/client-go/tools/clientcmd"
)
// 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
}
if qps > math.MaxFloat32 {
return nil, fmt.Errorf("client rate limit QPS must not be higher than %e", math.MaxFloat32)
}
clientConfig.Burst = burst
clientConfig.QPS = float32(qps)
return clientConfig, nil
}
// createClientConfig creates client config
func createClientConfig(kubeconfig string) (*rest.Config, error) {
if kubeconfig == "" {
logger.Info("Using in-cluster configuration")
return rest.InClusterConfig()
}
logger.V(4).Info("Using specified kubeconfig", "kubeconfig", kubeconfig)
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
// 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()
}