forked from kubernetes-sigs/cluster-api
/
util.go
215 lines (181 loc) · 4.88 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"fmt"
"math/rand"
"os"
"os/exec"
"os/user"
"strings"
"time"
"github.com/golang/glog"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
clustercommon "sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset"
client "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1"
)
const (
CharSet = "0123456789abcdefghijklmnopqrstuvwxyz"
)
var (
r = rand.New(rand.NewSource(time.Now().UnixNano()))
)
func RandomToken() string {
return fmt.Sprintf("%s.%s", RandomString(6), RandomString(16))
}
func RandomString(n int) string {
result := make([]byte, n)
for i := range result {
result[i] = CharSet[r.Intn(len(CharSet))]
}
return string(result)
}
func GetMaster(machines []*clusterv1.Machine) *clusterv1.Machine {
for _, machine := range machines {
if IsMaster(machine) {
return machine
}
}
return nil
}
func MachineP(machines []clusterv1.Machine) []*clusterv1.Machine {
// Convert to list of pointers
var ret []*clusterv1.Machine
for _, machine := range machines {
ret = append(ret, machine.DeepCopy())
}
return ret
}
func Home() string {
home := os.Getenv("HOME")
if strings.Contains(home, "root") {
return "/root"
}
usr, err := user.Current()
if err != nil {
glog.Warningf("unable to find user: %v", err)
return ""
}
return usr.HomeDir
}
func GetDefaultKubeConfigPath() string {
localDir := fmt.Sprintf("%s/.kube", Home())
if _, err := os.Stat(localDir); os.IsNotExist(err) {
if err := os.Mkdir(localDir, 0777); err != nil {
glog.Fatal(err)
}
}
return fmt.Sprintf("%s/config", localDir)
}
func NewClientSet(configPath string) (*clientset.Clientset, error) {
config, err := clientcmd.BuildConfigFromFlags("", configPath)
if err != nil {
return nil, err
}
cs, err := clientset.NewForConfig(config)
if err != nil {
return nil, err
}
return cs, nil
}
func NewKubernetesClient(configPath string) (*kubernetes.Clientset, error) {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return nil, fmt.Errorf("kubectl config file %s doesn't exist. Is kubectl configured to access a cluster?", configPath)
}
config, err := clientcmd.BuildConfigFromFlags("", configPath)
if err != nil {
return nil, err
}
c, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return c, nil
}
func GetMachineIfExists(machineClient client.MachineInterface, name string) (*clusterv1.Machine, error) {
if machineClient == nil {
// Being called before k8s is setup as part of master VM creation
return nil, nil
}
// Machines are identified by name
machine, err := machineClient.Get(name, metav1.GetOptions{})
if err != nil {
// TODO: Use formal way to check for not found
if strings.Contains(err.Error(), "not found") {
return nil, nil
}
return nil, err
}
return machine, nil
}
func RoleContains(a clustercommon.MachineRole, list []clustercommon.MachineRole) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func IsMaster(machine *clusterv1.Machine) bool {
return RoleContains(clustercommon.MasterRole, machine.Spec.Roles)
}
func IsNodeReady(node *v1.Node) bool {
for _, condition := range node.Status.Conditions {
if condition.Type == v1.NodeReady {
return condition.Status == v1.ConditionTrue
}
}
return false
}
func Copy(m *clusterv1.Machine) *clusterv1.Machine {
ret := &clusterv1.Machine{}
ret.APIVersion = m.APIVersion
ret.Kind = m.Kind
ret.ClusterName = m.ClusterName
ret.GenerateName = m.GenerateName
ret.Name = m.Name
ret.Namespace = m.Namespace
m.Spec.DeepCopyInto(&ret.Spec)
return ret
}
func ExecCommand(name string, args ...string) string {
cmdOut, _ := exec.Command(name, args...).Output()
return string(cmdOut)
}
func Filter(list []string, strToFilter string) (newList []string) {
for _, item := range list {
if item != strToFilter {
newList = append(newList, item)
}
}
return
}
func Contains(list []string, strToSearch string) bool {
for _, item := range list {
if item == strToSearch {
return true
}
}
return false
}
func GetNamespaceOrDefault(namespace string) string {
if namespace == "" {
return v1.NamespaceDefault
}
return namespace
}