Skip to content

Commit ba6261e

Browse files
committed
Tilt file
1 parent 560fd0a commit ba6261e

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
@@ -113,35 +156,58 @@ COPY --from=tilt-helper /restart.sh .
113156
COPY manager .
114157
"""
115158

159+
116160
# Build CAPG and add feature gates
117161
def capg():
118162
# Apply the kustomized yaml for this provider
119163
substitutions = settings.get("kustomize_substitutions", {})
120164
os.environ.update(substitutions)
121165

122-
# yaml = str(kustomizesub("./hack/observability")) # build an observable kind deployment by default
123-
yaml = str(kustomizesub("./config/default"))
166+
yaml = str(
167+
kustomizesub("./hack/observability")
168+
) # build an observable kind deployment by default
169+
# TODO: consider to remove
170+
# yaml = str(kustomizesub("./config/default"))
124171

125172
# add extra_args if they are defined
126173
if settings.get("extra_args"):
127174
gcp_extra_args = settings.get("extra_args").get("gcp")
128175
if gcp_extra_args:
129176
yaml_dict = decode_yaml_stream(yaml)
130-
append_arg_for_container_in_deployment(yaml_dict, "capg-controller-manager", "capg-system", "cluster-api-gcp-controller", gcp_extra_args)
177+
append_arg_for_container_in_deployment(
178+
yaml_dict,
179+
"capg-controller-manager",
180+
"capg-system",
181+
"cluster-api-gcp-controller",
182+
gcp_extra_args,
183+
)
131184
yaml = str(encode_yaml_stream(yaml_dict))
132185
yaml = fixup_yaml_empty_arrays(yaml)
133186

134187
# Set up a local_resource build of the provider's manager binary.
135188
local_resource(
136189
"manager",
137-
cmd = 'mkdir -p .tiltbuild;CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags \'-extldflags "-static"\' -o .tiltbuild/manager',
138-
deps = ["api", "cloud", "config", "controllers", "exp", "feature", "pkg", "go.mod", "go.sum", "main.go"],
190+
cmd="mkdir -p .tiltbuild;CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags '-extldflags \"-static\"' -o .tiltbuild/manager",
191+
deps=[
192+
"api",
193+
"cloud",
194+
"config",
195+
"controllers",
196+
"exp",
197+
"feature",
198+
"pkg",
199+
"go.mod",
200+
"go.sum",
201+
"main.go",
202+
],
139203
)
140204

141-
dockerfile_contents = "\n".join([
142-
tilt_helper_dockerfile_header,
143-
tilt_dockerfile_header,
144-
])
205+
dockerfile_contents = "\n".join(
206+
[
207+
tilt_helper_dockerfile_header,
208+
tilt_dockerfile_header,
209+
]
210+
)
145211

146212
entrypoint = ["sh", "/start.sh", "/manager"]
147213
extra_args = settings.get("extra_args")
@@ -151,45 +217,110 @@ def capg():
151217
# Set up an image build for the provider. The live update configuration syncs the output from the local_resource
152218
# build into the container.
153219
docker_build(
154-
ref = "gcr.io/k8s-staging-cluster-api-gcp/cluster-api-gcp-controller",
155-
context = "./.tiltbuild/",
156-
dockerfile_contents = dockerfile_contents,
157-
target = "tilt",
158-
entrypoint = entrypoint,
159-
only = "manager",
160-
live_update = [
220+
ref="gcr.io/k8s-staging-cluster-api-gcp/cluster-api-gcp-controller",
221+
context="./.tiltbuild/",
222+
dockerfile_contents=dockerfile_contents,
223+
target="tilt",
224+
entrypoint=entrypoint,
225+
only="manager",
226+
live_update=[
161227
sync(".tiltbuild/manager", "/manager"),
162228
run("sh /restart.sh"),
163229
],
164-
ignore = ["templates"],
230+
ignore=["templates"],
165231
)
166232

167233
k8s_yaml(blob(yaml))
168234

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

287+
173288
def base64_encode_file(path_to_encode):
174-
encode_blob = local("cat {} | tr -d '\n' | base64 - | tr -d '\n'".format(path_to_encode), quiet = True)
289+
encode_blob = local(
290+
"cat {} | tr -d '\n' | base64 - | tr -d '\n'".format(path_to_encode), quiet=True
291+
)
175292
return str(encode_blob)
176293

294+
177295
def read_file_from_path(path_to_read):
178-
str_blob = local("cat {} | tr -d '\n'".format(path_to_read), quiet = True)
296+
str_blob = local("cat {} | tr -d '\n'".format(path_to_read), quiet=True)
179297
return str(str_blob)
180298

299+
181300
def base64_decode(to_decode):
182-
decode_blob = local("echo '{}' | base64 --decode -".format(to_decode), quiet = True)
301+
decode_blob = local("echo '{}' | base64 --decode -".format(to_decode), quiet=True)
183302
return str(decode_blob)
184303

304+
185305
def kustomizesub(folder):
186-
yaml = local("hack/kustomize-sub.sh {}".format(folder), quiet = True)
306+
yaml = local("hack/kustomize-sub.sh {}".format(folder), quiet=True)
187307
return yaml
188308

309+
189310
def waitforsystem():
190-
local(kubectl_cmd + " wait --for=condition=ready --timeout=300s pod --all -n capi-kubeadm-bootstrap-system")
191-
local(kubectl_cmd + " wait --for=condition=ready --timeout=300s pod --all -n capi-kubeadm-control-plane-system")
192-
local(kubectl_cmd + " wait --for=condition=ready --timeout=300s pod --all -n capi-system")
311+
local(
312+
kubectl_cmd
313+
+ " wait --for=condition=ready --timeout=300s pod --all -n capi-kubeadm-bootstrap-system"
314+
)
315+
local(
316+
kubectl_cmd
317+
+ " wait --for=condition=ready --timeout=300s pod --all -n capi-kubeadm-control-plane-system"
318+
)
319+
local(
320+
kubectl_cmd
321+
+ " wait --for=condition=ready --timeout=300s pod --all -n capi-system"
322+
)
323+
193324

194325
##############################
195326
# Actual work happens here
@@ -208,4 +339,6 @@ deploy_capi()
208339

209340
capg()
210341

342+
observability()
343+
211344
waitforsystem()

0 commit comments

Comments
 (0)