Skip to content

Commit caafcd1

Browse files
committed
Improve conditions management
1 parent d87aaeb commit caafcd1

4 files changed

Lines changed: 235 additions & 90 deletions

File tree

api/v1alpha1/function_lifecycle.go

Lines changed: 159 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,170 @@ import (
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
88
)
99

10+
// Condition types
1011
const (
11-
FunctionConditionDeployed = "Deployed"
12+
// TypeReady indicates overall readiness (summary condition)
13+
TypeReady = "Ready"
14+
15+
// TypeSourceReady indicates git source was cloned successfully
16+
TypeSourceReady = "SourceReady"
17+
18+
// TypeDeployed indicates function is deployed
19+
TypeDeployed = "Deployed"
20+
21+
// TypeMiddlewareUpToDate indicates middleware is current
22+
TypeMiddlewareUpToDate = "MiddlewareUpToDate"
1223
)
1324

14-
func (s *FunctionStatus) MarkDeploySucceeded() bool {
15-
return meta.SetStatusCondition(&s.Conditions, metav1.Condition{
16-
Type: FunctionConditionDeployed,
17-
Status: metav1.ConditionTrue,
18-
Reason: "Succeeded",
19-
Message: "Function has been successfully deployed",
25+
var FunctionsConditions = []string{
26+
TypeSourceReady,
27+
TypeDeployed,
28+
TypeMiddlewareUpToDate,
29+
}
30+
31+
// InitializeConditions resets all conditions to ensure a fresh start for each reconcile.
32+
// This prevents stale conditions from previous reconciles from persisting.
33+
func (f *Function) InitializeConditions() {
34+
f.Status.Conditions = []metav1.Condition{}
35+
for _, condition := range FunctionsConditions {
36+
meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
37+
Type: condition,
38+
Status: metav1.ConditionUnknown,
39+
Reason: "unknown",
40+
ObservedGeneration: f.Generation,
41+
})
42+
}
43+
44+
meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
45+
Type: TypeReady,
46+
Status: metav1.ConditionUnknown,
47+
})
48+
}
49+
50+
func (f *Function) CalculateReadyCondition() {
51+
allReady := true
52+
reason := ""
53+
message := ""
54+
for _, condition := range f.Status.Conditions {
55+
if condition.Type != TypeReady && condition.Status == metav1.ConditionFalse {
56+
allReady = false
57+
reason = condition.Reason
58+
message = condition.Message
59+
continue
60+
}
61+
}
62+
63+
if allReady {
64+
f.MarkReady()
65+
} else {
66+
f.MarkNotReady(reason, "%s", message)
67+
}
68+
}
69+
70+
// Ready condition helpers
71+
72+
func (f *Function) MarkReady() bool {
73+
return meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
74+
Type: TypeReady,
75+
Status: metav1.ConditionTrue,
76+
Reason: "ReconcileSucceeded",
77+
Message: "Function is ready",
78+
ObservedGeneration: f.Generation,
79+
})
80+
}
81+
82+
func (f *Function) MarkNotReady(reason, messageFormat string, messageA ...interface{}) bool {
83+
return meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
84+
Type: TypeReady,
85+
Status: metav1.ConditionFalse,
86+
Reason: reason,
87+
Message: fmt.Sprintf(messageFormat, messageA...),
88+
ObservedGeneration: f.Generation,
89+
})
90+
}
91+
92+
func (f *Function) MarkTerminating() bool {
93+
return meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
94+
Type: TypeReady,
95+
Status: metav1.ConditionFalse,
96+
Reason: "FinalizerOperations",
97+
Message: "Performing cleanup operations before deletion",
98+
ObservedGeneration: f.Generation,
99+
})
100+
}
101+
102+
func (f *Function) MarkFinalizeFailed(err error) bool {
103+
return meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
104+
Type: TypeReady,
105+
Status: metav1.ConditionFalse,
106+
Reason: "FinalizeFailed",
107+
Message: fmt.Sprintf("Failed to finalize: %s", err.Error()),
108+
ObservedGeneration: f.Generation,
109+
})
110+
}
111+
112+
// Source condition helpers
113+
114+
func (f *Function) MarkSourceReady() bool {
115+
return meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
116+
Type: TypeSourceReady,
117+
Status: metav1.ConditionTrue,
118+
Reason: "CloneSucceeded",
119+
Message: "Source code cloned and metadata read successfully",
120+
ObservedGeneration: f.Generation,
121+
})
122+
}
123+
124+
func (f *Function) MarkSourceNotReady(reason, messageFormat string, messageA ...interface{}) bool {
125+
return meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
126+
Type: TypeSourceReady,
127+
Status: metav1.ConditionFalse,
128+
Reason: reason,
129+
Message: fmt.Sprintf(messageFormat, messageA...),
130+
ObservedGeneration: f.Generation,
131+
})
132+
}
133+
134+
// Deployment condition helpers
135+
136+
func (f *Function) MarkDeployReady() bool {
137+
return meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
138+
Type: TypeDeployed,
139+
Status: metav1.ConditionTrue,
140+
Reason: "DeploySucceeded",
141+
Message: "Function deployed successfully",
142+
ObservedGeneration: f.Generation,
143+
})
144+
}
145+
146+
func (f *Function) MarkDeployNotReady(reason, messageFormat string, messageA ...interface{}) bool {
147+
return meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
148+
Type: TypeDeployed,
149+
Status: metav1.ConditionFalse,
150+
Reason: reason,
151+
Message: fmt.Sprintf(messageFormat, messageA...),
152+
ObservedGeneration: f.Generation,
153+
})
154+
}
155+
156+
// Middleware condition helpers
157+
158+
func (f *Function) MarkMiddlewareUpToDate() bool {
159+
return meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
160+
Type: TypeMiddlewareUpToDate,
161+
Status: metav1.ConditionTrue,
162+
Reason: "UpToDate",
163+
Message: "Middleware is up to date",
164+
ObservedGeneration: f.Generation,
20165
})
21166
}
22167

