-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
67 lines (58 loc) · 2.22 KB
/
lambda_function.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
import json
import base64
image_mirrors = {
#'/':'<some dockerhub mirror>', #you can set dockerhub mirror with "/"
'gcr.io/': 'asia.gcr.io/',
'k8s.gcr.io/': 'asia.gcr.io/google-containers/'
}
def handler(event, context):
request_body = json.loads(event['body'])
json_patch = []
# get initContainers from request and replace image path with JSON Patch
initContainers = dict_get(request_body, 'request.object.spec.initContainers')
if initContainers:
json_patch += image_patch(initContainers, '/spec/initContainers')
# get containters from request and replace image path with JSON Patch
containers = dict_get(request_body, 'request.object.spec.containers')
if containers:
json_patch += image_patch(containers, '/spec/containers')
print(json.dumps(json_patch))
# set response body
patch_b64 = base64.b64encode(json.dumps(json_patch))
response_body = {
'response': {
'allowed': True,
'patch': patch_b64,
'patchType': 'JSONPatch'
}
}
return {
'body': json.dumps(response_body),
'headers': {
'Content-Type': 'application/json'
},
'statusCode': 200
}
def dict_get(dictionary, keys, default=None):
return reduce(lambda d, key: d.get(key, default) if isinstance(d, dict) else default, keys.split("."), dictionary)
def replace_dockerhub_prfix(image):
if image.startswith("library/"):
return image[len("library/"):]
elif image.startswith("docker.io/"):
return image.replace("docker.io/library/","").replace("docker.io/","")
def image_patch(containers, path_prefix):
json_patch = []
for idx, container in enumerate(containers):
image = container['image']
math_mirror=False
for orig_image, mirror_image in image_mirrors.iteritems():
if image.startswith(orig_image):
math_mirror=True
image = mirror_image + image[len(orig_image):]
if "/" in image_mirrors and math_mirror==False:
if image.startswith("docker.io/") or image.startswith("library/"):
image = image_mirrors["/"] + replace_dockerhub_prfix(image)
elif "." not in image.split("/")[0]:
image = image_mirrors["/"] + image
json_patch.append({'op': 'replace', 'path': '%s/%d/image' % (path_prefix, idx), 'value': image})
return json_patch