provisioning/docs/book/development/KCL_MODULE_GUIDE.html
Jesús Pérez 6a59d34bb1
chore: update provisioning configuration and documentation
Update configuration files, templates, and internal documentation
for the provisioning repository system.

Configuration Updates:
- KMS configuration modernization
- Plugin system settings
- Service port mappings
- Test cluster topologies
- Installation configuration examples
- VM configuration defaults
- Cedar authorization policies

Documentation Updates:
- Library module documentation
- Extension API guides
- AI system documentation
- Service management guides
- Test environment setup
- Plugin usage guides
- Validator configuration documentation

All changes are backward compatible.
2025-12-11 21:50:42 +00:00

462 lines
22 KiB
HTML

<!DOCTYPE HTML>
<html lang="en" class="ayu sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>KCL Module Guide - Provisioning Platform Documentation</title>
<!-- Custom HTML head -->
<meta name="description" content="Complete documentation for the Provisioning Platform - Infrastructure automation with Nushell, KCL, and Rust">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" id="highlight-css" href="../highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="../tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root and default themes to javascript -->
<script>
const path_to_root = "../";
const default_light_theme = "ayu";
const default_dark_theme = "navy";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="mdbook-help-container">
<div id="mdbook-help-popup">
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
<div>
<p>Press <kbd></kbd> or <kbd></kbd> to navigate between chapters</p>
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
<p>Press <kbd>?</kbd> to show this help</p>
<p>Press <kbd>Esc</kbd> to hide this help</p>
</div>
</div>
</div>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('ayu')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Provisioning Platform Documentation</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/provisioning/provisioning-platform" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/provisioning/provisioning-platform/edit/main/provisioning/docs/src/development/KCL_MODULE_GUIDE.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="kcl-module-organization-guide"><a class="header" href="#kcl-module-organization-guide">KCL Module Organization Guide</a></h1>
<p>This guide explains how to organize KCL modules and create extensions for the provisioning system.</p>
<h2 id="module-structure-overview"><a class="header" href="#module-structure-overview">Module Structure Overview</a></h2>
<pre><code>provisioning/
├── kcl/ # Core provisioning schemas
│ ├── settings.k # Main Settings schema
│ ├── defaults.k # Default configurations
│ └── main.k # Module entry point
├── extensions/
│ ├── kcl/ # KCL expects modules here
│ │ └── provisioning/0.0.1/ # Auto-generated from provisioning/kcl/
│ ├── providers/ # Cloud providers
│ │ ├── upcloud/kcl/
│ │ ├── aws/kcl/
│ │ └── local/kcl/
│ ├── taskservs/ # Infrastructure services
│ │ ├── kubernetes/kcl/
│ │ ├── cilium/kcl/
│ │ ├── redis/kcl/ # Our example
│ │ └── {service}/kcl/
│ └── clusters/ # Complete cluster definitions
└── config/ # TOML configuration files
workspace/
└── infra/
└── {your-infra}/ # Your infrastructure workspace
├── kcl.mod # Module dependencies
├── settings.k # Infrastructure settings
├── task-servs/ # Taskserver configurations
└── clusters/ # Cluster configurations
</code></pre>
<h2 id="import-path-conventions"><a class="header" href="#import-path-conventions">Import Path Conventions</a></h2>
<h3 id="1-core-provisioning-schemas"><a class="header" href="#1-core-provisioning-schemas">1. Core Provisioning Schemas</a></h3>
<pre><code class="language-kcl"># Import main provisioning schemas
import provisioning
# Use Settings schema
_settings = provisioning.Settings {
main_name = "my-infra"
# ... other settings
}
</code></pre>
<h3 id="2-taskserver-schemas"><a class="header" href="#2-taskserver-schemas">2. Taskserver Schemas</a></h3>
<pre><code class="language-kcl"># Import specific taskserver
import taskservs.{service}.kcl.{service} as {service}_schema
# Examples:
import taskservs.kubernetes.kcl.kubernetes as k8s_schema
import taskservs.cilium.kcl.cilium as cilium_schema
import taskservs.redis.kcl.redis as redis_schema
# Use the schema
_taskserv = redis_schema.Redis {
version = "7.2.3"
port = 6379
}
</code></pre>
<h3 id="3-provider-schemas"><a class="header" href="#3-provider-schemas">3. Provider Schemas</a></h3>
<pre><code class="language-kcl"># Import cloud provider schemas
import {provider}_prov.{provider} as {provider}_schema
# Examples:
import upcloud_prov.upcloud as upcloud_schema
import aws_prov.aws as aws_schema
</code></pre>
<h3 id="4-cluster-schemas"><a class="header" href="#4-cluster-schemas">4. Cluster Schemas</a></h3>
<pre><code class="language-kcl"># Import cluster definitions
import cluster.{cluster_name} as {cluster}_schema
</code></pre>
<h2 id="kcl-module-resolution-issues--solutions"><a class="header" href="#kcl-module-resolution-issues--solutions">KCL Module Resolution Issues &amp; Solutions</a></h2>
<h3 id="problem-path-resolution"><a class="header" href="#problem-path-resolution">Problem: Path Resolution</a></h3>
<p>KCL ignores the actual <code>path</code> in <code>kcl.mod</code> and uses convention-based resolution.</p>
<p><strong>What you write in kcl.mod:</strong></p>
<pre><code class="language-toml">[dependencies]
provisioning = { path = "../../../provisioning/kcl", version = "0.0.1" }
</code></pre>
<p><strong>Where KCL actually looks:</strong></p>
<pre><code>/provisioning/extensions/kcl/provisioning/0.0.1/
</code></pre>
<h3 id="solutions"><a class="header" href="#solutions">Solutions:</a></h3>
<h4 id="solution-1-use-expected-structure-recommended"><a class="header" href="#solution-1-use-expected-structure-recommended">Solution 1: Use Expected Structure (Recommended)</a></h4>
<p>Copy your KCL modules to where KCL expects them:</p>
<pre><code class="language-bash">mkdir -p provisioning/extensions/kcl/provisioning/0.0.1
cp -r provisioning/kcl/* provisioning/extensions/kcl/provisioning/0.0.1/
</code></pre>
<h4 id="solution-2-workspace-local-copies"><a class="header" href="#solution-2-workspace-local-copies">Solution 2: Workspace-Local Copies</a></h4>
<p>For development workspaces, copy modules locally:</p>
<pre><code class="language-bash">cp -r ../../../provisioning/kcl workspace/infra/wuji/provisioning
</code></pre>
<h4 id="solution-3-direct-file-imports-limited"><a class="header" href="#solution-3-direct-file-imports-limited">Solution 3: Direct File Imports (Limited)</a></h4>
<p>For simple cases, import files directly:</p>
<pre><code class="language-bash">kcl run ../../../provisioning/kcl/settings.k
</code></pre>
<h2 id="creating-new-taskservers"><a class="header" href="#creating-new-taskservers">Creating New Taskservers</a></h2>
<h3 id="directory-structure"><a class="header" href="#directory-structure">Directory Structure</a></h3>
<pre><code>provisioning/extensions/taskservs/{service}/
├── kcl/
│ ├── kcl.mod # Module definition
│ ├── {service}.k # KCL schema
│ └── dependencies.k # Optional dependencies
├── default/
│ ├── install-{service}.sh # Installation script
│ └── env-{service}.j2 # Environment template
└── README.md # Documentation
</code></pre>
<h3 id="kcl-schema-template-servicek"><a class="header" href="#kcl-schema-template-servicek">KCL Schema Template (<code>{service}.k</code>)</a></h3>
<pre><code class="language-kcl"># Info: {Service} KCL schemas for provisioning
# Author: Your Name
# Release: 0.0.1
schema {Service}:
"""
{Service} configuration schema for infrastructure provisioning
"""
name: str = "{service}"
version: str
# Service-specific configuration
port: int = {default_port}
# Add your configuration options here
# Validation
check:
port &gt; 0 and port &lt; 65536, "Port must be between 1 and 65535"
len(version) &gt; 0, "Version must be specified"
</code></pre>
<h3 id="module-configuration-kclmod"><a class="header" href="#module-configuration-kclmod">Module Configuration (<code>kcl.mod</code>)</a></h3>
<pre><code class="language-toml">[package]
name = "{service}"
edition = "v0.11.2"
version = "0.0.1"
[dependencies]
provisioning = { path = "../../../kcl", version = "0.0.1" }
taskservs = { path = "../..", version = "0.0.1" }
</code></pre>
<h3 id="usage-in-workspace"><a class="header" href="#usage-in-workspace">Usage in Workspace</a></h3>
<pre><code class="language-kcl"># In workspace/infra/{your-infra}/task-servs/{service}.k
import taskservs.{service}.kcl.{service} as {service}_schema
_taskserv = {service}_schema.{Service} {
version = "1.0.0"
port = {port}
# ... your configuration
}
_taskserv
</code></pre>
<h2 id="workspace-setup"><a class="header" href="#workspace-setup">Workspace Setup</a></h2>
<h3 id="1-create-workspace-directory"><a class="header" href="#1-create-workspace-directory">1. Create Workspace Directory</a></h3>
<pre><code class="language-bash">mkdir -p workspace/infra/{your-infra}/{task-servs,clusters,defs}
</code></pre>
<h3 id="2-create-kclmod"><a class="header" href="#2-create-kclmod">2. Create kcl.mod</a></h3>
<pre><code class="language-toml">[package]
name = "{your-infra}"
edition = "v0.11.2"
version = "0.0.1"
[dependencies]
provisioning = { path = "../../../provisioning/kcl", version = "0.0.1" }
taskservs = { path = "../../../provisioning/extensions/taskservs", version = "0.0.1" }
cluster = { path = "../../../provisioning/extensions/cluster", version = "0.0.1" }
upcloud_prov = { path = "../../../provisioning/extensions/providers/upcloud/kcl", version = "0.0.1" }
</code></pre>
<h3 id="3-create-settingsk"><a class="header" href="#3-create-settingsk">3. Create settings.k</a></h3>
<pre><code class="language-kcl">import provisioning
_settings = provisioning.Settings {
main_name = "{your-infra}"
main_title = "{Your Infrastructure Title}"
# ... other settings
}
_settings
</code></pre>
<h3 id="4-test-configuration"><a class="header" href="#4-test-configuration">4. Test Configuration</a></h3>
<pre><code class="language-bash">cd workspace/infra/{your-infra}
kcl run settings.k
</code></pre>
<h2 id="common-patterns"><a class="header" href="#common-patterns">Common Patterns</a></h2>
<h3 id="boolean-values"><a class="header" href="#boolean-values">Boolean Values</a></h3>
<p>Use <code>True</code> and <code>False</code> (capitalized) in KCL:</p>
<pre><code class="language-kcl">enabled: bool = True
disabled: bool = False
</code></pre>
<h3 id="optional-fields"><a class="header" href="#optional-fields">Optional Fields</a></h3>
<p>Use <code>?</code> for optional fields:</p>
<pre><code class="language-kcl">optional_field?: str
</code></pre>
<h3 id="union-types"><a class="header" href="#union-types">Union Types</a></h3>
<p>Use <code>|</code> for multiple allowed types:</p>
<pre><code class="language-kcl">log_level: "debug" | "info" | "warn" | "error" = "info"
</code></pre>
<h3 id="validation"><a class="header" href="#validation">Validation</a></h3>
<p>Add validation rules:</p>
<pre><code class="language-kcl">check:
port &gt; 0 and port &lt; 65536, "Port must be valid"
len(name) &gt; 0, "Name cannot be empty"
</code></pre>
<h2 id="testing-your-extensions"><a class="header" href="#testing-your-extensions">Testing Your Extensions</a></h2>
<h3 id="test-kcl-schema"><a class="header" href="#test-kcl-schema">Test KCL Schema</a></h3>
<pre><code class="language-bash">cd workspace/infra/{your-infra}
kcl run task-servs/{service}.k
</code></pre>
<h3 id="test-with-provisioning-system"><a class="header" href="#test-with-provisioning-system">Test with Provisioning System</a></h3>
<pre><code class="language-bash">provisioning -c -i {your-infra} taskserv create {service}
</code></pre>
<h2 id="best-practices"><a class="header" href="#best-practices">Best Practices</a></h2>
<ol>
<li><strong>Use descriptive schema names</strong>: <code>Redis</code>, <code>Kubernetes</code>, not <code>redis</code>, <code>k8s</code></li>
<li><strong>Add comprehensive validation</strong>: Check ports, required fields, etc.</li>
<li><strong>Provide sensible defaults</strong>: Make configuration easy to use</li>
<li><strong>Document all options</strong>: Use docstrings and comments</li>
<li><strong>Follow naming conventions</strong>: Use snake_case for fields, PascalCase for schemas</li>
<li><strong>Test thoroughly</strong>: Verify schemas work in workspaces</li>
<li><strong>Version properly</strong>: Use semantic versioning for modules</li>
<li><strong>Keep schemas focused</strong>: One service per schema file</li>
</ol>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../development/workspace-management.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../development/kcl/KCL_QUICK_REFERENCE.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../development/workspace-management.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../development/kcl/KCL_QUICK_REFERENCE.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>