-
Notifications
You must be signed in to change notification settings - Fork 53
/
cluster.go
44 lines (41 loc) · 1016 Bytes
/
cluster.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
package util
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type ClientOptions struct {
Kubeconfig *string
RestConfig *rest.Config
Scheme *runtime.Scheme
}
func NewK8sClient(options ClientOptions) (client.Client, error) {
crOpts := client.Options{
Scheme: options.Scheme,
}
var restConfig *rest.Config
switch {
case options.Kubeconfig != nil:
rules := clientcmd.NewDefaultClientConfigLoadingRules()
rules.ExplicitPath = *options.Kubeconfig
apiConfig, err := rules.Load()
if err != nil {
return nil, err
}
restConfig, err = clientcmd.NewDefaultClientConfig(
*apiConfig, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, err
}
case options.RestConfig != nil:
restConfig = options.RestConfig
default:
var err error
restConfig, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
}
return client.New(restConfig, crOpts)
}