diff --git a/api/v1alpha1/microvmmachine_webhook.go b/api/v1alpha1/microvmmachine_webhook.go index 468dd02..2e4a862 100644 --- a/api/v1alpha1/microvmmachine_webhook.go +++ b/api/v1alpha1/microvmmachine_webhook.go @@ -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" @@ -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. diff --git a/api/v1alpha1/types.go b/api/v1alpha1/types.go index 4fd1e15..44f3c4e 100644 --- a/api/v1alpha1/types.go +++ b/api/v1alpha1/types.go @@ -20,7 +20,7 @@ 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. @@ -28,7 +28,7 @@ type MicrovmSpec struct { 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. diff --git a/api/v1alpha1/validate.go b/api/v1alpha1/validate.go index 2edcd36..83a272b 100644 --- a/api/v1alpha1/validate.go +++ b/api/v1alpha1/validate.go @@ -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 +} diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_microvmmachines.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_microvmmachines.yaml index fa96743..5d15804 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_microvmmachines.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_microvmmachines.yaml @@ -198,10 +198,8 @@ spec: type: object type: array required: - - kernel - memoryMb - networkInterfaces - - rootVolume - vcpu type: object status: diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_microvmmachinetemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_microvmmachinetemplates.yaml index 8712ace..3aef3c3 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_microvmmachinetemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_microvmmachinetemplates.yaml @@ -241,10 +241,8 @@ spec: type: object type: array required: - - kernel - memoryMb - networkInterfaces - - rootVolume - vcpu type: object required: