-
Notifications
You must be signed in to change notification settings - Fork 185
/
upgradeable.go
243 lines (215 loc) · 7.74 KB
/
upgradeable.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
package cvo
import (
"fmt"
"sort"
"strings"
"time"
configv1 "github.com/openshift/api/config/v1"
configlistersv1 "github.com/openshift/client-go/config/listers/config/v1"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"
"github.com/openshift/cluster-version-operator/lib/resourcedelete"
"github.com/openshift/cluster-version-operator/lib/resourcemerge"
)
// syncUpgradeable. The status is only checked if it has been more than
// the minimumUpdateCheckInterval since the last check.
func (optr *Operator) syncUpgradeable(config *configv1.ClusterVersion) error {
// updates are only checked at most once per minimumUpdateCheckInterval or if the generation changes
u := optr.getUpgradeable()
if u != nil && u.RecentlyChanged(optr.minimumUpdateCheckInterval) {
klog.V(4).Infof("Upgradeable conditions were recently checked, will try later.")
return nil
}
now := metav1.Now()
var conds []configv1.ClusterOperatorStatusCondition
var reasons []string
for _, check := range optr.upgradeableChecks {
if cond := check.Check(); cond != nil {
reasons = append(reasons, cond.Reason)
cond.LastTransitionTime = now
conds = append(conds, *cond)
}
}
if len(conds) == 1 {
conds = []configv1.ClusterOperatorStatusCondition{{
Type: configv1.OperatorUpgradeable,
Status: configv1.ConditionFalse,
Reason: conds[0].Reason,
Message: conds[0].Message,
LastTransitionTime: now,
}}
} else if len(conds) > 1 {
conds = append(conds, configv1.ClusterOperatorStatusCondition{
Type: configv1.OperatorUpgradeable,
Status: configv1.ConditionFalse,
Reason: "MultipleReasons",
Message: fmt.Sprintf("Cluster should not be upgraded between minor versions for multiple reasons: %s", strings.Join(reasons, ",")),
LastTransitionTime: now,
})
}
sort.Slice(conds, func(i, j int) bool { return conds[i].Type < conds[j].Type })
optr.setUpgradeable(&upgradeable{
Conditions: conds,
})
// requeue
optr.queue.Add(optr.queueKey())
return nil
}
type upgradeable struct {
At time.Time
// these are sorted by Type
Conditions []configv1.ClusterOperatorStatusCondition
}
func (u *upgradeable) RecentlyChanged(interval time.Duration) bool {
return u.At.After(time.Now().Add(-interval))
}
func (u *upgradeable) NeedsUpdate(original *configv1.ClusterVersion) *configv1.ClusterVersion {
if u == nil {
return nil
}
origUpConditions := collectUpgradeableConditions(original.Status.Conditions)
if equality.Semantic.DeepEqual(u.Conditions, origUpConditions) {
return nil
}
config := original.DeepCopy()
for _, c := range u.Conditions {
resourcemerge.SetOperatorStatusCondition(&config.Status.Conditions, c)
}
for _, origc := range origUpConditions {
if c := resourcemerge.FindOperatorStatusCondition(u.Conditions, origc.Type); c == nil {
resourcemerge.RemoveOperatorStatusCondition(&config.Status.Conditions, origc.Type)
}
}
return config
}
func collectUpgradeableConditions(conditions []configv1.ClusterOperatorStatusCondition) []configv1.ClusterOperatorStatusCondition {
var ret []configv1.ClusterOperatorStatusCondition
for _, c := range conditions {
if strings.HasPrefix(string(c.Type), string(configv1.OperatorUpgradeable)) {
ret = append(ret, c)
}
}
sort.Slice(ret, func(i, j int) bool { return ret[i].Type < ret[j].Type })
return ret
}
// setUpgradeable updates the currently calculated status of Upgradeable
func (optr *Operator) setUpgradeable(u *upgradeable) {
if u != nil {
u.At = time.Now()
}
optr.upgradeableStatusLock.Lock()
defer optr.upgradeableStatusLock.Unlock()
optr.upgradeable = u
}
// getUpgradeable returns the current calculated status of upgradeable. It
// may be nil.
func (optr *Operator) getUpgradeable() *upgradeable {
optr.upgradeableStatusLock.Lock()
defer optr.upgradeableStatusLock.Unlock()
return optr.upgradeable
}
type upgradeableCheck interface {
// returns a not-nil condition when the check fails.
Check() *configv1.ClusterOperatorStatusCondition
}
type clusterOperatorsUpgradeable struct {
coLister configlistersv1.ClusterOperatorLister
}
func (check *clusterOperatorsUpgradeable) Check() *configv1.ClusterOperatorStatusCondition {
cond := &configv1.ClusterOperatorStatusCondition{
Type: configv1.ClusterStatusConditionType("UpgradeableClusterOperators"),
Status: configv1.ConditionFalse,
}
ops, err := check.coLister.List(labels.Everything())
if meta.IsNoMatchError(err) {
return nil
}
if err != nil {
cond.Reason = "FailedToListClusterOperators"
cond.Message = errors.Wrap(err, "failed to list cluster operators").Error()
return cond
}
type notUpgradeableCondition struct {
name string
condition *configv1.ClusterOperatorStatusCondition
}
var notup []notUpgradeableCondition
for _, op := range ops {
if up := resourcemerge.FindOperatorStatusCondition(op.Status.Conditions, configv1.OperatorUpgradeable); up != nil && up.Status == configv1.ConditionFalse {
notup = append(notup, notUpgradeableCondition{name: op.GetName(), condition: up})
}
}
if len(notup) == 0 {
return nil
}
msg := ""
reason := ""
if len(notup) == 1 {
reason = notup[0].condition.Reason
msg = fmt.Sprintf("Cluster operator %s should not be upgraded between minor versions: %s", notup[0].name, notup[0].condition.Message)
} else {
reason = "ClusterOperatorsNotUpgradeable"
var msgs []string
for _, cond := range notup {
msgs = append(msgs, fmt.Sprintf("Cluster operator %s should not be upgraded between minor versions: %s: %s", cond.name, cond.condition.Reason, cond.condition.Message))
}
msg = fmt.Sprintf("Multiple cluster operators should not be upgraded between minor versions:\n* %s", strings.Join(msgs, "\n* "))
}
cond.Reason = reason
cond.Message = msg
return cond
}
type clusterVersionOverridesUpgradeable struct {
name string
cvLister configlistersv1.ClusterVersionLister
}
func (check *clusterVersionOverridesUpgradeable) Check() *configv1.ClusterOperatorStatusCondition {
cond := &configv1.ClusterOperatorStatusCondition{
Type: configv1.ClusterStatusConditionType("UpgradeableClusterVersionOverrides"),
Status: configv1.ConditionFalse,
}
cv, err := check.cvLister.Get(check.name)
if meta.IsNoMatchError(err) || apierrors.IsNotFound(err) {
return nil
}
overrides := false
for _, o := range cv.Spec.Overrides {
if o.Unmanaged {
overrides = true
}
}
if !overrides {
return nil
}
cond.Reason = "ClusterVersionOverridesSet"
cond.Message = "Disabling ownership via cluster version overrides prevents upgrades. Please remove overrides before continuing."
return cond
}
type clusterManifestDeleteInProgressUpgradeable struct {
}
func (check *clusterManifestDeleteInProgressUpgradeable) Check() *configv1.ClusterOperatorStatusCondition {
cond := &configv1.ClusterOperatorStatusCondition{
Type: configv1.ClusterStatusConditionType("UpgradeableDeletesInProgress"),
Status: configv1.ConditionFalse,
}
if deletes := resourcedelete.DeletesInProgress(); len(deletes) > 0 {
resources := strings.Join(deletes, ",")
klog.V(4).Infof("Resource deletions in progress; resources=%s", resources)
cond.Reason = "ResourceDeletesInProgress"
cond.Message = fmt.Sprintf("Cluster minor level upgrades are not allowed while resource deletions are in progress; resources=%s", resources)
return cond
}
return nil
}
func (optr *Operator) defaultUpgradeableChecks() []upgradeableCheck {
return []upgradeableCheck{
&clusterOperatorsUpgradeable{coLister: optr.coLister},
&clusterVersionOverridesUpgradeable{name: optr.name, cvLister: optr.cvLister},
&clusterManifestDeleteInProgressUpgradeable{},
}
}