Skip to content

Commit 2c02663

Browse files
bordeuxclaude
andcommitted
feat: add Kubernetes reference functions and update documentation
Add three new Kubernetes helper functions for generating YAML references: - k8s_env_var_ref: Generate ConfigMap or Secret environment variable references - k8s_secret_ref: Generate Secret references with optional flag support - k8s_configmap_ref: Generate ConfigMap references with optional flag support These functions simplify Kubernetes manifest generation by automating the creation of valueFrom references for environment variables. Also update README.md with comprehensive documentation including: - Web & URL Functions section with all 4 URL utility functions - Kubernetes Functions section with all 6 Kubernetes helper functions - Updated Table of Contents and Features list - Real-world examples for all functions Tests: - Add 19 comprehensive tests for Kubernetes reference functions - All tests passing (19/19 k8s ref, 34/34 URL) - Code formatted and clippy checks passing 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent f950ad3 commit 2c02663

5 files changed

Lines changed: 694 additions & 4 deletions

File tree

README.md

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ A fast and simple command-line template rendering tool using [MiniJinja](https:/
2929
- [Data Serialization Functions](#data-serialization-functions)
3030
- [Object Manipulation Functions](#object-manipulation-functions)
3131
- [Validation Functions](#validation-functions)
32+
- [System & Network Functions](#system--network-functions)
33+
- [Math Functions](#math-functions)
34+
- [Array Functions](#array-functions)
35+
- [Statistical Functions](#statistical-functions)
36+
- [Predicate Functions](#predicate-functions)
37+
- [Kubernetes Functions](#kubernetes-functions)
38+
- [Web & URL Functions](#web--url-functions)
39+
- [Logic Functions](#logic-functions)
3240
- [Debugging & Development Functions](#debugging--development-functions)
3341
- [Advanced Examples](#advanced-examples)
3442
- [Error Handling](#error-handling)
@@ -72,6 +80,10 @@ tmpltool greeting.tmpl
7280
- **Object Manipulation**: Deep merge, get/set nested values by path, extract keys/values, check key existence
7381
- **Validation**: Validate emails, URLs, IPs, UUIDs, regex matching
7482
- **System & Network**: Get hostname, username, directories, IP addresses, DNS resolution, port availability
83+
- **Web & URL**: Parse and build URLs, generate query strings, HTTP Basic Auth headers
84+
- **Kubernetes**: Resource requests, label sanitization, ConfigMap/Secret references for manifests
85+
- **Math & Logic**: Min/max, rounding, percentages, default values, ternary operations, range checks
86+
- **Array & Statistics**: Sorting, grouping, chunking, sum/avg/median, unique values, flattening
7587
- **Debugging & Development**: Debug output, type checking, assertions, warnings, error handling
7688
- **String Filters**: 12+ filters for case conversion, indentation, padding, quoting, and more
7789
- **Security**: Built-in protections with optional `--trust` mode
@@ -2669,6 +2681,264 @@ metadata:
26692681
name: {{ k8s_dns_label_safe(value=service_name) }}
26702682
```
26712683
2684+
#### `k8s_env_var_ref(var_name, source, name)`
2685+
2686+
Generate Kubernetes environment variable reference (ConfigMap or Secret).
2687+
2688+
**Arguments:**
2689+
- `var_name` (required): The environment variable name/key
2690+
- `source` (optional): Source type - `"configmap"` or `"secret"` (default: `"configmap"`)
2691+
- `name` (optional): Name of the ConfigMap/Secret (default: auto-generated from var_name)
2692+
2693+
**Returns:** YAML-formatted `valueFrom` reference
2694+
2695+
**Example:**
2696+
```jinja
2697+
{# ConfigMap reference #}
2698+
- name: DATABASE_HOST
2699+
{{ k8s_env_var_ref(var_name="DATABASE_HOST", source="configmap", name="app-config") | indent(2) }}
2700+
{# Output:
2701+
valueFrom:
2702+
configMapKeyRef:
2703+
name: app-config
2704+
key: DATABASE_HOST
2705+
#}
2706+
2707+
{# Secret reference with auto-generated name #}
2708+
- name: DB_PASSWORD
2709+
{{ k8s_env_var_ref(var_name="DB_PASSWORD", source="secret") | indent(2) }}
2710+
{# Output:
2711+
valueFrom:
2712+
secretKeyRef:
2713+
name: db-password
2714+
key: DB_PASSWORD
2715+
#}
2716+
```
2717+
2718+
#### `k8s_secret_ref(secret_name, key, optional)`
2719+
2720+
Generate Kubernetes Secret reference for environment variables.
2721+
2722+
**Arguments:**
2723+
- `secret_name` (required): Name of the Secret
2724+
- `key` (required): Key within the Secret
2725+
- `optional` (optional): Whether the Secret is optional (default: `false`)
2726+
2727+
**Returns:** YAML-formatted `valueFrom` secretKeyRef
2728+
2729+
**Example:**
2730+
```jinja
2731+
{# Basic secret reference #}
2732+
- name: DB_PASSWORD
2733+
{{ k8s_secret_ref(secret_name="db-credentials", key="password") | indent(2) }}
2734+
{# Output:
2735+
valueFrom:
2736+
secretKeyRef:
2737+
name: db-credentials
2738+
key: password
2739+
#}
2740+
2741+
{# Optional secret #}
2742+
- name: OPTIONAL_TOKEN
2743+
{{ k8s_secret_ref(secret_name="tokens", key="api_token", optional=true) | indent(2) }}
2744+
{# Output:
2745+
valueFrom:
2746+
secretKeyRef:
2747+
name: tokens
2748+
key: api_token
2749+
optional: true
2750+
#}
2751+
```
2752+
2753+
#### `k8s_configmap_ref(configmap_name, key, optional)`
2754+
2755+
Generate Kubernetes ConfigMap reference for environment variables.
2756+
2757+
**Arguments:**
2758+
- `configmap_name` (required): Name of the ConfigMap
2759+
- `key` (required): Key within the ConfigMap
2760+
- `optional` (optional): Whether the ConfigMap is optional (default: `false`)
2761+
2762+
**Returns:** YAML-formatted `valueFrom` configMapKeyRef
2763+
2764+
**Example:**
2765+
```jinja
2766+
{# Basic ConfigMap reference #}
2767+
- name: DATABASE_HOST
2768+
{{ k8s_configmap_ref(configmap_name="app-config", key="database_host") | indent(2) }}
2769+
{# Output:
2770+
valueFrom:
2771+
configMapKeyRef:
2772+
name: app-config
2773+
key: database_host
2774+
#}
2775+
2776+
{# Optional ConfigMap #}
2777+
- name: FEATURE_FLAG
2778+
{{ k8s_configmap_ref(configmap_name="features", key="new_ui", optional=true) | indent(2) }}
2779+
{# Output:
2780+
valueFrom:
2781+
configMapKeyRef:
2782+
name: features
2783+
key: new_ui
2784+
optional: true
2785+
#}
2786+
2787+
{# Complete deployment example #}
2788+
apiVersion: apps/v1
2789+
kind: Deployment
2790+
metadata:
2791+
name: {{ k8s_dns_label_safe(value=app_name) }}
2792+
spec:
2793+
template:
2794+
spec:
2795+
containers:
2796+
- name: app
2797+
image: myapp:latest
2798+
env:
2799+
- name: ENVIRONMENT
2800+
value: "production"
2801+
- name: DATABASE_HOST
2802+
{{ k8s_configmap_ref(configmap_name="app-config", key="db_host") | indent(14) }}
2803+
- name: DATABASE_PASSWORD
2804+
{{ k8s_secret_ref(secret_name="db-credentials", key="password") | indent(14) }}
2805+
resources:
2806+
{{ k8s_resource_request(cpu="500m", memory="512Mi") | indent(12) }}
2807+
```
2808+
2809+
### Web & URL Functions
2810+
2811+
URL manipulation and HTTP authentication helpers.
2812+
2813+
#### `basic_auth(username, password)`
2814+
2815+
Generate HTTP Basic Authentication header value.
2816+
2817+
**Arguments:**
2818+
- `username` (required): The username for authentication
2819+
- `password` (required): The password for authentication
2820+
2821+
**Returns:** Base64-encoded Basic authentication header value
2822+
2823+
**Example:**
2824+
```jinja
2825+
{# Generate Basic Auth header #}
2826+
Authorization: {{ basic_auth(username="admin", password="secret123") }}
2827+
{# Output: Authorization: Basic YWRtaW46c2VjcmV0MTIz #}
2828+
2829+
{# Use with environment variables #}
2830+
Authorization: {{ basic_auth(username=get_env(name="API_USER"), password=get_env(name="API_PASS")) }}
2831+
2832+
{# In nginx config #}
2833+
proxy_set_header Authorization "{{ basic_auth(username="api_user", password="api_key") }}";
2834+
```
2835+
2836+
#### `parse_url(url)`
2837+
2838+
Parse a URL into its component parts.
2839+
2840+
**Arguments:**
2841+
- `url` (required): The URL string to parse
2842+
2843+
**Returns:** Object with the following fields:
2844+
- `scheme`: URL scheme (http, https, etc.)
2845+
- `host`: Hostname
2846+
- `port`: Port number (or default for scheme)
2847+
- `path`: Path component
2848+
- `query`: Query string (without ?)
2849+
- `fragment`: Fragment/hash (without #)
2850+
- `username`: Username from URL (if present)
2851+
- `password`: Password from URL (if present)
2852+
2853+
**Example:**
2854+
```jinja
2855+
{# Parse URL #}
2856+
{% set url = parse_url(url="https://user:pass@api.example.com:8080/v1/users?limit=10#section") %}
2857+
Scheme: {{ url.scheme }}
2858+
Host: {{ url.host }}
2859+
Port: {{ url.port }}
2860+
Path: {{ url.path }}
2861+
Query: {{ url.query }}
2862+
{# Output:
2863+
Scheme: https
2864+
Host: api.example.com
2865+
Port: 8080
2866+
Path: /v1/users
2867+
Query: limit=10
2868+
#}
2869+
2870+
{# Extract host from environment variable #}
2871+
{% set db_url = parse_url(url=get_env(name="DATABASE_URL")) %}
2872+
DB_HOST={{ db_url.host }}
2873+
DB_PORT={{ db_url.port }}
2874+
DB_NAME={{ url.path | trim_start_matches(pat="/") }}
2875+
```
2876+
2877+
#### `build_url(scheme, host, port, path, query)`
2878+
2879+
Construct a URL from components.
2880+
2881+
**Arguments:**
2882+
- `scheme` (optional): URL scheme (default: `"https"`)
2883+
- `host` (required): Hostname
2884+
- `port` (optional): Port number
2885+
- `path` (optional): Path component (default: `"/"`)
2886+
- `query` (optional): Query string (string) or query parameters (object)
2887+
2888+
**Returns:** Constructed URL string
2889+
2890+
**Example:**
2891+
```jinja
2892+
{# Basic URL with defaults #}
2893+
{{ build_url(host="api.example.com") }}
2894+
{# Output: https://api.example.com/ #}
2895+
2896+
{# Full URL with all components #}
2897+
{{ build_url(scheme="http", host="localhost", port=8080, path="/api/v1", query="debug=true") }}
2898+
{# Output: http://localhost:8080/api/v1?debug=true #}
2899+
2900+
{# Query as object (auto-serialized) #}
2901+
{{ build_url(host="api.example.com", path="/search", query={"q": "jinja templates", "limit": 20}) }}
2902+
{# Output: https://api.example.com/search?q=jinja+templates&limit=20 #}
2903+
2904+
{# Build API endpoint from config #}
2905+
{% set api_url = build_url(
2906+
scheme="https",
2907+
host=get_env(name="API_HOST", default="api.example.com"),
2908+
port=get_env(name="API_PORT") | default(value=443),
2909+
path="/v2/data"
2910+
) %}
2911+
API_ENDPOINT={{ api_url }}
2912+
```
2913+
2914+
#### `query_string(params)`
2915+
2916+
Build a URL query string from an object.
2917+
2918+
**Arguments:**
2919+
- `params` (required): Object containing key-value pairs for the query string
2920+
2921+
**Returns:** URL-encoded query string (without leading `?`)
2922+
2923+
**Example:**
2924+
```jinja
2925+
{# Basic query string #}
2926+
{% set params = {"name": "John Doe", "age": 30, "city": "New York"} %}
2927+
{{ query_string(params=params) }}
2928+
{# Output: name=John+Doe&age=30&city=New+York #}
2929+
2930+
{# URL encoding for special characters #}
2931+
{% set search = {"q": "hello world", "filter": "type=user&active=true"} %}
2932+
?{{ query_string(params=search) }}
2933+
{# Output: ?q=hello+world&filter=type%3Duser%26active%3Dtrue #}
2934+
2935+
{# Build complete URL with query #}
2936+
{% set endpoint = "https://api.example.com/search" %}
2937+
{% set params = {"page": 1, "limit": 50, "sort": "created_at"} %}
2938+
{{ endpoint }}?{{ query_string(params=params) }}
2939+
{# Output: https://api.example.com/search?page=1&limit=50&sort=created_at #}
2940+
```
2941+
26722942
### Logic Functions
26732943
26742944
Conditional logic and default value handling.

TODO.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ This document contains ideas for new functions and features to make tmpltool mor
231231
- [x] `k8s_label_safe(string)` - Convert to Kubernetes-safe label
232232
- [x] `k8s_dns_label_safe(string)` - Convert to DNS-safe label (max 63 chars)
233233
- [x] `k8s_resource_request(cpu, memory)` - Format k8s resource request
234-
- [ ] `env_var_ref(var_name)` - Format environment variable reference
235-
- [ ] `secret_ref(secret_name, key)` - Format secret reference
236-
- [ ] `configmap_ref(cm_name, key)` - Format ConfigMap reference
234+
- [x] `k8s_env_var_ref(var_name, source, name)` - Format environment variable reference
235+
- [x] `k8s_secret_ref(secret_name, key, optional)` - Format secret reference
236+
- [x] `k8s_configmap_ref(configmap_name, key, optional)` - Format ConfigMap reference
237237

238238
### 🌐 Web & API Helpers
239239
*For nginx, apache, API configs*

0 commit comments

Comments
 (0)