forked from tilt-dev/tilt-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-apply-helper.py
85 lines (70 loc) · 3.23 KB
/
helm-apply-helper.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
# Helper script for the Tiltfile's apply cmd. Not intended to be called independently.
#
# Usage:
# python3 helm-apply-helper.py ... [image config keys in order]
import os
import subprocess
import sys
from typing import Dict
from namespacing import add_default_namespace
def _parse_image_string(image: str) -> Dict:
if '.' in image or 'localhost' in image or image.count(":") > 1:
registry, repository = image.split('/', 1)
repository, tag = repository.rsplit(':', 1)
return {"registry": registry, "repository": repository, "tag": tag}
repository, tag = image.rsplit(':', 1)
return {"registry": None, "repository": repository, "tag": tag}
flags = sys.argv[1:]
image_count = int(os.environ['TILT_IMAGE_COUNT'])
for i in range(image_count):
image = os.environ['TILT_IMAGE_%s' % i]
count = int(os.environ['TILT_IMAGE_KEY_COUNT_%s' % i])
for j in range(count):
suffix = '%s_%s' % (i, j)
key = os.environ.get('TILT_IMAGE_KEY_%s' % suffix, '')
if key:
flags.extend(['--set', '%s=%s' % (key, image)])
continue
image_parts = _parse_image_string(image)
key0 = os.environ.get('TILT_IMAGE_KEY_REGISTRY_%s' % suffix, '')
key1 = os.environ.get('TILT_IMAGE_KEY_REPO_%s' % suffix, '')
key2 = os.environ.get('TILT_IMAGE_KEY_TAG_%s' % suffix, '')
if image_parts['registry']:
if key0 != '':
# Image has a registry AND a specific helm key for the registry
flags.extend(['--set', '%s=%s' % (key0, image_parts["registry"]),
'--set', '%s=%s' % (key1, image_parts["repository"])])
else:
# Image has a registry but does not have a specific helm key for registry
flags.extend(['--set', '%s=%s/%s' % (key1, image_parts["registry"], image_parts["repository"])])
else:
# Image does NOT have a registry component
flags.extend(['--set', '%s=%s' % (key1, image_parts["repository"])])
flags.extend(['--set', '%s=%s' % (key2, image_parts["tag"])])
install_cmd = ['helm', 'upgrade', '--install']
install_cmd.extend(flags)
get_cmd = ['helm', 'get', 'manifest']
kubectl_cmd = ['kubectl', 'get']
release_name = os.environ['RELEASE_NAME']
chart = os.environ['CHART']
namespace = os.environ.get('NAMESPACE', '')
if namespace:
install_cmd.extend(['--namespace', namespace])
get_cmd.extend(['--namespace', namespace])
install_cmd.extend([release_name, chart])
get_cmd.extend([release_name])
kubectl_cmd.extend(['-oyaml', '-f', '-'])
if os.environ.get('TILT_HELM_RELEASE_DEPENDENCY_UPDATE', False) == 'true':
dependency_update_cmd = ['helm', 'dependency', 'update', chart]
print("Running cmd: %s" % dependency_update_cmd, file=sys.stderr)
subprocess.check_call(dependency_update_cmd, stdout=sys.stderr)
print("Running cmd: %s" % install_cmd, file=sys.stderr)
subprocess.check_call(install_cmd, stdout=sys.stderr)
print("Running cmd: %s" % get_cmd, file=sys.stderr)
out = subprocess.check_output(get_cmd).decode('utf-8')
is_windows = True if os.name == 'nt' else False
out_with_right_line_endings = out.replace(os.linesep, '\n') if is_windows else out
input = add_default_namespace(out_with_right_line_endings, namespace).encode('utf-8')
print("Running cmd: %s" % kubectl_cmd, file=sys.stderr)
completed = subprocess.run(kubectl_cmd, input=input)
completed.check_returncode()