-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
56 lines (48 loc) · 1.58 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package k8s
import (
"errors"
"log/slog"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
type Config struct {
QPS float32
Burst int
}
// NewClient creates a new Kubernetes client and automatically detects in-cluster and out-of-cluster config.
func NewClient(kubeconfig *string, config Config) (kubernetes.Interface, error) {
restConfig, err := loadRESTConfig(kubeconfig)
if err != nil {
return nil, err
}
restConfig.QPS = config.QPS
restConfig.Burst = config.Burst
clientset, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, err
}
return clientset, nil
}
// NewDynamicClient creates a new dynamic Kubernetes client and automatically detects in-cluster and out-of-cluster config.
func NewDynamicClient(kubeconfig *string, config Config) (dynamic.Interface, error) {
restConfig, err := loadRESTConfig(kubeconfig)
if err != nil {
return nil, err
}
restConfig.QPS = config.QPS
restConfig.Burst = config.Burst
return dynamic.NewForConfig(restConfig)
}
// loadRESTConfig first tries to create an in-cluster config, and if it errors with ErrNotInCluster,
// it tries to create an out-of-cluster config based on provided kubeconfig path.
func loadRESTConfig(kubeconfig *string) (config *rest.Config, err error) {
config, err = rest.InClusterConfig()
if errors.Is(err, rest.ErrNotInCluster) {
slog.Info("creating k8s config for out of cluster client")
return clientcmd.BuildConfigFromFlags("", *kubeconfig)
}
slog.Info("running with in cluster client configuration")
return config, err
}