forked from kubernetes-sigs/cluster-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tiltfile
223 lines (194 loc) · 7.71 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# -*- mode: Python -*-
# set defaults
settings = {
"deploy_cert_manager": True,
"preload_images_for_kind": True,
"enable_providers": ["docker"],
"kind_cluster_name": "kind",
}
# global settings
settings.update(read_json(
"tilt-settings.json",
default = {},
))
if settings.get("trigger_mode") == "manual":
trigger_mode(TRIGGER_MODE_MANUAL)
allow_k8s_contexts(settings.get("allowed_contexts"))
default_registry(settings.get("default_registry"))
always_enable_providers = ["core"]
providers = {
"core": {
"context": ".",
"image": "gcr.io/k8s-staging-cluster-api/cluster-api-controller",
"live_reload_deps": [
"main.go",
"go.mod",
"go.sum",
"api",
"cmd",
"controllers",
"errors",
"third_party",
"util",
],
},
"kubeadm-bootstrap": {
"context": "bootstrap/kubeadm",
"image": "gcr.io/k8s-staging-cluster-api/kubeadm-bootstrap-controller",
"live_reload_deps": [
"main.go",
"api",
"controllers",
"internal",
],
},
"kubeadm-control-plane": {
"context": "controlplane/kubeadm",
"image": "gcr.io/k8s-staging-cluster-api/kubeadm-control-plane-controller",
"live_reload_deps": [
"main.go",
"api",
"controllers",
],
},
"docker": {
"context": "test/infrastructure/docker",
"image": "gcr.io/k8s-staging-capi-docker/capd-manager",
"live_reload_deps": [
"main.go",
"go.mod",
"go.sum",
"api",
"cloudinit",
"controllers",
"docker",
"third_party",
],
"additional_docker_helper_commands": """
RUN wget -qO- https://dl.k8s.io/v1.14.4/kubernetes-client-linux-amd64.tar.gz | tar xvz
RUN wget -qO- https://get.docker.com | sh
""",
"additional_docker_build_commands": """
COPY --from=tilt-helper /usr/bin/docker /usr/bin/docker
COPY --from=tilt-helper /go/kubernetes/client/bin/kubectl /usr/bin/kubectl
""",
},
}
# Reads a provider's tilt-provider.json file and merges it into the providers map. An example file looks like this:
# {
# "name": "aws",
# "config": {
# "image": "gcr.io/k8s-staging-cluster-api-aws/cluster-api-aws-controller",
# "live_reload_deps": [
# "main.go", "go.mod", "go.sum", "api", "cmd", "controllers", "pkg"
# ]
# }
# }
def load_provider_tiltfiles():
provider_repos = settings.get("provider_repos", [])
for repo in provider_repos:
file = repo + "/tilt-provider.json"
provider_details = read_json(file, default = {})
provider_name = provider_details["name"]
provider_config = provider_details["config"]
provider_config["context"] = repo
providers[provider_name] = provider_config
tilt_helper_dockerfile_header = """
# Tilt image
FROM golang:1.13.7 as tilt-helper
# Support live reloading with Tilt
RUN wget --output-document /restart.sh --quiet https://raw.githubusercontent.com/windmilleng/rerun-process-wrapper/master/restart.sh && \
wget --output-document /start.sh --quiet https://raw.githubusercontent.com/windmilleng/rerun-process-wrapper/master/start.sh && \
chmod +x /start.sh && chmod +x /restart.sh
"""
tilt_dockerfile_header = """
FROM gcr.io/distroless/base:debug as tilt
WORKDIR /
COPY --from=tilt-helper /start.sh .
COPY --from=tilt-helper /restart.sh .
COPY .tiltbuild/manager .
"""
# Configures a provider by doing the following:
#
# 1. Enables a local_resource go build of the provider's manager binary
# 2. Configures a docker build for the provider, with live updating of the manager binary
# 3. Runs kustomize for the provider's config/ and applies it
def enable_provider(name):
p = providers.get(name)
context = p.get("context")
# Prefix each live reload dependency with context. For example, for if the context is
# test/infra/docker and main.go is listed as a dep, the result is test/infra/docker/main.go. This adjustment is
# needed so Tilt can watch the correct paths for changes.
live_reload_deps = []
for d in p.get("live_reload_deps", []):
live_reload_deps.append(context + "/" + d)
# Set up a local_resource build of the provider's manager binary. The provider is expected to have a main.go in
# manager_build_path. The binary is written to .tiltbuild/manager.
local_resource(
name + "_manager",
cmd = "cd " + context + ';mkdir -p .tiltbuild;CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags \'-extldflags "-static"\' -o .tiltbuild/manager',
deps = live_reload_deps,
)
additional_docker_helper_commands = p.get("additional_docker_helper_commands", "")
additional_docker_build_commands = p.get("additional_docker_build_commands", "")
dockerfile_contents = "\n".join([
tilt_helper_dockerfile_header,
additional_docker_helper_commands,
tilt_dockerfile_header,
additional_docker_build_commands,
])
# Set up an image build for the provider. The live update configuration syncs the output from the local_resource
# build into the container.
docker_build(
ref = p.get("image"),
context = context,
dockerfile_contents = dockerfile_contents,
target = "tilt",
entrypoint = ["sh", "/start.sh", "/manager"],
only = ".tiltbuild/manager",
live_update = [
sync(context + "/.tiltbuild/manager", "/manager"),
run("sh /restart.sh"),
],
)
# Apply the kustomized yaml for this provider
yaml = str(kustomize(context + "/config"))
substitutions = settings.get("kustomize_substitutions", {})
for substitution in substitutions:
value = substitutions[substitution]
yaml = yaml.replace("${" + substitution + "}", value)
k8s_yaml(blob(yaml))
# Prepull all the cert-manager images to your local environment and then load them directly into kind. This speeds up
# setup if you're repeatedly destroying and recreating your kind cluster, as it doesn't have to pull the images over
# the network each time.
def deploy_cert_manager():
registry = "quay.io/jetstack"
version = "v0.11.0"
images = ["cert-manager-controller", "cert-manager-cainjector", "cert-manager-webhook"]
if settings.get("preload_images_for_kind"):
for image in images:
local("docker pull {}/{}:{}".format(registry, image, version))
local("kind load docker-image --name {} {}/{}:{}".format(settings.get("kind_cluster_name"), registry, image, version))
local("kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/{}/cert-manager.yaml".format(version))
# wait for the service to become available
local("kubectl wait --for=condition=Available --timeout=300s apiservice v1beta1.webhook.cert-manager.io")
# Users may define their own Tilt customizations in tilt.d. This directory is excluded from git and these files will
# not be checked in to version control.
def include_user_tilt_files():
user_tiltfiles = listdir("tilt.d")
for f in user_tiltfiles:
include(f)
# Enable core cluster-api plus everything listed in 'enable_providers' in tilt-settings.json
def enable_providers():
user_enable_providers = settings.get("enable_providers", [])
union_enable_providers = {k: "" for k in user_enable_providers + always_enable_providers}.keys()
for name in union_enable_providers:
enable_provider(name)
##############################
# Actual work happens here
##############################
include_user_tilt_files()
load_provider_tiltfiles()
if settings.get("deploy_cert_manager"):
deploy_cert_manager()
enable_providers()