A KubeVirt add-on providing native, user-friendly templating workflows for KubeVirt virtual machines.
cat <<'EOF' | kubectl apply -f -
apiVersion: template.kubevirt.io/v1alpha1
kind: VirtualMachineTemplate
metadata:
name: fedora
spec:
parameters:
- name: NAME
required: true
- name: INSTANCETYPE
value: u1.medium
- name: PREFERENCE
value: fedora
virtualMachine:
metadata:
name: ${NAME}
spec:
runStrategy: Always
instancetype:
name: ${INSTANCETYPE}
preference:
name: ${PREFERENCE}
template:
spec:
domain:
devices: {}
terminationGracePeriodSeconds: 180
volumes:
- containerDisk:
image: quay.io/containerdisks/fedora:latest
name: containerdisk-0
- cloudInitNoCloud:
userData: |
#cloud-config
password: fedora
chpasswd: { expire: False }
ssh_pwauth: True
name: cloudinitdisk-0
EOF# Process and create a VM
virttemplatectl process fedora -p NAME=my-fedora --createvirt-template enables users to create, share, and manage VM blueprints
directly within the cluster. The project provides native support for in-cluster
templating through the VirtualMachineTemplate custom resource, which allows
you to define reusable VM templates with configurable parameters that can be
substituted at runtime to create VirtualMachine instances.
Developed as a separate operator outside core KubeVirt, virt-template is
influenced by OpenShift's Template CRD and designed to provide a more
traditional virtualization experience within Kubernetes.
virt-template-controller: Kubernetes controller that watches and validates VirtualMachineTemplate resourcesvirt-template-apiserver: API server providing subresource APIs for templatesvirttemplatectl: CLI tool for local template processing and management
- VM Blueprints: Create reusable VM templates with parameter placeholders
using
${NAME}syntax - Parameter Substitution: Define static values, generated values, or required parameters for flexible template instantiation
- Server-Side Processing: Process templates in-cluster via the
processsubresource API - Cross-Namespace Sharing: Share and reuse templates across namespaces within your cluster
For development:
- Go version v1.24.0+
- Container tool: Podman (default) or Docker
- kubectl
For deployment on Kubernetes:
- cert-manager installed in the cluster
- KubeVirt installed in the cluster
For deployment on OpenShift:
- OpenShift Virtualization installed in the cluster
Build binaries locally:
make build # Build controller binary
make build-apiserver # Build apiserver binary
make build-virttemplatectl # Build CLI toolRun linting and formatting:
make fmt vet lintRun unit tests:
make testThe easiest way to develop and test is using kubevirtci:
make cluster-up # Start local KubeVirt cluster
make cluster-sync # Build, push, and deploy to cluster
make cluster-functest # Run functional tests
make cluster-down # Stop clustervirt-template supports two deployment configurations:
config/default- Standard Kubernetes deployment (requires cert-manager)config/openshift- OpenShift deployment (uses built-in Service CA operator)
Build and push container images:
make container-build container-push \
IMG_REGISTRY=<registry> \
IMG_TAG=<tag>The build supports multi-arch images (amd64, arm64, s390x). Use
CONTAINER_TOOL=docker or CONTAINER_TOOL=podman.
Deploy on Kubernetes:
make install
make deploy IMG_REGISTRY=<registry> IMG_TAG=<tag>Deploy on OpenShift:
make install
make deploy-openshift IMG_REGISTRY=<registry> IMG_TAG=<tag>Create a sample VirtualMachineTemplate:
kubectl apply -f config/samples/template_v1alpha1_virtualmachinetemplate.yamlUninstall from Kubernetes:
kubectl delete vmt --all # Delete sample instances
make undeploy # Remove controllers
make uninstall # Remove CRDsUninstall from OpenShift:
kubectl delete vmt --all # Delete sample instances
make undeploy-openshift # Remove controllers
make uninstall # Remove CRDsProcess a template locally:
virttemplatectl process -f template.yaml -p NAME=myvm -p INSTANCETYPE=u1.smallConvert OpenShift templates to VirtualMachineTemplate:
virttemplatectl convert -f openshift-template.yamlThe VirtualMachineTemplate custom resource allows you to define reusable VM
blueprints with parameters:
apiVersion: template.kubevirt.io/v1alpha1
kind: VirtualMachineTemplate
metadata:
name: my-template
spec:
parameters:
- name: NAME
description: Name of the VM
required: true
- name: MEMORY
description: Memory size
value: "2Gi"
- name: PASSWORD
generate: expression
from: "[a-zA-Z0-9]{16}"
virtualMachine:
metadata:
name: ${NAME}
[...]Parameters are referenced using ${PARAMETER_NAME} syntax. They can have:
- Static values: Pre-defined default values (
value: "2Gi") - Generated values: Random values from expression generator
- Required flag: Mark parameters as mandatory (
required: true)
The expression generator creates random values using regex-like syntax:
parameters:
- name: PASSWORD
generate: expression
from: "[a-zA-Z0-9]{16}" # Generates 16 random alphanumeric chars
- name: API_KEY
generate: expression
from: "[A-F0-9]{32}" # Generates 32 random hex chars (uppercase)Supported character classes:
[a-z]- lowercase letters[A-Z]- uppercase letters[0-9]- digits[a-zA-Z0-9]- alphanumeric\w- word characters (letters, digits, underscore)\d- digits\a- alphabetic characters\A- special characters
Process VirtualMachineTemplate via API:
virttemplatectl process my-template \
-p NAME=myvm \
-p MEMORY=4GiProcess VirtualMachineTemplate and create VirtualMachine via API:
virttemplatectl process my-template \
-p NAME=myvm \
-p MEMORY=4Gi \
--createProcess local template file:
virttemplatectl process -f my-template.yaml \
-p NAME=myvm \
-p MEMORY=4GiProcess from stdin:
cat my-template.yaml | virttemplatectl process -f - \
-p NAME=myvm \
-p MEMORY=4GiGenerate a single YAML file with all resources:
For Kubernetes:
make build-installer IMG_REGISTRY=<registry> IMG_TAG=<tag>This creates dist/install.yaml which can be deployed with:
kubectl apply -f dist/install.yamlFor OpenShift:
make build-installer-openshift IMG_REGISTRY=<registry> IMG_TAG=<tag>This creates dist/install-openshift.yaml which can be deployed with:
kubectl apply -f dist/install-openshift.yamlRun make help to see all available make targets.
Kubevirtci workflows:
The project provides two kubevirtci-based development environments:
cluster-*targets: Test with a stable KubeVirt releasekubevirt-*targets: Test with KubeVirt from git main
Example workflow:
make cluster-up # Start kubevirtci cluster
make cluster-sync # Build, deploy, and test
make cluster-functest # Run functional tests
make cluster-down # Stop clusterContributions are welcome! Please ensure:
- All tests pass:
make test functest make allpasses (runs formatting, vetting, linting, and code generation)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright The KubeVirt Authors.