Skip to content

Commit b426c8f

Browse files
authored
Merge pull request kubernetes-sigs#673 from jroden/master
✨ Spin flavor clusters as tilt resources
2 parents 6be4c90 + 6e3a83d commit b426c8f

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

Tiltfile

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,86 @@ def capz():
194194

195195
k8s_yaml(blob(yaml))
196196

197+
198+
# run worker clusters specified from 'tilt up' or in 'tilt_config.json'
199+
def flavors():
200+
config.define_string_list("templates-to-run", args=True)
201+
config.define_string_list("worker-flavors")
202+
cfg = config.parse()
203+
worker_templates = cfg.get('templates-to-run', [])
204+
for flavor in cfg.get("worker-flavors", []):
205+
if flavor not in worker_templates:
206+
worker_templates.append(flavor)
207+
for flavor in worker_templates:
208+
deploy_worker_templates(flavor)
209+
210+
211+
def deploy_worker_templates(flavor):
212+
# validate required metadata
213+
required_metadata = ["AZURE_SUBSCRIPTION_ID","AZURE_TENANT_ID","AZURE_CLIENT_ID","AZURE_CLIENT_SECRET", "AZURE_SSH_PUBLIC_KEY"]
214+
metadata = settings.get("kustomize_substitutions", {})
215+
missing = [k for k in required_metadata if k not in metadata]
216+
if missing:
217+
fail("missing kustomize_substitutions keys {} in tilt-setting.json".format(missing))
218+
219+
# validate flavor exists
220+
if flavor == "default":
221+
yaml_file = "./templates/cluster-template.yaml"
222+
else:
223+
yaml_file = "./templates/cluster-template-" + flavor + ".yaml"
224+
if not os.path.exists(yaml_file):
225+
fail(yaml_file + " not found")
226+
227+
yaml = str(read_file(yaml_file))
228+
229+
# azure account information replacements
230+
substitutions = settings.get("kustomize_substitutions", {})
231+
for substitution in substitutions:
232+
value = substitutions[substitution]
233+
yaml = yaml.replace("${" + substitution + "}", value)
234+
235+
# if metadata defined for worker-templates in tilt_settings
236+
if "worker-templates" in settings:
237+
# first priority replacements defined per template
238+
if "flavors" in settings.get("worker-templates", {}):
239+
substitutions = settings.get("worker-templates").get("flavors").get(flavor, {})
240+
for substitution in substitutions:
241+
value = substitutions[substitution]
242+
yaml = yaml.replace("${" + substitution + "}", value)
243+
244+
# second priority replacements defined common to templates
245+
if "metadata" in settings.get("worker-templates", {}):
246+
substitutions = settings.get("worker-templates").get("metadata", {})
247+
for substitution in substitutions:
248+
value = substitutions[substitution]
249+
yaml = yaml.replace("${" + substitution + "}", value)
250+
251+
# programmatically define any remaining vars
252+
substitutions = {
253+
"CLUSTER_NAME": flavor + "-template",
254+
"AZURE_LOCATION": "eastus",
255+
"AZURE_VNET_NAME": flavor + "-template-vnet",
256+
"AZURE_RESOURCE_GROUP": flavor + "-template-rg",
257+
"CONTROL_PLANE_MACHINE_COUNT": "1",
258+
"KUBERNETES_VERSION": "v1.18.3",
259+
"AZURE_CONTROL_PLANE_MACHINE_TYPE": "Standard_D2s_v3",
260+
"WORKER_MACHINE_COUNT": "2",
261+
"AZURE_NODE_MACHINE_TYPE": "Standard_D2s_v3",
262+
}
263+
for substitution in substitutions:
264+
value = substitutions[substitution]
265+
yaml = yaml.replace("${" + substitution + "}", value)
266+
267+
yaml = yaml.replace('"', '\\"') # add escape character to double quotes in yaml
268+
269+
local_resource(
270+
"worker-" + flavor,
271+
cmd = "make generate-flavors; echo \"" + yaml + "\" > ./.tiltbuild/worker-" + flavor + ".yaml; kubectl apply -f ./.tiltbuild/worker-" + flavor + ".yaml",
272+
auto_init = False,
273+
trigger_mode = TRIGGER_MODE_MANUAL
274+
)
275+
276+
197277
##############################
198278
# Actual work happens here
199279
##############################
@@ -208,3 +288,5 @@ if settings.get("deploy_cert_manager"):
208288
deploy_capi()
209289

210290
capz()
291+
292+
flavors()

