Skip to content

Commit 47f1c0a

Browse files
authored
Merge pull request #831 from n3wscott/manager
Introduce a conditions manager.
2 parents db9545b + 22869d9 commit 47f1c0a

File tree

4 files changed

+207
-2
lines changed

4 files changed

+207
-2
lines changed

apis/common/v1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hack/boilerplate.go.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2019 The Crossplane Authors.
2+
Copyright 2025 The Crossplane Authors.
33

44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

pkg/conditions/manager.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2025 The Crossplane Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package conditions enables consistent interactions with an object's status conditions.
18+
package conditions
19+
20+
import (
21+
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
22+
"github.com/crossplane/crossplane-runtime/pkg/resource"
23+
)
24+
25+
// ObjectWithConditions is the interface definition that allows.
26+
type ObjectWithConditions interface {
27+
resource.Object
28+
resource.Conditioned
29+
}
30+
31+
// Manager is an interface for a stateless factory-like object that produces ConditionSet objects.
32+
type Manager interface {
33+
// For returns an implementation of a ConditionSet to operate on a specific ObjectWithConditions.
34+
For(o ObjectWithConditions) ConditionSet
35+
}
36+
37+
// ConditionSet holds operations for interacting with an object's conditions.
38+
type ConditionSet interface {
39+
// MarkConditions adds or updates the conditions onto the managed resource object. Unlike a "Set" method, this also
40+
// can add contextual updates to the condition such as propagating the correct observedGeneration to the conditions
41+
// being changed.
42+
MarkConditions(condition ...xpv1.Condition)
43+
}
44+
45+
// ObservedGenerationPropagationManager is the top level factor for producing a ConditionSet
46+
// on behalf of a ObjectWithConditions resource, the ConditionSet is only currently concerned with
47+
// propagating observedGeneration to conditions that are being updated.
48+
// observedGenerationPropagationManager implements Manager.
49+
type ObservedGenerationPropagationManager struct{}
50+
51+
// For implements Manager.For.
52+
func (m ObservedGenerationPropagationManager) For(o ObjectWithConditions) ConditionSet {
53+
return &observedGenerationPropagationConditionSet{o: o}
54+
}
55+
56+
// observedGenerationPropagationConditionSet propagates the meta.generation of the given object
57+
// to the observedGeneration of any condition being set via the `MarkConditions` method.
58+
type observedGenerationPropagationConditionSet struct {
59+
o ObjectWithConditions
60+
}
61+
62+
// MarkConditions implements ConditionSet.MarkConditions.
63+
func (c *observedGenerationPropagationConditionSet) MarkConditions(condition ...xpv1.Condition) {
64+
if c == nil || c.o == nil {
65+
return
66+
}
67+
// Foreach condition we have been sent to mark, update the observed generation.
68+
for i := range condition {
69+
condition[i].ObservedGeneration = c.o.GetGeneration()
70+
}
71+
c.o.SetConditions(condition...)
72+
}

