-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathTiltfile
104 lines (87 loc) · 3.25 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
# -*- mode: Python -*-
def namespace_yaml(name, annotations=[], labels=[]):
"""Returns YAML for a namespace
Args:
name: The namespace name. Currently not validated.
annotations: Annotations applied to the namespace metadata
labels: Labels applied to the namespace metadata
Returns:
The namespace YAML as a blob
"""
annotations_str = ''
labels_str = ''
if (len(annotations) > 0):
annotations_str = """ annotations:
%s
""" % '\n'.join([" %s" % s for s in annotations])
if (len(labels) > 0):
labels_str = """ labels:
%s
""" % '\n'.join([" %s" % s for s in labels])
return blob("""apiVersion: v1
kind: Namespace
metadata:
name: %s
%s%s""" % (name, annotations_str, labels_str))
def namespace_create(name, allow_duplicates=False, annotations=[], labels=[]):
"""Creates a namespace in the current Kubernetes cluster.
Args:
name: The namespace name. Currently not validated.
allow_duplicates: Whether or not k8s should be allowed to have two
instances of the same resource. Useful if you have more than one
Tiltfile trying to create the same namespace.
annotations: Annotations applied to the namespace metadata
labels: Labels applied to the namespace metadata
"""
k8s_yaml(
namespace_yaml(name, annotations=annotations, labels=labels),
allow_duplicates=allow_duplicates
)
def namespace_inject(x, ns):
"""Takes K8s yaml, sets its namespace to `ns`, and returns it as a blob.
This modifies the yaml in two ways:
1. Sets .metadata.namespace to `ns`
2. Sets ..template.metadata.namespace to `ns`
This ensures the namespace in, e.g., Deployment Pod Template Specs is
set, but might have false positives if you have a CRD with some other
element named 'template'.
Args:
x: K8s yaml. Either a filename (string) or the yaml itself (Blob)
ns: The namespace to set the K8s objects to.
Returns:
Blob containing the K8s objects as yaml, with namespaces set to `ns`.
"""
return _mutate_yaml(x, lambda o: _set_k8s_yaml_namespace(o, ns))
def _mutate_yaml(x, f):
if type(x) == 'string':
objects = read_yaml_stream(x)
elif type(x) == 'blob':
objects = decode_yaml_stream(x)
else:
fail('only takes string or blob, got: %s' % type(x))
return encode_yaml_stream([f(o) for o in objects])
def _set_k8s_yaml_namespace(o, ns):
if type(o) == 'dict' and type(o.get('metadata', None)) == 'dict':
o['metadata']['namespace'] = ns
else:
fail('Cannot inject namespace. Object does not have an object field named metadata:\n%s' % encode_yaml(o))
_set_template_namespace(o, ns)
_set_rolebinding_namespace(o, ns)
return o
def _set_template_namespace(o, ns):
if type(o) == 'dict':
for k, v in o.items():
if k == 'template' and type(v) == 'dict' and type(v.get('metadata', None)) == 'dict':
v['metadata']['namespace'] = ns
if type(v) == 'dict' or type(v) == 'list':
_set_template_namespace(v, ns)
elif type(o) == 'list':
for v in o:
_set_template_namespace(v, ns)
def _set_rolebinding_namespace(o, ns):
if type(o) == 'dict':
for k,v in o.items():
if k == 'subjects' and type(v) == 'list':
for s in v:
if type(s) == 'dict' and s.get('kind', None) == "ServiceAccount":
s['namespace'] = ns