forked from openshift/library-go
/
helpers.go
261 lines (219 loc) · 7.83 KB
/
helpers.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
package v1helpers
import (
"strings"
"time"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/util/retry"
configv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
)
// SetOperandVersion sets the new version and returns the previous value.
func SetOperandVersion(versions *[]configv1.OperandVersion, operandVersion configv1.OperandVersion) string {
if versions == nil {
versions = &[]configv1.OperandVersion{}
}
existingVersion := FindOperandVersion(*versions, operandVersion.Name)
if existingVersion == nil {
*versions = append(*versions, operandVersion)
return ""
}
previous := existingVersion.Version
existingVersion.Version = operandVersion.Version
return previous
}
func FindOperandVersion(versions []configv1.OperandVersion, name string) *configv1.OperandVersion {
if versions == nil {
return nil
}
for i := range versions {
if versions[i].Name == name {
return &versions[i]
}
}
return nil
}
func SetOperatorCondition(conditions *[]operatorv1.OperatorCondition, newCondition operatorv1.OperatorCondition) {
if conditions == nil {
conditions = &[]operatorv1.OperatorCondition{}
}
existingCondition := FindOperatorCondition(*conditions, newCondition.Type)
if existingCondition == nil {
newCondition.LastTransitionTime = metav1.NewTime(time.Now())
*conditions = append(*conditions, newCondition)
return
}
if existingCondition.Status != newCondition.Status {
existingCondition.Status = newCondition.Status
existingCondition.LastTransitionTime = metav1.NewTime(time.Now())
}
existingCondition.Reason = newCondition.Reason
existingCondition.Message = newCondition.Message
}
func RemoveOperatorCondition(conditions *[]operatorv1.OperatorCondition, conditionType string) {
if conditions == nil {
conditions = &[]operatorv1.OperatorCondition{}
}
newConditions := []operatorv1.OperatorCondition{}
for _, condition := range *conditions {
if condition.Type != conditionType {
newConditions = append(newConditions, condition)
}
}
*conditions = newConditions
}
func FindOperatorCondition(conditions []operatorv1.OperatorCondition, conditionType string) *operatorv1.OperatorCondition {
for i := range conditions {
if conditions[i].Type == conditionType {
return &conditions[i]
}
}
return nil
}
func IsOperatorConditionTrue(conditions []operatorv1.OperatorCondition, conditionType string) bool {
return IsOperatorConditionPresentAndEqual(conditions, conditionType, operatorv1.ConditionTrue)
}
func IsOperatorConditionFalse(conditions []operatorv1.OperatorCondition, conditionType string) bool {
return IsOperatorConditionPresentAndEqual(conditions, conditionType, operatorv1.ConditionFalse)
}
func IsOperatorConditionPresentAndEqual(conditions []operatorv1.OperatorCondition, conditionType string, status operatorv1.ConditionStatus) bool {
for _, condition := range conditions {
if condition.Type == conditionType {
return condition.Status == status
}
}
return false
}
// UpdateOperatorSpecFunc is a func that mutates an operator spec.
type UpdateOperatorSpecFunc func(spec *operatorv1.OperatorSpec) error
// UpdateSpec applies the update funcs to the oldStatus and tries to update via the client.
func UpdateSpec(client OperatorClient, updateFuncs ...UpdateOperatorSpecFunc) (*operatorv1.OperatorSpec, bool, error) {
updated := false
var operatorSpec *operatorv1.OperatorSpec
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
oldSpec, _, resourceVersion, err := client.GetOperatorState()
if err != nil {
return err
}
newSpec := oldSpec.DeepCopy()
for _, update := range updateFuncs {
if err := update(newSpec); err != nil {
return err
}
}
if equality.Semantic.DeepEqual(oldSpec, newSpec) {
return nil
}
operatorSpec, _, err = client.UpdateOperatorSpec(resourceVersion, newSpec)
updated = err == nil
return err
})
return operatorSpec, updated, err
}
// UpdateSpecConfigFn returns a func to update the config.
func UpdateObservedConfigFn(config map[string]interface{}) UpdateOperatorSpecFunc {
return func(oldSpec *operatorv1.OperatorSpec) error {
oldSpec.ObservedConfig = runtime.RawExtension{Object: &unstructured.Unstructured{Object: config}}
return nil
}
}
// UpdateStatusFunc is a func that mutates an operator status.
type UpdateStatusFunc func(status *operatorv1.OperatorStatus) error
// UpdateStatus applies the update funcs to the oldStatus and tries to update via the client.
func UpdateStatus(client OperatorClient, updateFuncs ...UpdateStatusFunc) (*operatorv1.OperatorStatus, bool, error) {
updated := false
var updatedOperatorStatus *operatorv1.OperatorStatus
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
_, oldStatus, resourceVersion, err := client.GetOperatorState()
if err != nil {
return err
}
newStatus := oldStatus.DeepCopy()
for _, update := range updateFuncs {
if err := update(newStatus); err != nil {
return err
}
}
if equality.Semantic.DeepEqual(oldStatus, newStatus) {
// We return the newStatus which is a deep copy of oldStatus but with all update funcs applied.
updatedOperatorStatus = newStatus
return nil
}
updatedOperatorStatus, err = client.UpdateOperatorStatus(resourceVersion, newStatus)
updated = err == nil
return err
})
return updatedOperatorStatus, updated, err
}
// UpdateConditionFunc returns a func to update a condition.
func UpdateConditionFn(cond operatorv1.OperatorCondition) UpdateStatusFunc {
return func(oldStatus *operatorv1.OperatorStatus) error {
SetOperatorCondition(&oldStatus.Conditions, cond)
return nil
}
}
// UpdateStatusFunc is a func that mutates an operator status.
type UpdateStaticPodStatusFunc func(status *operatorv1.StaticPodOperatorStatus) error
// UpdateStaticPodStatus applies the update funcs to the oldStatus abd tries to update via the client.
func UpdateStaticPodStatus(client StaticPodOperatorClient, updateFuncs ...UpdateStaticPodStatusFunc) (*operatorv1.StaticPodOperatorStatus, bool, error) {
updated := false
var updatedOperatorStatus *operatorv1.StaticPodOperatorStatus
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
_, oldStatus, resourceVersion, err := client.GetStaticPodOperatorState()
if err != nil {
return err
}
newStatus := oldStatus.DeepCopy()
for _, update := range updateFuncs {
if err := update(newStatus); err != nil {
return err
}
}
if equality.Semantic.DeepEqual(oldStatus, newStatus) {
// We return the newStatus which is a deep copy of oldStatus but with all update funcs applied.
updatedOperatorStatus = newStatus
return nil
}
updatedOperatorStatus, err = client.UpdateStaticPodOperatorStatus(resourceVersion, newStatus)
updated = err == nil
return err
})
return updatedOperatorStatus, updated, err
}
// UpdateStaticPodConditionFn returns a func to update a condition.
func UpdateStaticPodConditionFn(cond operatorv1.OperatorCondition) UpdateStaticPodStatusFunc {
return func(oldStatus *operatorv1.StaticPodOperatorStatus) error {
SetOperatorCondition(&oldStatus.Conditions, cond)
return nil
}
}
type aggregate []error
var _ utilerrors.Aggregate = aggregate{}
// NewMultiLineAggregate returns an aggregate error with multi-line output
func NewMultiLineAggregate(errList []error) error {
var errs []error
for _, e := range errList {
if e != nil {
errs = append(errs, e)
}
}
if len(errs) == 0 {
return nil
}
return aggregate(errs)
}
// Error is part of the error interface.
func (agg aggregate) Error() string {
msgs := make([]string, len(agg))
for i := range agg {
msgs[i] = agg[i].Error()
}
return strings.Join(msgs, "\n")
}
// Errors is part of the Aggregate interface.
func (agg aggregate) Errors() []error {
return []error(agg)
}