Skip to content

Commit c086f7a

Browse files
meobilivangrichardcase
authored andcommitted
Tilt file
1 parent 27dd649 commit c086f7a

File tree

1 file changed

+179
-46
lines changed

1 file changed

+179
-46
lines changed

Tiltfile

Lines changed: 179 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ tools_bin = "./hack/tools/bin"
55
kubectl_cmd = "./hack/tools/bin/kubectl"
66
kind_cmd = "./hack/tools/bin/kind"
77

8-
#Add tools to path
8+
# Add tools to path
99
os.putenv("PATH", os.getenv("PATH") + ":" + tools_bin)
1010

11-
update_settings(k8s_upsert_timeout_secs = 60) # on first tilt up, often can take longer than 30 seconds
11+
update_settings(
12+
k8s_upsert_timeout_secs=60
13+
) # on first tilt up, often can take longer than 30 seconds
1214

1315
# set defaults
1416
settings = {
@@ -26,10 +28,12 @@ settings = {
2628
keys = ["GCP_B64ENCODED_CREDENTIALS"]
2729

2830
# global settings
29-
settings.update(read_json(
30-
"tilt-settings.json",
31-
default = {},
32-
))
31+
settings.update(
32+
read_json(
33+
"tilt-settings.json",
34+
default={},
35+
)
36+
)
3337

3438
if settings.get("trigger_mode") == "manual":
3539
trigger_mode(TRIGGER_MODE_MANUAL)
@@ -40,36 +44,61 @@ if "allowed_contexts" in settings:
4044
if "default_registry" in settings:
4145
default_registry(settings.get("default_registry"))
4246

47+
4348
# deploy CAPI
4449
def deploy_capi():
4550
version = settings.get("capi_version")
46-
capi_uri = "https://github.com/kubernetes-sigs/cluster-api/releases/download/{}/cluster-api-components.yaml".format(version)
47-
cmd = "curl -sSL {} | {} | {} apply -f -".format(capi_uri, envsubst_cmd, kubectl_cmd)
48-
local(cmd, quiet = True)
51+
capi_uri = "https://github.com/kubernetes-sigs/cluster-api/releases/download/{}/cluster-api-components.yaml".format(
52+
version
53+
)
54+
cmd = "curl -sSL {} | {} | {} apply -f -".format(
55+
capi_uri, envsubst_cmd, kubectl_cmd
56+
)
57+
local(cmd, quiet=True)
4958
if settings.get("extra_args"):
5059
extra_args = settings.get("extra_args")
5160
if extra_args.get("core"):
5261
core_extra_args = extra_args.get("core")
5362
if core_extra_args:
5463
for namespace in ["capi-system"]:
55-
patch_args_with_extra_args(namespace, "capi-controller-manager", core_extra_args)
64+
patch_args_with_extra_args(
65+
namespace, "capi-controller-manager", core_extra_args
66+
)
5667
if extra_args.get("kubeadm-bootstrap"):
5768
kb_extra_args = extra_args.get("kubeadm-bootstrap")
5869
if kb_extra_args:
59-
patch_args_with_extra_args("capi-kubeadm-bootstrap-system", "capi-kubeadm-bootstrap-controller-manager", kb_extra_args)
70+
patch_args_with_extra_args(
71+
"capi-kubeadm-bootstrap-system",
72+
"capi-kubeadm-bootstrap-controller-manager",
73+
kb_extra_args,
74+
)
75+
6076

6177
def patch_args_with_extra_args(namespace, name, extra_args):
62-
args_str = str(local("{} get deployments {} -n {} -o jsonpath={{.spec.template.spec.containers[0].args}}".format(kubectl_cmd, name, namespace)))
78+
args_str = str(
79+
local(
80+
"{} get deployments {} -n {} -o jsonpath={{.spec.template.spec.containers[0].args}}".format(
81+
kubectl_cmd, name, namespace
82+
)
83+
)
84+
)
6385
args_to_add = [arg for arg in extra_args if arg not in args_str]
6486
if args_to_add:
6587
args = args_str[1:-1].split()
6688
args.extend(args_to_add)
67-
patch = [{
68-
"op": "replace",
69-
"path": "/spec/template/spec/containers/0/args",
70-
"value": args,
71-
}]
72-
local("{} patch deployment {} -n {} --type json -p='{}'".format(kubectl_cmd, name, namespace, str(encode_json(patch)).replace("\n", "")))
89+
patch = [
90+
{
91+
"op": "replace",
92+
"path": "/spec/template/spec/containers/0/args",
93+
"value": args,
94+
}
95+
]
96+
local(
97+
"{} patch deployment {} -n {} --type json -p='{}'".format(
98+
kubectl_cmd, name, namespace, str(encode_json(patch)).replace("\n", "")
99+
)
100+
)
101+
73102

74103
# Users may define their own Tilt customizations in tilt.d. This directory is excluded from git and these files will
75104
# not be checked in to version control.
@@ -78,23 +107,37 @@ def include_user_tilt_files():
78107
for f in user_tiltfiles:
79108
include(f)
80109

81-
def append_arg_for_container_in_deployment(yaml_stream, name, namespace, contains_image_name, args):
110+
111+
def append_arg_for_container_in_deployment(
112+
yaml_stream, name, namespace, contains_image_name, args
113+
):
82114
for item in yaml_stream:
83-
if item["kind"] == "Deployment" and item.get("metadata").get("name") == name and item.get("metadata").get("namespace") == namespace:
115+
if (
116+
item["kind"] == "Deployment"
117+
and item.get("metadata").get("name") == name
118+
and item.get("metadata").get("namespace") == namespace
119+
):
84120
containers = item.get("spec").get("template").get("spec").get("containers")
85121
for container in containers:
86122
if contains_image_name in container.get("image"):
87123
container.get("args").extend(args)
88124

125+
89126
def fixup_yaml_empty_arrays(yaml_str):
90127
yaml_str = yaml_str.replace("conditions: null", "conditions: []")
91128
return yaml_str.replace("storedVersions: null", "storedVersions: []")
92129

130+
93131
def validate_auth():
94132
substitutions = settings.get("kustomize_substitutions", {})
95133
missing = [k for k in keys if k not in substitutions]
96134
if missing:
97-
fail("missing kustomize_substitutions keys {} in tilt-settings.json".format(missing))
135+
fail(
136+
"missing kustomize_substitutions keys {} in tilt-settings.json".format(
137+
missing
138+
)
139+
)
140+
98141

99142
tilt_helper_dockerfile_header = """
100143
# Tilt image
@@ -118,35 +161,58 @@ COPY --from=tilt-helper /go/bin/dlv .
118161
COPY manager .
119162
"""
120163

164+
121165
# Build CAPG and add feature gates
122166
def capg():
123167
# Apply the kustomized yaml for this provider
124168
substitutions = settings.get("kustomize_substitutions", {})
125169
os.environ.update(substitutions)
126170

127-
# yaml = str(kustomizesub("./hack/observability")) # build an observable kind deployment by default
128-
yaml = str(kustomizesub("./config/default"))
171+
yaml = str(
172+
kustomizesub("./hack/observability")
173+
) # build an observable kind deployment by default
174+
# TODO: consider to remove
175+
# yaml = str(kustomizesub("./config/default"))
129176

130177
# add extra_args if they are defined
131178
if settings.get("extra_args"):
132179
gcp_extra_args = settings.get("extra_args").get("gcp")
133180
if gcp_extra_args:
134181
yaml_dict = decode_yaml_stream(yaml)
135-
append_arg_for_container_in_deployment(yaml_dict, "capg-controller-manager", "capg-system", "cluster-api-gcp-controller", gcp_extra_args)
182+
append_arg_for_container_in_deployment(
183+
yaml_dict,
184+
"capg-controller-manager",
185+
"capg-system",
186+
"cluster-api-gcp-controller",
187+
gcp_extra_args,
188+
)
136189
yaml = str(encode_yaml_stream(yaml_dict))
137190
yaml = fixup_yaml_empty_arrays(yaml)
138191

139192
# Set up a local_resource build of the provider's manager binary.
140193
local_resource(
141194
"manager",
142-
cmd = 'mkdir -p .tiltbuild;CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags \'-extldflags "-static"\' -o .tiltbuild/manager',
143-
deps = ["api", "cloud", "config", "controllers", "exp", "feature", "pkg", "go.mod", "go.sum", "main.go"],
195+
cmd="mkdir -p .tiltbuild;CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags '-extldflags \"-static\"' -o .tiltbuild/manager",
196+
deps=[
197+
"api",
198+
"cloud",
199+
"config",
200+
"controllers",
201+
"exp",
202+
"feature",
203+
"pkg",
204+
"go.mod",
205+
"go.sum",
206+
"main.go",
207+
],
144208
)
145209

146-
dockerfile_contents = "\n".join([
147-
tilt_helper_dockerfile_header,
148-
tilt_dockerfile_header,
149-
])
210+
dockerfile_contents = "\n".join(
211+
[
212+
tilt_helper_dockerfile_header,
213+
tilt_dockerfile_header,
214+
]
215+
)
150216

151217
entrypoint = ["sh", "/start.sh", "/manager"]
152218
extra_args = settings.get("extra_args")
@@ -156,45 +222,110 @@ def capg():
156222
# Set up an image build for the provider. The live update configuration syncs the output from the local_resource
157223
# build into the container.
158224
docker_build(
159-
ref = "gcr.io/k8s-staging-cluster-api-gcp/cluster-api-gcp-controller",
160-
context = "./.tiltbuild/",
161-
dockerfile_contents = dockerfile_contents,
162-
target = "tilt",
163-
entrypoint = entrypoint,
164-
only = "manager",
165-
live_update = [
225+
ref="gcr.io/k8s-staging-cluster-api-gcp/cluster-api-gcp-controller",
226+
context="./.tiltbuild/",
227+
dockerfile_contents=dockerfile_contents,
228+
target="tilt",
229+
entrypoint=entrypoint,
230+
only="manager",
231+
live_update=[
166232
sync(".tiltbuild/manager", "/manager"),
167233
run("sh /restart.sh"),
168234
],
169-
ignore = ["templates"],
235+
ignore=["templates"],
170236
)
171237

172238
k8s_yaml(blob(yaml))
173239

240+
241+
def observability():
242+
# Install the OpenTelemetry helm chart
243+
gcp_project_id = os.getenv("GCP_PROJECT_ID", "")
244+
245+
k8s_yaml(
246+
helm(
247+
"./hack/observability/opentelemetry/chart",
248+
name="opentelemetry-collector",
249+
namespace="capg-system",
250+
values=["./hack/observability/opentelemetry/values.yaml"],
251+
# refer https://github.com/helm/helm/issues/1987
252+
set=[
253+
"extraEnvs[0].name=GCP_PROJECT_ID",
254+
"extraEnvs[0].value=" + gcp_project_id,
255+
],
256+
)
257+
)
258+
259+
k8s_yaml(
260+
helm(
261+
"./hack/observability/jaeger/chart",
262+
name="jaeger-all-in-one",
263+
namespace="capg-system",
264+
set=[
265+
# TODO: consider to remove
266+
# "crd.install=false",
267+
# "rbac.create=false",
268+
"resources.limits.cpu=200m",
269+
"resources.limits.memory=256Mi",
270+
],
271+
)
272+
)
273+
274+
k8s_resource(
275+
workload="jaeger-all-in-one",
276+
new_name="traces: jaeger-all-in-one",
277+
port_forwards=[
278+
port_forward(16686, name="View traces", link_path="/search?service=capg")
279+
],
280+
labels=["observability"],
281+
)
282+
283+
k8s_resource(workload="opentelemetry-collector", labels=["observability"])
284+
285+
174286
def base64_encode(to_encode):
175-
encode_blob = local("echo '{}' | tr -d '\n' | base64 - | tr -d '\n'".format(to_encode), quiet = True)
287+
encode_blob = local(
288+
"echo '{}' | tr -d '\n' | base64 - | tr -d '\n'".format(to_encode), quiet=True
289+
)
176290
return str(encode_blob)
177291

292+
178293
def base64_encode_file(path_to_encode):
179-
encode_blob = local("cat {} | tr -d '\n' | base64 - | tr -d '\n'".format(path_to_encode), quiet = True)
294+
encode_blob = local(
295+
"cat {} | tr -d '\n' | base64 - | tr -d '\n'".format(path_to_encode), quiet=True
296+
)
180297
return str(encode_blob)
181298

299+
182300
def read_file_from_path(path_to_read):
183-
str_blob = local("cat {} | tr -d '\n'".format(path_to_read), quiet = True)
301+
str_blob = local("cat {} | tr -d '\n'".format(path_to_read), quiet=True)
184302
return str(str_blob)
185303

304+
186305
def base64_decode(to_decode):
187-
decode_blob = local("echo '{}' | base64 --decode -".format(to_decode), quiet = True)
306+
decode_blob = local("echo '{}' | base64 --decode -".format(to_decode), quiet=True)
188307
return str(decode_blob)
189308

309+
190310
def kustomizesub(folder):
191-
yaml = local("hack/kustomize-sub.sh {}".format(folder), quiet = True)
311+
yaml = local("hack/kustomize-sub.sh {}".format(folder), quiet=True)
192312
return yaml
193313

314+
194315
def waitforsystem():
195-
local(kubectl_cmd + " wait --for=condition=ready --timeout=300s pod --all -n capi-kubeadm-bootstrap-system")
196-
local(kubectl_cmd + " wait --for=condition=ready --timeout=300s pod --all -n capi-kubeadm-control-plane-system")
197-
local(kubectl_cmd + " wait --for=condition=ready --timeout=300s pod --all -n capi-system")
316+
local(
317+
kubectl_cmd
318+
+ " wait --for=condition=ready --timeout=300s pod --all -n capi-kubeadm-bootstrap-system"
319+
)
320+
local(
321+
kubectl_cmd
322+
+ " wait --for=condition=ready --timeout=300s pod --all -n capi-kubeadm-control-plane-system"
323+
)
324+
local(
325+
kubectl_cmd
326+
+ " wait --for=condition=ready --timeout=300s pod --all -n capi-system"
327+
)
328+
198329

199330
##############################
200331
# Actual work happens here
@@ -213,4 +344,6 @@ deploy_capi()
213344

214345
capg()
215346

347+
observability()
348+
216349
waitforsystem()

0 commit comments

Comments
 (0)