-
Notifications
You must be signed in to change notification settings - Fork 250
/
client.go
42 lines (36 loc) · 1.25 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
package util
import (
"fmt"
"os"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func LoadClusterConfig() (*rest.Config, error) {
if env := os.Getenv(clientcmd.RecommendedConfigPathEnvVar); env != "" {
credentials, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
if err != nil {
return nil, fmt.Errorf("could not load credentials from config: %w", err)
}
clusterConfig, err := clientcmd.NewDefaultClientConfig(*credentials, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, fmt.Errorf("could not load client configuration: %w", err)
}
return clusterConfig, nil
}
// otherwise, prefer in-cluster config
return rest.InClusterConfig()
}
// LoadKubeConfig loads a kubeconfig from the file and uses the default context
func LoadKubeConfig(path string) (*rest.Config, error) {
loader := clientcmd.NewDefaultClientConfigLoadingRules()
loader.ExplicitPath = path
cfg, err := loader.Load()
if err != nil {
return nil, fmt.Errorf("could not load kubeconfig: %w", err)
}
clusterConfig, err := clientcmd.NewDefaultClientConfig(*cfg, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, fmt.Errorf("could not load client configuration: %w", err)
}
return clusterConfig, nil
}