Kubernetes Overview
Pants is currently building support for Kubernetes. Simple use cases might be supported, but many options are missing.
Please share feedback for what you need to use Pants with your Kubernetes queries by either opening a GitHub issue or joining our Slack!
Initial setupâ
First, activate the relevant backend in pants.toml
:
[GLOBAL]
backend_packages = [
...
"pants.backend.experimental.k8s",
]
The Kubernetes backend adds k8s_source
and
k8s_sources
target types for Kubernetes object
files.
For example, create a file src/k8s/webpages.yaml
:
---
apiVersion: v1
kind: ConfigMap
metadata:
name: webpages
data:
index.html: |
<html>
<head>Hello pants!</head>
<body>Hello pants!</body>
</html>
Now add a k8s_sources
target in src/k8s/BUILD
:
k8s_sources()
Deploying objects to a clusterâ
We'll be using a local kind cluster throughout the tutorial. First, spin up a cluster:
kind create cluster
Creating cluster "kind" ...
â Ensuring node image (kindest/node:v1.25.3) đŧ
â Preparing nodes đĻ
â Writing configuration đ
â Starting control-plane đšī¸
â Installing CNI đ
â Installing StorageClass đž
Set kubectl context to "kind-kind"
Second, configure the list of available contexts in pants.toml
:
...
[k8s]
available_contexts = [
"kind-kind",
]
Third, create a deployable target k8s_bundle
in src/k8s/BUILD
:
k8s_sources()
k8s_bundle(
name="webpages",
sources=("src/k8s/webpages.yaml",),
context="kind-kind",
)
Now you can deploy the target:
pants experimental-deploy src/k8s:webpages
â src/k8s:webpages deployed to context kind-kind
To prevent accidentally deploying kubernetes manifests to the wrong cluster,
the context field is required on k8s_bundle
for deployment. For deploying the
same k8s_bundle
to multiple contexts, consider using parametrize
builtin
like k8s_bundle(..., context=parametrize("stage", "prod"))
. For CI agents
which will only have access to a single context, set the
[kubectl].pass_context
to false in pants.toml
to have them use their
default context.