/
status.go
86 lines (76 loc) · 2.87 KB
/
status.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
/*
Copyright 2020 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 cspicontroller
import (
"time"
cstor "github.com/openebs/api/pkg/apis/cstor/v1"
cspiutil "github.com/openebs/cstor-operators/pkg/controllers/cspi-controller/util"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
)
//TODO: Update the code to use patch instead of Update call
// UpdateStatusConditionEventually updates the CSPI in etcd with provided
// condition. Below function retries for three times to update the CSPI with
// provided conditions
func (c *CStorPoolInstanceController) UpdateStatusConditionEventually(
cspi *cstor.CStorPoolInstance,
condition cstor.CStorPoolInstanceCondition) (*cstor.CStorPoolInstance, error) {
maxRetry := 3
cspiCopy := cspi.DeepCopy()
updatedCSPI, err := c.UpdateStatusCondition(cspiCopy, condition)
if err != nil {
klog.Errorf(
"failed to update CSPI %s status with condition %s will retry %d times at 2s interval: {%s}",
cspi.Name, condition.Type, maxRetry, err.Error())
for maxRetry > 0 {
newCSPI, err := c.clientset.
CstorV1().
CStorPoolInstances(cspi.Namespace).
Get(cspi.Name, metav1.GetOptions{})
if err != nil {
// This is possible due to etcd unavailability so do not retry more here
return cspi, errors.Wrapf(err, "failed to update cspi status")
}
updatedCSPI, err = c.UpdateStatusCondition(newCSPI, condition)
if err != nil {
maxRetry = maxRetry - 1
klog.Errorf(
"failed to update CSPI %s status with condition %s will retry %d times at 2s interval: {%s}",
cspi.Name, condition.Type, maxRetry, err.Error())
time.Sleep(2 * time.Second)
continue
}
return updatedCSPI, nil
}
// When retries are completed and still failed to update in etcd
// then it will return original object
return cspi, err
}
return updatedCSPI, nil
}
func (c *CStorPoolInstanceController) UpdateStatusCondition(
cspi *cstor.CStorPoolInstance,
condition cstor.CStorPoolInstanceCondition) (*cstor.CStorPoolInstance, error) {
cspiutil.SetCSPICondition(&cspi.Status, condition)
updatedCSPI, err := c.clientset.
CstorV1().
CStorPoolInstances(cspi.Namespace).
Update(cspi)
if err != nil {
// cspi object has already updated with the conditions so returning
// same object may or maynot make sense
return nil, errors.Wrapf(err, "failed to update cspi conditions")
}
return updatedCSPI, nil
}