This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
/
metadata.go
248 lines (206 loc) · 6.13 KB
/
metadata.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package k8sutil
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/golang/glog"
v1api "k8s.io/api/core/v1"
v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/watch"
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
)
const (
updateConfPath = "/usr/share/coreos/update.conf"
updateConfOverridePath = "/etc/coreos/update.conf"
osReleasePath = "/etc/os-release"
)
// NodeAnnotationCondition returns a condition function that succeeds when a
// node being watched has an annotation of key equal to value.
func NodeAnnotationCondition(selector fields.Selector) watch.ConditionFunc {
return func(event watch.Event) (bool, error) {
switch event.Type {
case watch.Modified:
node := event.Object.(*v1api.Node)
return selector.Matches(fields.Set(node.Annotations)), nil
}
return false, fmt.Errorf("unhandled watch case for %#v", event)
}
}
// GetNodeRetry gets a node object, retrying up to DefaultBackoff number of times if it fails
func GetNodeRetry(nc v1core.NodeInterface, node string) (*v1api.Node, error) {
var apiNode *v1api.Node
err := RetryOnError(DefaultBackoff, func() error {
n, getErr := nc.Get(node, v1meta.GetOptions{})
if getErr != nil {
return fmt.Errorf("failed to get node %q: %v", node, getErr)
}
apiNode = n
return nil
})
return apiNode, err
}
// UpdateNodeRetry calls f to update a node object in Kubernetes.
// It will attempt to update the node by applying f to it up to DefaultBackoff
// number of times.
// f will be called each time since the node object will likely have changed if
// a retry is necessary.
func UpdateNodeRetry(nc v1core.NodeInterface, node string, f func(*v1api.Node)) error {
err := RetryOnConflict(DefaultBackoff, func() error {
n, getErr := nc.Get(node, v1meta.GetOptions{})
if getErr != nil {
return fmt.Errorf("failed to get node %q: %v", node, getErr)
}
f(n)
_, err := nc.Update(n)
return err
})
if err != nil {
// may be conflict if max retries were hit
return fmt.Errorf("unable to update node %q: %v", node, err)
}
return nil
}
// SetNodeLabels sets all keys in m to their respective values in
// node's labels.
func SetNodeLabels(nc v1core.NodeInterface, node string, m map[string]string) error {
return UpdateNodeRetry(nc, node, func(n *v1api.Node) {
for k, v := range m {
n.Labels[k] = v
}
})
}
// SetNodeAnnotations sets all keys in m to their respective values in
// node's annotations.
func SetNodeAnnotations(nc v1core.NodeInterface, node string, m map[string]string) error {
return UpdateNodeRetry(nc, node, func(n *v1api.Node) {
for k, v := range m {
n.Annotations[k] = v
}
})
}
// SetNodeAnnotationsLabels sets all keys in a and l to their values in
// node's annotations and labels, respectively
func SetNodeAnnotationsLabels(nc v1core.NodeInterface, node string, a, l map[string]string) error {
return UpdateNodeRetry(nc, node, func(n *v1api.Node) {
for k, v := range a {
n.Annotations[k] = v
}
for k, v := range l {
n.Labels[k] = v
}
})
}
// DeleteNodeLabels deletes all keys in ks
func DeleteNodeLabels(nc v1core.NodeInterface, node string, ks []string) error {
return UpdateNodeRetry(nc, node, func(n *v1api.Node) {
for _, k := range ks {
delete(n.Labels, k)
}
})
}
// DeleteNodeAnnotations deletes all annotations with keys in ks
func DeleteNodeAnnotations(nc v1core.NodeInterface, node string, ks []string) error {
return UpdateNodeRetry(nc, node, func(n *v1api.Node) {
for _, k := range ks {
delete(n.Annotations, k)
}
})
}
// Unschedulable marks node as schedulable or unschedulable according to sched.
func Unschedulable(nc v1core.NodeInterface, node string, sched bool) error {
n, err := nc.Get(node, v1meta.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get node %q: %v", node, err)
}
n.Spec.Unschedulable = sched
if err := RetryOnConflict(DefaultBackoff, func() (err error) {
n, err = nc.Update(n)
return
}); err != nil {
return fmt.Errorf("unable to set 'Unschedulable' property of node %q to %t: %v", node, sched, err)
}
return nil
}
// splits newline-delimited KEY=VAL pairs and update map
func splitNewlineEnv(m map[string]string, envs string) {
sc := bufio.NewScanner(strings.NewReader(envs))
for sc.Scan() {
spl := strings.SplitN(sc.Text(), "=", 2)
// just skip empty lines or lines without a value
if len(spl) == 1 {
continue
}
m[spl[0]] = spl[1]
}
}
// VersionInfo contains CoreOS version and update information.
type VersionInfo struct {
Name string
ID string
Group string
Version string
}
func getUpdateMap() (map[string]string, error) {
infomap := map[string]string{}
// this file should always be present on CoreOS
uconf, err := os.Open(updateConfPath)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(uconf)
uconf.Close()
if err != nil {
return nil, err
}
splitNewlineEnv(infomap, string(b))
// if present and readable, this file has overrides
econf, err := os.Open(updateConfOverridePath)
if err != nil {
glog.Infof("Skipping missing update.conf: %v", err)
}
b, err = ioutil.ReadAll(econf)
econf.Close()
if err == nil {
splitNewlineEnv(infomap, string(b))
}
return infomap, nil
}
func getReleaseMap() (map[string]string, error) {
infomap := map[string]string{}
// this file should always be present on CoreOS
osrelease, err := os.Open(osReleasePath)
if err != nil {
return nil, err
}
defer osrelease.Close()
b, err := ioutil.ReadAll(osrelease)
osrelease.Close()
if err != nil {
return nil, err
}
splitNewlineEnv(infomap, string(b))
return infomap, nil
}
// GetVersionInfo returns VersionInfo from the current CoreOS system.
//
// Should probably live in a different package.
func GetVersionInfo() (*VersionInfo, error) {
updateconf, err := getUpdateMap()
if err != nil {
return nil, fmt.Errorf("unable to get update configuration: %v", err)
}
osrelease, err := getReleaseMap()
if err != nil {
return nil, fmt.Errorf("unable to get os release info: %v", err)
}
vi := &VersionInfo{
Name: osrelease["NAME"],
ID: osrelease["ID"],
Group: updateconf["GROUP"],
Version: osrelease["VERSION"],
}
return vi, nil
}