Custom resources and any other kind¶
This page shows how to declare objects the typed App has no call for, such
as an operator’s custom resources (a cert-manager Certificate, a Prometheus
ServiceMonitor), with a typed spec generated from the operator’s
CustomResourceDefinition (CRD). The spec is validated when you declare it,
renders with the Kubernetes field names and is planned, applied, pruned and
rolled back like every other object of the app.
Maturity: preview
app.resource(...) and piceli codegen crd are new in 0.7.0. Names and
options may still change before 1.0; the generated modules depend only on
pydantic.
Prerequisites¶
Piceli 0.7.0 or later.
The operator’s CRD: its manifest file (operators publish one per release), or a cluster where it is installed and a kubeconfig context that may read
customresourcedefinitions.The operator and its CRDs are installed outside the app. An app never creates, changes or deletes a CRD, a Namespace or a PersistentVolume.
Steps¶
Generate the models. From the CRD file:
piceli codegen crd cert-manager.crds.yaml --crd certificates.cert-manager.io \ --out deploy/crds/certificate.pyor from a cluster, through an explicit kubeconfig and context (one read of the CRD, nothing is written to the cluster):
piceli codegen crd --from-cluster --kubeconfig cluster.kubeconfig \ --context my-cluster --crd servicemonitors.monitoring.coreos.com \ --out deploy/crds/service_monitor.py--crdtakes the CRD’s name (plural.group) or its kind; it is needed when the file holds several CRDs.--versionpicks a version (default: the storage version). Without--outthe module is printed.Declare the resource. The generated module has
API_VERSION,KINDandSCOPEconstants and a<Kind>Specmodel:tls = app.resource( certificate.API_VERSION, certificate.KIND, "shop-tls", certificate.CertificateSpec( secret_name="shop-tls", dns_names=["shop.dev.example.com"], issuer_ref={"name": "letsencrypt", "kind": "ClusterIssuer"}, ), ) monitor = app.resource( service_monitor.API_VERSION, service_monitor.KIND, "api", service_monitor.ServiceMonitorSpec( selector={"matchLabels": api.selector_labels}, endpoints=[{"port": "http", "path": "/metrics", "interval": "30s"}], ), component="monitoring", )
Fields are snake_case (
secret_name); the Kubernetes names (secretName) are accepted too, and nested objects can be models or mappings. A misspelt field, a wrong enum value or a missing required field raises a pydanticValidationErrorat declaration.Render and release it like any app:
piceli render deploy/app.py:app, thenpiceli release planorpiceli deploy --plan(see Describe an app in typed Python and Deploy an app from source).
Expected output¶
piceli codegen crd --out FILE prints one JSON summary on stdout:
{"api_version": "cert-manager.io/v1", "classes": 15, "crd": "certificates.cert-manager.io",
"generator": "piceli-codegen-crd/1", "kind": "Certificate", "out": "deploy/crds/certificate.py",
"schema_sha256": "sha256:…", "scope": "namespaced", "source_sha256": "sha256:…",
"spec_class": "CertificateSpec", "state": "generated", "version": "v1"}
The declared Certificate renders as:
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
labels:
app.kubernetes.io/part-of: shop
name: shop-tls
namespace: shop-prod
spec:
dnsNames:
- shop.example.com
issuerRef:
kind: ClusterIssuer
name: letsencrypt
secretName: shop-tls
Only the fields you set are rendered. Schema defaults are not copied into the model: the API server applies them, so Piceli never claims ownership of fields you did not declare.
If it fails¶
Reason |
Meaning |
Fix |
|---|---|---|
|
Not an |
Pass the CRD manifest the operator publishes; check |
|
|
Pass |
|
|
Choose another path, or |
|
The API server refused or failed the read |
Check the context’s permission to get CRDs. |
|
The declared scope contradicts the server’s discovery |
Use the scope the CRD states (the generated |
|
A field whose name looks like a secret holds a literal value |
Put the value in a Secret and reference it, or declare the field |
The generated module¶
piceli codegen crd is a small generator for Kubernetes structural
schemas (every CRD version served by apiextensions.k8s.io/v1 has one), not
a general OpenAPI tool:
Deterministic. Properties are sorted and class names come from the field path (
CertificateSpecPrivateKey), with no timestamps or tool versions, so the same schema always generates the same file, from a file or from a cluster.SCHEMA_SHA256identifies the schema;GENERATORthe output format. Regenerate after upgrading the operator and review the diff.Kubernetes extensions.
x-kubernetes-int-or-stringbecomesint | str;x-kubernetes-preserve-unknown-fieldsobjects accept unknown fields (others refuse them); maps becomedict[str, …];nullablefields acceptNone; enums becomeLiteral[...];minimum,maximum,minLength,maxLength,pattern,minItemsandmaxItemsbecome field constraints (a pattern Python cannot compile is left out).Names. A field named like a Python keyword gets a trailing underscore (
class_); one in pydantic’smodel_namespace gets anf_prefix; the Kubernetes name is always its alias.Formatting. The module starts with
# ruff: noqa; keep generated files out of your formatter (asextend-exclude), so regenerating stays a clean diff.No new dependency. The module imports only pydantic (and the standard library); Piceli needs no extra for code generation. General JSON-schema generators do not know the Kubernetes extensions above and their output changes between versions, so Piceli ships its own.
Rules the model enforces¶
Scope¶
app.resource(api_version, kind, name, spec, *, fields=None, scope=None, public=(), labels=None, annotations=None, component=None) declares one
object:
scopedefaults to the generated spec’sSCOPE, toclusterfor built-in cluster-scoped kinds (PriorityClass,StorageClass, webhooks, …), and tonamespacedotherwise. At plan time the release compares it with the API server’s discovery and refuses a contradiction (resource-scope-mismatch).A generated spec also checks its identity: using a
CertificateSpecwith anotherkindorapiVersionis aValidationError.fieldssets other top-level keys for kinds whose body is not underspec(fields={"value": 1000}for a PriorityClass).apiVersion,kind,metadata,specandstatuscannot be set there.
Cluster-scoped objects are per namespace¶
A cluster-scoped resource (a ClusterIssuer, a PriorityClass) follows the
rules of the app’s ClusterRoles (see Describe an app in typed Python):
it renders without a namespace and annotated
piceli.io/namespace: <release namespace>;only the release in that namespace (with its owner) manages, changes or prunes it; an object of the same name managed from another namespace is refused with
resource-requires-adoptionuntil adopted explicitly;plans flag it with
"cluster_scoped": true([cluster-scoped]in the text);Namespace,CustomResourceDefinitionandPersistentVolumeare refused: they belong to the cluster’s administration.
Names are not prefixed: when the same app is deployed to several namespaces of one cluster, give each namespace’s cluster-scoped objects their own names (for example with an environment override).
Secret references and redacted fields¶
Plans never show secret values: fields whose names look sensitive
(password, token, secret, privateKey, …) are redacted, and a release
refuses to apply a redacted literal. References are recognized and shown:
secretName and other *Name/*File/*Path strings, {name, key}
selectors (bearerTokenSecret, passwordSecretRef) and metadata templates
(secretTemplate). A field that looks sensitive but holds no secret material,
such as a Certificate’s privateKey settings, is declared with public=:
app.resource(
certificate.API_VERSION,
certificate.KIND,
"shop-tls",
certificate.CertificateSpec(
secret_name="shop-tls",
issuer_ref={"name": "letsencrypt", "kind": "ClusterIssuer"},
private_key={"algorithm": "ECDSA", "size": 256},
),
public=["spec.privateKey"],
)
public renders as the piceli.io/public-fields annotation, so a reviewer
sees it in the plan. It never applies to a Secret’s data. Real secret values
belong in app.secret(...) and are referenced by name.
Readiness¶
A release waits for each object to be ready. A custom resource follows the
common status conventions: it is not ready while its observedGeneration is
behind its generation, while a Reconciling or Stalled condition is
True, or while a Ready condition is not True; one whose controller
reports none of these is ready once applied. A Certificate that cannot be
issued therefore fails the release after readiness_seconds, like a
Deployment that never becomes ready. The same rule applies to every kind
without a specific one; see When an object is ready.
Overrides and environments¶
app.override(resource, patch) patches the rendered object like any other,
and an environment replaces a resource’s spec with
specs={"shop-tls": …} (validated into the same generated model).
piceli codegen crd contract¶
piceli codegen crd [FILE] [--crd NAME] [--version V] [--out FILE] [--force] [--json]
piceli codegen crd --from-cluster --kubeconfig F --context C --crd NAME
[--transport https|loopback-http] [--allow-exec] [--exec-sha256 S] [...]
Input |
A CRD file (YAML or JSON, several documents or a |
Output |
Without |
Side effects |
|
Retry |
Always safe: the output depends only on the schema. |
Approval |
None. |
Exit codes |
|
A complete example¶
examples/environments/ renders a Deployment, a Service, a ConfigMap, a
cert-manager Certificate and a Prometheus ServiceMonitor for three
environments. Its models in examples/environments/crds/ were generated
from the CRDs vendored in tests/fixtures/crds/ (cert-manager v1.16.2 and
prometheus-operator v0.78.2, Apache-2.0; each file records its source URL),
and a test regenerates them byte for byte.