forked from tilt-dev/tilt-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tiltfile
84 lines (71 loc) · 3.12 KB
/
Tiltfile
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
# -*- mode: Python -*-
# module function
# non build development with only file sync
def file_sync_only(image='', manifests=[], deps=["."], live_update=[], allow_duplicates=False):
if type(manifests) != "list":
manifests = [manifests]
deployed_image = ""
deployed_tag = ""
# get current tags from image setting from first arg in file_sync_only() or in manifests
# ex) image is "nginx" or "nginx:1.17" format
# "nginx" => deployed_image="nginx" and deployed_tag="1.16" (from manifests)
# "nginx:1.17" => deployed_image="nginx" and deployed_tag="1.17"
p = image.split(':')
if len(p) == 2:
deployed_image = p[0]
deployed_tag = p[1]
else:
deployed_image = image
# get kubernetes resources using target image and tags
k8s_resources, tag_in_manifests = _get_related_resources(manifests, deployed_image)
if deployed_tag == "":
deployed_tag = tag_in_manifests
# ":" is null command
custom_build(deployed_image, ':',
tag=deployed_tag,
deps=deps,
skips_local_docker=True,
live_update=live_update
)
k8s_yaml(manifests, allow_duplicates=allow_duplicates)
_first_sync_from_liveupdate(deployed_image, live_update, k8s_resources)
# sync files at first time
def _first_sync_from_liveupdate(image, live_update, k8s_resources):
for l in live_update:
if type(l) == "live_update_sync_step":
local_path, remote_path = _get_sync_params(l)
syncname = "firstsync-%s-%s" % (image.replace("/", "_"), local_path.replace("/", "_"))
local_resource(syncname,
'touch %s' % local_path,
resource_deps=k8s_resources)
# return sync(local_path, remote_path)'s args
def _get_sync_params(sync):
output = str(sync).split("'")
local_path = output[1].replace(os.getcwd() + "/", "")
remote_path = output[3]
return local_path, remote_path
# get k8s workloads resources using specific image
def _get_related_resources(manifests, image):
k8s_resources = []
tags_in_manifests = []
for m in manifests:
if type(m) == 'string':
objects = read_yaml_stream(m)
elif type(m) == 'blob':
objects = decode_yaml_stream(m)
else:
fail('only takes string or blob, got: %s' % type(m))
for o in objects:
if ("spec" in o) and ("template" in o["spec"]) and ("spec" in o["spec"]["template"]) and ("containers" in o["spec"]["template"]["spec"]):
for c in o["spec"]["template"]["spec"]["containers"]:
sp = c["image"].split(":")
if image == sp[0]:
k8s_resources.append(o["metadata"]["name"])
if len(sp) == 2 and sp[1] not in tags_in_manifests:
tags_in_manifests.append(sp[1])
# if num of tags_in_manifests is >1, tags are ambiguous
if len(tags_in_manifests) == 0:
tags_in_manifests = ["latest"]
elif len(tags_in_manifests) > 1:
fail("found multiple image tags for %s in manifests %s" % (image, manifests))
return k8s_resources, tags_in_manifests[0]