Skip to content
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
47 changes: 47 additions & 0 deletions images/virtualization-artifact/pkg/builder/cvi/cvi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2025 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 cvi

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

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

func New(options ...Option) *v1alpha2.ClusterVirtualImage {
cvi := NewEmpty("")
ApplyOptions(cvi, options)
return cvi
}

func ApplyOptions(cvi *v1alpha2.ClusterVirtualImage, opts []Option) {
if cvi == nil {
return
}
for _, opt := range opts {
opt(cvi)
}
}

func NewEmpty(name string) *v1alpha2.ClusterVirtualImage {
return &v1alpha2.ClusterVirtualImage{
TypeMeta: metav1.TypeMeta{
APIVersion: v1alpha2.SchemeGroupVersion.String(),
Kind: v1alpha2.ClusterVirtualImageKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
}
41 changes: 41 additions & 0 deletions images/virtualization-artifact/pkg/builder/cvi/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2025 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 cvi

import (
"github.com/deckhouse/virtualization-controller/pkg/builder/meta"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
)

type Option func(cvi *v1alpha2.ClusterVirtualImage)

var (
WithName = meta.WithName[*v1alpha2.ClusterVirtualImage]
WithLabel = meta.WithLabel[*v1alpha2.ClusterVirtualImage]
WithLabels = meta.WithLabels[*v1alpha2.ClusterVirtualImage]
WithAnnotation = meta.WithAnnotation[*v1alpha2.ClusterVirtualImage]
WithAnnotations = meta.WithAnnotations[*v1alpha2.ClusterVirtualImage]
)

func WithPhase(phase v1alpha2.ImagePhase) func(vi *v1alpha2.ClusterVirtualImage) {
return func(vi *v1alpha2.ClusterVirtualImage) {
vi.Status.Phase = phase
}
}

func WithCDROM(cdrom bool) func(vi *v1alpha2.ClusterVirtualImage) {
return func(vi *v1alpha2.ClusterVirtualImage) {
vi.Status.CDROM = cdrom
}
}
30 changes: 30 additions & 0 deletions images/virtualization-artifact/pkg/builder/vd/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2025 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 vd

import (
"github.com/deckhouse/virtualization-controller/pkg/builder/meta"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
)

type Option func(vd *v1alpha2.VirtualDisk)

var (
WithName = meta.WithName[*v1alpha2.VirtualDisk]
WithNamespace = meta.WithNamespace[*v1alpha2.VirtualDisk]
WithLabel = meta.WithLabel[*v1alpha2.VirtualDisk]
WithLabels = meta.WithLabels[*v1alpha2.VirtualDisk]
WithAnnotation = meta.WithAnnotation[*v1alpha2.VirtualDisk]
WithAnnotations = meta.WithAnnotations[*v1alpha2.VirtualDisk]
)
48 changes: 48 additions & 0 deletions images/virtualization-artifact/pkg/builder/vd/vd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2025 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 vd

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

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

func New(options ...Option) *v1alpha2.VirtualDisk {
vd := NewEmpty("", "")
ApplyOptions(vd, options)
return vd
}

func ApplyOptions(vd *v1alpha2.VirtualDisk, opts []Option) {
if vd == nil {
return
}
for _, opt := range opts {
opt(vd)
}
}

func NewEmpty(name, namespace string) *v1alpha2.VirtualDisk {
return &v1alpha2.VirtualDisk{
TypeMeta: metav1.TypeMeta{
APIVersion: v1alpha2.SchemeGroupVersion.String(),
Kind: v1alpha2.VirtualDiskKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
}
42 changes: 42 additions & 0 deletions images/virtualization-artifact/pkg/builder/vi/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2025 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 vi

import (
"github.com/deckhouse/virtualization-controller/pkg/builder/meta"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
)

type Option func(vi *v1alpha2.VirtualImage)

var (
WithName = meta.WithName[*v1alpha2.VirtualImage]
WithNamespace = meta.WithNamespace[*v1alpha2.VirtualImage]
WithLabel = meta.WithLabel[*v1alpha2.VirtualImage]
WithLabels = meta.WithLabels[*v1alpha2.VirtualImage]
WithAnnotation = meta.WithAnnotation[*v1alpha2.VirtualImage]
WithAnnotations = meta.WithAnnotations[*v1alpha2.VirtualImage]
)

func WithPhase(phase v1alpha2.ImagePhase) func(vi *v1alpha2.VirtualImage) {
return func(vi *v1alpha2.VirtualImage) {
vi.Status.Phase = phase
}
}

func WithCDROM(cdrom bool) func(vi *v1alpha2.VirtualImage) {
return func(vi *v1alpha2.VirtualImage) {
vi.Status.CDROM = cdrom
}
}
48 changes: 48 additions & 0 deletions images/virtualization-artifact/pkg/builder/vi/vi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2025 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 vi

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

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

func New(options ...Option) *v1alpha2.VirtualImage {
vi := NewEmpty("", "")
ApplyOptions(vi, options)
return vi
}

func ApplyOptions(vi *v1alpha2.VirtualImage, opts []Option) {
if vi == nil {
return
}
for _, opt := range opts {
opt(vi)
}
}

func NewEmpty(name, namespace string) *v1alpha2.VirtualImage {
return &v1alpha2.VirtualImage{
TypeMeta: metav1.TypeMeta{
APIVersion: v1alpha2.SchemeGroupVersion.String(),
Kind: v1alpha2.VirtualImageKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
}
7 changes: 7 additions & 0 deletions images/virtualization-artifact/pkg/builder/vm/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ func WithMemory(size resource.Quantity) Option {
vm.Spec.Memory.Size = size
}
}

func WithBlockDeviceRefs(bdRefs ...v1alpha2.BlockDeviceSpecRef) Option {
return func(vm *v1alpha2.VirtualMachine) {
vm.Spec.BlockDeviceRefs = []v1alpha2.BlockDeviceSpecRef{}
vm.Spec.BlockDeviceRefs = append(vm.Spec.BlockDeviceRefs, bdRefs...)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright 2025 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"
"reflect"

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

"github.com/deckhouse/virtualization-controller/pkg/common/object"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
)

const (
viMainErrorMessage = "a non-CDROM VirtualImage cannot occupy the first position in block devices"
cviMainErrorMessage = "a non-CDROM ClusterVirtualImage cannot occupy the first position in block devices"
)

type FirstBlockDeviceValidator struct {
client client.Client
}

func NewFirstDiskValidator(client client.Client) *FirstBlockDeviceValidator {
return &FirstBlockDeviceValidator{client: client}
}

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

func (v *FirstBlockDeviceValidator) ValidateUpdate(ctx context.Context, oldVM, newVM *v1alpha2.VirtualMachine) (admission.Warnings, error) {
if reflect.DeepEqual(oldVM.Spec.BlockDeviceRefs, newVM.Spec.BlockDeviceRefs) {
return nil, nil
}

return v.Validate(ctx, newVM)
}

func (v *FirstBlockDeviceValidator) Validate(ctx context.Context, vm *v1alpha2.VirtualMachine) (admission.Warnings, error) {
if len(vm.Spec.BlockDeviceRefs) == 0 {
return nil, nil
}

switch vm.Spec.BlockDeviceRefs[0].Kind {
case v1alpha2.ImageDevice:
return nil, v.ValidateVI(ctx, vm.Spec.BlockDeviceRefs[0].Name, vm.GetNamespace())
case v1alpha2.ClusterImageDevice:
return nil, v.ValidateCVI(ctx, vm.Spec.BlockDeviceRefs[0].Name)
}

return nil, nil
}

func (v *FirstBlockDeviceValidator) ValidateCVI(ctx context.Context, name string) error {
cvi, err := object.FetchObject(ctx, types.NamespacedName{Name: name}, v.client, &v1alpha2.ClusterVirtualImage{})
if err != nil {
return err
}
if cvi == nil {
return nil
}

if !cvi.Status.CDROM && cvi.Status.Phase == v1alpha2.ImageReady {
return fmt.Errorf(
"%s: ClusterVirtualImage %s is not CDROM",
cviMainErrorMessage,
name,
)
}

return nil
}

func (v *FirstBlockDeviceValidator) ValidateVI(ctx context.Context, name, namespace string) error {
vi, err := object.FetchObject(ctx, types.NamespacedName{Name: name, Namespace: namespace}, v.client, &v1alpha2.VirtualImage{})
if err != nil {
return err
}
if vi == nil {
return nil
}

if !vi.Status.CDROM && vi.Status.Phase == v1alpha2.ImageReady {
return fmt.Errorf(
"%s: VirtualImage %s is not CDROM",
viMainErrorMessage,
name,
)
}

return nil
}
Loading
Loading