github.com/openebs/api@v1.12.0/pkg/apis/openebs.io/v1alpha1/blockdeviceclaimbuilder.go (about) 1 /* 2 Copyright 2020 The OpenEBS Authors 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 v1alpha1 18 19 import ( 20 "github.com/openebs/api/pkg/util" 21 "github.com/pkg/errors" 22 corev1 "k8s.io/api/core/v1" 23 "k8s.io/apimachinery/pkg/api/resource" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 ) 26 27 const ( 28 // StoragePoolKindCSPC holds the value of CStorPoolCluster 29 StoragePoolKindCSPC = "CStorPoolCluster" 30 // APIVersion holds the value of OpenEBS version 31 APIVersion = "openebs.io/v1alpha1" 32 33 // bdTagKey defines the label selector key 34 // used for grouping block devices using a tag. 35 bdTagKey = "openebs.io/block-device-tag" 36 ) 37 38 func NewBlockDeviceClaim() *BlockDeviceClaim { 39 return &BlockDeviceClaim{} 40 } 41 42 // WithName sets the Name field of BDC with provided value. 43 func (bdc *BlockDeviceClaim) WithName(name string) *BlockDeviceClaim { 44 bdc.Name = name 45 return bdc 46 } 47 48 // WithNamespace sets the Namespace field of BDC provided arguments 49 func (bdc *BlockDeviceClaim) WithNamespace(namespace string) *BlockDeviceClaim { 50 bdc.Namespace = namespace 51 return bdc 52 } 53 54 // WithAnnotationsNew sets the Annotations field of BDC with provided arguments 55 func (bdc *BlockDeviceClaim) WithAnnotationsNew(annotations map[string]string) *BlockDeviceClaim { 56 bdc.Annotations = make(map[string]string) 57 for key, value := range annotations { 58 bdc.Annotations[key] = value 59 } 60 return bdc 61 } 62 63 // WithAnnotations appends or overwrites existing Annotations 64 // values of BDC with provided arguments 65 func (bdc *BlockDeviceClaim) WithAnnotations(annotations map[string]string) *BlockDeviceClaim { 66 if bdc.Annotations == nil { 67 return bdc.WithAnnotationsNew(annotations) 68 } 69 for key, value := range annotations { 70 bdc.Annotations[key] = value 71 } 72 return bdc 73 } 74 75 // WithLabelsNew sets the Labels field of BDC with provided arguments 76 func (bdc *BlockDeviceClaim) WithLabelsNew(labels map[string]string) *BlockDeviceClaim { 77 bdc.Labels = make(map[string]string) 78 for key, value := range labels { 79 bdc.Labels[key] = value 80 } 81 return bdc 82 } 83 84 // WithLabels appends or overwrites existing Labels 85 // values of BDC with provided arguments 86 func (bdc *BlockDeviceClaim) WithLabels(labels map[string]string) *BlockDeviceClaim { 87 if bdc.Labels == nil { 88 return bdc.WithLabelsNew(labels) 89 } 90 for key, value := range labels { 91 bdc.Labels[key] = value 92 } 93 return bdc 94 } 95 96 // WithBlockDeviceName sets the BlockDeviceName field of BDC provided arguments 97 func (bdc *BlockDeviceClaim) WithBlockDeviceName(bdName string) *BlockDeviceClaim { 98 bdc.Spec.BlockDeviceName = bdName 99 return bdc 100 } 101 102 // WithDeviceType sets the DeviceType field of BDC provided arguments 103 func (bdc *BlockDeviceClaim) WithDeviceType(dType string) *BlockDeviceClaim { 104 bdc.Spec.DeviceType = dType 105 return bdc 106 } 107 108 // WithHostName sets the hostName field of BDC provided arguments 109 func (bdc *BlockDeviceClaim) WithHostName(hName string) *BlockDeviceClaim { 110 bdc.Spec.BlockDeviceNodeAttributes.HostName = hName 111 return bdc 112 } 113 114 // WithNodeName sets the node name field of BDC provided arguments 115 func (bdc *BlockDeviceClaim) WithNodeName(nName string) *BlockDeviceClaim { 116 bdc.Spec.BlockDeviceNodeAttributes.NodeName = nName 117 return bdc 118 } 119 120 // WithCapacity sets the Capacity field in BDC with provided arguments 121 func (bdc *BlockDeviceClaim) WithCapacity(capacity resource.Quantity) *BlockDeviceClaim { 122 resourceList := corev1.ResourceList{ 123 corev1.ResourceName(ResourceStorage): capacity, 124 } 125 bdc.Spec.Resources.Requests = resourceList 126 return bdc 127 } 128 129 // WithCSPCOwnerReference sets the OwnerReference field in BDC with required 130 //fields 131 func (bdc *BlockDeviceClaim) WithCSPCOwnerReference(reference metav1.OwnerReference) *BlockDeviceClaim { 132 bdc.OwnerReferences = append(bdc.OwnerReferences, reference) 133 return bdc 134 } 135 136 // WithFinalizer sets the finalizer field in the BDC 137 func (bdc *BlockDeviceClaim) WithFinalizer(finalizers ...string) *BlockDeviceClaim { 138 bdc.Finalizers = append(bdc.Finalizers, finalizers...) 139 return bdc 140 } 141 142 // WithBlockVolumeMode sets the volumeMode as volumeModeBlock, 143 // if persistentVolumeMode is set to "Block" 144 func (bdc *BlockDeviceClaim) WithBlockVolumeMode(mode corev1.PersistentVolumeMode) *BlockDeviceClaim { 145 if mode == corev1.PersistentVolumeBlock { 146 bdc.Spec.Details.BlockVolumeMode = VolumeModeBlock 147 } 148 return bdc 149 } 150 151 // WithBlockDeviceTag appends (or creates) the BDC Label Selector 152 // by setting the provided value to the fixed key 153 // openebs.io/block-device-tag 154 // This will enable the NDM to pick only devices that 155 // match the node (hostname) and block device tag value. 156 func (bdc *BlockDeviceClaim) WithBlockDeviceTag(bdTagValue string) *BlockDeviceClaim { 157 if bdc.Spec.Selector == nil { 158 bdc.Spec.Selector = &metav1.LabelSelector{} 159 } 160 if bdc.Spec.Selector.MatchLabels == nil { 161 bdc.Spec.Selector.MatchLabels = map[string]string{} 162 } 163 164 bdc.Spec.Selector.MatchLabels[bdTagKey] = bdTagValue 165 return bdc 166 } 167 168 // RemoveFinalizer removes the given finalizer from the object. 169 func (bdc *BlockDeviceClaim) RemoveFinalizer(finalizer string) { 170 bdc.Finalizers = util.RemoveString(bdc.Finalizers, finalizer) 171 } 172 173 // GetBlockDeviceClaimFromBDName return block device claim if claim exists for 174 // provided blockdevice name in claim list else return error 175 func (bdcl *BlockDeviceClaimList) GetBlockDeviceClaimFromBDName( 176 bdName string) (*BlockDeviceClaim, error) { 177 for _, bdc := range bdcl.Items { 178 // pin it 179 bdc := bdc 180 if bdc.Spec.BlockDeviceName == bdName { 181 return &bdc, nil 182 } 183 } 184 return nil, errors.Errorf("claim doesn't exist for blockdevice %s", bdName) 185 }