Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add specification validation for correct VMI creation #627

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions images/virtualization-artifact/pkg/controller/cvi/cvi_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package cvi

import (
"context"
"errors"
"fmt"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -41,8 +43,15 @@ func NewValidator(logger *log.Logger) *Validator {
}

func (v *Validator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
err := fmt.Errorf("misconfigured webhook rules: create operation not implemented")
v.logger.Error("Ensure the correctness of ValidatingWebhookConfiguration", "err", err)
cvi, ok := obj.(*virtv2.ClusterVirtualImage)
if !ok {
return nil, fmt.Errorf("expected a new ClusterVirtualImage but got a %T", obj)
}

if strings.Contains(cvi.ObjectMeta.Name, ".") {
return nil, errors.New("ClusterVirtualImage name is invalid: '.' is forbidden, allowed name symbols are [0-9a-zA-Z-]")
}

return nil, nil
}

Expand All @@ -59,6 +68,8 @@ func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Obj

v.logger.Info("Validating ClusterVirtualImage")

var warnings admission.Warnings

if oldCVI.Generation == newCVI.Generation {
return nil, nil
}
Expand All @@ -68,7 +79,11 @@ func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Obj
return nil, fmt.Errorf("ClusterVirtualImage is in a Ready state: configuration changes are not available")
}

return nil, nil
if strings.Contains(newCVI.ObjectMeta.Name, ".") {
warnings = append(warnings, "ClusterVirtualImage name is invalid as it contains now forbidden symbol '.', allowed symbols for name are [0-9a-zA-Z-]. Create another image with valid name to avoid problems with future updates.")
}

return warnings, nil
}

func (v *Validator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2024 Flant JSC

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.
*/

package validator

import (
"context"
"errors"
"strings"

"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

type NameValidator struct{}

func NewNameValidator() *NameValidator {
return &NameValidator{}
}

func (v *NameValidator) ValidateCreate(_ context.Context, vd *virtv2.VirtualDisk) (admission.Warnings, error) {
if strings.Contains(vd.ObjectMeta.Name, ".") {
return nil, errors.New("VirtualDisk name is invalid: '.' is forbidden, allowed name symbols are [0-9a-zA-Z-]")
}

return nil, nil
}

func (v *NameValidator) ValidateUpdate(_ context.Context, _, newVD *virtv2.VirtualDisk) (admission.Warnings, error) {
if strings.Contains(newVD.ObjectMeta.Name, ".") {
var warnings admission.Warnings
warnings = append(warnings, "VirtualDisk name is invalid as it contains now forbidden symbol '.', allowed symbols for name are [0-9a-zA-Z-]. Create another disk with valid name to avoid problems with future updates.")
return warnings, nil
}

return nil, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func NewValidator(client client.Client) *Validator {
validator.NewPVCSizeValidator(client),
validator.NewSpecChangesValidator(),
validator.NewISOSourceValidator(client),
validator.NewNameValidator(),
},
}
}
Expand Down
14 changes: 13 additions & 1 deletion images/virtualization-artifact/pkg/controller/vi/vi_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package vi

import (
"context"
"errors"
"fmt"
"reflect"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -47,6 +49,10 @@ func (v *Validator) ValidateCreate(_ context.Context, obj runtime.Object) (admis
return nil, fmt.Errorf("expected a new VirtualMachine but got a %T", obj)
}

if strings.Contains(vi.ObjectMeta.Name, ".") {
return nil, errors.New("VirtualImage name is invalid: '.' is forbidden, allowed name symbols are [0-9a-zA-Z-]")
}

if vi.Spec.Storage == virtv2.StorageKubernetes {
warnings := admission.Warnings{
fmt.Sprintf("Using the `%s` storage type is deprecated. It is recommended to use `%s` instead.",
Expand All @@ -71,6 +77,8 @@ func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Obj

v.logger.Info("Validating VirtualImage")

var warnings admission.Warnings

if oldVI.Generation == newVI.Generation {
return nil, nil
}
Expand All @@ -86,7 +94,11 @@ func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Obj
}
}

return nil, nil
if strings.Contains(newVI.ObjectMeta.Name, ".") {
warnings = append(warnings, "VirtualImage name is invalid as it contains now forbidden symbol '.', allowed symbols for name are [0-9a-zA-Z-]. Create another image with valid name to avoid problems with future updates.")
}

return warnings, nil
}

func (v *Validator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2024 Flant JSC

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.
*/

package validators

import (
"context"
"fmt"

k8sfield "k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

k8sUtils "github.com/deckhouse/virtualization-controller/pkg/controller/k8s-validation"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
)

type AffinityValidator struct{}

func NewAffinityValidator() *AffinityValidator {
return &AffinityValidator{}
}

func (v *AffinityValidator) ValidateCreate(_ context.Context, vm *v1alpha2.VirtualMachine) (admission.Warnings, error) {
return v.Validate(vm)
}

func (v *AffinityValidator) ValidateUpdate(_ context.Context, _, newVM *v1alpha2.VirtualMachine) (admission.Warnings, error) {
return v.Validate(newVM)
}

func (v *AffinityValidator) Validate(vm *v1alpha2.VirtualMachine) (admission.Warnings, error) {
errs := k8sUtils.ValidateAffinity(vm.Spec.Affinity, k8sfield.NewPath("spec"))

if len(errs) > 0 {
return nil, fmt.Errorf("errors while validating affinity: %w", errs.ToAggregate())
}

return nil, nil
}
Loading
Loading