-
Notifications
You must be signed in to change notification settings - Fork 82
/
operatorstatus.go
256 lines (221 loc) · 8.48 KB
/
operatorstatus.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
package operator
import (
"fmt"
"reflect"
"github.com/sirupsen/logrus"
metaapi "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/api/errors"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/util/retry"
configv1 "github.com/openshift/api/config/v1"
configv1client "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
v1 "github.com/openshift/cluster-samples-operator/pkg/apis/samples/v1"
"github.com/openshift/cluster-samples-operator/pkg/metrics"
)
const (
ClusterOperatorName = "openshift-samples"
doingDelete = "DeletionInProgress"
nonX86 = "NonX86Platform"
)
// ClusterOperatorHandler allows for wrappering access to configv1.ClusterOperator
type ClusterOperatorHandler struct {
ClusterOperatorWrapper ClusterOperatorWrapper
}
// NewClusterOperatorHandler is the default initializer function for CVOOperatorStatusHandler
func NewClusterOperatorHandler(cfgclient *configv1client.ConfigV1Client) *ClusterOperatorHandler {
handler := ClusterOperatorHandler{}
handler.ClusterOperatorWrapper = &defaultClusterStatusWrapper{configclient: cfgclient}
return &handler
}
type defaultClusterStatusWrapper struct {
configclient *configv1client.ConfigV1Client
}
func (g *defaultClusterStatusWrapper) Get(name string) (*configv1.ClusterOperator, error) {
return g.configclient.ClusterOperators().Get(name, metaapi.GetOptions{})
}
func (g *defaultClusterStatusWrapper) UpdateStatus(state *configv1.ClusterOperator) error {
_, err := g.configclient.ClusterOperators().UpdateStatus(state)
return err
}
func (g *defaultClusterStatusWrapper) Create(state *configv1.ClusterOperator) error {
_, err := g.configclient.ClusterOperators().Create(state)
return err
}
// ClusterOperatorWrapper is the interface that wrappers actuall access to github.com/operator-framework/operator-sdk/pkg/sdk
type ClusterOperatorWrapper interface {
Get(name string) (*configv1.ClusterOperator, error)
UpdateStatus(state *configv1.ClusterOperator) (err error)
Create(state *configv1.ClusterOperator) (err error)
}
// this method ensures that Available==true and Degraded==false, regardless of other conditions; it currently
// allows Progressing to be explicitly set to true or false based on the scenario the caller is addressing
func (o *ClusterOperatorHandler) setOperatorStatusWithoutInterrogatingConfig(progressing configv1.ConditionStatus, cfg *v1.Config, reason string) {
err := o.setOperatorStatus(configv1.OperatorAvailable, configv1.ConditionTrue, "", cfg.Status.Version, reason)
if err != nil {
logrus.Warningf("error occurred while attempting to set available condition: %s", err.Error())
}
err = o.setOperatorStatus(configv1.OperatorProgressing, progressing, "", cfg.Status.Version, reason)
if err != nil {
logrus.Warningf("error occurred while attempting to set progressing condition: %s", err.Error())
}
err = o.setOperatorStatus(configv1.OperatorDegraded, configv1.ConditionFalse, "", cfg.Status.Version, reason)
if err != nil {
logrus.Warningf("error occurred while attempting to set degraded condition: %s", err.Error())
}
}
func (o *ClusterOperatorHandler) UpdateOperatorStatus(cfg *v1.Config, deletionInProgress bool) error {
if deletionInProgress {
o.setOperatorStatusWithoutInterrogatingConfig(configv1.ConditionTrue, cfg, doingDelete)
// will ignore errors in delete path, but we at least log them above
return nil
}
if len(cfg.Spec.Architectures) > 0 && cfg.Spec.Architectures[0] != v1.AMDArchitecture && cfg.Spec.Architectures[0] != v1.X86Architecture {
o.setOperatorStatusWithoutInterrogatingConfig(configv1.ConditionFalse, cfg, nonX86)
// will ignore errors in non-x86 path, but we at least log them above
return nil
}
errs := []error{}
degraded, degradedReason, degradedDetail := cfg.ClusterOperatorStatusDegradedCondition()
err := o.setOperatorStatus(configv1.OperatorDegraded,
degraded,
degradedDetail,
"",
degradedReason)
if err != nil {
// note error but try the other conditions
errs = append(errs, err)
logrus.Warningf("error occurred while attempting to set degraded condition: %s", err.Error())
}
flagForMetric := false
if degraded == configv1.ConditionTrue {
flagForMetric = true
}
metrics.Degraded(flagForMetric)
available, reasonForAvailable, msgForAvailable := cfg.ClusterOperatorStatusAvailableCondition()
// if we're setting the operator status to available, also set the operator version
// to the current version.
err = o.setOperatorStatus(configv1.OperatorAvailable,
available,
msgForAvailable,
cfg.Status.Version,
reasonForAvailable)
if err != nil {
// note error but try the other conditions
errs = append(errs, err)
logrus.Warningf("error occurred while attempting to set available condition: %s", err.Error())
}
progressing, reasonForProgressing, msgForProgressing := cfg.ClusterOperatorStatusProgressingCondition(degradedReason, available)
err = o.setOperatorStatus(configv1.OperatorProgressing,
progressing,
msgForProgressing,
"",
reasonForProgressing)
if err != nil {
// note error but try the other conditions
errs = append(errs, err)
logrus.Warningf("error occurred while attempting to set progressing condition: %s", err.Error())
}
return utilerrors.NewAggregate(errs)
}
func (o *ClusterOperatorHandler) setOperatorStatus(condtype configv1.ClusterStatusConditionType, status configv1.ConditionStatus, msg, currentVersion, reason string) error {
logrus.Debugf("setting clusteroperator status condition %s to %s with version %s", condtype, status, currentVersion)
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
state, err := o.ClusterOperatorWrapper.Get(ClusterOperatorName)
if err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get cluster operator resource %s/%s: %s", state.ObjectMeta.Namespace, state.ObjectMeta.Name, err)
}
state = &configv1.ClusterOperator{}
state.Name = ClusterOperatorName
state.Status.Conditions = []configv1.ClusterOperatorStatusCondition{
{
Type: configv1.OperatorAvailable,
Status: configv1.ConditionUnknown,
LastTransitionTime: metaapi.Now(),
},
{
Type: configv1.OperatorProgressing,
Status: configv1.ConditionUnknown,
LastTransitionTime: metaapi.Now(),
},
{
Type: configv1.OperatorDegraded,
Status: configv1.ConditionUnknown,
LastTransitionTime: metaapi.Now(),
},
}
if len(currentVersion) > 0 {
state.Status.Versions = []configv1.OperandVersion{
{
Name: "operator",
Version: currentVersion,
},
}
}
o.updateOperatorCondition(state, &configv1.ClusterOperatorStatusCondition{
Type: condtype,
Status: status,
Message: msg,
Reason: reason,
LastTransitionTime: metaapi.Now(),
})
return o.ClusterOperatorWrapper.Create(state)
}
modified := o.updateOperatorCondition(state, &configv1.ClusterOperatorStatusCondition{
Type: condtype,
Status: status,
Message: msg,
Reason: reason,
LastTransitionTime: metaapi.Now(),
})
// set a new current version when it is provided
if len(currentVersion) > 0 {
oldVersions := state.Status.Versions
state.Status.Versions = []configv1.OperandVersion{
{
Name: "operator",
Version: currentVersion,
},
}
if !reflect.DeepEqual(state.Status.Versions, oldVersions) {
modified = true
}
}
if !modified {
return nil
}
state.Status.RelatedObjects = []configv1.ObjectReference{
{Group: v1.GroupName, Resource: "configs", Name: "cluster"},
{Resource: "namespaces", Name: v1.OperatorNamespace},
{Resource: "namespaces", Name: "openshift"},
}
return o.ClusterOperatorWrapper.UpdateStatus(state)
})
}
func (o *ClusterOperatorHandler) updateOperatorCondition(op *configv1.ClusterOperator, condition *configv1.ClusterOperatorStatusCondition) (modified bool) {
found := false
conditions := []configv1.ClusterOperatorStatusCondition{}
for _, c := range op.Status.Conditions {
if condition.Type != c.Type {
conditions = append(conditions, c)
continue
}
if condition.Status != c.Status {
modified = true
}
if condition.Message != c.Message {
modified = true
}
if condition.Reason != c.Reason {
modified = true
}
conditions = append(conditions, *condition)
found = true
}
if !found {
conditions = append(conditions, *condition)
modified = true
}
op.Status.Conditions = conditions
return
}