-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathTiltfile
199 lines (151 loc) · 6.66 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
195
196
197
198
199
# (C) Copyright 2020 Hewlett Packard Enterprise Development LP
# Find the directory where Tilt loaded modules.
# This is usually the XDG data directory for tilt data.
def _find_tilt_modules_load_dir():
current = os.path.abspath('./')
while True:
if os.path.exists(os.path.join(current, 'tilt_modules')):
return current
next_dir = os.path.dirname(current)
if next_dir == current:
fail('Could not find root Tiltfile')
current = next_dir
def _find_cache_dir():
cachedir = os.getenv('TILT_CACHE_DIR', '')
if cachedir == '':
cachedir = os.path.join(_find_tilt_modules_load_dir(), '.tilt-cache')
if not os.path.exists(cachedir):
local('mkdir -p %s' % shlex.quote(cachedir), echo_off=True)
os.putenv('TILT_CACHE_DIR', cachedir)
return cachedir
def _create_temp_dir():
from_env = os.getenv('TILT_COREOS_PROMETHEUS_TEMP_DIR', '')
if from_env != '':
return from_env
tmpdir = str(local("mktemp -d", echo_off=True, quiet=True)).strip()
os.putenv('TILT_COREOS_PROMETHEUS_TEMP_DIR', tmpdir)
return tmpdir
def name(c):
return c['metadata']['name']
def replace_target(src, target):
if not os.path.exists(src):
fail("src path '%s' not found!" % src)
if not os.path.exists(target):
fail("target path '%s' not found!" % target)
local("cp %s %s" % (shlex.quote(src), shlex.quote(target)))
def download_files():
cachedir = _find_cache_dir()
kube_prometheus_version = '0.14.0'
kube_prometheus_tarball = os.path.join(cachedir, 'coreos-kube-prometheus-%s.tar.gz' % kube_prometheus_version)
kube_prometheus_tarball_quoted = shlex.quote(kube_prometheus_tarball)
tmpdir = _create_temp_dir()
extract_path = os.path.join(tmpdir, 'prometheus-operator-kube-prometheus-%s' % kube_prometheus_version)
extract_path_quoted = shlex.quote(extract_path)
if not os.path.exists(kube_prometheus_tarball):
local("curl -sSL https://github.com/prometheus-operator/kube-prometheus/archive/refs/tags/v%s.tar.gz -o %s.tmp" % (kube_prometheus_version, shlex.quote(kube_prometheus_tarball)), echo_off=True)
# only copy file to final location to prevent redoing the above if the above
# have completed successfully.
local("mv %s.tmp %s" % (kube_prometheus_tarball_quoted, kube_prometheus_tarball_quoted), echo_off=True)
local("rm -rf %s.tmp && mkdir %s.tmp" % (extract_path_quoted, extract_path_quoted), echo_off=True)
local("tar -C %s.tmp -xzf %s --strip-components=1 'kube-prometheus-%s/manifests'" % (extract_path_quoted, kube_prometheus_tarball_quoted, kube_prometheus_version), echo_off=True)
# only move path to final location if extraction completed successfully
local("rm -rf %s && mv %s.tmp %s" % (extract_path_quoted, extract_path_quoted, extract_path_quoted), echo_off=True)
# return path containing manifests directory
return extract_path
def patch_prometheus(path):
manifestsPath = os.path.join(path, 'manifests')
prometheusPatchesPath = os.path.join(os.path.dirname(__file__), 'prometheus')
# patches to modify certain behaviour
# modify clusterrole for greater access
# ensure all namespaces are scanned for alerts
localFiles = listdir(prometheusPatchesPath, recursive=True)
for f in localFiles:
target = os.path.join(manifestsPath, os.path.basename(f))
replace_target(f, target)
def setup_monitoring():
k8s_yaml(setupManifests)
# Wait until the CRDs are ready.
local_resource(
'prometheus-crds-ready',
cmd=' && '.join([('kubectl wait --for=condition=Established crd %s' % name(c)) for c in crds]),
resource_deps=['prometheus-crds'],
)
k8s_yaml(mainManifests)
crdResources = {}
for yDoc in mainManifests:
r = read_yaml(yDoc)
if r['kind'] in crdTypes:
rname = name(r)
if rname not in crdResources:
crdResources[rname] = []
crdResources[rname].append('%s:%s' % (rname, r['kind']))
k8s_resource(
new_name='prometheus-crds',
objects = [('%s' % name(c)) for c in crds],
resource_deps=['prometheus-operator'],
)
resources = {
'grafana': {
'port_forwards': ["3000"],
},
'node-exporter': {},
'kube-state-metrics': {},
'prometheus': {
'new_name': 'prometheus',
'objects': ['prometheus-k8s:service'],
'crdResources': ['prometheus', 'k8s'],
'extra_pod_selectors': [{'prometheus': 'k8s'}],
'port_forwards': ["9090"],
},
'alertmanager': {
'new_name': 'alertmanager',
'objects': ['alertmanager-main:service'],
'crdResources': ['main'],
'extra_pod_selectors': [{'alertmanager': 'main'}],
'port_forwards': ["9093"],
}
}
for rname, resource in resources.items():
args = []
if 'new_name' not in resource:
args.append(rname)
for crd in resource.pop('crdResources', []):
resource['objects'].extend(crdResources.pop(crd, []))
k8s_resource(resource_deps=['prometheus-crds'], *args, **resource)
remainingCrdResources = []
for res in crdResources.values():
remainingCrdResources.extend(res)
k8s_resource(
new_name='uncategorized-prometheus-resources-requiring-crds',
objects=remainingCrdResources,
resource_deps=['prometheus-crds'],
)
def list_prometheus_crd_types():
return crdTypes
def get_prometheus_dependencies():
return ['prometheus-crds']
def get_prometheus_resources(yaml, name):
results = []
for crd in list_prometheus_crd_types():
found, _ = filter_yaml(yaml, name="^%s$" % name, kind="^%s$" % crd)
if found:
results.append('%s:%s' % (name, crd))
return results
# To allow resources that require CRDs created by this extension to be selectively
# pruned from other components if deploying of prometheus is disabled by the
# file loading this one, need to always download and unpack the required files
# and perform a limited amount of parsing.
basedir = download_files()
patch_prometheus(basedir)
# Namespace and CRDs
setupManifests = listdir(os.path.join(basedir, 'manifests/setup'), recursive=True)
# Main resources depending on setup
mainManifests = listdir(os.path.join(basedir, 'manifests'))
crds = []
for doc in setupManifests:
r = read_yaml(doc)
if r['kind'] == 'CustomResourceDefinition':
crds.append(r)
# crdTypes must be known to allow filtering out of these resources to allow
# enabling/disabling use of prometheus in an environment
crdTypes = [crd['spec']['names']['kind'] for crd in crds]