forked from opencost/opencost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tiltfile.opencost
169 lines (149 loc) · 5.51 KB
/
Tiltfile.opencost
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
load('ext://helm_resource', 'helm_resource', 'helm_repo')
load('ext://restart_process', 'docker_build_with_restart')
load('ext://secret', 'secret_create_generic')
def get_docker_platform(arch):
if arch == "arm64":
return "linux/arm64"
else:
return "linux/amd64"
def get_go_arch(arch):
if arch == "arm64":
return "arm64"
else:
return "amd64"
# run_opencost is encapsulated as a function to make import easier for running alongside kubecost.
# The `../opencost` pattern that repeats across this function is to deal with how multiple tilt
# files work - the base directory (`.`) is always relative to the Tiltfile executed and not the
# directory containing this file.
def run_opencost(options):
docker_platform = get_docker_platform(options["arch"])
go_arch = get_go_arch(options["arch"])
is_cloud_integration = options["cloud_integration"] != '' and os.path.exists(options["cloud_integration"])
is_service_key = options["service_key"] != '' and os.path.exists(options["service_key"])
continue_flag = '--continue'
if options["delve_continue"] == False:
continue_flag = ''
# Build and update opencost back end binary when code changes
local_resource(
name='build-costmodel',
dir='.',
cmd='CGO_ENABLED=0 GOOS=linux GOARCH='+go_arch+' go build -o ../opencost/cmd/costmodel/costmodel-tilt ../opencost/cmd/costmodel/main.go',
deps=[
'../opencost/cmd/costmodel/main.go',
'../opencost/pkg',
],
allow_parallel=True,
)
# Build back end docker container
# If the binary is updated, update the running container and restart binary in dlv
docker_build_with_restart(
ref=options["docker_repo"]+'opencost-costmodel',
context='../opencost',
# remove --continue flag to make dlv wait until debugger is attached to start
entrypoint='/go/bin/dlv exec --listen=:40000 --api-version=2 --headless=true --accept-multiclient --log '+continue_flag+' /app/main',
dockerfile='../opencost/Dockerfile.debug',
platform=docker_platform,
build_args={'binary_path': './cmd/costmodel/costmodel-tilt'},
only=[
'cmd/costmodel/costmodel-tilt',
'configs',
'THIRD_PARTY_LICENSES.txt',
],
live_update=[
sync('../opencost/cmd/costmodel/costmodel-tilt', '/app/main'),
],
)
# npm install if package.json changes
local_resource(
name='build-npm-install',
dir='../opencost/ui',
cmd='npm install',
deps=[
'../opencost/ui/package.json',
],
allow_parallel=True,
)
# Build FE locally when code changes
local_resource(
name='build-ui',
dir='../opencost/ui',
cmd='npx parcel build src/index.html',
deps=[
'../opencost/ui/src',
'../opencost/ui/package.json',
],
allow_parallel=True,
resource_deps=['build-npm-install'],
)
# update container when relevant files change
docker_build(
ref=options["docker_repo"]+'opencost-ui',
context='../opencost/ui',
dockerfile='../opencost/ui/Dockerfile.debug',
only=[
'dist',
'nginx.conf',
'default.nginx.conf.template',
'docker-entrypoint.sh',
],
live_update=[
sync('../opencost/ui/dist', '/var/www'),
],
)
values_set = [
'opencost.ui.image.fullImageName='+options["docker_repo"]+'opencost-ui',
'opencost.exporter.image.fullImageName='+options["docker_repo"]+'opencost-costmodel',
'opencost.prometheus.internal.namespaceName='+k8s_namespace(),
'opencost.exporter.debugPort=40000',
]
if is_cloud_integration:
values_set.append('opencost.cloudIntegrationSecret=cloud-integration')
values_set.append('opencost.cloudCost.enabled=true')
else:
values_set.append('opencost.cloudCost.enabled=false')
if is_cloud_integration:
secret_create_generic(
name='cloud-integration',
namespace=k8s_namespace(),
from_file=options["cloud_integration"],
secret_type=None,
from_env_file=None
)
if is_service_key:
secret_create_generic(
name='service-key',
namespace=k8s_namespace(),
from_file=options["service_key"],
secret_type=None,
from_env_file=None
)
# build yaml for deployment to k8s
yaml = helm(
'../opencost-helm-chart/charts/opencost',
name='opencost',
values=[options["helm_values"]],
set=values_set
)
k8s_yaml(yaml) # put resulting yaml into k8s
port_forwards = [
options['port_costmodel']+':9003',
options['port_ui']+':9090',
options['port_debug']+':40000',
]
k8s_resource(workload='opencost', port_forwards=port_forwards)
helm_repo('prometheus-community', 'https://prometheus-community.github.io/helm-charts')
helm_resource(
name='prometheus',
chart='prometheus-community/prometheus',
resource_deps=['prometheus-community'])
k8s_resource(workload='prometheus', port_forwards=[options['port_prometheus']+':9090'])
local_resource(
name='costmodel-test',
dir='../opencost',
cmd='go test ./...',
deps=[
'./pkg',
],
allow_parallel=True,
resource_deps=['opencost'], # run tests after build to speed up deployment
)