/
pv.go
402 lines (367 loc) · 11 KB
/
pv.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
*
* Copyright © 2022-2023 Dell Inc. or its subsidiaries. All Rights Reserved.
*
* 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 pv
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/dell/cert-csi/pkg/k8sclient/resources/commonparams"
"github.com/dell/cert-csi/pkg/k8sclient/resources/va"
"github.com/dell/cert-csi/pkg/utils"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
tcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
const (
// Poll is a poll interval for PVC tests
Poll = 2 * time.Second
// Timeout is a timeout interval for PVC operations
Timeout = 1800 * time.Second
)
// Client contains pvc interface and kubeclient
type Client struct {
// KubeClient *core.KubeClient
Interface tcorev1.PersistentVolumeInterface
Timeout int
}
// PersistentVolume conatins pv client and claim
type PersistentVolume struct {
Client *Client
Object *v1.PersistentVolume
Deleted bool
// Used when error arises in syncable methods
error error
}
// Delete deletes PersistentVolumeClaim from Kubernetes
func (c *Client) Delete(ctx context.Context, pv *v1.PersistentVolume) *PersistentVolume {
var funcErr error
err := c.Interface.Delete(ctx, pv.Name, metav1.DeleteOptions{})
if err != nil {
funcErr = err
}
log.Debugf("Deleted PV %s", pv.GetName())
return &PersistentVolume{
Client: c,
Object: pv,
Deleted: true,
error: funcErr,
}
}
// DeleteAllPV deletes all pvs in timely manner on the basis of namespace
func (c *Client) DeleteAllPV(ctx context.Context, ns string, vaClient *va.Client) error {
podList, podErr := c.Interface.List(ctx, metav1.ListOptions{})
if podErr != nil {
return podErr
}
log.Debugf("Deleting all PVs")
for i, pv := range podList.Items {
if pv.Spec.PersistentVolumeSource.CSI == nil {
log.Debugf("CSI is nil, continuing")
continue
}
// try to delete only those PVs that has namespace matching with the suite's ns
if val, ok := pv.Spec.PersistentVolumeSource.CSI.VolumeAttributes["csi.storage.k8s.io/pvc/namespace"]; ok && val == ns {
log.Debugf("Deleting pv %s", pv.Name)
err := vaClient.DeleteVaBasedOnPVName(ctx, pv.Name)
if err != nil && !strings.Contains(err.Error(), "not found") {
log.Errorf("Can't delete VolumeAttachment for %s; error=%v", pv.Name, err)
continue
}
err = c.Delete(ctx, &podList.Items[i]).Sync(ctx).GetError()
if err != nil {
log.Errorf("Can't delete pv %s; error=%v", pv.Name, err)
}
}
}
return nil
}
// DeleteAll deletes all client pvs in timely manner
func (c *Client) DeleteAll(ctx context.Context) error {
podList, podErr := c.Interface.List(ctx, metav1.ListOptions{})
if podErr != nil {
return podErr
}
log.Debugf("Deleting all PVs")
for i, pvc := range podList.Items {
log.Debugf("Deleting pv %s", pvc.Name)
err := c.Delete(ctx, &podList.Items[i]).Sync(ctx).GetError()
if err != nil {
log.Errorf("Can't delete pv %s; error=%v", pvc.Name, err)
}
}
return nil
}
// Get uses client interface to make API call for getting provided PersistentVolume
func (c *Client) Get(ctx context.Context, name string) *PersistentVolume {
log := utils.GetLoggerFromContext(ctx)
var funcErr error
newPV, err := c.Interface.Get(ctx, name, metav1.GetOptions{})
if err != nil {
funcErr = err
}
log.Debugf("Got PV %s", newPV.GetName())
return &PersistentVolume{
Client: c,
Object: newPV,
Deleted: false,
error: funcErr,
}
}
// Update updates a PersistentVolume
func (c *Client) Update(ctx context.Context, pv *v1.PersistentVolume) *PersistentVolume {
var funcErr error
updatedPV, err := c.Interface.Update(ctx, pv, metav1.UpdateOptions{})
if err != nil {
funcErr = err
}
log.Debugf("Updated PV %s", pv.GetName())
return &PersistentVolume{
Client: c,
Object: updatedPV,
Deleted: false,
error: funcErr,
}
}
// WaitPV waits when pv will be available
func (c *Client) WaitPV(ctx context.Context, newPVName string) error {
log.Debugf("Waiting for PV %s to be %s", newPVName, color.GreenString("PRESENT"))
startTime := time.Now()
timeout := Timeout
if c.Timeout != 0 {
timeout = time.Duration(c.Timeout) * time.Second
}
pollErr := wait.PollImmediate(Poll, timeout,
func() (bool, error) {
select {
case <-ctx.Done():
log.Infof("Stopping pv wait polling")
return true, fmt.Errorf("stopped waiting to be bound")
default:
break
}
_, err := c.Interface.Get(ctx, newPVName, metav1.GetOptions{})
if err != nil {
log.Debugf("PV %s is still not present", newPVName)
return false, nil
}
return true, nil
})
if pollErr != nil {
return pollErr
}
yellow := color.New(color.FgHiYellow)
log.Debugf("PV %s is present in %s", newPVName, yellow.Sprint(time.Since(startTime)))
return nil
}
// WaitToBeBound waits for PVC to be in Bound state
func (pv *PersistentVolume) WaitToBeBound(ctx context.Context) error {
log.Debugf("Waiting for PV %s to be %s", pv.Object.Name, color.GreenString("PRESENT"))
startTime := time.Now()
timeout := Timeout
if pv.Client.Timeout != 0 {
timeout = time.Duration(pv.Client.Timeout) * time.Second
}
pollErr := wait.PollImmediate(Poll, timeout,
func() (bool, error) {
select {
case <-ctx.Done():
log.Infof("Stopping pv wait polling")
return true, fmt.Errorf("stopped waiting to be bound")
default:
break
}
_, err := pv.Client.Interface.Get(ctx, pv.Object.Name, metav1.GetOptions{})
if err != nil {
log.Debugf("PV %s is still not present", pv.Object.Name)
return false, nil
}
return true, nil
})
if pollErr != nil {
return pollErr
}
yellow := color.New(color.FgHiYellow)
log.Debugf("All PVs in %s are presented in %s", pv.Object.Namespace, yellow.Sprint(time.Since(startTime)))
return nil
}
// CheckReplicationAnnotationsForPV checks for replication related annotations and labels on PV
func (c *Client) CheckReplicationAnnotationsForPV(ctx context.Context, object *v1.PersistentVolume) error {
pvName := object.Name
log.Infof("Checking Replication Annotations and Labels on PV %s", pvName)
timeout := Timeout
if c.Timeout != 0 {
timeout = time.Duration(c.Timeout) * time.Second
}
pollErr := wait.PollImmediate(Poll, timeout,
func() (bool, error) {
select {
case <-ctx.Done():
log.Infof("Stopping pv check polling")
return true, fmt.Errorf("stopped checking pv Annotations")
default:
break
}
gotObj, err := c.Interface.Get(ctx, pvName, metav1.GetOptions{})
if err != nil {
return false, err
}
found := true
for _, v := range commonparams.LocalPVAnnotation {
if gotObj.Annotations[v] == "" {
log.Debugf("Annotations %s are not added for PV %s", v, pvName)
found = false
}
}
for _, v := range commonparams.LocalPVLabels {
if gotObj.Labels[v] == "" {
log.Debugf("Labels %s are not added for PV %s", v, pvName)
found = false
}
}
if !found {
return false, nil
}
return true, nil
})
if pollErr != nil {
return pollErr
}
return nil
}
// CheckReplicationAnnotationsForRemotePV checks for replication related annotations and labels for remote PV
func (c *Client) CheckReplicationAnnotationsForRemotePV(ctx context.Context, object *v1.PersistentVolume) error {
pvName := object.Name
log.Infof("Checking Replication Annotations and labels on remote PV %s ", pvName)
timeout := Timeout
if c.Timeout != 0 {
timeout = time.Duration(c.Timeout) * time.Second
}
pollErr := wait.PollImmediate(Poll, timeout,
func() (bool, error) {
select {
case <-ctx.Done():
log.Infof("Stopping pvc wait polling")
return true, fmt.Errorf("stopped waiting to be bound")
default:
break
}
gotObj, err := c.Interface.Get(ctx, pvName, metav1.GetOptions{})
if err != nil {
return false, err
}
found := true
for _, v := range commonparams.RemotePVAnnotations {
if gotObj.Annotations[v] == "" {
log.Debugf("Annotations %s are not added for remote PV %s", v, pvName)
found = false
}
}
for _, v := range commonparams.RemotePVLabels {
if gotObj.Labels[v] == "" {
log.Debugf("Labels %s are not added for remote PV %s ", v, pvName)
found = false
}
}
if !found {
return false, nil
}
return true, nil
})
if pollErr != nil {
return pollErr
}
return nil
}
func (pv *PersistentVolume) pollWait(ctx context.Context) (bool, error) {
select {
case <-ctx.Done():
log.Infof("Stopping pv wait polling")
return true, fmt.Errorf("stopped waiting to be bound")
default:
break
}
if _, err := pv.Client.Interface.Get(ctx, pv.Object.Name, metav1.GetOptions{}); err != nil {
if apierrs.IsNotFound(err) {
return true, nil
}
log.Errorf("Error while waiting for pv to be terminated: %v", err)
return false, err
}
return false, nil
}
// WaitUntilGone stalls until said Pod no longer can be found in Kubernetes
func (pv *PersistentVolume) WaitUntilGone(ctx context.Context) error {
startTime := time.Now()
timeout := Timeout
if pv.Client.Timeout != 0 {
timeout = time.Duration(pv.Client.Timeout) * time.Second
}
pollErr := wait.PollImmediate(Poll, timeout/100*95, func() (done bool, err error) {
done, err = pv.pollWait(ctx)
return done, err
})
if pollErr != nil {
gotpv, err := pv.Client.Interface.Get(ctx, pv.Object.Name, metav1.GetOptions{})
if err != nil {
return err
}
log.Errorf("Failed to delete pv: %v \n", pollErr)
log.Info("Forcing finalizers cleanup")
gotpv.SetFinalizers([]string{})
_, er := pv.Client.Interface.Update(ctx, gotpv, metav1.UpdateOptions{})
if er != nil {
return er
}
pollErr = wait.PollImmediate(Poll, timeout/2, func() (done bool, err error) {
done, err = pv.pollWait(ctx)
return done, err
})
if pollErr != nil {
log.Errorf("Failed to delete pv: %v \n", pollErr)
log.Info("Forcing finalizers cleanup")
return errors.New("failed to delete even with finalizers cleaned up")
}
}
yellow := color.New(color.FgHiYellow)
log.Debugf("Pv %s was deleted in %s", pv.Object.Name, yellow.Sprint(time.Since(startTime)))
return nil
}
// Sync waits until PV is deleted or bound
func (pv *PersistentVolume) Sync(ctx context.Context) *PersistentVolume {
if pv.Deleted {
pv.error = pv.WaitUntilGone(ctx)
} else {
pv.error = pv.WaitToBeBound(ctx)
}
return pv
}
// HasError checks if PV contains error
func (pv *PersistentVolume) HasError() bool {
if pv.error != nil {
return true
}
return false
}
// GetError returns PV error
func (pv *PersistentVolume) GetError() error {
return pv.error
}