|
| 1 | +/* |
| 2 | +Copyright 2025 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package vd |
| 18 | + |
| 19 | +import ( |
| 20 | + "k8s.io/apimachinery/pkg/api/resource" |
| 21 | + "k8s.io/utils/ptr" |
| 22 | + |
| 23 | + "github.com/deckhouse/virtualization-controller/pkg/builder/meta" |
| 24 | + "github.com/deckhouse/virtualization/api/core/v1alpha2" |
| 25 | +) |
| 26 | + |
| 27 | +type Option func(vmop *v1alpha2.VirtualDisk) |
| 28 | + |
| 29 | +var ( |
| 30 | + WithName = meta.WithName[*v1alpha2.VirtualDisk] |
| 31 | + WithNamespace = meta.WithNamespace[*v1alpha2.VirtualDisk] |
| 32 | + WithGenerateName = meta.WithGenerateName[*v1alpha2.VirtualDisk] |
| 33 | + WithLabel = meta.WithLabel[*v1alpha2.VirtualDisk] |
| 34 | + WithLabels = meta.WithLabels[*v1alpha2.VirtualDisk] |
| 35 | + WithAnnotation = meta.WithAnnotation[*v1alpha2.VirtualDisk] |
| 36 | + WithAnnotations = meta.WithAnnotations[*v1alpha2.VirtualDisk] |
| 37 | + WithFinalizer = meta.WithFinalizer[*v1alpha2.VirtualDisk] |
| 38 | +) |
| 39 | + |
| 40 | +func WithDataSourceHTTP(url string, checksum *v1alpha2.Checksum, caBundle []byte) Option { |
| 41 | + return func(vd *v1alpha2.VirtualDisk) { |
| 42 | + vd.Spec.DataSource = &v1alpha2.VirtualDiskDataSource{ |
| 43 | + Type: v1alpha2.DataSourceTypeHTTP, |
| 44 | + HTTP: &v1alpha2.DataSourceHTTP{ |
| 45 | + URL: url, |
| 46 | + Checksum: checksum, |
| 47 | + CABundle: caBundle, |
| 48 | + }, |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func WithDataSourceContainerImage(image, imagePullSecretName string, caBundle []byte) Option { |
| 54 | + return func(vd *v1alpha2.VirtualDisk) { |
| 55 | + vd.Spec.DataSource = &v1alpha2.VirtualDiskDataSource{ |
| 56 | + Type: v1alpha2.DataSourceTypeContainerImage, |
| 57 | + ContainerImage: &v1alpha2.VirtualDiskContainerImage{ |
| 58 | + Image: image, |
| 59 | + ImagePullSecret: v1alpha2.ImagePullSecretName{ |
| 60 | + Name: imagePullSecretName, |
| 61 | + }, |
| 62 | + CABundle: caBundle, |
| 63 | + }, |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func WithDataSourceObjectRef(kind v1alpha2.VirtualDiskObjectRefKind, name string) Option { |
| 69 | + return func(vd *v1alpha2.VirtualDisk) { |
| 70 | + vd.Spec.DataSource = &v1alpha2.VirtualDiskDataSource{ |
| 71 | + Type: v1alpha2.DataSourceTypeObjectRef, |
| 72 | + ObjectRef: &v1alpha2.VirtualDiskObjectRef{ |
| 73 | + Kind: kind, |
| 74 | + Name: name, |
| 75 | + }, |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func WithDataSourceObjectRefFromCVI(cvi *v1alpha2.ClusterVirtualImage) Option { |
| 81 | + return WithDataSourceObjectRef(v1alpha2.VirtualDiskObjectRefKindClusterVirtualImage, cvi.Name) |
| 82 | +} |
| 83 | + |
| 84 | +func WithDataSourceObjectRefFromVI(vi *v1alpha2.VirtualImage) Option { |
| 85 | + return WithDataSourceObjectRef(v1alpha2.VirtualDiskObjectRefKindVirtualImage, vi.Name) |
| 86 | +} |
| 87 | + |
| 88 | +func WithPersistentVolumeClaim(storageClass *string, size *resource.Quantity) Option { |
| 89 | + return func(vd *v1alpha2.VirtualDisk) { |
| 90 | + vd.Spec.PersistentVolumeClaim = v1alpha2.VirtualDiskPersistentVolumeClaim{ |
| 91 | + StorageClass: storageClass, |
| 92 | + Size: size, |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func WithStorageClass(storageClass string) Option { |
| 98 | + return func(vd *v1alpha2.VirtualDisk) { |
| 99 | + vd.Spec.PersistentVolumeClaim.StorageClass = ptr.To(storageClass) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func WithSize(size *resource.Quantity) Option { |
| 104 | + return func(vd *v1alpha2.VirtualDisk) { |
| 105 | + vd.Spec.PersistentVolumeClaim.Size = size |
| 106 | + } |
| 107 | +} |
0 commit comments