kcli-plan-authoring
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesekcli Plan Authoring
kcli计划文件编写
Plan File Structure
计划文件结构
Plans are YAML files with Jinja2 templating. Resources are defined as top-level keys with a field.
typeyaml
parameters:
param1: value1
param2: value2
resourcename:
type: resourcetype
key1: value1
key2: {{ param1 }}计划文件是带有Jinja2模板的YAML文件。资源以顶级键的形式定义,并包含字段。
typeyaml
parameters:
param1: value1
param2: value2
resourcename:
type: resourcetype
key1: value1
key2: {{ param1 }}Resource Types
资源类型
VM (default if no type specified)
VM(未指定type时的默认类型)
yaml
myvm:
image: fedora40
memory: 4096
numcpus: 2
disks:
- size: 20
- size: 10
pool: otherpool
nets:
- name: default
- name: mynet
ip: 192.168.1.10
netmask: 255.255.255.0
gateway: 192.168.1.1
cmds:
- echo hello > /tmp/test
files:
- path: /etc/myconfig
content: |
key=valueyaml
myvm:
image: fedora40
memory: 4096
numcpus: 2
disks:
- size: 20
- size: 10
pool: otherpool
nets:
- name: default
- name: mynet
ip: 192.168.1.10
netmask: 255.255.255.0
gateway: 192.168.1.1
cmds:
- echo hello > /tmp/test
files:
- path: /etc/myconfig
content: |
key=valueProfile
Profile
yaml
myprofile:
type: profile
image: centos9stream
memory: 2048
numcpus: 2
disks:
- 10
nets:
- defaultyaml
myprofile:
type: profile
image: centos9stream
memory: 2048
numcpus: 2
disks:
- 10
nets:
- defaultNetwork
Network
yaml
mynetwork:
type: network
cidr: 192.168.100.0/24
dhcp: true
nat: true
domain: mylab.localyaml
mynetwork:
type: network
cidr: 192.168.100.0/24
dhcp: true
nat: true
domain: mylab.localImage
Image
yaml
myimage:
type: image
url: https://example.com/image.qcow2
pool: defaultyaml
myimage:
type: image
url: https://example.com/image.qcow2
pool: defaultContainer
Container
yaml
mycontainer:
type: container
image: nginx:latest
ports:
- 8080:80yaml
mycontainer:
type: container
image: nginx:latest
ports:
- 8080:80Jinja2 Templating
Jinja2模板使用
Parameter Substitution
参数替换
yaml
parameters:
cluster_name: mycluster
worker_count: 3
{{ cluster_name }}-master:
image: rhcos
{% for i in range(worker_count) %}
{{ cluster_name }}-worker-{{ i }}:
image: rhcos
{% endfor %}yaml
parameters:
cluster_name: mycluster
worker_count: 3
{{ cluster_name }}-master:
image: rhcos
{% for i in range(worker_count) %}
{{ cluster_name }}-worker-{{ i }}:
image: rhcos
{% endfor %}Conditionals
条件判断
yaml
parameters:
enable_storage: true
myvm:
image: fedora40
{% if enable_storage %}
disks:
- size: 100
{% endif %}yaml
parameters:
enable_storage: true
myvm:
image: fedora40
{% if enable_storage %}
disks:
- size: 100
{% endif %}Custom Filters
自定义过滤器
kcli provides custom Jinja2 filters in :
kvirt/jinjafilters/jinjafilters.pyPath/File Filters:
- - Get filename from path
basename - - Get directory from path
dirname - - Convert to /dev/ path if needed
diskpath - - Check if file/path exists
exists - - Handle workdir paths in containers
pwd_path - - Get real/absolute path
real_path - - Read file contents
read_file
String/Data Filters:
- - Return empty string if None
none - - Return type name (string, int, dict, list)
type - - Base64 encode value
base64 - - Wrap in BEGIN/END CERTIFICATE if needed
certificate - - Count occurrences of character
count
Kubernetes/Cluster Filters:
- - Generate node names for cluster
kubenodes - - Generate default node list
defaultnodes - - Check if list has ctlplane/master entries
has_ctlplane
Version/Release Filters:
- - Get latest version from GitHub releases
github_version - - Compare OpenShift versions (minimum)
min_ocp_version - - Compare OpenShift versions (maximum)
max_ocp_version
Network Filters:
- - Get local IP for network interface
local_ip - - Get IP from network CIDR
network_ip - - Wrap IPv6 addresses in brackets
ipv6_wrap
Utility Filters:
- - Get VM info via kcli command
kcli_info - - Find YAML manifests in directory
find_manifests - - Generate wait script for CRD creation
wait_crd - - Generate wait script for CSV readiness
wait_csv - - Filter BGP peer list
filter_bgp_peers
Standard Jinja2 filters (default, join, upper, lower, etc.) also work
kcli在中提供了自定义Jinja2过滤器:
kvirt/jinjafilters/jinjafilters.py路径/文件过滤器:
- - 从路径中获取文件名
basename - - 从路径中获取目录名
dirname - - 按需转换为/dev/路径
diskpath - - 检查文件/路径是否存在
exists - - 处理容器中的工作目录路径
pwd_path - - 获取真实/绝对路径
real_path - - 读取文件内容
read_file
字符串/数据过滤器:
- - 如果值为None则返回空字符串
none - - 返回类型名称(string、int、dict、list)
type - - Base64编码值
base64 - - 按需包裹BEGIN/END CERTIFICATE
certificate - - 统计字符出现次数
count
Kubernetes/集群过滤器:
- - 生成集群节点名称
kubenodes - - 生成默认节点列表
defaultnodes - - 检查列表中是否包含ctlplane/master条目
has_ctlplane
版本/发布过滤器:
- - 从GitHub发布中获取最新版本
github_version - - 比较OpenShift版本(最小值)
min_ocp_version - - 比较OpenShift版本(最大值)
max_ocp_version
网络过滤器:
- - 获取网络接口的本地IP
local_ip - - 从网络CIDR中获取IP
network_ip - - 用方括号包裹IPv6地址
ipv6_wrap
实用工具过滤器:
- - 通过kcli命令获取VM信息
kcli_info - - 在目录中查找YAML清单
find_manifests - - 生成CRD创建的等待脚本
wait_crd - - 生成CSV就绪的等待脚本
wait_csv - - 过滤BGP对等体列表
filter_bgp_peers
标准Jinja2过滤器(default、join、upper、lower等)同样适用
Parameter Files
参数文件
Create alongside your plan:
kcli_parameters.ymlyaml
cluster_name: prod
worker_count: 5
memory: 8192Override at runtime:
bash
kcli create plan -f myplan.yml -P worker_count=10 myplan在计划文件旁创建:
kcli_parameters.ymlyaml
cluster_name: prod
worker_count: 5
memory: 8192在运行时覆盖参数:
bash
kcli create plan -f myplan.yml -P worker_count=10 myplanCommon VM Parameters
常见VM参数
| Parameter | Default | Description |
|---|---|---|
| 2 | Number of CPUs |
| 512 | Memory in MB |
| default | Storage pool |
| None | Base image name |
| [default] | Network list |
| [{size:10}] | Disk list |
| [] | Post-boot commands |
| [] | Files to inject |
| [] | SSH public keys |
| true | Auto-start VM |
| true | Enable cloud-init |
| 参数 | 默认值 | 说明 |
|---|---|---|
| 2 | CPU数量 |
| 512 | 内存(MB) |
| default | 存储池 |
| None | 基础镜像名称 |
| [default] | 网络列表 |
| [{size:10}] | 磁盘列表 |
| [] | 开机后执行的命令 |
| [] | 要注入的文件 |
| [] | SSH公钥 |
| true | 自动启动VM |
| true | 启用cloud-init |
Plan Execution
计划执行
bash
undefinedbash
undefinedCreate plan
创建计划
kcli create plan -f myplan.yml myplanname
kcli create plan -f myplan.yml myplanname
Create with parameter overrides
覆盖参数创建计划
kcli create plan -f myplan.yml -P memory=4096 -P image=fedora40 myplanname
kcli create plan -f myplan.yml -P memory=4096 -P image=fedora40 myplanname
List plans
列出计划
kcli list plan
kcli list plan
Get plan info
获取计划信息
kcli info plan myplanname
kcli info plan myplanname
Delete plan (and all its resources)
删除计划(及其所有资源)
kcli delete plan myplanname
kcli delete plan myplanname
Update existing plan
更新现有计划
kcli update plan -f myplan.yml myplanname
undefinedkcli update plan -f myplan.yml myplanname
undefinedDebugging Plans
计划调试
- Validate YAML syntax - Use
python -c "import yaml; yaml.safe_load(open('plan.yml'))" - Check Jinja2 rendering - Look for unbalanced
{{or}}{% %} - Run with debug -
kcli -d create plan -f plan.yml test - Check dependencies - Ensure images/networks exist before VMs reference them
- 验证YAML语法 - 使用
python -c "import yaml; yaml.safe_load(open('plan.yml'))" - 检查Jinja2渲染 - 查找未匹配的
{{或}}{% %} - 启用调试模式运行 -
kcli -d create plan -f plan.yml test - 检查依赖项 - 确保VM引用的镜像/网络已存在
Example: Multi-VM Plan
示例:多VM计划
yaml
parameters:
domain: lab.local
base_image: centos9stream
labnetwork:
type: network
cidr: 10.0.0.0/24
dhcp: true
domain: {{ domain }}
webserver:
image: {{ base_image }}
memory: 2048
nets:
- labnetwork
cmds:
- dnf -y install nginx
- systemctl enable --now nginx
database:
image: {{ base_image }}
memory: 4096
disks:
- size: 20
- size: 50
nets:
- labnetwork
cmds:
- dnf -y install postgresql-server
- postgresql-setup --initdbyaml
parameters:
domain: lab.local
base_image: centos9stream
labnetwork:
type: network
cidr: 10.0.0.0/24
dhcp: true
domain: {{ domain }}
webserver:
image: {{ base_image }}
memory: 2048
nets:
- labnetwork
cmds:
- dnf -y install nginx
- systemctl enable --now nginx
database:
image: {{ base_image }}
memory: 4096
disks:
- size: 20
- size: 50
nets:
- labnetwork
cmds:
- dnf -y install postgresql-server
- postgresql-setup --initdb