Skip to content

Commit 0bb5691

Browse files
committed
feat(sub): add subscription installplan status conditions
- Add Subscription InstallPlan status conditions - Add better condition getters/setters to InstallPlan status - Add hashstructure set tag to Subscription condition - Update internal version types to match
1 parent f0f5ddc commit 0bb5691

File tree

4 files changed

+147
-40
lines changed

4 files changed

+147
-40
lines changed

pkg/api/apis/operators/installplan_types.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,23 @@ type InstallPlanCondition struct {
9898
// allow overwriting `now` function for deterministic tests
9999
var now = metav1.Now
100100

101-
// SetCondition adds or updates a condition, using `Type` as merge key
102-
func (s *InstallPlanStatus) SetCondition(cond InstallPlanCondition) InstallPlanCondition {
103-
updated := now()
104-
cond.LastUpdateTime = updated
105-
cond.LastTransitionTime = updated
101+
// GetCondition returns the InstallPlanCondition of the given type if it exists in the InstallPlanStatus' Conditions.
102+
// Returns a condition of the given type with a ConditionStatus of "Unknown" if not found.
103+
func (s InstallPlanStatus) GetCondition(conditionType InstallPlanConditionType) InstallPlanCondition {
104+
for _, cond := range s.Conditions {
105+
if cond.Type == conditionType {
106+
return cond
107+
}
108+
}
109+
110+
return InstallPlanCondition{
111+
Type: conditionType,
112+
Status: corev1.ConditionUnknown,
113+
}
114+
}
106115

116+
// SetCondition adds or updates a condition, using `Type` as merge key.
117+
func (s *InstallPlanStatus) SetCondition(cond InstallPlanCondition) InstallPlanCondition {
107118
for i, existing := range s.Conditions {
108119
if existing.Type != cond.Type {
109120
continue
@@ -118,19 +129,23 @@ func (s *InstallPlanStatus) SetCondition(cond InstallPlanCondition) InstallPlanC
118129
return cond
119130
}
120131

121-
func ConditionFailed(cond InstallPlanConditionType, reason InstallPlanConditionReason, err error) InstallPlanCondition {
132+
func ConditionFailed(cond InstallPlanConditionType, reason InstallPlanConditionReason, message string, now *metav1.Time) InstallPlanCondition {
122133
return InstallPlanCondition{
123-
Type: cond,
124-
Status: corev1.ConditionFalse,
125-
Reason: reason,
126-
Message: err.Error(),
134+
Type: cond,
135+
Status: corev1.ConditionFalse,
136+
Reason: reason,
137+
Message: message,
138+
LastUpdateTime: *now,
139+
LastTransitionTime: *now,
127140
}
128141
}
129142

130-
func ConditionMet(cond InstallPlanConditionType) InstallPlanCondition {
143+
func ConditionMet(cond InstallPlanConditionType, now *metav1.Time) InstallPlanCondition {
131144
return InstallPlanCondition{
132-
Type: cond,
133-
Status: corev1.ConditionTrue,
145+
Type: cond,
146+
Status: corev1.ConditionTrue,
147+
LastUpdateTime: *now,
148+
LastTransitionTime: *now,
134149
}
135150
}
136151

pkg/api/apis/operators/subscription_types.go

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,47 @@ type SubscriptionConditionType string
4242
const (
4343
// SubscriptionCatalogSourcesUnhealthy indicates that some or all of the CatalogSources to be used in resolution are unhealthy.
4444
SubscriptionCatalogSourcesUnhealthy SubscriptionConditionType = "CatalogSourcesUnhealthy"
45+
46+
// SubscriptionInstallPlanMissing indicates that a Subscription's InstallPlan is missing.
47+
SubscriptionInstallPlanMissing SubscriptionConditionType = "InstallPlanMissing"
48+
49+
// SubscriptionInstallPlanPending indicates that a Subscription's InstallPlan is pending installation.
50+
SubscriptionInstallPlanPending SubscriptionConditionType = "InstallPlanPending"
51+
52+
// SubscriptionInstallPlanFailed indicates that the installation of a Subscription's InstallPlan has failed.
53+
SubscriptionInstallPlanFailed SubscriptionConditionType = "InstallPlanFailed"
4554
)
4655

4756
const (
4857
// NoCatalogSourcesFound is a reason string for Subscriptions with unhealthy CatalogSources due to none being available.
4958
NoCatalogSourcesFound = "NoCatalogSourcesFound"
5059

51-
// AllCatalogSourcesHealthy is a reason string for Subscriptions whose CatalogSources are all healthy.
60+
// AllCatalogSourcesHealthy is a reason string for Subscriptions that transitioned due to all CatalogSources being healthy.
5261
AllCatalogSourcesHealthy = "AllCatalogSourcesHealthy"
5362

5463
// CatalogSourcesAdded is a reason string for Subscriptions that transitioned due to CatalogSources being added.
5564
CatalogSourcesAdded = "CatalogSourcesAdded"
5665

57-
// CatalogSourcesUpdated is a reason string for Subscriptions that transitioned due to CatalogSource being updated..
66+
// CatalogSourcesUpdated is a reason string for Subscriptions that transitioned due to CatalogSource being updated.
5867
CatalogSourcesUpdated = "CatalogSourcesUpdated"
5968

6069
// CatalogSourcesDeleted is a reason string for Subscriptions that transitioned due to CatalogSources being removed.
6170
CatalogSourcesDeleted = "CatalogSourcesDeleted"
6271

6372
// UnhealthyCatalogSourceFound is a reason string for Subscriptions that transitioned because an unhealthy CatalogSource was found.
6473
UnhealthyCatalogSourceFound = "UnhealthyCatalogSourceFound"
74+
75+
// ReferencedInstallPlanNotFound is a reason string for Subscriptions that transitioned due to a referenced InstallPlan not being found.
76+
ReferencedInstallPlanNotFound = "ReferencedInstallPlanNotFound"
77+
78+
// InstallPlanNotYetReconciled is a reason string for Subscriptions that transitioned due to a referenced InstallPlan not being reconciled yet.
79+
InstallPlanNotYetReconciled = "InstallPlanNotYetReconciled"
80+
81+
// InstallPlanFailed is a reason string for Subscriptions that transitioned due to a referenced InstallPlan failing without setting an explicit failure condition.
82+
InstallPlanFailed = "InstallPlanFailed"
6583
)
6684

85+
// SubscriptionCondition represents the latest available observations of a Subscription's state.
6786
type SubscriptionCondition struct {
6887
// Type is the type of Subscription condition.
6988
Type SubscriptionConditionType
@@ -127,16 +146,16 @@ type SubscriptionStatus struct {
127146

128147
// Conditions is a list of the latest available observations about a Subscription's current state.
129148
// +optional
130-
Conditions []SubscriptionCondition
149+
Conditions []SubscriptionCondition `hash:"set"`
131150

132151
// LastUpdated represents the last time that the Subscription status was updated.
133152
LastUpdated metav1.Time
134153
}
135154

136155
// GetCondition returns the SubscriptionCondition of the given type if it exists in the SubscriptionStatus' Conditions.
137156
// Returns a condition of the given type with a ConditionStatus of "Unknown" if not found.
138-
func (status SubscriptionStatus) GetCondition(conditionType SubscriptionConditionType) SubscriptionCondition {
139-
for _, cond := range status.Conditions {
157+
func (s SubscriptionStatus) GetCondition(conditionType SubscriptionConditionType) SubscriptionCondition {
158+
for _, cond := range s.Conditions {
140159
if cond.Type == conditionType {
141160
return cond
142161
}
@@ -149,15 +168,34 @@ func (status SubscriptionStatus) GetCondition(conditionType SubscriptionConditio
149168
}
150169

151170
// SetCondition sets the given SubscriptionCondition in the SubscriptionStatus' Conditions.
152-
func (status *SubscriptionStatus) SetCondition(condition SubscriptionCondition) {
153-
for i, cond := range status.Conditions {
171+
func (s *SubscriptionStatus) SetCondition(condition SubscriptionCondition) {
172+
for i, cond := range s.Conditions {
154173
if cond.Type == condition.Type {
155-
status.Conditions[i] = condition
174+
s.Conditions[i] = condition
156175
return
157176
}
158177
}
159178

160-
status.Conditions = append(status.Conditions, condition)
179+
s.Conditions = append(s.Conditions, condition)
180+
}
181+
182+
// RemoveConditions removes any conditions of the given types from the SubscriptionStatus' Conditions.
183+
func (s *SubscriptionStatus) RemoveConditions(remove ...SubscriptionConditionType) {
184+
exclusions := map[SubscriptionConditionType]struct{}{}
185+
for _, r := range remove {
186+
exclusions[r] = struct{}{}
187+
}
188+
189+
var filtered []SubscriptionCondition
190+
for _, cond := range s.Conditions {
191+
if _, ok := exclusions[cond.Type]; ok {
192+
// Skip excluded condition types
193+
continue
194+
}
195+
filtered = append(filtered, cond)
196+
}
197+
198+
s.Conditions = filtered
161199
}
162200

163201
type InstallPlanReference struct {

pkg/api/apis/operators/v1alpha1/installplan_types.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,23 @@ type InstallPlanCondition struct {
100100
// allow overwriting `now` function for deterministic tests
101101
var now = metav1.Now
102102

103-
// SetCondition adds or updates a condition, using `Type` as merge key
104-
func (s *InstallPlanStatus) SetCondition(cond InstallPlanCondition) InstallPlanCondition {
105-
updated := now()
106-
cond.LastUpdateTime = updated
107-
cond.LastTransitionTime = updated
103+
// GetCondition returns the InstallPlanCondition of the given type if it exists in the InstallPlanStatus' Conditions.
104+
// Returns a condition of the given type with a ConditionStatus of "Unknown" if not found.
105+
func (s InstallPlanStatus) GetCondition(conditionType InstallPlanConditionType) InstallPlanCondition {
106+
for _, cond := range s.Conditions {
107+
if cond.Type == conditionType {
108+
return cond
109+
}
110+
}
108111

112+
return InstallPlanCondition{
113+
Type: conditionType,
114+
Status: corev1.ConditionUnknown,
115+
}
116+
}
117+
118+
// SetCondition adds or updates a condition, using `Type` as merge key.
119+
func (s *InstallPlanStatus) SetCondition(cond InstallPlanCondition) InstallPlanCondition {
109120
for i, existing := range s.Conditions {
110121
if existing.Type != cond.Type {
111122
continue
@@ -120,19 +131,24 @@ func (s *InstallPlanStatus) SetCondition(cond InstallPlanCondition) InstallPlanC
120131
return cond
121132
}
122133

123-
func ConditionFailed(cond InstallPlanConditionType, reason InstallPlanConditionReason, err error) InstallPlanCondition {
134+
135+
func ConditionFailed(cond InstallPlanConditionType, reason InstallPlanConditionReason, message string, now *metav1.Time) InstallPlanCondition {
124136
return InstallPlanCondition{
125137
Type: cond,
126138
Status: corev1.ConditionFalse,
127139
Reason: reason,
128-
Message: err.Error(),
140+
Message: message,
141+
LastUpdateTime: *now,
142+
LastTransitionTime: *now,
129143
}
130144
}
131145

132-
func ConditionMet(cond InstallPlanConditionType) InstallPlanCondition {
146+
func ConditionMet(cond InstallPlanConditionType, now *metav1.Time) InstallPlanCondition {
133147
return InstallPlanCondition{
134148
Type: cond,
135149
Status: corev1.ConditionTrue,
150+
LastUpdateTime: *now,
151+
LastTransitionTime: *now,
136152
}
137153
}
138154

pkg/api/apis/operators/v1alpha1/subscription_types.go

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,47 @@ type SubscriptionConditionType string
4444
const (
4545
// SubscriptionCatalogSourcesUnhealthy indicates that some or all of the CatalogSources to be used in resolution are unhealthy.
4646
SubscriptionCatalogSourcesUnhealthy SubscriptionConditionType = "CatalogSourcesUnhealthy"
47+
48+
// SubscriptionInstallPlanMissing indicates that a Subscription's InstallPlan is missing.
49+
SubscriptionInstallPlanMissing SubscriptionConditionType = "InstallPlanMissing"
50+
51+
// SubscriptionInstallPlanPending indicates that a Subscription's InstallPlan is pending installation.
52+
SubscriptionInstallPlanPending SubscriptionConditionType = "InstallPlanPending"
53+
54+
// SubscriptionInstallPlanFailed indicates that the installation of a Subscription's InstallPlan has failed.
55+
SubscriptionInstallPlanFailed SubscriptionConditionType = "InstallPlanFailed"
4756
)
4857

4958
const (
5059
// NoCatalogSourcesFound is a reason string for Subscriptions with unhealthy CatalogSources due to none being available.
5160
NoCatalogSourcesFound = "NoCatalogSourcesFound"
5261

53-
// AllCatalogSourcesHealthy is a reason string for Subscriptions whose CatalogSources are all healthy.
62+
// AllCatalogSourcesHealthy is a reason string for Subscriptions that transitioned due to all CatalogSources being healthy.
5463
AllCatalogSourcesHealthy = "AllCatalogSourcesHealthy"
5564

5665
// CatalogSourcesAdded is a reason string for Subscriptions that transitioned due to CatalogSources being added.
5766
CatalogSourcesAdded = "CatalogSourcesAdded"
5867

59-
// CatalogSourcesUpdated is a reason string for Subscriptions that transitioned due to CatalogSource being updated..
68+
// CatalogSourcesUpdated is a reason string for Subscriptions that transitioned due to CatalogSource being updated.
6069
CatalogSourcesUpdated = "CatalogSourcesUpdated"
6170

6271
// CatalogSourcesDeleted is a reason string for Subscriptions that transitioned due to CatalogSources being removed.
6372
CatalogSourcesDeleted = "CatalogSourcesDeleted"
6473

6574
// UnhealthyCatalogSourceFound is a reason string for Subscriptions that transitioned because an unhealthy CatalogSource was found.
6675
UnhealthyCatalogSourceFound = "UnhealthyCatalogSourceFound"
76+
77+
// ReferencedInstallPlanNotFound is a reason string for Subscriptions that transitioned due to a referenced InstallPlan not being found.
78+
ReferencedInstallPlanNotFound = "ReferencedInstallPlanNotFound"
79+
80+
// InstallPlanNotYetReconciled is a reason string for Subscriptions that transitioned due to a referenced InstallPlan not being reconciled yet.
81+
InstallPlanNotYetReconciled = "InstallPlanNotYetReconciled"
82+
83+
// InstallPlanFailed is a reason string for Subscriptions that transitioned due to a referenced InstallPlan failing without setting an explicit failure condition.
84+
InstallPlanFailed = "InstallPlanFailed"
6785
)
6886

87+
// SubscriptionCondition represents the latest available observations of a Subscription's state.
6988
type SubscriptionCondition struct {
7089
// Type is the type of Subscription condition.
7190
Type SubscriptionConditionType `json:"type" description:"type of Subscription condition"`
@@ -87,7 +106,7 @@ type SubscriptionCondition struct {
87106

88107
// LastTransitionTime is the last time the condition transit from one status to another
89108
// +optional
90-
LastTransitionTime *metav1.Time `json:"lastTransitionTime,omitempty" description:"last time the condition transit from one status to another"`
109+
LastTransitionTime *metav1.Time `json:"lastTransitionTime,omitempty" description:"last time the condition transit from one status to another" hash:"ignore"`
91110
}
92111

93112
// Equals returns true if a SubscriptionCondition equals the one given, false otherwise.
@@ -129,16 +148,16 @@ type SubscriptionStatus struct {
129148

130149
// Conditions is a list of the latest available observations about a Subscription's current state.
131150
// +optional
132-
Conditions []SubscriptionCondition `json:"conditions,omitempty"`
151+
Conditions []SubscriptionCondition `json:"conditions,omitempty" hash:"set"`
133152

134153
// LastUpdated represents the last time that the Subscription status was updated.
135154
LastUpdated metav1.Time `json:"lastUpdated"`
136155
}
137156

138157
// GetCondition returns the SubscriptionCondition of the given type if it exists in the SubscriptionStatus' Conditions.
139158
// Returns a condition of the given type with a ConditionStatus of "Unknown" if not found.
140-
func (status SubscriptionStatus) GetCondition(conditionType SubscriptionConditionType) SubscriptionCondition {
141-
for _, cond := range status.Conditions {
159+
func (s SubscriptionStatus) GetCondition(conditionType SubscriptionConditionType) SubscriptionCondition {
160+
for _, cond := range s.Conditions {
142161
if cond.Type == conditionType {
143162
return cond
144163
}
@@ -151,15 +170,34 @@ func (status SubscriptionStatus) GetCondition(conditionType SubscriptionConditio
151170
}
152171

153172
// SetCondition sets the given SubscriptionCondition in the SubscriptionStatus' Conditions.
154-
func (status *SubscriptionStatus) SetCondition(condition SubscriptionCondition) {
155-
for i, cond := range status.Conditions {
173+
func (s *SubscriptionStatus) SetCondition(condition SubscriptionCondition) {
174+
for i, cond := range s.Conditions {
156175
if cond.Type == condition.Type {
157-
status.Conditions[i] = condition
176+
s.Conditions[i] = condition
158177
return
159178
}
160179
}
161180

162-
status.Conditions = append(status.Conditions, condition)
181+
s.Conditions = append(s.Conditions, condition)
182+
}
183+
184+
// RemoveConditions removes any conditions of the given types from the SubscriptionStatus' Conditions.
185+
func (s *SubscriptionStatus) RemoveConditions(remove ...SubscriptionConditionType) {
186+
exclusions := map[SubscriptionConditionType]struct{}{}
187+
for _, r := range remove {
188+
exclusions[r] = struct{}{}
189+
}
190+
191+
var filtered []SubscriptionCondition
192+
for _, cond := range s.Conditions {
193+
if _, ok := exclusions[cond.Type]; ok {
194+
// Skip excluded condition types
195+
continue
196+
}
197+
filtered = append(filtered, cond)
198+
}
199+
200+
s.Conditions = filtered
163201
}
164202

165203
type InstallPlanReference struct {

0 commit comments

Comments
 (0)