/
util.go
80 lines (69 loc) · 1.72 KB
/
util.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package controller
import (
"fmt"
"github.com/golang/glog"
"gopkg.in/yaml.v2"
"io/ioutil"
coreV1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func GetAliases(app string, config []Config) []coreV1.HostAlias {
for _, conf := range config {
glog.V(5).Infof("looking for %s using %s", app, conf.Label)
if conf.Label == app {
return conf.Aliases
}
}
return nil
}
func GetAliasesByKV(k, v string, config []Config) []coreV1.HostAlias {
for _, conf := range config {
glog.V(5).Infof("looking for %s, %s using %s", k, v, conf.Label)
if conf.App == k && conf.Label == v {
return conf.Aliases
}
}
return nil
}
func ConfigMapToConfig(cm *coreV1.ConfigMap) (*[]Config, error) {
var c []Config
err := yaml.Unmarshal([]byte(cm.Data["config"]), &c)
if err != nil {
return nil, err
}
glog.V(5).Infof("configs %+v", c)
return &c, err
}
func FileToConfig(filePath string) (*[]Config, error) {
var c []Config
data, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open %s: %v", filePath, err)
}
err = yaml.Unmarshal(data, &c)
if err != nil {
return nil, err
}
glog.V(5).Infof("configs %+v", c)
return &c, err
}
// Get a clientset with in-cluster config.
func GetClient(kubeMaster, kubeConfig string) *kubernetes.Clientset {
var clusterConfig *rest.Config
var err error
if len(kubeMaster) > 0 || len(kubeConfig) > 0 {
clusterConfig, err = clientcmd.BuildConfigFromFlags(kubeMaster, kubeConfig)
} else {
clusterConfig, err = rest.InClusterConfig()
}
if err != nil {
glog.Fatal(err.Error())
}
clientset, err := kubernetes.NewForConfig(clusterConfig)
if err != nil {
glog.Fatal(err)
}
return clientset
}