Core Builders¶
Builder functions that convert spec models into Kubernetes manifest dictionaries.
Custom Resources¶
Use ResourceBuilder to create builders for custom Kubernetes resources (CRDs, operators, etc.).
How It Works¶
ResourceBuilder automatically:
- Routes
name,namespace,labels,annotationstometadata - Routes other fields to
spec(or top-level for special resources) - Transforms nested
KubeModelinstances to dicts via.to_dict() - Uses Pydantic field aliases for camelCase output
Parameters¶
| Parameter | Default | Description |
|---|---|---|
spec |
required | The specification model instance |
api_version |
required | Kubernetes API version (e.g., "v1", "apps/v1") |
kind |
required | Resource kind (e.g., "Service", "Deployment") |
include_spec |
True |
Set False for resources without a spec section (Namespace, ConfigMap, RBAC) |
top_level_fields |
None |
Field names placed at resource root instead of spec (e.g., {"rules"} for Role) |
skip_fields |
None |
Field names to skip (for manual handling) |
Basic Example¶
from k8smith import ResourceBuilder, KubeModel
from pydantic import Field
class MyCustomSpec(KubeModel):
name: str
namespace: str = "default"
replicas: int = 1
custom_field: str = Field(alias="customField")
def build_my_resource(spec: MyCustomSpec) -> dict:
return ResourceBuilder.build(spec, "example.com/v1", "MyResource")
# Usage
spec = MyCustomSpec(name="my-app", custom_field="value")
manifest = build_my_resource(spec)
Advanced: Resources Without spec Section¶
Some Kubernetes resources (Namespace, ConfigMap, RBAC) don't have a spec section:
def build_my_config(spec: MyConfigSpec) -> dict:
return ResourceBuilder.build(
spec, "v1", "ConfigMap",
include_spec=False,
top_level_fields={"data"},
)
ResourceBuilder
¶
Generic builder for Kubernetes resources.
Handles the common pattern of building K8s resources by: 1. Creating the base structure (apiVersion, kind, metadata, spec) 2. Routing fields to the appropriate location based on field name 3. Auto-transforming nested KubeModel instances to dicts
Examples:
Simple resource (all fields go to spec):
Metadata-only resource (no spec section):
Resource with top-level fields (RBAC):
>>> ResourceBuilder.build(spec, "rbac.authorization.k8s.io/v1", "Role",
... top_level_fields={"rules"})
Resource with custom field handling:
Source code in src/k8smith/core/builder.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
build(spec, api_version, kind, *, include_spec=True, top_level_fields=None, skip_fields=None)
classmethod
¶
Build a Kubernetes resource from a spec model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
KubeModel
|
The specification model (e.g., ServiceSpec, IngressSpec) |
required |
api_version
|
str
|
Kubernetes API version (e.g., "v1", "apps/v1") |
required |
kind
|
str
|
Resource kind (e.g., "Service", "Deployment") |
required |
include_spec
|
bool
|
Whether to include a spec section (False for Namespace) |
True
|
top_level_fields
|
set[str] | None
|
Field names that go at resource root, not in spec (e.g., {"rules"} for Role, {"roleRef", "subjects"} for RoleBinding) |
None
|
skip_fields
|
set[str] | None
|
Field names to skip (handled manually by the caller) |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Kubernetes resource as a dict ready for YAML serialization |
Source code in src/k8smith/core/builder.py
Workloads¶
build_deployment(spec)
¶
Build a Kubernetes Deployment resource.
Note: selector and template are handled manually because: 1. selector must be wrapped as {"matchLabels": ...} in output 2. selector labels must be merged into template.metadata.labels (Kubernetes requires pod labels to match the selector) 3. selector auto-generates from spec.name if not provided These cross-field dependencies can't be expressed in ResourceBuilder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
DeploymentSpec
|
Deployment specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes Deployment resource as a dict |
Example
from k8smith.core.models import ( ... Container, DeploymentSpec, PodSpec, PodTemplateSpec, ... ) spec = DeploymentSpec( ... name="web", ... namespace="production", ... replicas=3, ... template=PodTemplateSpec( ... spec=PodSpec( ... containers=[Container(name="web", image="nginx:1.25")] ... ) ... ), ... ) deployment = build_deployment(spec)
Source code in src/k8smith/core/deployment.py
build_statefulset(spec)
¶
Build a Kubernetes StatefulSet resource.
Note: selector and template are handled manually because: 1. selector must be wrapped as {"matchLabels": ...} in output 2. selector labels must be merged into template.metadata.labels (Kubernetes requires pod labels to match the selector) 3. selector auto-generates from spec.name if not provided These cross-field dependencies can't be expressed in ResourceBuilder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
StatefulSetSpec
|
StatefulSet specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes StatefulSet resource as a dict |
Source code in src/k8smith/core/statefulset.py
build_daemonset(spec)
¶
Build a Kubernetes DaemonSet resource.
Note: selector and template are handled manually because: 1. selector must be wrapped as {"matchLabels": ...} in output 2. selector labels must be merged into template.metadata.labels (Kubernetes requires pod labels to match the selector) 3. selector auto-generates from spec.name if not provided These cross-field dependencies can't be expressed in ResourceBuilder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
DaemonSetSpec
|
DaemonSet specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes DaemonSet resource as a dict |
Source code in src/k8smith/core/daemonset.py
build_cronjob(spec)
¶
Build a Kubernetes CronJob resource.
Note: job_template is handled manually because Kubernetes expects the nested structure spec.jobTemplate.spec.template, which can't be expressed through ResourceBuilder's simple field routing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
CronJobSpec
|
CronJob specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes CronJob resource as a dict |
Source code in src/k8smith/core/cronjob.py
Services¶
build_service(spec)
¶
Build a Kubernetes Service resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
ServiceSpec
|
Service specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes Service resource as a dict |
Configuration¶
build_configmap(spec)
¶
Build a Kubernetes ConfigMap resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
ConfigMapSpec
|
ConfigMap specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes ConfigMap resource as a dict |
Source code in src/k8smith/core/configmap.py
build_secret(spec)
¶
Build a Kubernetes Secret resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
SecretSpec
|
Secret specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes Secret resource as a dict |
Source code in src/k8smith/core/secret.py
Scaling & Availability¶
build_hpa(spec)
¶
Build a Kubernetes HorizontalPodAutoscaler resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
HPASpec
|
HPA specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes HorizontalPodAutoscaler resource as a dict |
Source code in src/k8smith/core/hpa.py
build_pdb(spec)
¶
Build a Kubernetes PodDisruptionBudget resource.
Note: selector is handled manually because it must be wrapped as {"matchLabels": selector} in the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
PDBSpec
|
PDB specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes PodDisruptionBudget resource as a dict |
Source code in src/k8smith/core/pdb.py
Identity¶
build_serviceaccount(spec)
¶
Build a Kubernetes ServiceAccount resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
ServiceAccountSpec
|
ServiceAccount specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes ServiceAccount resource as a dict |
Source code in src/k8smith/core/serviceaccount.py
build_namespace(spec)
¶
Build a Kubernetes Namespace resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
NamespaceSpec
|
Namespace specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes Namespace resource as a dict |
Source code in src/k8smith/core/namespace.py
RBAC¶
build_role(spec)
¶
Build a Kubernetes Role resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
RoleSpec
|
Role specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes Role resource as a dict |
Example
from k8smith.core.models import RoleSpec, PolicyRule spec = RoleSpec( ... name="pod-reader", ... namespace="production", ... rules=[ ... PolicyRule(api_groups=[""], resources=["pods"], verbs=["get", "list", "watch"]), ... ], ... ) role = build_role(spec)
Source code in src/k8smith/core/rbac.py
build_clusterrole(spec)
¶
Build a Kubernetes ClusterRole resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
ClusterRoleSpec
|
ClusterRole specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes ClusterRole resource as a dict |
Example
from k8smith.core.models import ClusterRoleSpec, PolicyRule spec = ClusterRoleSpec( ... name="node-reader", ... rules=[ ... PolicyRule( ... api_groups=[""], ... resources=["nodes"], ... verbs=["get", "list", "watch"], ... ), ... ], ... ) cluster_role = build_clusterrole(spec)
Source code in src/k8smith/core/rbac.py
build_rolebinding(spec)
¶
Build a Kubernetes RoleBinding resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
RoleBindingSpec
|
RoleBinding specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes RoleBinding resource as a dict |
Example
from k8smith.core.models import RoleBindingSpec, RoleBindingSubject, RoleRef spec = RoleBindingSpec( ... name="read-pods", ... namespace="production", ... subjects=[ ... RoleBindingSubject(kind="ServiceAccount", name="my-sa", namespace="production"), ... ], ... role_ref=RoleRef(kind="Role", name="pod-reader"), ... ) binding = build_rolebinding(spec)
Source code in src/k8smith/core/rbac.py
build_clusterrolebinding(spec)
¶
Build a Kubernetes ClusterRoleBinding resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
ClusterRoleBindingSpec
|
ClusterRoleBinding specification |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Kubernetes ClusterRoleBinding resource as a dict |
Example
from k8smith.core.models import ClusterRoleBindingSpec, RoleBindingSubject, RoleRef spec = ClusterRoleBindingSpec( ... name="read-nodes-global", ... subjects=[ ... RoleBindingSubject( ... kind="ServiceAccount", ... name="monitoring", ... namespace="kube-system", ... ), ... ], ... role_ref=RoleRef(kind="ClusterRole", name="node-reader"), ... ) binding = build_clusterrolebinding(spec)