/
content.go
215 lines (188 loc) · 5.85 KB
/
content.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
/*
*
* 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 v1
import (
"context"
"errors"
"fmt"
"time"
"github.com/dell/cert-csi/pkg/utils"
"github.com/fatih/color"
v1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
snapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/clientset/versioned/typed/volumesnapshot/v1"
"github.com/sirupsen/logrus"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
)
const (
// Poll is a poll interval for Pod
Poll = 2 * time.Second
// Timeout is a timeout for Pod operations
Timeout = 300 * time.Second
)
// SnapshotContentClient is a client for managing volume snapshot content
type SnapshotContentClient struct {
Interface snapshotv1.VolumeSnapshotContentInterface
Namespace string
Timeout int
}
// SnapshotContent contains parameters needed for managing snapshot content
type SnapshotContent struct {
Client *SnapshotContentClient
Object *v1.VolumeSnapshotContent
Deleted bool
// Used when error arises in syncable methods
error error
}
// Delete deletes a volume snapshot content
func (scc *SnapshotContentClient) Delete(ctx context.Context, snap *v1.VolumeSnapshotContent) *SnapshotContent {
var funcErr error
err := scc.Interface.Delete(ctx, snap.Name, metav1.DeleteOptions{})
if err != nil {
funcErr = err
}
logrus.Debugf("Deleted SnapshotContent %s", snap.Name)
return &SnapshotContent{
Client: scc,
Object: snap,
Deleted: true,
error: funcErr,
}
}
// DeleteAll deletes all snapshot contents for a PVC
func (scc *SnapshotContentClient) DeleteAll(ctx context.Context) error {
log := utils.GetLoggerFromContext(ctx)
snapList, snapErr := scc.Interface.List(ctx, metav1.ListOptions{})
if snapErr != nil {
return snapErr
}
log.Debugf("Deleting all SnapContents")
for i, pvc := range snapList.Items {
log.Debugf("Deleting snapContent %s", pvc.Name)
err := scc.Delete(ctx, &snapList.Items[i]).Sync(ctx).GetError()
if err != nil {
log.Errorf("Can't delete snapContent %s; error=%v", pvc.Name, err)
}
}
return nil
}
// WaitUntilGone waits until snapshot content is deleted
func (cont *SnapshotContent) WaitUntilGone(ctx context.Context) error {
log := utils.GetLoggerFromContext(ctx)
startTime := time.Now()
timeout := Timeout
if cont.Client.Timeout != 0 {
timeout = time.Duration(cont.Client.Timeout) * time.Second
}
pollErr := wait.PollImmediate(Poll, timeout/2, func() (done bool, err error) {
done, err = cont.pollWait(ctx)
return done, err
})
if pollErr != nil {
log.Errorf("Failed to delete snap: %v \n", pollErr)
log.Info("Forcing finalizers cleanup")
gotsnap, err := cont.Client.Interface.Get(ctx, cont.Object.Name, metav1.GetOptions{})
if err != nil {
return err
}
gotsnap.SetFinalizers([]string{})
_, er := cont.Client.Interface.Update(ctx, gotsnap, metav1.UpdateOptions{})
if er != nil {
return er
}
pollErr = wait.PollImmediate(Poll, timeout/100*95, func() (done bool, err error) {
done, err = cont.pollWait(ctx)
return done, err
})
if pollErr != nil {
log.Errorf("Failed to delete snap: %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("snap %s was deleted in %s", cont.Object.Name, yellow.Sprint(time.Since(startTime)))
return nil
}
func (cont *SnapshotContent) pollWait(ctx context.Context) (bool, error) {
log := utils.GetLoggerFromContext(ctx)
select {
case <-ctx.Done():
log.Infof("Stopping Snap wait polling")
return true, fmt.Errorf("stopped waiting to be ready")
default:
break
}
if _, err := cont.Client.Interface.Get(ctx, cont.Object.Name, metav1.GetOptions{}); err != nil {
if apierrs.IsNotFound(err) {
return true, nil
}
log.Errorf("Error while waiting for Snap to be terminated: %v", err)
return false, err
}
return false, nil
}
// WaitForRunning stalls until pod is ready
func (cont *SnapshotContent) WaitForRunning(ctx context.Context) error {
log := utils.GetLoggerFromContext(ctx)
log.Infof("Waiting for SnapshotCont '%s' to be Presented", cont.Object.Name)
timeout := Timeout
if cont.Client.Timeout != 0 {
timeout = time.Duration(cont.Client.Timeout) * time.Second
}
pollErr := wait.PollImmediate(Poll, timeout,
func() (bool, error) {
select {
case <-ctx.Done():
log.Infof("Stopping Snap wait polling")
return true, fmt.Errorf("stopped waiting to be ready")
default:
break
}
_, err := cont.Client.Interface.Get(ctx, cont.Object.Name, metav1.GetOptions{})
if err != nil {
log.Errorf("Can't find snap %s", cont.Object.Name)
return false, err
}
ready := true
return ready, nil
})
if pollErr != nil {
return pollErr
}
return nil
}
// HasError checks if snapshot content has error
func (cont *SnapshotContent) HasError() bool {
if cont.error != nil {
return true
}
return false
}
// GetError returns snapshot content error
func (cont *SnapshotContent) GetError() error {
return cont.error
}
// Sync updates snapshot content state
func (cont *SnapshotContent) Sync(ctx context.Context) *SnapshotContent {
if cont.Deleted {
cont.error = cont.WaitUntilGone(ctx)
} else {
cont.error = cont.WaitForRunning(ctx)
}
return cont
}