Skip to content

Commit a3711b4

Browse files
authored
Merge pull request #11253 from m-messiah/export-compare
🌱Export conditions.HasSameState method
2 parents 4bb8fd5 + 491860c commit a3711b4

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

util/conditions/getter.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Getter interface {
3333
GetConditions() clusterv1.Conditions
3434
}
3535

36-
// Get returns the condition with the given type, if the condition does not exists,
36+
// Get returns the condition with the given type, if the condition does not exist,
3737
// it returns nil.
3838
func Get(from Getter, t clusterv1.ConditionType) *clusterv1.Condition {
3939
conditions := from.GetConditions()
@@ -54,7 +54,7 @@ func Has(from Getter, t clusterv1.ConditionType) bool {
5454
return Get(from, t) != nil
5555
}
5656

57-
// IsTrue is true if the condition with the given type is True, otherwise it return false
57+
// IsTrue is true if the condition with the given type is True, otherwise it returns false
5858
// if the condition is not True or if the condition does not exist (is nil).
5959
func IsTrue(from Getter, t clusterv1.ConditionType) bool {
6060
if c := Get(from, t); c != nil {
@@ -63,7 +63,7 @@ func IsTrue(from Getter, t clusterv1.ConditionType) bool {
6363
return false
6464
}
6565

66-
// IsFalse is true if the condition with the given type is False, otherwise it return false
66+
// IsFalse is true if the condition with the given type is False, otherwise it returns false
6767
// if the condition is not False or if the condition does not exist (is nil).
6868
func IsFalse(from Getter, t clusterv1.ConditionType) bool {
6969
if c := Get(from, t); c != nil {
@@ -208,7 +208,7 @@ type mirrorOptions struct {
208208
// MirrorOptions defines an option for mirroring conditions.
209209
type MirrorOptions func(*mirrorOptions)
210210

211-
// WithFallbackValue specify a fallback value to use in case the mirrored condition does not exists;
211+
// WithFallbackValue specify a fallback value to use in case the mirrored condition does not exist;
212212
// in case the fallbackValue is false, given values for reason, severity and message will be used.
213213
func WithFallbackValue(fallbackValue bool, reason string, severity clusterv1.ConditionSeverity, message string) MirrorOptions {
214214
return func(c *mirrorOptions) {
@@ -220,7 +220,7 @@ func WithFallbackValue(fallbackValue bool, reason string, severity clusterv1.Con
220220
}
221221

222222
// mirror mirrors the Ready condition from a dependent object into the target condition;
223-
// if the Ready condition does not exists in the source object, no target conditions is generated.
223+
// if the Ready condition does not exist in the source object, no target conditions is generated.
224224
// NOTE: Considering that we are mirroring Ready conditions with positive polarity, also the resulting condition will have positive polarity.
225225
func mirror(from Getter, targetCondition clusterv1.ConditionType, options ...MirrorOptions) *clusterv1.Condition {
226226
mirrorOpt := &mirrorOptions{}
@@ -247,7 +247,7 @@ func mirror(from Getter, targetCondition clusterv1.ConditionType, options ...Mir
247247
}
248248

249249
// Aggregates all the Ready condition from a list of dependent objects into the target object;
250-
// if the Ready condition does not exists in one of the source object, the object is excluded from
250+
// if the Ready condition does not exist in one of the source object, the object is excluded from
251251
// the aggregation; if none of the source object have ready condition, no target conditions is generated.
252252
// NOTE: Considering that we are aggregating Ready conditions with positive polarity, also the resulting condition will have positive polarity.
253253
func aggregate(from []Getter, targetCondition clusterv1.ConditionType, options ...MergeOption) *clusterv1.Condition {

util/conditions/matchers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (matcher *conditionMatcher) Match(actual interface{}) (success bool, err er
4242
return false, errors.New("value should be a condition")
4343
}
4444

45-
return hasSameState(actualCondition, matcher.Expected), nil
45+
return HasSameState(actualCondition, matcher.Expected), nil
4646
}
4747

4848
func (matcher *conditionMatcher) FailureMessage(actual interface{}) (message string) {

util/conditions/merge_strategies.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type mergeOptions struct {
3838
type MergeOption func(*mergeOptions)
3939

4040
// WithConditions instructs merge about the condition types to consider when doing a merge operation;
41-
// if this option is not specified, all the conditions (excepts Ready) will be considered. This is required
41+
// if this option is not specified, all the conditions (excepts Ready) will be considered. This is required,
4242
// so we can provide some guarantees about the semantic of the target condition without worrying about
4343
// side effects if someone or something adds custom conditions to the objects.
4444
//

util/conditions/patch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (p Patch) Apply(latest Setter, options ...ApplyOption) error {
152152
// If the condition is already on latest, check if latest and after agree on the change; if not, this is a conflict.
153153
if latestCondition := Get(latest, conditionPatch.After.Type); latestCondition != nil {
154154
// If latest and after agree on the change, then it is a conflict.
155-
if !hasSameState(latestCondition, conditionPatch.After) {
155+
if !HasSameState(latestCondition, conditionPatch.After) {
156156
return errors.Errorf("error patching conditions: The condition %q was modified by a different process and this caused a merge/AddCondition conflict: %v", conditionPatch.After.Type, cmp.Diff(latestCondition, conditionPatch.After))
157157
}
158158
// otherwise, the latest is already as intended.
@@ -179,7 +179,7 @@ func (p Patch) Apply(latest Setter, options ...ApplyOption) error {
179179
// If the condition on the latest is different from the base condition, check if
180180
// the after state corresponds to the desired value. If not this is a conflict (unless we should ignore conflicts for this condition type).
181181
if !reflect.DeepEqual(latestCondition, conditionPatch.Before) {
182-
if !hasSameState(latestCondition, conditionPatch.After) {
182+
if !HasSameState(latestCondition, conditionPatch.After) {
183183
return errors.Errorf("error patching conditions: The condition %q was modified by a different process and this caused a merge/ChangeCondition conflict: %v", conditionPatch.After.Type, cmp.Diff(latestCondition, conditionPatch.After))
184184
}
185185
// Otherwise the latest is already as intended.
@@ -199,7 +199,7 @@ func (p Patch) Apply(latest Setter, options ...ApplyOption) error {
199199
// If the condition is still on the latest, check if it is changed in the meantime;
200200
// if so then this is a conflict.
201201
if latestCondition := Get(latest, conditionPatch.Before.Type); latestCondition != nil {
202-
if !hasSameState(latestCondition, conditionPatch.Before) {
202+
if !HasSameState(latestCondition, conditionPatch.Before) {
203203
return errors.Errorf("error patching conditions: The condition %q was modified by a different process and this caused a merge/RemoveCondition conflict: %v", conditionPatch.Before.Type, cmp.Diff(latestCondition, conditionPatch.Before))
204204
}
205205
}

util/conditions/setter.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Set(to Setter, condition *clusterv1.Condition) {
5151
existingCondition := conditions[i]
5252
if existingCondition.Type == condition.Type {
5353
exists = true
54-
if !hasSameState(&existingCondition, condition) {
54+
if !HasSameState(&existingCondition, condition) {
5555
condition.LastTransitionTime = metav1.NewTime(time.Now().UTC().Truncate(time.Second))
5656
conditions[i] = *condition
5757
break
@@ -157,13 +157,13 @@ func SetSummary(to Setter, options ...MergeOption) {
157157
}
158158

159159
// SetMirror creates a new condition by mirroring the Ready condition from a dependent object;
160-
// if the Ready condition does not exists in the source object, no target conditions is generated.
160+
// if the Ready condition does not exist in the source object, no target conditions is generated.
161161
func SetMirror(to Setter, targetCondition clusterv1.ConditionType, from Getter, options ...MirrorOptions) {
162162
Set(to, mirror(from, targetCondition, options...))
163163
}
164164

165165
// SetAggregate creates a new condition with the aggregation of all the Ready condition
166-
// from a list of dependent objects; if the Ready condition does not exists in one of the source object,
166+
// from a list of dependent objects; if the Ready condition does not exist in one of the source object,
167167
// the object is excluded from the aggregation; if none of the source object have ready condition,
168168
// no target conditions is generated.
169169
func SetAggregate(to Setter, targetCondition clusterv1.ConditionType, from []Getter, options ...MergeOption) {
@@ -186,17 +186,17 @@ func Delete(to Setter, t clusterv1.ConditionType) {
186186
to.SetConditions(newConditions)
187187
}
188188

189-
// lexicographicLess returns true if a condition is less than another with regards to the
190-
// to order of conditions designed for convenience of the consumer, i.e. kubectl.
189+
// lexicographicLess returns true if a condition is less than another in regard to
190+
// the order of conditions designed for convenience of the consumer, i.e. kubectl.
191191
// According to this order the Ready condition always goes first, followed by all the other
192192
// conditions sorted by Type.
193193
func lexicographicLess(i, j *clusterv1.Condition) bool {
194194
return (i.Type == clusterv1.ReadyCondition || i.Type < j.Type) && j.Type != clusterv1.ReadyCondition
195195
}
196196

197-
// hasSameState returns true if a condition has the same state of another; state is defined
197+
// HasSameState returns true if a condition has the same state of another; state is defined
198198
// by the union of following fields: Type, Status, Reason, Severity and Message (it excludes LastTransitionTime).
199-
func hasSameState(i, j *clusterv1.Condition) bool {
199+
func HasSameState(i, j *clusterv1.Condition) bool {
200200
return i.Type == j.Type &&
201201
i.Status == j.Status &&
202202
i.Reason == j.Reason &&

util/conditions/setter_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,33 @@ func TestHasSameState(t *testing.T) {
3535

3636
// same condition
3737
falseInfo2 := falseInfo1.DeepCopy()
38-
g.Expect(hasSameState(falseInfo1, falseInfo2)).To(BeTrue())
38+
g.Expect(HasSameState(falseInfo1, falseInfo2)).To(BeTrue())
3939

4040
// different LastTransitionTime does not impact state
4141
falseInfo2 = falseInfo1.DeepCopy()
4242
falseInfo2.LastTransitionTime = metav1.NewTime(time.Date(1900, time.November, 10, 23, 0, 0, 0, time.UTC))
43-
g.Expect(hasSameState(falseInfo1, falseInfo2)).To(BeTrue())
43+
g.Expect(HasSameState(falseInfo1, falseInfo2)).To(BeTrue())
4444

4545
// different Type, Status, Reason, Severity and Message determine different state
4646
falseInfo2 = falseInfo1.DeepCopy()
4747
falseInfo2.Type = "another type"
48-
g.Expect(hasSameState(falseInfo1, falseInfo2)).To(BeFalse())
48+
g.Expect(HasSameState(falseInfo1, falseInfo2)).To(BeFalse())
4949

5050
falseInfo2 = falseInfo1.DeepCopy()
5151
falseInfo2.Status = corev1.ConditionTrue
52-
g.Expect(hasSameState(falseInfo1, falseInfo2)).To(BeFalse())
52+
g.Expect(HasSameState(falseInfo1, falseInfo2)).To(BeFalse())
5353

5454
falseInfo2 = falseInfo1.DeepCopy()
5555
falseInfo2.Severity = clusterv1.ConditionSeverityWarning
56-
g.Expect(hasSameState(falseInfo1, falseInfo2)).To(BeFalse())
56+
g.Expect(HasSameState(falseInfo1, falseInfo2)).To(BeFalse())
5757

5858
falseInfo2 = falseInfo1.DeepCopy()
5959
falseInfo2.Reason = "another severity"
60-
g.Expect(hasSameState(falseInfo1, falseInfo2)).To(BeFalse())
60+
g.Expect(HasSameState(falseInfo1, falseInfo2)).To(BeFalse())
6161

6262
falseInfo2 = falseInfo1.DeepCopy()
6363
falseInfo2.Message = "another message"
64-
g.Expect(hasSameState(falseInfo1, falseInfo2)).To(BeFalse())
64+
g.Expect(HasSameState(falseInfo1, falseInfo2)).To(BeFalse())
6565
}
6666

6767
func TestLexicographicLess(t *testing.T) {
@@ -304,7 +304,7 @@ func (matcher *ConditionsMatcher) Match(actual interface{}) (success bool, err e
304304
}
305305

306306
for i := range actualConditions {
307-
if !hasSameState(&actualConditions[i], &matcher.Expected[i]) {
307+
if !HasSameState(&actualConditions[i], &matcher.Expected[i]) {
308308
return false, nil
309309
}
310310
}

0 commit comments

Comments
 (0)