Skip to content

super-phenix/vmsnapshot-scheduler

Repository files navigation

VirtualMachine Snapshot Scheduler

The VirtualMachine Snapshot Scheduler is a Kubernetes operator designed to automate the lifecycle of KubeVirt VirtualMachineSnapshot resources. It creates snapshots at defined intervals using cron expressions and manages their retention.

Features

  • Cron-based Scheduling: Automatically creates VirtualMachineSnapshot objects at specified intervals.
  • Multiple VM Support: Selects VirtualMachines to snapshot using a LabelSelector.
  • Retention Management: Automatically deletes old snapshots based on a maximum count or TTL (Time to Live).
  • Disable Schedule: Disable snapshot creation by setting disabled: true while maintaining existing snapshots.
  • Leader Election: Supports high availability with leader election.
  • CRD Configuration: Fully configurable with simple YAML manifests.

Example CRD Configuration

apiVersion: snapscheduler.kubevirt.io/v1alpha1
kind: VirtualMachineSnapshotSchedule
metadata:
  name: example-schedule
  namespace: default
spec:
  # Standard cron expression for scheduling snapshots
  # Also supports descriptors (@daily, @monthly, @yearly...
  schedule: "0 2 * * *"
  
  # Selection of VirtualMachines to snapshot
  # Supports both matchLabels and matchExpressions
  virtualMachineSelector:
    matchLabels:
      vm.kubevirt.io/name: my-vm
    matchExpressions:
    - key: environment
      operator: In
      values:
      - production
  
  # Retention policies (can be used together)
  maxCount: 7    # Keep only the last 7 snapshots
  ttl: "168h"    # Keep snapshots for 7 days (e.g., 24h, 1h, 30m)
  
  # Template for the created VirtualMachineSnapshot objects
  snapshotTemplate:
    metadata:
      labels:
        backup-type: scheduled
        
  # Set to true to pause the schedule without deleting it
  disabled: false

Usage

To enable scheduled snapshots for your VirtualMachines, create a VirtualMachineSnapshotSchedule resource.

Single VirtualMachine Snapshot

apiVersion: snapscheduler.kubevirt.io/v1alpha1
kind: VirtualMachineSnapshotSchedule
metadata:
  name: daily-backup
spec:
  schedule: "0 2 * * *" # Every day at 2 AM
  maxCount: 7 # Keep last 7 snapshots
  virtualMachineSelector:
    matchLabels:
      vm.kubevirt.io/name: my-vm

Multiple VirtualMachines with Selector

apiVersion: snapscheduler.kubevirt.io/v1alpha1
kind: VirtualMachineSnapshotSchedule
metadata:
  name: production-vms-backup
spec:
  schedule: "0 */4 * * *" # Every 4 hours
  ttl: "48h" # Keep snapshots for 48 hours
  virtualMachineSelector:
    matchLabels:
      environment: production
  snapshotTemplate:
    metadata:
      labels:
        backup-type: scheduled

Configuration

The controller can be configured using command-line flags or through the Helm chart:

Command-line Flags

Flag Default Description
--kubeconfig - Path to a kubeconfig file.
--leader-elect false Enable leader election.
--enable-owner-references false Enable owner references and finalizers for snapshots.

Deployment

Helm Chart

A Helm chart is provided in the charts/vmsnapshot-scheduler directory.

helm install vmsnapshot-scheduler ./charts/vmsnapshot-scheduler -n vmsnapshot-scheduler --create-namespace

Ensure you configure the namespace correctly in your values or via --set.

Enable enableOwnerReferences if you want to use owner references and finalizers for snapshots. When VirtualMachineSnapshotSchedule objects are deleted, all the related VirtualMachineSnapshot objects will be deleted as well.

Build

Prerequisites

  • Go 1.25+
  • Docker (optional, for containerized builds)

Building the binary

make build

Building the Docker image

make docker-build IMG=vmsnapshot-scheduler:latest

About

A controller that handles scheduling Kubevirt VirtualMachine snapshots.

Topics

Resources

License

Stars

1 star

Watchers

1 watching

Forks

Packages

 
 
 

Contributors