forked from tilt-dev/tilt-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTiltfile
194 lines (164 loc) · 6.76 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
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
# -*- mode: Python -*-
version_settings(constraint='>=0.23.4')
_helm_apply_path = os.path.abspath('helm-apply-helper.py')
_helm_delete_path = os.path.abspath('helm-delete-helper.py')
def helm_resource(
name,
chart,
deps=[],
release_name='',
namespace=None,
image_deps=None,
image_keys=None,
flags=None,
image_selector='',
container_selector='',
live_update=None,
resource_deps=None,
labels=None,
port_forwards=[],
auto_init=True,
pod_readiness='',
update_dependencies=False,
links=[]):
"""Installs a helm chart to a cluster.
Args:
name: The name of the resource in the Tilt UI.
chart: A reference to a chart. Uses the same syntax as `helm install
[release-name] [chart]`. May be a local path, a local tarball, or a `repo/name`
reference.
deps: A list of file dependencies that should trigger a deployment.
release_name: The name of the release. If not specified, defaults to the Tilt UI resource name.
namespace: Install into the specified namespace.
image_deps: A list of images built by Tilt to inject into the chart. If Tilt doesn't know
how to build one of these images, this will be an error.
image_keys: A list of specifications for how to inject images into the
chart. Must be the same length as `image_deps`. There are two common patterns.
- If your chart accepts an image as a single tagged image reference, specify the key as a string.
- If your chart accepts an image as a 'repository' and a 'tag', spectify the key
as a tuple ('image.repository', 'image.tag'). This is how charts created with
`helm create` are structured.
- If your chart accepts an image as a 'registry', 'repository' and a 'tag', specify the key
as a tuple ('image.registry', 'image.repository', 'image.tag'). This is another common pattern used
by many charts.
flags: Additional flags to pass to `helm install` (e.g., `['--set', 'key=value']`)
image_selector: Image reference to determine containers eligible for Live Update.
Only applicable if there are no images in `image_deps`.
container_selector: Container reference to determine containers eligible for Live Update.
Only applicable if there are no images in `image_deps`.
live_update: Live Update steps for images not built by Tilt.
Only applicable if there are no images in `image_deps`.
resource_deps: Tilt resources to depend on. Useful for
adding a dependency on a helm repo install.
labels: Labels for categorizing the resource.
port_forwards: Host port to connect to the pod.
auto_init: Whether this resource runs on `tilt up`. Defaults to `True`.
pod_readiness: Possible values: 'ignore', 'wait'. Controls whether Tilt waits for
pods to be ready before the resource is considered healthy (and dependencies
can start building). By default, Tilt will wait for pods to be ready if it
thinks a resource has pods.
update_dependencies: Runs `helm dependency update` before installing the chart.
links: One or more links to be associated with this resource in the UI.
"""
if not release_name:
release_name=name
if not image_deps:
image_deps = []
if not image_keys:
image_keys = []
if len(image_deps) != len(image_keys):
fail('image_deps and image_keys must have the same length, so that we know how to inject images into the helm chart;')
is_windows = True if os.name == "nt" else False
python_cmd = ['py', '-3'] if is_windows else ['python3']
apply_cmd = python_cmd + [_helm_apply_path]
delete_cmd = python_cmd + [_helm_delete_path]
env = {
'CHART': chart,
'RELEASE_NAME': release_name,
'NAMESPACE': namespace or '',
'TILT_IMAGE_COUNT': str(len(image_keys)),
}
if update_dependencies:
env['TILT_HELM_RELEASE_DEPENDENCY_UPDATE'] = 'true'
if flags:
apply_cmd.extend(flags)
dupe_deps = {}
for i in range(len(image_deps)):
if image_deps[i] in dupe_deps:
fail(('helm_resource: image_deps should be unique. Found duplicate: %s\n' % image_deps[i]) +
(' If you need to inject an image twice, the corresponding element of image_keys can be a list of values'))
dupe_deps[image_deps[i]] = True
key = image_keys[i]
_set_image_env_helper_list(env, key, i)
k8s_custom_deploy(
name,
apply_cmd=apply_cmd,
apply_env=env,
delete_cmd=delete_cmd,
delete_env=env,
deps=deps,
image_deps=image_deps,
image_selector=image_selector,
container_selector=container_selector,
live_update=live_update)
if resource_deps:
k8s_resource(name, resource_deps=resource_deps)
if labels:
k8s_resource(name, labels=labels)
if len(port_forwards):
k8s_resource(name, port_forwards=port_forwards)
if not auto_init:
k8s_resource(name, auto_init=auto_init)
if pod_readiness:
k8s_resource(name, pod_readiness = pod_readiness)
if links:
k8s_resource(name, links=links)
def helm_repo(
name,
url,
resource_name='',
username='',
password='',
**kwargs):
"""Installs a helm repo on tilt up.
name: The name of the helm repo.
url: The url of the helm repo.
resource_name: The name of the resource in the tilt UI. Defaults to `[name]`.
username: The username for authenticating (if the helm repo is private).
password: The password for authenticating (if the helm repo is private).
**kwargs: Arguments to pass to the underlying resource like `labels` (for organization).
"""
if not resource_name:
resource_name=name
args = [
'helm', 'repo', 'add', name, url,
'--force-update', # Replace the repo if it already exists.
]
if username:
args.extend(['--username', username])
if password:
args.extend(['--password', password])
local_resource(resource_name, args, allow_parallel=True, **kwargs)
# Helper functions for setting env variables for images.
def _set_image_env_helper_list(env, key, i):
if type(key) == 'list':
env['TILT_IMAGE_KEY_COUNT_%s' % i] = '%s' % len(key)
for j in range(len(key)):
_set_image_env_helper_item(env, key[j], i, '%s_%s' % (i, j))
else:
env['TILT_IMAGE_KEY_COUNT_%s' % i] = '1'
_set_image_env_helper_item(env, key, i, '%s_0' % i)
# Helper functions for setting env variables for images.
def _set_image_env_helper_item(env, key, i, suffix):
if type(key) == 'string':
env['TILT_IMAGE_KEY_%s' % suffix] = key
elif type(key) == 'tuple':
if len(key) == 2:
env['TILT_IMAGE_KEY_REPO_%s' % suffix] = key[0]
env['TILT_IMAGE_KEY_TAG_%s' % suffix] = key[1]
if len(key) == 3:
env['TILT_IMAGE_KEY_REGISTRY_%s' % suffix] = key[0]
env['TILT_IMAGE_KEY_REPO_%s' % suffix] = key[1]
env['TILT_IMAGE_KEY_TAG_%s' % suffix] = key[2]
else:
fail("invalid argument to image_keys at %s: %s" % (i, type(key)))