-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Valeriy Khorunzhin <[email protected]>
- Loading branch information
Valeriy Khorunzhin
committed
Jan 14, 2025
1 parent
b185f6e
commit 714aa1b
Showing
11 changed files
with
801 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
337 changes: 337 additions & 0 deletions
337
images/virtualization-artifact/pkg/controller/k8s-validation/validate-k8s-utils.go
Large diffs are not rendered by default.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
images/virtualization-artifact/pkg/controller/vd/internal/validator/name_validator.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("virtual disk name cannot contain '.'") | ||
} | ||
|
||
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, "virtual disk name contain '.', it may be cause of problems in future, please recreate resource.") | ||
return warnings, nil | ||
} | ||
|
||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
images/virtualization-artifact/pkg/controller/vm/internal/validators/affinity_validator.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
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" | ||
"errors" | ||
"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) { | ||
var errs []error | ||
|
||
errorList := k8sUtils.ValidateAffinity(vm.Spec.Affinity, k8sfield.NewPath("spec")) | ||
for _, err := range errorList { | ||
errs = append(errs, err) | ||
} | ||
|
||
if len(errs) > 0 { | ||
return nil, fmt.Errorf("errors while validating affinity: %w", errors.Join(errs...)) | ||
} | ||
|
||
return nil, nil | ||
} |
Oops, something went wrong.