Skip to content

Commit be65b84

Browse files
committed
Add docker image section
1 parent 5ed78ac commit be65b84

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

docs/docs/kubernetes/index.mdx

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ default context.
114114

115115
## Simple templates
116116

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.
121121

122122
First, add the codegen backend:
123123

@@ -131,7 +131,8 @@ backend_packages = [
131131
]
132132
```
133133

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:
135136

136137
```yaml title="src/k8s/webpages.yaml
137138
---
@@ -169,3 +170,51 @@ Now you can deploy the bundle:
169170
```bash
170171
pants experimental-deploy src/k8s/:webpages
171172
```
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

Comments
 (0)