/
build.go
355 lines (321 loc) · 9.35 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
Copyright 2019 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
ndm "github.com/openebs/maya/pkg/apis/openebs.io/ndm/v1alpha1"
apis "github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1"
errors "github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// StoragePoolKind holds the value of StoragePoolClaim
StoragePoolKind = "StoragePoolClaim"
// StoragePoolKindCSPC holds the value of CStorPoolCluster
StoragePoolKindCSPC = "CStorPoolCluster"
// APIVersion holds the value of OpenEBS version
APIVersion = "openebs.io/v1alpha1"
// bdTagKey defines the label selector key
// used for grouping block devices using a tag.
bdTagKey = "openebs.io/block-device-tag"
)
// Builder is the builder object for BlockDeviceClaim
type Builder struct {
BDC *BlockDeviceClaim
errs []error
}
// NewBuilder returns an empty instance of the Builder object
func NewBuilder() *Builder {
return &Builder{
BDC: &BlockDeviceClaim{&ndm.BlockDeviceClaim{}, ""},
errs: []error{},
}
}
// BuilderForObject returns an instance of the Builder object based on block
// device object
func BuilderForObject(BlockDeviceClaim *BlockDeviceClaim) *Builder {
return &Builder{
BDC: BlockDeviceClaim,
errs: []error{},
}
}
// BuilderForAPIObject returns an instance of the Builder object based on block
// device claim api object.
func BuilderForAPIObject(bdc *ndm.BlockDeviceClaim) *Builder {
return &Builder{
BDC: &BlockDeviceClaim{bdc, ""},
errs: []error{},
}
}
// WithConfigPath sets the path for k8s config
func (b *Builder) WithConfigPath(configpath string) *Builder {
b.BDC.configPath = configpath
return b
}
// Build returns the BlockDeviceClaim instance
func (b *Builder) Build() (*BlockDeviceClaim, error) {
if len(b.errs) > 0 {
return nil, errors.Errorf("%+v", b.errs)
}
return b.BDC, nil
}
// WithName sets the Name field of BDC with provided value.
func (b *Builder) WithName(name string) *Builder {
if len(name) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing BDC name"),
)
return b
}
b.BDC.Object.Name = name
return b
}
// WithNamespace sets the Namespace field of BDC provided arguments
func (b *Builder) WithNamespace(namespace string) *Builder {
if len(namespace) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing BDC namespace"),
)
return b
}
b.BDC.Object.Namespace = namespace
return b
}
// WithAnnotationsNew sets the Annotations field of BDC with provided arguments
func (b *Builder) WithAnnotationsNew(annotations map[string]string) *Builder {
if len(annotations) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing annotations"),
)
return b
}
b.BDC.Object.Annotations = make(map[string]string)
for key, value := range annotations {
b.BDC.Object.Annotations[key] = value
}
return b
}
// WithAnnotations appends or overwrites existing Annotations
// values of BDC with provided arguments
func (b *Builder) WithAnnotations(annotations map[string]string) *Builder {
if len(annotations) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing annotations"),
)
return b
}
if b.BDC.Object.Annotations == nil {
return b.WithAnnotationsNew(annotations)
}
for key, value := range annotations {
b.BDC.Object.Annotations[key] = value
}
return b
}
// WithLabelsNew sets the Labels field of BDC with provided arguments
func (b *Builder) WithLabelsNew(labels map[string]string) *Builder {
if len(labels) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing labels"),
)
return b
}
b.BDC.Object.Labels = make(map[string]string)
for key, value := range labels {
b.BDC.Object.Labels[key] = value
}
return b
}
// WithLabels appends or overwrites existing Labels
// values of BDC with provided arguments
func (b *Builder) WithLabels(labels map[string]string) *Builder {
if len(labels) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing labels"),
)
return b
}
if b.BDC.Object.Labels == nil {
return b.WithLabelsNew(labels)
}
for key, value := range labels {
b.BDC.Object.Labels[key] = value
}
return b
}
// WithBlockDeviceName sets the BlockDeviceName field of BDC provided arguments
func (b *Builder) WithBlockDeviceName(bdName string) *Builder {
if len(bdName) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing BlockDevice name"),
)
return b
}
b.BDC.Object.Spec.BlockDeviceName = bdName
return b
}
// WithDeviceType sets the DeviceType field of BDC provided arguments
func (b *Builder) WithDeviceType(dType string) *Builder {
if len(dType) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing device type"),
)
return b
}
b.BDC.Object.Spec.DeviceType = dType
return b
}
// WithHostName sets the hostName field of BDC provided arguments
func (b *Builder) WithHostName(hName string) *Builder {
if len(hName) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing host name"),
)
return b
}
b.BDC.Object.Spec.BlockDeviceNodeAttributes.HostName = hName
return b
}
// WithNodeName sets the node name field of BDC provided arguments
func (b *Builder) WithNodeName(nName string) *Builder {
if len(nName) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing node name"),
)
return b
}
b.BDC.Object.Spec.BlockDeviceNodeAttributes.NodeName = nName
return b
}
// WithCapacity sets the Capacity field in BDC with provided arguments
func (b *Builder) WithCapacity(capacity string) *Builder {
resCapacity, err := resource.ParseQuantity(capacity)
if err != nil {
b.errs = append(
b.errs,
errors.Wrapf(
err, "failed to build BDC object: failed to parse capacity {%s}",
capacity,
),
)
return b
}
resourceList := corev1.ResourceList{
corev1.ResourceName(ndm.ResourceStorage): resCapacity,
}
b.BDC.Object.Spec.Resources.Requests = resourceList
return b
}
// WithOwnerReference sets the OwnerReference field in BDC with required
//fields
func (b *Builder) WithOwnerReference(spc *apis.StoragePoolClaim) *Builder {
if spc == nil {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: spc object is nil"),
)
return b
}
trueVal := true
reference := metav1.OwnerReference{
APIVersion: APIVersion,
Kind: StoragePoolKind,
UID: spc.ObjectMeta.UID,
Name: spc.ObjectMeta.Name,
BlockOwnerDeletion: &trueVal,
Controller: &trueVal,
}
b.BDC.Object.OwnerReferences = append(b.BDC.Object.OwnerReferences, reference)
return b
}
// WithCSPCOwnerReference sets the OwnerReference field in BDC with required
//fields
func (b *Builder) WithCSPCOwnerReference(cspc *apis.CStorPoolCluster) *Builder {
if cspc == nil {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: cspc object is nil"),
)
return b
}
trueVal := true
reference := metav1.OwnerReference{
APIVersion: APIVersion,
Kind: StoragePoolKindCSPC,
UID: cspc.ObjectMeta.UID,
Name: cspc.ObjectMeta.Name,
BlockOwnerDeletion: &trueVal,
Controller: &trueVal,
}
b.BDC.Object.OwnerReferences = append(b.BDC.Object.OwnerReferences, reference)
return b
}
// WithFinalizer sets the finalizer field in the BDC
func (b *Builder) WithFinalizer(finalizers ...string) *Builder {
if len(finalizers) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing finalizer"),
)
return b
}
b.BDC.Object.Finalizers = append(b.BDC.Object.Finalizers, finalizers...)
return b
}
// WithBlockVolumeMode sets the volumeMode as volumeModeBlock,
// if persistentVolumeMode is set to "Block"
func (b *Builder) WithBlockVolumeMode(mode corev1.PersistentVolumeMode) *Builder {
if len(mode) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing PersistentVolumeMode"),
)
}
if mode == corev1.PersistentVolumeBlock {
b.BDC.Object.Spec.Details.BlockVolumeMode = ndm.VolumeModeBlock
}
return b
}
// WithBlockDeviceTag appends (or creates) the BDC Label Selector
// by setting the provided value to the fixed key
// openebs.io/block-device-tag
// This will enable the NDM to pick only devices that
// match the node (hostname) and block device tag value.
func (b *Builder) WithBlockDeviceTag(bdTagValue string) *Builder {
if len(bdTagValue) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing block device tag value"),
)
return b
}
if b.BDC.Object.Spec.Selector == nil {
b.BDC.Object.Spec.Selector = &metav1.LabelSelector{}
}
if b.BDC.Object.Spec.Selector.MatchLabels == nil {
b.BDC.Object.Spec.Selector.MatchLabels = map[string]string{}
}
b.BDC.Object.Spec.Selector.MatchLabels[bdTagKey] = bdTagValue
return b
}