/
handler.go
214 lines (186 loc) · 6.61 KB
/
handler.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
/*
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 restorecontroller
import (
"fmt"
"os"
"reflect"
"github.com/openebs/maya/cmd/cstor-pool-mgmt/controller/common"
"github.com/openebs/maya/cmd/cstor-pool-mgmt/volumereplica"
apis "github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog"
)
// syncHandler compares the actual state with the desired, and attempts to
// converge the two. It then updates the Status block of the CStorReplicaUpdated resource
// with the current status of the resource.
func (c *RestoreController) syncHandler(key string, operation common.QueueOperation) error {
klog.Infof("Sync handler called for key:%s with op:%s", key, operation)
rst, err := c.getCStorRestoreResource(key)
if err != nil {
return err
}
if rst == nil {
return fmt.Errorf("can not retrieve CStorRestore %q", key)
}
if IsDoneStatus(rst) || IsFailedStatus(rst) {
return nil
}
status, err := c.rstEventHandler(operation, rst)
if status == "" {
return nil
}
if err != nil {
klog.Errorf(err.Error())
rst.Status = apis.RSTCStorStatusFailed
} else {
rst.Status = apis.CStorRestoreStatus(status)
}
nrst, err := c.clientset.OpenebsV1alpha1().CStorRestores(rst.Namespace).Get(rst.Name, metav1.GetOptions{})
if err != nil {
return err
}
nrst.Status = rst.Status
_, err = c.clientset.OpenebsV1alpha1().CStorRestores(nrst.Namespace).Update(nrst)
if err != nil {
return err
}
klog.Infof("Completed operation:%v for restore:%v, status:%v", operation, nrst.Name, nrst.Status)
return nil
}
// eventHandler will execute a function according to a given operation
func (c *RestoreController) rstEventHandler(operation common.QueueOperation, rst *apis.CStorRestore) (string, error) {
switch operation {
case common.QOpAdd:
return c.addEventHandler(rst)
case common.QOpDestroy:
/*
status, err := c.rstDestroyEventHandler(rstGot)
return status, err
klog.Infof("Processing restore delete event %v, %v", rstGot.ObjectMeta.Name, string(rstGot.GetUID()))
*/
return "", nil
case common.QOpSync:
return c.syncEventHandler(rst)
case common.QOpModify:
return "", nil
}
return string(apis.RSTCStorStatusInvalid), nil
}
// addEventHandler will change the state of restore to Init state.
func (c *RestoreController) addEventHandler(rst *apis.CStorRestore) (string, error) {
if !IsPendingStatus(rst) {
return string(apis.RSTCStorStatusInvalid), nil
}
return string(apis.RSTCStorStatusInit), nil
}
// syncEventHandler will perform the restore if a given restore is in init state
func (c *RestoreController) syncEventHandler(rst *apis.CStorRestore) (string, error) {
// If the restore is in init state then only we will complete the restore
if IsInitStatus(rst) {
rst.Status = apis.RSTCStorStatusInProgress
_, err := c.clientset.OpenebsV1alpha1().CStorRestores(rst.Namespace).Update(rst)
if err != nil {
klog.Errorf("Failed to update restore:%s status : %v", rst.Name, err.Error())
return "", err
}
err = volumereplica.CreateVolumeRestore(rst)
if err != nil {
klog.Errorf("restore creation failure: %v", err.Error())
return string(apis.RSTCStorStatusFailed), err
}
c.recorder.Event(rst, corev1.EventTypeNormal, string(common.SuccessCreated), string(common.MessageResourceCreated))
klog.Infof("restore creation successful: %v, %v", rst.ObjectMeta.Name, string(rst.GetUID()))
return string(apis.RSTCStorStatusDone), nil
} else if IsPendingStatus(rst) {
klog.Infof("Updating restore:%s status to %v", rst.Name, apis.RSTCStorStatusInit)
return string(apis.RSTCStorStatusInit), nil
}
return "", nil
}
// getCStorRestoreResource returns a restore object corresponding to the resource key
func (c *RestoreController) getCStorRestoreResource(key string) (*apis.CStorRestore, error) {
// Convert the key(namespace/name) string into a distinct name
klog.V(1).Infof("Finding restore for key:%s", key)
ns, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
runtime.HandleError(fmt.Errorf("invalid resource key: %s", key))
return nil, nil
}
rst, err := c.clientset.OpenebsV1alpha1().CStorRestores(ns).Get(name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Restore resource for key:%s is missing", name)
return nil, err
}
return rst, nil
}
// IsPendingStatus is to check if the restore is in a pending state.
func IsPendingStatus(rst *apis.CStorRestore) bool {
if string(rst.Status) == string(apis.RSTCStorStatusPending) {
return true
}
return false
}
// IsInProgressStatus is to check if the restore is in in-progress state.
func IsInProgressStatus(rst *apis.CStorRestore) bool {
if string(rst.Status) == string(apis.RSTCStorStatusInProgress) {
return true
}
return false
}
// IsInitStatus is to check if the restore is in init state.
func IsInitStatus(rst *apis.CStorRestore) bool {
if string(rst.Status) == string(apis.RSTCStorStatusInit) {
return true
}
return false
}
// IsDoneStatus is to check if the restore is completed or not
func IsDoneStatus(rst *apis.CStorRestore) bool {
if string(rst.Status) == string(apis.RSTCStorStatusDone) {
return true
}
return false
}
// IsFailedStatus is to check if the restore is failed or not
func IsFailedStatus(rst *apis.CStorRestore) bool {
if string(rst.Status) == string(apis.RSTCStorStatusFailed) {
return true
}
return false
}
// IsRightCStorPoolMgmt is to check if the restore request is for particular pod/application.
func IsRightCStorPoolMgmt(rst *apis.CStorRestore) bool {
if os.Getenv(string(common.OpenEBSIOCStorID)) == rst.ObjectMeta.Labels["cstorpool.openebs.io/uid"] {
return true
}
return false
}
// IsDestroyEvent is to check if the call is for restore destroy.
func IsDestroyEvent(rst *apis.CStorRestore) bool {
if rst.ObjectMeta.DeletionTimestamp != nil {
return true
}
return false
}
// IsOnlyStatusChange is to check only status change of restore object.
func IsOnlyStatusChange(oldrst, newrst *apis.CStorRestore) bool {
if reflect.DeepEqual(oldrst.Spec, newrst.Spec) &&
!reflect.DeepEqual(oldrst.Status, newrst.Status) {
return true
}
return false
}