Skip to content

Commit

Permalink
feat: Validate rootvol and kernel spec in webhook
Browse files Browse the repository at this point in the history
The previous commit made these fields optional, but we need to verify
that at least some kernel and root volume information is set.

This validation will check whether the short syntax for kernel and root
volume config are set, and if not, will validate that long syntax fields
are provided. Errors are returned if not.

Also add new `OS_VERSION` and `KERNEL_VERSION` fields to templates.
  • Loading branch information
Callisto13 committed Feb 17, 2023
1 parent 64cb6ac commit 20e95e4
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 11 deletions.
11 changes: 10 additions & 1 deletion api/v1alpha1/microvmmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"reflect"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -34,7 +35,15 @@ var (

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *MicrovmMachine) ValidateCreate() error {
return nil
var allErrs field.ErrorList

allErrs = append(allErrs, r.Spec.MicrovmSpec.Validate()...)

if len(allErrs) == 0 {
return nil
}

return apierrors.NewInvalid(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
Expand Down
30 changes: 30 additions & 0 deletions api/v1alpha1/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,33 @@ func (p *Placement) Validate() []*field.Error {

return errs
}

func (s *MicrovmSpec) Validate() []*field.Error {
var errs field.ErrorList

if s.OsVersion == "" {
if s.RootVolume.ID == "" {
errs = append(errs,
field.Required(field.NewPath("spec", "rootVolume", "id"), "must be set if osVersion is omitted"))
}

if s.RootVolume.Image == "" {
errs = append(errs,
field.Required(field.NewPath("spec", "rootVolume", "image"), "must be set if osVersion is omitted"))
}
}

if s.KernelVersion == "" {
if s.Kernel.Image == "" {
errs = append(errs,
field.Required(field.NewPath("spec", "kernel", "image"), "must be set if kernelVersion is omitted"))
}

if s.Kernel.Filename == "" {
errs = append(errs,
field.Required(field.NewPath("spec", "kernel", "filename"), "must be set if kernelVersion is omitted"))
}
}

return errs
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,8 @@ spec:
type: object
type: array
required:
- kernel
- memoryMb
- networkInterfaces
- rootVolume
- vcpu
type: object
status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,8 @@ spec:
type: object
type: array
required:
- kernel
- memoryMb
- networkInterfaces
- rootVolume
- vcpu
type: object
required:
Expand Down
8 changes: 6 additions & 2 deletions templates/cluster-template-cilium.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ metadata:
spec:
template:
spec:
kernelVersion: "${KERNEL_VERSION}"
osVersion: "${OS_VERSION}"
vcpu: 2
memoryMb: 2048
rootVolume:
id: root
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-kubernetes:1.23.5}"
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-k8s-os:1.23.5}"
kernel:
filename: "boot/vmlinux"
image: "${MVM_KERNEL_IMAGE:=ghcr.io/weaveworks-liquidmetal/kernel-bin:5.10.77}"
Expand Down Expand Up @@ -119,11 +121,13 @@ metadata:
spec:
template:
spec:
kernelVersion: "${KERNEL_VERSION}"
osVersion: "${OS_VERSION}"
vcpu: 2
memoryMb: 2048
rootVolume:
id: root
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-kubernetes:1.23.5}"
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-k8s-os:1.23.5}"
kernel:
filename: "boot/vmlinux"
image: "${MVM_KERNEL_IMAGE:=ghcr.io/weaveworks-liquidmetal/kernel-bin:5.10.77}"
Expand Down
8 changes: 6 additions & 2 deletions templates/cluster-template-flannel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ metadata:
spec:
template:
spec:
kernelVersion: "${KERNEL_VERSION}"
osVersion: "${OS_VERSION}"
rootVolume:
id: root
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-kubernetes:1.23.5}"
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-k8s-os:1.23.5}"
kernel:
filename: boot/vmlinux
image: "${MVM_KERNEL_IMAGE:=ghcr.io/weaveworks-liquidmetal/kernel-bin:5.10.77}"
Expand Down Expand Up @@ -121,9 +123,11 @@ metadata:
spec:
template:
spec:
kernelVersion: "${KERNEL_VERSION}"
osVersion: "${OS_VERSION}"
rootVolume:
id: root
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-kubernetes:1.23.5}"
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-k8s-os:1.23.5}"
kernel:
filename: boot/vmlinux
image: "${MVM_KERNEL_IMAGE:=ghcr.io/weaveworks-liquidmetal/kernel-bin:5.10.77}"
Expand Down
8 changes: 6 additions & 2 deletions templates/cluster-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ spec:
spec:
vcpu: 2
memoryMb: 2048
kernelVersion: "${KERNEL_VERSION}"
osVersion: "${OS_VERSION}"
rootVolume:
id: root
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-kubernetes:1.23.5}"
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-k8s-os:1.23.5}"
kernel:
filename: "boot/vmlinux"
image: "${MVM_KERNEL_IMAGE:=ghcr.io/weaveworks-liquidmetal/kernel-bin:5.10.77}"
Expand Down Expand Up @@ -117,9 +119,11 @@ spec:
spec:
vcpu: 2
memoryMb: 2048
kernelVersion: "${KERNEL_VERSION}"
osVersion: "${OS_VERSION}"
rootVolume:
id: root
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-kubernetes:1.23.5}"
image: "${MVM_ROOT_IMAGE:=ghcr.io/weaveworks-liquidmetal/capmvm-k8s-os:1.23.5}"
kernel:
filename: "boot/vmlinux"
image: "${MVM_KERNEL_IMAGE:=ghcr.io/weaveworks-liquidmetal/kernel-bin:5.10.77}"
Expand Down

0 comments on commit 20e95e4

Please sign in to comment.