-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
43 lines (35 loc) · 916 Bytes
/
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
package configmap
import (
"fmt"
"io/ioutil"
"sync"
"github.com/ericchiang/k8s"
"github.com/ghodss/yaml"
)
type KubernetesClient struct {
sync.RWMutex
client k8s.Client
defaultNamespace string
}
func newInClusterClient() (*k8s.Client, error) {
return k8s.NewInClusterClient()
}
func newOutOfClusterClient(kubeconfigPath string) (*k8s.Client, error) {
data, err := ioutil.ReadFile(kubeconfigPath)
if err != nil {
return nil, fmt.Errorf("read kubeconfig: %v", err)
}
// Unmarshal YAML into a Kubernetes config object.
var config k8s.Config
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("unmarshal kubeconfig: %v", err)
}
return k8s.NewClient(&config)
}
func NewKubernetesClient(inCluster bool, kubeconfigPath string) (*k8s.Client, error) {
if inCluster {
return newInClusterClient()
} else {
return newOutOfClusterClient(kubeconfigPath)
}
}