github.com/openebs/api@v1.12.0/pkg/internalapis/apis/openebs.io/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 openebs
    18  
    19  import (
    20  	"github.com/openebs/api/pkg/internalapis/apis/cstor"
    21  	corev1 "k8s.io/api/core/v1"
    22  	"k8s.io/apimachinery/pkg/api/resource"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  const (
    27  	// StoragePoolKindCSPC holds the value of CStorPoolCluster
    28  	StoragePoolKindCSPC = "CStorPoolCluster"
    29  	// APIVersion holds the value of OpenEBS version
    30  	APIVersion = "openebs.io/v1alpha1"
    31  )
    32  
    33  func NewBlockDeviceClaim() *BlockDeviceClaim {
    34  	return &BlockDeviceClaim{}
    35  }
    36  
    37  // WithName sets the Name field of BDC with provided value.
    38  func (bdc *BlockDeviceClaim) WithName(name string) *BlockDeviceClaim {
    39  	bdc.Name = name
    40  	return bdc
    41  }
    42  
    43  // WithNamespace sets the Namespace field of BDC provided arguments
    44  func (bdc *BlockDeviceClaim) WithNamespace(namespace string) *BlockDeviceClaim {
    45  	bdc.Namespace = namespace
    46  	return bdc
    47  }
    48  
    49  // WithAnnotationsNew sets the Annotations field of BDC with provided arguments
    50  func (bdc *BlockDeviceClaim) WithAnnotationsNew(annotations map[string]string) *BlockDeviceClaim {
    51  	bdc.Annotations = make(map[string]string)
    52  	for key, value := range annotations {
    53  		bdc.Annotations[key] = value
    54  	}
    55  	return bdc
    56  }
    57  
    58  // WithAnnotations appends or overwrites existing Annotations
    59  // values of BDC with provided arguments
    60  func (bdc *BlockDeviceClaim) WithAnnotations(annotations map[string]string) *BlockDeviceClaim {
    61  	if bdc.Annotations == nil {
    62  		return bdc.WithAnnotationsNew(annotations)
    63  	}
    64  	for key, value := range annotations {
    65  		bdc.Annotations[key] = value
    66  	}
    67  	return bdc
    68  }
    69  
    70  // WithLabelsNew sets the Labels field of BDC with provided arguments
    71  func (bdc *BlockDeviceClaim) WithLabelsNew(labels map[string]string) *BlockDeviceClaim {
    72  	bdc.Labels = make(map[string]string)
    73  	for key, value := range labels {
    74  		bdc.Labels[key] = value
    75  	}
    76  	return bdc
    77  }
    78  
    79  // WithLabels appends or overwrites existing Labels
    80  // values of BDC with provided arguments
    81  func (bdc *BlockDeviceClaim) WithLabels(labels map[string]string) *BlockDeviceClaim {
    82  	if bdc.Labels == nil {
    83  		return bdc.WithLabelsNew(labels)
    84  	}
    85  	for key, value := range labels {
    86  		bdc.Labels[key] = value
    87  	}
    88  	return bdc
    89  }
    90  
    91  // WithBlockDeviceName sets the BlockDeviceName field of BDC provided arguments
    92  func (bdc *BlockDeviceClaim) WithBlockDeviceName(bdName string) *BlockDeviceClaim {
    93  	bdc.Spec.BlockDeviceName = bdName
    94  	return bdc
    95  }
    96  
    97  // WithDeviceType sets the DeviceType field of BDC provided arguments
    98  func (bdc *BlockDeviceClaim) WithDeviceType(dType string) *BlockDeviceClaim {
    99  	bdc.Spec.DeviceType = dType
   100  	return bdc
   101  }
   102  
   103  // WithHostName sets the hostName field of BDC provided arguments
   104  func (bdc *BlockDeviceClaim) WithHostName(hName string) *BlockDeviceClaim {
   105  	bdc.Spec.BlockDeviceNodeAttributes.HostName = hName
   106  	return bdc
   107  }
   108  
   109  // WithNodeName sets the node name field of BDC provided arguments
   110  func (bdc *BlockDeviceClaim) WithNodeName(nName string) *BlockDeviceClaim {
   111  	bdc.Spec.BlockDeviceNodeAttributes.NodeName = nName
   112  	return bdc
   113  }
   114  
   115  // WithCapacity sets the Capacity field in BDC with provided arguments
   116  func (bdc *BlockDeviceClaim) WithCapacity(capacity resource.Quantity) *BlockDeviceClaim {
   117  	resourceList := corev1.ResourceList{
   118  		corev1.ResourceName(ResourceStorage): capacity,
   119  	}
   120  	bdc.Spec.Resources.Requests = resourceList
   121  	return bdc
   122  }
   123  
   124  // WithCSPCOwnerReference sets the OwnerReference field in BDC with required
   125  //fields
   126  func (bdc *BlockDeviceClaim) WithCSPCOwnerReference(cspc *cstor.CStorPoolCluster) *BlockDeviceClaim {
   127  	trueVal := true
   128  	reference := metav1.OwnerReference{
   129  		APIVersion:         APIVersion,
   130  		Kind:               StoragePoolKindCSPC,
   131  		UID:                cspc.ObjectMeta.UID,
   132  		Name:               cspc.ObjectMeta.Name,
   133  		BlockOwnerDeletion: &trueVal,
   134  		Controller:         &trueVal,
   135  	}
   136  	bdc.OwnerReferences = append(bdc.OwnerReferences, reference)
   137  	return bdc
   138  }
   139  
   140  // WithFinalizer sets the finalizer field in the BDC
   141  func (bdc *BlockDeviceClaim) WithFinalizer(finalizers ...string) *BlockDeviceClaim {
   142  	bdc.Finalizers = append(bdc.Finalizers, finalizers...)
   143  	return bdc
   144  }
   145  
   146  // WithBlockVolumeMode sets the volumeMode as volumeModeBlock,
   147  // if persistentVolumeMode is set to "Block"
   148  func (bdc *BlockDeviceClaim) WithBlockVolumeMode(mode corev1.PersistentVolumeMode) *BlockDeviceClaim {
   149  	if mode == corev1.PersistentVolumeBlock {
   150  		bdc.Spec.Details.BlockVolumeMode = VolumeModeBlock
   151  	}
   152  	return bdc
   153  }