-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkubeyaml.py
313 lines (275 loc) · 9.74 KB
/
kubeyaml.py
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import sys
import argparse
import functools
import collections
import re
from ruamel.yaml import YAML
# The container name, by proclamation, used for an image supplied in a
# FluxHelmRelease
FHR_CONTAINER = 'chart-image'
class NotFound(Exception):
pass
class UnresolvablePath(Exception):
pass
def parse_args():
p = argparse.ArgumentParser()
subparsers = p.add_subparsers()
image = subparsers.add_parser('image', help='update an image ref')
image.add_argument('--namespace', required=True)
image.add_argument('--kind', required=True)
image.add_argument('--name', required=True)
image.add_argument('--container', required=True)
image.add_argument('--image', required=True)
image.set_defaults(func=update_image)
def keyValuePair(s):
k, v = s.split('=')
return k, v
annotation = subparsers.add_parser('annotate', help='update annotations')
annotation.add_argument('--namespace', required=True)
annotation.add_argument('--kind', required=True)
annotation.add_argument('--name', required=True)
annotation.add_argument('notes', nargs='+', type=keyValuePair)
annotation.set_defaults(func=update_annotations)
set = subparsers.add_parser('set', help='update values by their dot notation paths')
set.add_argument('--namespace', required=True)
set.add_argument('--kind', required=True)
set.add_argument('--name', required=True)
set.add_argument('paths', nargs='+', type=keyValuePair)
set.set_defaults(func=set_paths)
return p.parse_args()
def yaml():
y = YAML()
y.explicit_start = True
y.explicit_end = False
y.preserve_quotes = True
return y
def bail(reason):
sys.stderr.write(reason); sys.stderr.write('\n')
sys.exit(2)
class AlwaysFalse(object):
def __init__(self):
pass
def __get__(self, instance, owner):
return False
def __set__(self, instance, value):
pass
def apply_to_yaml(fn, infile, outfile):
# fn :: iterator a -> iterator b
y = yaml()
# Hack to make sure no end-of-document ("...") is ever added
y.Emitter.open_ended = AlwaysFalse()
docs = y.load_all(infile)
y.dump_all(fn(docs), outfile)
def update_image(args, docs):
"""Update the manifest specified by args, in the stream of docs"""
found = False
for doc in docs:
if not found:
for m in manifests(doc):
c = find_container(args, m)
if c != None:
set_container_image(m, c, args.image)
found = True
break
yield doc
if not found:
raise NotFound()
def update_annotations(spec, docs):
def ensure(d, *keys):
for k in keys:
try:
d = d[k]
except KeyError:
d[k] = dict()
d = d[k]
return d
found = False
for doc in docs:
if not found:
for m in manifests(doc):
if match_manifest(spec, m):
notes = ensure(m, 'metadata', 'annotations')
for k, v in spec.notes:
if v == '':
try:
del notes[k]
except KeyError:
pass
else:
notes[k] = v
if len(notes) == 0:
del m['metadata']['annotations']
found = True
break
yield doc
if not found:
raise NotFound()
def set_paths(spec, docs):
def set_path(d, path, value):
keys = path.split(".")
for k in keys[:-1]:
if k not in d:
return False
d = d[k]
if isinstance(d[keys[-1]], collections.Mapping):
return False
d[keys[-1]] = value
return True
found = False
unresolvable = list()
for doc in docs:
if not found:
for m in manifests(doc):
if match_manifest(spec, m):
for k, v in spec.paths:
if not set_path(m, k, v):
unresolvable.append(k)
found = True
break
yield doc
if len(unresolvable):
raise UnresolvablePath(unresolvable)
if not found:
raise NotFound()
def manifests(doc):
if doc == None:
return collections.OrderedDict()
if doc['kind'].endswith('List'):
for m in doc['items']:
yield m
else:
yield doc
def match_manifest(spec, manifest):
try:
# NB treat the Kind as case-insensitive
if manifest['kind'].lower() != spec.kind.lower():
return False
if manifest['metadata'].get('namespace', 'default') != spec.namespace:
return False
if manifest['metadata']['name'] != spec.name:
return False
except KeyError:
return False
return True
def podspec(manifest):
if manifest['kind'] == 'CronJob':
spec = manifest['spec']['jobTemplate']['spec']['template']['spec']
else:
spec = manifest['spec']['template']['spec']
return spec
def containers(manifest):
if manifest['kind'] in ['FluxHelmRelease', 'HelmRelease']:
return fluxhelmrelease_containers(manifest)
spec = podspec(manifest)
return spec.get('containers', []) + spec.get('initContainers', [])
def find_container(spec, manifest):
if not match_manifest(spec, manifest):
return None
for c in containers(manifest):
if c['name'] == spec.container:
return c
return None
def set_container_image(manifest, container, image):
if manifest['kind'] in ['FluxHelmRelease', 'HelmRelease']:
set_fluxhelmrelease_container(manifest, container, image)
else:
container['image'] = image
def mappings(values):
return ((k, values[k]) for k in values if isinstance(values[k], collections.Mapping))
# There are different ways of interpreting FluxHelmRelease values as
# images, and we have to sniff to see which to use.
def fluxhelmrelease_containers(manifest):
def get_image(values):
image = values['image']
if isinstance(image, collections.Mapping) and 'repository' in image:
values = image
image = image['repository']
if 'registry' in values and values['registry'] != '':
image = '%s/%s' % (values['registry'], image)
if 'tag' in values and values['tag'] != '':
image = '%s:%s' % (image, values['tag'])
return image
containers = []
values = manifest['spec']['values']
# Easiest one: the values section has a key called `image`, which
# has the image used somewhere in the templates. Since we don't
# know which container it appears in, it gets a standard name.
if 'image' in values:
containers = [{
'name': FHR_CONTAINER,
'image': get_image(values),
}]
# Second easiest: if there's at least one dict in values that has
# a key `image`, then all such dicts are treated as containers,
# named for their key.
for k, v in mappings(values):
if 'image' in v:
containers.append({'name': k, 'image': get_image(v)})
return containers
def set_fluxhelmrelease_container(manifest, container, replace):
# The logic within this method (almost) equals:
# /~https://github.com/weaveworks/flux/blob/5b15a94397d58b69a2daedae3bcc377e4901435b/image/image.go#L136
def parse_ref():
reg, im, tag = '', '', ''
try:
segments = replace.split('/')
if len(segments) == 1:
im = replace
elif len(segments) == 2:
domainComponent = '([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])'
domain = '(localhost|(%s([.]%s)+))(:[0-9]+)?' % (domainComponent, domainComponent)
if re.fullmatch(domain, segments[0]):
reg = segments[0]
im = segments[1]
else:
im = replace
else:
reg = segments[0]
im = '/'.join(segments[1:])
segments = im.split(':')
if len(segments) == 2:
im, tag = segments
elif len(segments) == 3:
im = ':'.join(segments[:2])
tag = segments[2]
except ValueError:
pass
return reg, im, tag
def set_image(values):
image = values['image']
imageKey = 'image'
if isinstance(image, collections.Mapping) and 'repository' in image:
values = image
imageKey = 'repository'
reg, im, tag = parse_ref()
if 'registry' in values and 'tag' in values:
values['registry'] = reg
values[imageKey] = im
values['tag'] = tag
elif 'registry' in values:
values['registry'] = reg
values[imageKey] = ':'.join(filter(None, [im, tag]))
elif 'tag' in values:
values[imageKey] = '/'.join(filter(None, [reg, im]))
values['tag'] = tag
else:
values[imageKey] = replace
values = manifest['spec']['values']
if container['name'] == FHR_CONTAINER and 'image' in values:
set_image(values)
return
for k, v in mappings(values):
if k == container['name'] and 'image' in v:
set_image(v)
return
raise NotFound
def main():
args = parse_args()
try:
apply_to_yaml(functools.partial(args.func, args), sys.stdin, sys.stdout)
except NotFound:
bail("manifest not found")
except UnresolvablePath as e:
bail("unable to resolve path(s):\n" + '\n'.join(e.args[0]))
if __name__ == "__main__":
main()