-
Notifications
You must be signed in to change notification settings - Fork 65
/
condition_manager.go
163 lines (133 loc) · 5.12 KB
/
condition_manager.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
// Copyright 2021 VMware
//
// 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 conditions
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
import (
"reflect"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// To learn more about condition conventions:
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties
// Polarity represents how a Status is represented as success.
type Polarity string
// Positive Polarity means a "True" ConditionStatus is a success
const Positive Polarity = "Positive"
// Negative Polarity means a "False" ConditionStatus is a success
const Negative Polarity = "Negative"
//counterfeiter:generate . ConditionManager
// ConditionManager supports collecting condition statuses for your controller
// It adds a complete top level condition when Finalize is called.
//
// TBD: either error or warn if the same Condition.Type is reused
// TBD2: should accept an existing []Condition slice to compare against
// should only update LastTransitionTime if the other fields have changed
type ConditionManager interface {
// Add a condition and associate a polarity with it.
Add(condition metav1.Condition, positive Polarity)
// AddPositive Adds a condition with a positive polarity
AddPositive(condition metav1.Condition)
// AddNegative Adds a condition with a negative polarity
AddNegative(condition metav1.Condition)
// Finalize returns all conditions
// not idempotent! subsequent finalizes will keep adding Parent conditions
// The changed result represents whether the conditions have changed enough to warrant an update to the APIServer
Finalize() (conditions []metav1.Condition, changed bool)
// IsSuccessful can be called any time. Start's off true, but an
// add of an unsuccessful condition (Positive-False or Negative-True)
// causes this to return false
IsSuccessful() bool
}
type conditionManager struct {
previousConditions []metav1.Condition
conditions []metav1.Condition
topLevelType string
status metav1.ConditionStatus
changed bool
reason, message string
}
type ConditionManagerBuilder func(topLevelType string, previousConditions []metav1.Condition) ConditionManager
// NewConditionManager returns a ConditionManager with a top level
// Condition.Type specified in topLevelType
func NewConditionManager(topLevelType string, previousConditions []metav1.Condition) ConditionManager {
return &conditionManager{
previousConditions: previousConditions,
conditions: []metav1.Condition{},
topLevelType: topLevelType,
status: metav1.ConditionTrue,
}
}
func (c *conditionManager) Add(condition metav1.Condition, polarity Polarity) {
condition.LastTransitionTime = metav1.Now()
if (condition.Status == metav1.ConditionFalse && polarity == Positive) ||
(condition.Status == metav1.ConditionTrue && polarity == Negative) {
c.status = metav1.ConditionFalse
c.reason = condition.Reason
c.message = condition.Message
} else if condition.Status == metav1.ConditionUnknown {
if c.status == metav1.ConditionTrue {
c.status = metav1.ConditionUnknown
c.reason = condition.Reason
c.message = condition.Message
}
}
isNewCondition := true
for _, previousCondition := range c.previousConditions {
if previousCondition.Type == condition.Type {
isNewCondition = false
lastTransitionTime := condition.LastTransitionTime
condition.LastTransitionTime = previousCondition.LastTransitionTime
if !reflect.DeepEqual(previousCondition, condition) {
condition.LastTransitionTime = lastTransitionTime
c.changed = true
}
}
}
if isNewCondition {
c.changed = true
}
c.conditions = append(c.conditions, condition)
}
func (c *conditionManager) IsSuccessful() bool {
return !(c.status == metav1.ConditionFalse)
}
func (c *conditionManager) AddPositive(condition metav1.Condition) {
c.Add(condition, Positive)
}
func (c *conditionManager) AddNegative(condition metav1.Condition) {
c.Add(condition, Negative)
}
func (c *conditionManager) Finalize() ([]metav1.Condition, bool) {
if len(c.conditions) == 0 {
c.status = metav1.ConditionFalse
return []metav1.Condition{{
Type: c.topLevelType,
Status: "Unknown",
Reason: "Unknown",
LastTransitionTime: metav1.Now(),
}}, true
}
if c.status == metav1.ConditionTrue {
c.reason = "Ready"
}
c.AddPositive(
metav1.Condition{
Type: c.topLevelType,
Status: c.status,
LastTransitionTime: metav1.Now(),
Reason: c.reason,
Message: c.message,
},
)
return c.conditions, c.changed
}