23-
func (s *FunctionStatus) MarkDeployFailed(reason, messageFormat string, messageA ...interface{}) bool {
24-
return meta.SetStatusCondition(&s.Conditions, metav1.Condition{
25-
Type: FunctionConditionDeployed,
26-
Status: metav1.ConditionFalse,
27-
Reason: reason,
28-
Message: fmt.Sprintf(messageFormat, messageA...),
168+
func (f *Function) MarkMiddlewareNotUpToDate(reason, messageFormat string, messageA ...interface{}) bool {
169+
return meta.SetStatusCondition(&f.Status.Conditions, metav1.Condition{
170+
Type: TypeMiddlewareUpToDate,
171+
Status: metav1.ConditionFalse,
172+
Reason: reason,
173+
Message: fmt.Sprintf(messageFormat, messageA...),
174+
ObservedGeneration: f.Generation,
29175
})
30176
}

api/v1alpha1/function_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323

2424
// +kubebuilder:object:root=true
2525
// +kubebuilder:subresource:status
26+
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description="Ready status"
27+
// +kubebuilder:printcolumn:name="Reason",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].reason",description="Ready reason"
2628
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
27-
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Deployed\")].status",description="Ready Condition Status"
28-
// +kubebuilder:printcolumn:name="Reason",type="string",JSONPath=".status.conditions[?(@.type==\"Deployed\")].reason",description="Ready Condition Reason"
2929

3030
// Function is the Schema for the functions API.
3131
type Function struct {

config/crd/bases/functions.dev_functions.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ spec:
1515
scope: Namespaced
1616
versions:
1717
- additionalPrinterColumns:
18-
- jsonPath: .metadata.creationTimestamp
19-
name: Age
20-
type: date
21-
- description: Ready Condition Status
22-
jsonPath: .status.conditions[?(@.type=="Deployed")].status
18+
- description: Ready status
19+
jsonPath: .status.conditions[?(@.type=="Ready")].status
2320
name: Ready
2421
type: string
25-
- description: Ready Condition Reason
26-
jsonPath: .status.conditions[?(@.type=="Deployed")].reason
22+
- description: Ready reason
23+
jsonPath: .status.conditions[?(@.type=="Ready")].reason
2724
name: Reason
2825
type: string
26+
- jsonPath: .metadata.creationTimestamp
27+
name: Age
28+
type: date
2929
name: v1alpha1
3030
schema:
3131
openAPIV3Schema:

0 commit comments

Comments
 (0)