docs/development.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [Tilt for dev in CAPZ](#tilt-for-dev-in-capz)
2121
- [Tilt for dev in both CAPZ and CAPI](#tilt-for-dev-in-both-capz-and-capi)
2222
- [Deploying a workload cluster](#deploying-a-workload-cluster)
23+
- [Deploying a flavor cluster as a local tilt resource](../templates/flavors/README.md#Running-flavor-clusters-as-a-tilt-resource)
2324
- [Manual Testing](#manual-testing)
2425
- [Creating a dev cluster](#creating-a-dev-cluster)
2526
- [Building and pushing dev images](#building-and-pushing-dev-images)
@@ -145,6 +146,8 @@ If you would like to enable these features, add `extra_args` as specified in [Th
145146

146147
Once your kind management cluster is up and running, you can [deploy a workload cluster](#deploying-a-workload-cluster).
147148

149+
You can also [deploy a flavor cluster as a local tilt resource](../templates/flavors/README.md#Running-flavor-clusters-as-a-tilt-resource).
150+
148151
To tear down the kind cluster built by the command above, just run:
149152

150153
```shell

templates/flavors/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,76 @@ appended to the end of the cluster-template.yaml, e.g cluster-template-{director
1414
flavor can be used by specifying `--flavor {directory-name}`.
1515

1616
To generate all CAPZ flavors, run `make generate-flavors`.
17+
18+
19+
## Running flavor clusters as a tilt resource
20+
21+
#### From CLI
22+
run ```tilt up ${flavors}``` to spin up worker clusters in Azure represented by a Tilt local resource. You can also modify flavors while Tilt is up by running ```tilt args ${flavors}```
23+
24+
#### From Tilt Config
25+
Add your desired flavors to tilt_config.json:
26+
```json
27+
{
28+
"worker-flavors": ["default", "aks", "external-cloud-provider", "machinepool", "system-assigned-identity", "user-assigned-identity"]
29+
}
30+
```
31+
32+
#### Requirements
33+
Please note your tilt-settings.json must contain at minimum the following fields when using tilt resources to deploy cluster flavors:
34+
```json
35+
{
36+
"kustomize_substitutions": {
37+
"AZURE_SUBSCRIPTION_ID_B64": "******",
38+
"AZURE_TENANT_ID_B64": "******",
39+
"AZURE_CLIENT_SECRET_B64": "******",
40+
"AZURE_CLIENT_ID_B64": "******",
41+
"AZURE_SUBSCRIPTION_ID": "******",
42+
"AZURE_TENANT_ID": "******",
43+
"AZURE_CLIENT_ID": "******",
44+
"AZURE_CLIENT_SECRET": "******",
45+
"AZURE_SSH_PUBLIC_KEY": "********"
46+
}
47+
}
48+
```
49+
50+
#### Defining Variable Overrides
51+
If you wish to override the default variables for flavor workers, you can specify them as part of your tilt-settings.json as seen in the example below. Please note, the precedence of variables is as follows:
52+
53+
1. explicitly defined vars for each flavor i.e. worker-templates.flavors[0].AZURE_VNET_NAME
54+
2. vars defined at 'metadata' level-- spans workers i.e. metadata.AZURE_VNET_NAME
55+
3. programmatically defined default vars i.e. everything except azure tenant, client, subscription
56+
57+
58+
```json
59+
{
60+
"kustomize_substitutions": {
61+
"AZURE_SUBSCRIPTION_ID_B64": "****",
62+
"AZURE_TENANT_ID_B64": "****",
63+
"AZURE_CLIENT_SECRET_B64": "****",
64+
"AZURE_CLIENT_ID_B64": "****"
65+
},
66+
"worker-templates": {
67+
"flavors": [{
68+
"default": {
69+
"CLUSTER_NAME": "example-default-cluster-name",
70+
"AZURE_VNET_NAME": "example-vnet-one"
71+
},
72+
"system-assigned-identity": {
73+
"CLUSTER_NAME": "example-SAI-cluster-name",
74+
"AZURE_LOCATION": "westus",
75+
"AZURE_VNET_NAME": "example-vnet-two"
76+
}
77+
}],
78+
"metadata": {
79+
"AZURE_LOCATION": "eastus",
80+
"AZURE_RESOURCE_GROUP": "test-resource-group-name",
81+
"CONTROL_PLANE_MACHINE_COUNT": "1",
82+
"KUBERNETES_VERSION": "v1.18.3",
83+
"AZURE_CONTROL_PLANE_MACHINE_TYPE": "Standard_D2s_v3",
84+
"WORKER_MACHINE_COUNT": "2",
85+
"AZURE_NODE_MACHINE_TYPE": "Standard_D2s_v3"
86+
}
87+
}
88+
}
89+
```

0 commit comments

Comments
 (0)