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.
  • Loading branch information
Callisto13 committed Jan 9, 2023
1 parent fbb1fdf commit 8d06036
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 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
4 changes: 2 additions & 2 deletions api/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ type MicrovmSpec struct {
MemoryMb int64 `json:"memoryMb"`

// RootVolume specifies the volume to use for the root of the microvm.
// +kubebuilder:validation:Required
// +optional
RootVolume Volume `json:"rootVolume"`

// AdditionalVolumes specifies additional non-root volumes to attach to the microvm.
// +optional
AdditionalVolumes []Volume `json:"volumes,omitempty"`

// Kernel specifies the kernel and its arguments to use.
// +kubebuilder:validation:Required
// +optional
Kernel ContainerFileSource `json:"kernel"`

// KernelCmdLine are the additional args to use for the kernel cmdline.
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 @@ -198,10 +198,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 @@ -241,10 +241,8 @@ spec:
type: object
type: array
required:
- kernel
- memoryMb
- networkInterfaces
- rootVolume
- vcpu
type: object
required:
Expand Down

0 comments on commit 8d06036

Please sign in to comment.