github.com/openebs/api@v1.12.0/pkg/apis/cstor/v1/cstorpoolclusterbuilder.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 v1
    18  
    19  import (
    20  	"github.com/openebs/api/pkg/util"
    21  	corev1 "k8s.io/api/core/v1"
    22  )
    23  
    24  func NewCStorPoolCluster() *CStorPoolCluster {
    25  	return &CStorPoolCluster{}
    26  }
    27  
    28  // WithName sets the Name field of cspc with provided value.
    29  func (cspc *CStorPoolCluster) WithName(name string) *CStorPoolCluster {
    30  	cspc.Name = name
    31  	return cspc
    32  }
    33  
    34  // WithNamespace sets the Namespace field of cspc provided arguments
    35  func (cspc *CStorPoolCluster) WithNamespace(namespace string) *CStorPoolCluster {
    36  	cspc.Namespace = namespace
    37  	return cspc
    38  }
    39  
    40  // WithAnnotationsNew sets the Annotations field of cspc with provided arguments
    41  func (cspc *CStorPoolCluster) WithAnnotationsNew(annotations map[string]string) *CStorPoolCluster {
    42  	cspc.Annotations = make(map[string]string)
    43  	for key, value := range annotations {
    44  		cspc.Annotations[key] = value
    45  	}
    46  	return cspc
    47  }
    48  
    49  // WithAnnotations appends or overwrites existing Annotations
    50  // values of cspc with provided arguments
    51  func (cspc *CStorPoolCluster) WithAnnotations(annotations map[string]string) *CStorPoolCluster {
    52  
    53  	if cspc.Annotations == nil {
    54  		return cspc.WithAnnotationsNew(annotations)
    55  	}
    56  	for key, value := range annotations {
    57  		cspc.Annotations[key] = value
    58  	}
    59  	return cspc
    60  }
    61  
    62  // WithLabelsNew sets the Labels field of cspc with provided arguments
    63  func (cspc *CStorPoolCluster) WithLabelsNew(labels map[string]string) *CStorPoolCluster {
    64  	cspc.Labels = make(map[string]string)
    65  	for key, value := range labels {
    66  		cspc.Labels[key] = value
    67  	}
    68  	return cspc
    69  }
    70  
    71  // WithLabels appends or overwrites existing Labels
    72  // values of cspc with provided arguments
    73  func (cspc *CStorPoolCluster) WithLabels(labels map[string]string) *CStorPoolCluster {
    74  	if cspc.Labels == nil {
    75  		return cspc.WithLabelsNew(labels)
    76  	}
    77  	for key, value := range labels {
    78  		cspc.Labels[key] = value
    79  	}
    80  	return cspc
    81  }
    82  
    83  // WithFinalizer sets the finalizer field in the CSPC
    84  func (cspc *CStorPoolCluster) WithFinalizer(finalizers ...string) *CStorPoolCluster {
    85  	cspc.Finalizers = append(cspc.Finalizers, finalizers...)
    86  	return cspc
    87  }
    88  
    89  // WithDefaultResource sets the DefaultResources field in the CSPC
    90  func (cspc *CStorPoolCluster) WithDefaultResource(resources corev1.ResourceRequirements) *CStorPoolCluster {
    91  	cspc.Spec.DefaultResources = &resources
    92  	return cspc
    93  }
    94  
    95  // WithDefaultAuxResources sets the DefaultAuxResources field in the CSPC
    96  func (cspc *CStorPoolCluster) WithDefaultAuxResources(resources corev1.ResourceRequirements) *CStorPoolCluster {
    97  	cspc.Spec.DefaultAuxResources = &resources
    98  	return cspc
    99  }
   100  
   101  // WithTolerations sets the Tolerations field in the CSPC
   102  func (cspc *CStorPoolCluster) WithTolerations(tolerations []corev1.Toleration) *CStorPoolCluster {
   103  	cspc.Spec.Tolerations = tolerations
   104  	return cspc
   105  }
   106  
   107  // WithDefaultPriorityClassName sets the DefaultPriorityClassName field in the CSPC
   108  func (cspc *CStorPoolCluster) WithDefaultPriorityClassName(priorityClassName string) *CStorPoolCluster {
   109  	cspc.Spec.DefaultPriorityClassName = priorityClassName
   110  	return cspc
   111  }
   112  
   113  // WithPoolSpecs sets the Pools field in the CSPC
   114  func (cspc *CStorPoolCluster) WithPoolSpecs(pools ...PoolSpec) *CStorPoolCluster {
   115  	cspc.Spec.Pools = append(cspc.Spec.Pools, pools...)
   116  	return cspc
   117  }
   118  
   119  func NewPoolSpec() *PoolSpec {
   120  	return &PoolSpec{}
   121  }
   122  
   123  // WithNodeSelector sets the NodeSelector field in poolSpec
   124  func (ps *PoolSpec) WithNodeSelector(nodeSelector map[string]string) *PoolSpec {
   125  	ps.NodeSelector = nodeSelector
   126  	return ps
   127  }
   128  
   129  // WithDataRaidGroups sets the DataRaidGroups field in poolSpec
   130  func (ps *PoolSpec) WithDataRaidGroups(dataRaidGroups ...RaidGroup) *PoolSpec {
   131  	ps.DataRaidGroups = append(ps.DataRaidGroups, dataRaidGroups...)
   132  	return ps
   133  }
   134  
   135  // WithWriteCacheRaidGroups sets the WriteCacheRaidGroups field in poolSpec
   136  func (ps *PoolSpec) WithWriteCacheRaidGroups(writeCacheRaidGroups ...RaidGroup) *PoolSpec {
   137  	ps.WriteCacheRaidGroups = append(ps.WriteCacheRaidGroups, writeCacheRaidGroups...)
   138  	return ps
   139  }
   140  
   141  // WithPoolConfig sets the PoolConfig field in poolSpec
   142  func (ps *PoolSpec) WithPoolConfig(poolConfig PoolConfig) *PoolSpec {
   143  	ps.PoolConfig = poolConfig
   144  	return ps
   145  }
   146  
   147  func NewPoolConfig() *PoolConfig {
   148  	return &PoolConfig{}
   149  }
   150  
   151  // WithDataRaidGroupType sets the DataRaidGroupType field in PoolConfig
   152  func (pc *PoolConfig) WithDataRaidGroupType(dataRaidGroupType string) *PoolConfig {
   153  	pc.DataRaidGroupType = dataRaidGroupType
   154  	return pc
   155  }
   156  
   157  // WithWriteCacheGroupType sets the WriteCacheGroupType field in PoolConfig
   158  func (pc *PoolConfig) WithWriteCacheGroupType(writeCacheGroupType string) *PoolConfig {
   159  	pc.WriteCacheGroupType = writeCacheGroupType
   160  	return pc
   161  }
   162  
   163  // WithThickProvision sets the ThickProvision field in PoolConfig
   164  func (pc *PoolConfig) WithThickProvision(thickProvision bool) *PoolConfig {
   165  	pc.ThickProvision = thickProvision
   166  	return pc
   167  }
   168  
   169  // WithResources sets the Resources field in PoolConfig
   170  func (pc *PoolConfig) WithResources(resources *corev1.ResourceRequirements) *PoolConfig {
   171  	pc.Resources = resources
   172  	return pc
   173  }
   174  
   175  // WithAuxResources sets the auxResources field in PoolConfig
   176  func (pc *PoolConfig) WithAuxResources(auxResources *corev1.ResourceRequirements) *PoolConfig {
   177  	pc.AuxResources = auxResources
   178  	return pc
   179  }
   180  
   181  // WithTolerations sets the Tolerations field in PoolConfig
   182  func (pc *PoolConfig) WithTolerations(tolerations []corev1.Toleration) *PoolConfig {
   183  	pc.Tolerations = tolerations
   184  	return pc
   185  }
   186  
   187  // WithPriorityClassName sets the PriorityClassName field in PoolConfig
   188  func (pc *PoolConfig) WithPriorityClassName(priorityClassName *string) *PoolConfig {
   189  	pc.PriorityClassName = priorityClassName
   190  	return pc
   191  }
   192  
   193  // WithROThresholdLimit sets the ROThresholdLimit field in PoolConfig
   194  func (pc *PoolConfig) WithROThresholdLimit(rOThresholdLimit *int) *PoolConfig {
   195  	pc.ROThresholdLimit = rOThresholdLimit
   196  	return pc
   197  }
   198  
   199  // NewRaidGroup returns an empty instance of raid group
   200  func NewRaidGroup() *RaidGroup {
   201  	return &RaidGroup{}
   202  }
   203  
   204  // WithROThresholdLimit sets the ROThresholdLimit field in PoolConfig
   205  func (rg *RaidGroup) WithCStorPoolInstanceBlockDevices(cStorPoolInstanceBlockDevices ...CStorPoolInstanceBlockDevice) *RaidGroup {
   206  	rg.CStorPoolInstanceBlockDevices = append(rg.CStorPoolInstanceBlockDevices, cStorPoolInstanceBlockDevices...)
   207  	return rg
   208  }
   209  
   210  // NewCStorPoolInstanceBlockDevice returns an empty instance of CStorPoolInstanceBlockDevice
   211  func NewCStorPoolInstanceBlockDevice() *CStorPoolInstanceBlockDevice {
   212  	return &CStorPoolInstanceBlockDevice{}
   213  }
   214  
   215  // WithName sets the BlockDeviceName field in CStorPoolInstanceBlockDevice
   216  func (cspibd *CStorPoolInstanceBlockDevice) WithName(name string) *CStorPoolInstanceBlockDevice {
   217  	cspibd.BlockDeviceName = name
   218  	return cspibd
   219  }
   220  
   221  // HasFinalizer returns true if the provided finalizer is present on the object.
   222  func (cspc *CStorPoolCluster) HasFinalizer(finalizer string) bool {
   223  	finalizersList := cspc.GetFinalizers()
   224  	return util.ContainsString(finalizersList, finalizer)
   225  }
   226  
   227  // RemoveFinalizer removes the given finalizer from the object.
   228  func (cspc *CStorPoolCluster) RemoveFinalizer(finalizer string) {
   229  	cspc.Finalizers = util.RemoveString(cspc.Finalizers, finalizer)
   230  }
   231  
   232  // HasAnnotation return true if provided annotation
   233  // key and value are present on the object.
   234  func (cspc *CStorPoolCluster) HasAnnotation(key, value string) bool {
   235  	val, ok := cspc.GetAnnotations()[key]
   236  	if ok {
   237  		return val == value
   238  	}
   239  	return false
   240  }
   241  
   242  // HasLabel returns true if provided label
   243  // key and value are present on the object.
   244  func (cspc *CStorPoolCluster) HasLabel(key, value string) bool {
   245  	val, ok := cspc.GetLabels()[key]
   246  	if ok {
   247  		return val == value
   248  	}
   249  	return false
   250  }
   251  
   252  // GetBlockDevices returns list of blockdevice names exist in the raid group
   253  func (rg RaidGroup) GetBlockDevices() []string {
   254  	var bdNames []string
   255  	for _, cspcBD := range rg.CStorPoolInstanceBlockDevices {
   256  		bdNames = append(bdNames, cspcBD.BlockDeviceName)
   257  	}
   258  	return bdNames
   259  }