@@ -114,10 +114,10 @@ default context.
114
114
115
115
## Simple templates
116
116
117
- To use docker images you most likely will need some templating. This is because
118
- your docker image tags will be probably versioned . The simplest way to
119
- introduce templating is to use ` python_format_string ` target that will generate
120
- ` k8s_sources ` based on values you provide in the BUILD file.
117
+ At some point, you may need to inject variables from a BUILD file into
118
+ Kubernetes resources — also known as templating . The simplest way to achieve
119
+ this is by using the ` python_format_string ` target, which generates
120
+ ` k8s_sources ` by substituting the values you specify in the BUILD file.
121
121
122
122
First, add the codegen backend:
123
123
@@ -131,7 +131,8 @@ backend_packages = [
131
131
]
132
132
```
133
133
134
- Then parametrize the yaml using python format string syntax:
134
+ Then parametrize the yaml using python format string syntax, e.g. the
135
+ namespace:
135
136
136
137
``` yaml title="src/k8s/webpages.yaml
137
138
---
@@ -169,3 +170,51 @@ Now you can deploy the bundle:
169
170
` ` ` bash
170
171
pants experimental-deploy src/k8s/:webpages
171
172
` ` `
173
+
174
+ This setup can now be used to deploy the same resource to multiple namespaces
175
+ with [`parametrize`
176
+ builtin](../using-pants/key-concepts/targets-and-build-files.mdx#parametrizing-targets):
177
+
178
+ ` ` ` python title="src/k8s/BUILD"
179
+ python_format_string(
180
+ name="webpages-template",
181
+ source="webpages.yaml",
182
+ **parametrize("default", values={"namespace": "default"}),
183
+ **parametrize("web", values={"namespace": "web"}),
184
+ )
185
+ k8s_bundle(
186
+ name="webpages",
187
+ context="kind-kind",
188
+ **parametrize("default", sources=("src/k8s:webpages-template@parametrize=web",)),
189
+ **parametrize("web", sources=("src/k8s:webpages-template@parametrize=web",)),
190
+ )
191
+ ` ` `
192
+
193
+ # # Docker images
194
+
195
+ To use docker images you most likely will need some templating. This is because
196
+ your docker image tags will be probably versioned.
197
+
198
+ You might want to use git to generate the version for your images, but you
199
+ can't directly run git commands in a BUILD file. You can use a
200
+ `.pants.bootstrap` file as a workaround :
201
+
202
+ ` ` ` python title=".pants.bootstrap"
203
+ #!/bin/sh
204
+
205
+ VERSION="${VERSION:-$(git describe --tags --dirty --match "[0-9\. ]*" || echo 0.0.1)}"
206
+ export VERSION
207
+ ` ` `
208
+
209
+ This script will run before every pants command, so you can now use the
210
+ `VERSION` env var in the BUILD file :
211
+
212
+ ` ` ` python title="src/k8s/BUILD"
213
+ docker_image(
214
+ name="nginx",
215
+ instructions=["FROM nginx"],
216
+ image_tags=[env("VERSION")],
217
+ )
218
+ # TODO
219
+ ` ` `
220
+
0 commit comments