github.com/openebs/api@v1.12.0/pkg/apis/cstor/v1/cstorpoolinstancebuilder.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 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 22 ) 23 24 const ( 25 StoragePoolKindCSPC = "CStorPoolCluster" 26 // APIVersion holds the value of OpenEBS version 27 APIVersion = "cstor.openebs.io/v1" 28 ) 29 30 func NewCStorPoolInstance() *CStorPoolInstance { 31 return &CStorPoolInstance{} 32 } 33 34 // WithName sets the Name field of CSPI with provided value. 35 func (cspi *CStorPoolInstance) WithName(name string) *CStorPoolInstance { 36 cspi.Name = name 37 return cspi 38 } 39 40 // WithNamespace sets the Namespace field of CSPI provided arguments 41 func (cspi *CStorPoolInstance) WithNamespace(namespace string) *CStorPoolInstance { 42 cspi.Namespace = namespace 43 return cspi 44 } 45 46 // WithAnnotationsNew sets the Annotations field of CSPI with provided arguments 47 func (cspi *CStorPoolInstance) WithAnnotationsNew(annotations map[string]string) *CStorPoolInstance { 48 cspi.Annotations = make(map[string]string) 49 for key, value := range annotations { 50 cspi.Annotations[key] = value 51 } 52 return cspi 53 } 54 55 // WithAnnotations appends or overwrites existing Annotations 56 // values of CSPI with provided arguments 57 func (cspi *CStorPoolInstance) WithAnnotations(annotations map[string]string) *CStorPoolInstance { 58 59 if cspi.Annotations == nil { 60 return cspi.WithAnnotationsNew(annotations) 61 } 62 for key, value := range annotations { 63 cspi.Annotations[key] = value 64 } 65 return cspi 66 } 67 68 // WithLabelsNew sets the Labels field of CSPI with provided arguments 69 func (cspi *CStorPoolInstance) WithLabelsNew(labels map[string]string) *CStorPoolInstance { 70 cspi.Labels = make(map[string]string) 71 for key, value := range labels { 72 cspi.Labels[key] = value 73 } 74 return cspi 75 } 76 77 // WithLabels appends or overwrites existing Labels 78 // values of CSPI with provided arguments 79 func (cspi *CStorPoolInstance) WithLabels(labels map[string]string) *CStorPoolInstance { 80 if cspi.Labels == nil { 81 return cspi.WithLabelsNew(labels) 82 } 83 for key, value := range labels { 84 cspi.Labels[key] = value 85 } 86 return cspi 87 } 88 89 // WithNodeSelectorByReference sets the node selector field of CSPI with provided argument. 90 func (cspi *CStorPoolInstance) WithNodeSelectorByReference(nodeSelector map[string]string) *CStorPoolInstance { 91 cspi.Spec.NodeSelector = nodeSelector 92 return cspi 93 } 94 95 // WithNodeName sets the HostName field of CSPI with the provided argument. 96 func (cspi *CStorPoolInstance) WithNodeName(nodeName string) *CStorPoolInstance { 97 cspi.Spec.HostName = nodeName 98 return cspi 99 } 100 101 // WithPoolConfig sets the pool config field of the CSPI with the provided config. 102 func (cspi *CStorPoolInstance) WithPoolConfig(poolConfig PoolConfig) *CStorPoolInstance { 103 cspi.Spec.PoolConfig = poolConfig 104 return cspi 105 } 106 107 // WithDataRaidGroups sets the DataRaidGroups of the CSPI with the provided raid groups. 108 func (cspi *CStorPoolInstance) WithDataRaidGroups(raidGroup []RaidGroup) *CStorPoolInstance { 109 cspi.Spec.DataRaidGroups = raidGroup 110 return cspi 111 } 112 113 // WithWriteCacheRaidGroups sets the WriteCacheRaidGroups of the CSPI with the provided raid groups. 114 func (cspi *CStorPoolInstance) WithWriteCacheRaidGroups(raidGroup []RaidGroup) *CStorPoolInstance { 115 cspi.Spec.WriteCacheRaidGroups = raidGroup 116 return cspi 117 } 118 119 // WithFinalizer sets the finalizer field in the BDC 120 func (cspi *CStorPoolInstance) WithFinalizer(finalizers ...string) *CStorPoolInstance { 121 cspi.Finalizers = append(cspi.Finalizers, finalizers...) 122 return cspi 123 } 124 125 // WithCSPCOwnerReference sets the OwnerReference field in CSPI with required 126 //fields 127 func (cspi *CStorPoolInstance) WithCSPCOwnerReference(reference metav1.OwnerReference) *CStorPoolInstance { 128 cspi.OwnerReferences = append(cspi.OwnerReferences, reference) 129 return cspi 130 } 131 132 // WithNewVersion sets the current and desired version field of 133 // CSPI with provided arguments 134 func (cspi *CStorPoolInstance) WithNewVersion(version string) *CStorPoolInstance { 135 cspi.VersionDetails.Status.Current = version 136 cspi.VersionDetails.Desired = version 137 return cspi 138 } 139 140 // WithDependentsUpgraded sets the field to true for new CSPI 141 func (cspi *CStorPoolInstance) WithDependentsUpgraded() *CStorPoolInstance { 142 cspi.VersionDetails.Status.DependentsUpgraded = true 143 return cspi 144 } 145 146 // HasFinalizer returns true if the provided finalizer is present on the object. 147 func (cspi *CStorPoolInstance) HasFinalizer(finalizer string) bool { 148 finalizersList := cspi.GetFinalizers() 149 return util.ContainsString(finalizersList, finalizer) 150 } 151 152 // RemoveFinalizer removes the given finalizer from the object. 153 func (cspi *CStorPoolInstance) RemoveFinalizer(finalizer string) { 154 cspi.Finalizers = util.RemoveString(cspi.Finalizers, finalizer) 155 } 156 157 // IsDestroyed returns true if CSPI is deletion time stamp is set 158 func (cspi *CStorPoolInstance) IsDestroyed() bool { 159 return !cspi.DeletionTimestamp.IsZero() 160 } 161 162 // IsEmptyStatus is to check whether the status of cStorPoolInstance is empty. 163 func (cspi *CStorPoolInstance) IsEmptyStatus() bool { 164 return cspi.Status.Phase == CStorPoolStatusEmpty 165 } 166 167 // IsPendingStatus is to check if the status of cStorPoolInstance is pending. 168 func (cspi *CStorPoolInstance) IsPendingStatus() bool { 169 return cspi.Status.Phase == CStorPoolStatusPending 170 } 171 172 // IsOnlineStatus is to check if the status of cStorPoolInstance is online. 173 func (cspi *CStorPoolInstance) IsOnlineStatus() bool { 174 return cspi.Status.Phase == CStorPoolStatusOnline 175 } 176 177 // GetAllRaidGroups returns list of all raid groups presents in cspi 178 func (cspi *CStorPoolInstance) GetAllRaidGroups() []RaidGroup { 179 var rgs []RaidGroup 180 rgs = append(rgs, cspi.Spec.DataRaidGroups...) 181 rgs = append(rgs, cspi.Spec.WriteCacheRaidGroups...) 182 return rgs 183 } 184 185 // HasAnnotation return true if provided annotation 186 // key and value are present on the object. 187 func (cspi *CStorPoolInstance) HasAnnotation(key, value string) bool { 188 val, ok := cspi.GetAnnotations()[key] 189 if ok { 190 return val == value 191 } 192 return false 193 } 194 195 // HasLabel returns true if provided label 196 // key and value are present on the object. 197 func (cspi *CStorPoolInstance) HasLabel(key, value string) bool { 198 val, ok := cspi.GetLabels()[key] 199 if ok { 200 return val == value 201 } 202 return false 203 } 204 205 // HasNodeName returns true if the CSPI belongs 206 // to the provided node name. 207 func (cspi *CStorPoolInstance) HasNodeName(nodeName string) bool { 208 return cspi.Spec.HostName == nodeName 209 } 210 211 // HasNodeName is predicate to filter out based on 212 // node name of CSPI instances. 213 func HasNodeName(nodeName string) CSPIPredicate { 214 return func(cspi *CStorPoolInstance) bool { 215 return cspi.HasNodeName(nodeName) 216 } 217 } 218 219 // IsOnline is predicate to filter out based on 220 // online CSPI instances. 221 func IsOnline() CSPIPredicate { 222 return func(cspi *CStorPoolInstance) bool { 223 return cspi.IsOnlineStatus() 224 } 225 } 226 227 // Predicate defines an abstraction to determine conditional checks against the 228 // provided CStorPoolInstance 229 type CSPIPredicate func(*CStorPoolInstance) bool 230 231 // PredicateList holds the list of Predicates 232 type cspiPredicateList []CSPIPredicate 233 234 // all returns true if all the predicates succeed against the provided block 235 // device instance. 236 func (l cspiPredicateList) all(cspi *CStorPoolInstance) bool { 237 for _, pred := range l { 238 if !pred(cspi) { 239 return false 240 } 241 } 242 return true 243 } 244 245 // Filter will filter the csp instances 246 // if all the predicates succeed against that 247 // csp. 248 func (cspiList *CStorPoolInstanceList) Filter(p ...CSPIPredicate) *CStorPoolInstanceList { 249 var plist cspiPredicateList 250 plist = append(plist, p...) 251 if len(plist) == 0 { 252 return cspiList 253 } 254 255 filtered := &CStorPoolInstanceList{} 256 for _, cspi := range cspiList.Items { 257 cspi := cspi // pin it 258 if plist.all(&cspi) { 259 filtered.Items = append(filtered.Items, cspi) 260 } 261 } 262 return filtered 263 }