pkg/conditions/manager_test.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
Copyright 2025 The Crossplane Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package conditions
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
"time"
23+
24+
"github.com/google/go-cmp/cmp"
25+
"github.com/google/go-cmp/cmp/cmpopts"
26+
27+
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
28+
"github.com/crossplane/crossplane-runtime/pkg/resource/fake"
29+
"github.com/crossplane/crossplane-runtime/pkg/test"
30+
)
31+
32+
// Check that conditionsImpl implements ConditionManager.
33+
var _ Manager = (*ObservedGenerationPropagationManager)(nil)
34+
35+
// Check that conditionSet implements ConditionSet.
36+
var _ ConditionSet = (*observedGenerationPropagationConditionSet)(nil)
37+
38+
func TestOGConditionSetMark(t *testing.T) {
39+
manager := new(ObservedGenerationPropagationManager)
40+
41+
tests := map[string]struct {
42+
reason string
43+
start []xpv1.Condition
44+
mark []xpv1.Condition
45+
want []xpv1.Condition
46+
}{
47+
"ProvideNoConditions": {
48+
reason: "If updating a resource without conditions with no new conditions, conditions should remain empty.",
49+
start: nil,
50+
mark: nil,
51+
want: nil,
52+
},
53+
"EmptyAppendCondition": {
54+
reason: "If starting with a resource without conditions, and we mark a condition, it should propagate to conditions with the correct generation.",
55+
start: nil,
56+
mark: []xpv1.Condition{xpv1.ReconcileSuccess()},
57+
want: []xpv1.Condition{xpv1.ReconcileSuccess().WithObservedGeneration(42)},
58+
},
59+
"ExistingMarkNothing": {
60+
reason: "If the resource has a condition and we update nothing, nothing should change.",
61+
start: []xpv1.Condition{xpv1.Available().WithObservedGeneration(1)},
62+
mark: nil,
63+
want: []xpv1.Condition{xpv1.Available().WithObservedGeneration(1)},
64+
},
65+
"ExistingUpdated": {
66+
reason: "If a resource starts with a condition, and we update it, we should see the observedGeneration be updated",
67+
start: []xpv1.Condition{xpv1.ReconcileSuccess().WithObservedGeneration(1)},
68+
mark: []xpv1.Condition{xpv1.ReconcileSuccess()},
69+
want: []xpv1.Condition{xpv1.ReconcileSuccess().WithObservedGeneration(42)},
70+
},
71+
"ExistingAppended": {
72+
reason: "If a resource has an existing condition and we make another condition, the new condition should merge into the conditions list.",
73+
start: []xpv1.Condition{xpv1.Available().WithObservedGeneration(1)},
74+
mark: []xpv1.Condition{xpv1.ReconcileSuccess()},
75+
want: []xpv1.Condition{xpv1.Available().WithObservedGeneration(1), xpv1.ReconcileSuccess().WithObservedGeneration(42)},
76+
},
77+
}
78+
for name, tt := range tests {
79+
t.Run(name, func(t *testing.T) {
80+
ut := newManaged(42, tt.start...)
81+
c := manager.For(ut)
82+
c.MarkConditions(tt.mark...)
83+
if diff := cmp.Diff(tt.want, ut.Conditions, test.EquateConditions(), cmpopts.EquateApproxTime(1*time.Second)); diff != "" {
84+
t.Errorf("\nReason: %s\n-want, +got:\n%s", tt.reason, diff)
85+
}
86+
})
87+
}
88+
89+
t.Run("ManageNilObject", func(t *testing.T) {
90+
c := manager.For(nil)
91+
if c == nil {
92+
t.Errorf("manager.For(nil) = %v, want non-nil", c)
93+
}
94+
// Test that Marking on a Manager that has a nil object does not end up panicking.
95+
c.MarkConditions(xpv1.ReconcileSuccess())
96+
// Success!
97+
})
98+
}
99+
100+
func TestOGManagerFor(t *testing.T) {
101+
tests := map[string]struct {
102+
reason string
103+
o ObjectWithConditions
104+
want ConditionSet
105+
}{
106+
"NilObject": {
107+
reason: "Even if an object is nil, the manager should return a non-nil ConditionSet",
108+
want: &observedGenerationPropagationConditionSet{},
109+
},
110+
"Object": {
111+
reason: "Object propagates into manager.",
112+
o: &fake.Managed{},
113+
want: &observedGenerationPropagationConditionSet{
114+
o: &fake.Managed{},
115+
},
116+
},
117+
}
118+
for name, tt := range tests {
119+
t.Run(name, func(t *testing.T) {
120+
m := &ObservedGenerationPropagationManager{}
121+
if got := m.For(tt.o); !reflect.DeepEqual(got, tt.want) {
122+
t.Errorf("\nReason: %s\nFor() = %v, want %v", tt.reason, got, tt.want)
123+
}
124+
})
125+
}
126+
}
127+
128+
func newManaged(generation int64, conditions ...xpv1.Condition) *fake.Managed {
129+
mg := &fake.Managed{}
130+
mg.Generation = generation
131+
mg.SetConditions(conditions...)
132+
return mg
133+
}

0 commit comments

Comments
 (0)