Skip to content

Commit 82f5b1e

Browse files
feat: simplify
1 parent 36c3e59 commit 82f5b1e

11 files changed

+181
-194
lines changed

api/clusters/v1alpha1/constants.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ const (
1313
PURPOSE_MCP = "mcp"
1414
)
1515

16-
const (
17-
// CONDITION_UNKNOWN represents an unknown status for the condition.
18-
CONDITION_UNKNOWN ConditionStatus = "Unknown"
19-
// CONDITION_TRUE marks the condition as true.
20-
CONDITION_TRUE ConditionStatus = "True"
21-
// CONDITION_FALSE marks the condition as false.
22-
CONDITION_FALSE ConditionStatus = "False"
23-
)
24-
2516
const (
2617
// PHASE_UNKNOWN represents an unknown phase for the cluster.
2718
PHASE_UNKNOWN ClusterPhase = "Unknown"
Lines changed: 60 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,31 @@
11
package v1alpha1
22

33
import (
4-
"time"
5-
4+
"k8s.io/apimachinery/pkg/api/meta"
65
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
76
)
87

9-
type ConditionStatus string
10-
11-
type Condition struct {
12-
// Type is the type of the condition.
13-
// Must be unique within the resource.
14-
Type string `json:"type"`
15-
16-
// Status is the status of the condition.
17-
Status ConditionStatus `json:"status"`
18-
19-
// Reason is expected to contain a CamelCased string that provides further information regarding the condition.
20-
// It should have a fixed value set (like an enum) to be machine-readable. The value set depends on the condition type.
21-
// It is optional, but should be filled at least when Status is not "True".
22-
// +optional
23-
Reason string `json:"reason,omitempty"`
24-
25-
// Message contains further details regarding the condition.
26-
// It is meant for human users, Reason should be used for programmatic evaluation instead.
27-
// It is optional, but should be filled at least when Status is not "True".
28-
// +optional
29-
Message string `json:"message,omitempty"`
30-
31-
// LastTransitionTime specifies the time when this condition's status last changed.
32-
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
33-
}
34-
35-
// Implement the Condition interface from our controller-utils library
36-
func (c *Condition) GetType() string {
37-
return c.Type
38-
}
39-
func (c *Condition) SetType(t string) {
40-
c.Type = t
41-
}
42-
func (c *Condition) GetStatus() ConditionStatus {
43-
return c.Status
44-
}
45-
func (c *Condition) SetStatus(s ConditionStatus) {
46-
c.Status = s
47-
}
48-
func (c *Condition) GetReason() string {
49-
return c.Reason
50-
}
51-
func (c *Condition) SetReason(r string) {
52-
c.Reason = r
53-
}
54-
func (c *Condition) GetMessage() string {
55-
return c.Message
56-
}
57-
func (c *Condition) SetMessage(m string) {
58-
c.Message = m
59-
}
60-
func (c *Condition) GetLastTransitionTime() time.Time {
61-
return c.LastTransitionTime.Time
62-
}
63-
func (c *Condition) SetLastTransitionTime(t time.Time) {
64-
c.LastTransitionTime = metav1.NewTime(t)
65-
}
66-
67-
// ConditionList is a list of Conditions.
68-
type ConditionList []Condition
69-
70-
// ConditionStatusFromBoolPtr converts a bool pointer into the corresponding ConditionStatus.
71-
// If nil, "Unknown" is returned.
72-
func ConditionStatusFromBoolPtr(src *bool) ConditionStatus {
73-
if src == nil {
74-
return CONDITION_UNKNOWN
75-
}
76-
return ConditionStatusFromBool(*src)
77-
}
78-
79-
// ConditionStatusFromBool converts a bool into the corresponding ConditionStatus.
80-
func ConditionStatusFromBool(src bool) ConditionStatus {
81-
if src {
82-
return CONDITION_TRUE
83-
}
84-
return CONDITION_FALSE
85-
}
86-
87-
// IsTrue returns true if the Condition's status is "True".
88-
// Note that the status can be "Unknown", so !IsTrue() is not the same as IsFalse().
89-
func (cc Condition) IsTrue() bool {
90-
return cc.Status == CONDITION_TRUE
91-
}
92-
93-
// IsFalse returns true if the Condition's status is "False".
94-
// Note that the status can be "Unknown", so !IsFalse() is not the same as IsTrue().
95-
func (cc Condition) IsFalse() bool {
96-
return cc.Status == CONDITION_FALSE
97-
}
8+
// These are standard condition types that can be used across resources
9+
const (
10+
// ReadyConditionType indicates whether the resource is ready
11+
ReadyConditionType = "Ready"
12+
// AvailableConditionType indicates whether the resource is available
13+
AvailableConditionType = "Available"
14+
// ReconcileSuccessConditionType indicates whether the last reconciliation was successful
15+
ReconcileSuccessConditionType = "ReconcileSuccess"
16+
)
9817

99-
// IsUnknown returns true if the Condition's status is "Unknown".
100-
func (cc Condition) IsUnknown() bool {
101-
return cc.Status == CONDITION_UNKNOWN
102-
}
18+
// These are standard condition reasons that can be used across resources
19+
const (
20+
// ReconcileSuccessReason indicates that the reconciliation was successful
21+
ReconcileSuccessReason = "ReconcileSuccess"
22+
// ReconcileErrorReason indicates that there was an error during reconciliation
23+
ReconcileErrorReason = "ReconcileError"
24+
// ResourceAvailableReason indicates that the resource is available
25+
ResourceAvailableReason = "ResourceAvailable"
26+
// ResourceUnavailableReason indicates that the resource is not available
27+
ResourceUnavailableReason = "ResourceUnavailable"
28+
)
10329

10430
type ObjectReference struct {
10531
// Name is the name of the referenced resource.
@@ -114,9 +40,8 @@ type NamespacedObjectReference struct {
11440
Namespace string `json:"namespace"`
11541
}
11642

117-
// CommonStatus is a status shared by multiple resource.
118-
// Note that a 'phase' is also part of the status, but it cannot be included in this struct.
119-
// The reason is that we want to use string-like types for the phase, but the goddamn code generation does not support generics, no matter which annotations are added.
43+
// CommonStatus is a status shared by multiple resources.
44+
// It uses standard Kubernetes conditions for status representation.
12045
type CommonStatus struct {
12146
// ObservedGeneration is the generation of this resource that was last reconciled by the controller.
12247
ObservedGeneration int64 `json:"observedGeneration"`
@@ -132,7 +57,42 @@ type CommonStatus struct {
13257
// +optional
13358
Message string `json:"message,omitempty"`
13459

135-
// Conditions contains the conditions.
60+
// Conditions contains the conditions of this resource using the standard Kubernetes condition format.
13661
// +optional
137-
Conditions ConditionList `json:"conditions,omitempty"`
62+
Conditions []metav1.Condition `json:"conditions,omitempty"`
63+
}
64+
65+
// GetCondition returns the condition with the provided type.
66+
// Returns nil if the condition is not found.
67+
func (s *CommonStatus) GetCondition(conditionType string) *metav1.Condition {
68+
return meta.FindStatusCondition(s.Conditions, conditionType)
69+
}
70+
71+
// SetCondition sets the status condition. It either overwrites the existing condition or
72+
// creates a new one if the condition wasn't present.
73+
func (s *CommonStatus) SetCondition(condition metav1.Condition) {
74+
meta.SetStatusCondition(&s.Conditions, condition)
75+
}
76+
77+
// RemoveCondition removes the condition with the specified type.
78+
func (s *CommonStatus) RemoveCondition(conditionType string) {
79+
meta.RemoveStatusCondition(&s.Conditions, conditionType)
80+
}
81+
82+
// IsReady returns true if the Ready condition is present and set to True.
83+
func (s *CommonStatus) IsReady() bool {
84+
condition := s.GetCondition(ReadyConditionType)
85+
return condition != nil && condition.Status == metav1.ConditionTrue
86+
}
87+
88+
// IsAvailable returns true if the Available condition is present and set to True.
89+
func (s *CommonStatus) IsAvailable() bool {
90+
condition := s.GetCondition(AvailableConditionType)
91+
return condition != nil && condition.Status == metav1.ConditionTrue
92+
}
93+
94+
// HasReconcileSucceeded returns true if the ReconcileSuccess condition is present and set to True.
95+
func (s *CommonStatus) HasReconcileSucceeded() bool {
96+
condition := s.GetCondition(ReconcileSuccessConditionType)
97+
return condition != nil && condition.Status == metav1.ConditionTrue
13898
}

api/clusters/v1alpha1/zz_generated.deepcopy.go

Lines changed: 2 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/crds/manifests/clusters.openmcp.cloud_accessrequests.yaml

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.18.0
6+
controller-gen.kubebuilder.io/version: v0.17.3
77
labels:
88
openmcp.cloud/cluster: platform
99
name: accessrequests.clusters.openmcp.cloud
@@ -164,35 +164,59 @@ spec:
164164
description: AccessRequestStatus defines the observed state of AccessRequest
165165
properties:
166166
conditions:
167-
description: Conditions contains the conditions.
167+
description: Conditions contains the conditions of this resource using
168+
the standard Kubernetes condition format.
168169
items:
170+
description: Condition contains details for one aspect of the current
171+
state of this API Resource.
169172
properties:
170173
lastTransitionTime:
171-
description: LastTransitionTime specifies the time when this
172-
condition's status last changed.
174+
description: |-
175+
lastTransitionTime is the last time the condition transitioned from one status to another.
176+
This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
173177
format: date-time
174178
type: string
175179
message:
176180
description: |-
177-
Message contains further details regarding the condition.
178-
It is meant for human users, Reason should be used for programmatic evaluation instead.
179-
It is optional, but should be filled at least when Status is not "True".
181+
message is a human readable message indicating details about the transition.
182+
This may be an empty string.
183+
maxLength: 32768
180184
type: string
185+
observedGeneration:
186+
description: |-
187+
observedGeneration represents the .metadata.generation that the condition was set based upon.
188+
For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date
189+
with respect to the current state of the instance.
190+
format: int64
191+
minimum: 0
192+
type: integer
181193
reason:
182194
description: |-
183-
Reason is expected to contain a CamelCased string that provides further information regarding the condition.
184-
It should have a fixed value set (like an enum) to be machine-readable. The value set depends on the condition type.
185-
It is optional, but should be filled at least when Status is not "True".
195+
reason contains a programmatic identifier indicating the reason for the condition's last transition.
196+
Producers of specific condition types may define expected values and meanings for this field,
197+
and whether the values are considered a guaranteed API.
198+
The value should be a CamelCase string.
199+
This field may not be empty.
200+
maxLength: 1024
201+
minLength: 1
202+
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
186203
type: string
187204
status:
188-
description: Status is the status of the condition.
205+
description: status of the condition, one of True, False, Unknown.
206+
enum:
207+
- "True"
208+
- "False"
209+
- Unknown
189210
type: string
190211
type:
191-
description: |-
192-
Type is the type of the condition.
193-
Must be unique within the resource.
212+
description: type of condition in CamelCase or in foo.example.com/CamelCase.
213+
maxLength: 316
214+
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
194215
type: string
195216
required:
217+
- lastTransitionTime
218+
- message
219+
- reason
196220
- status
197221
- type
198222
type: object

api/crds/manifests/clusters.openmcp.cloud_clusterprofiles.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.18.0
6+
controller-gen.kubebuilder.io/version: v0.17.3
77
labels:
88
openmcp.cloud/cluster: platform
99
name: clusterprofiles.clusters.openmcp.cloud

0 commit comments

Comments
 (0)