Skip to content

Commit d87aaeb

Browse files
committed
Update status conditions on reconcile error
1 parent acec1dd commit d87aaeb

4 files changed

Lines changed: 83 additions & 2 deletions

File tree

api/v1alpha1/function_lifecycle.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package v1alpha1
2+
3+
import (
4+
"fmt"
5+
6+
"k8s.io/apimachinery/pkg/api/meta"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
)
9+
10+
const (
11+
FunctionConditionDeployed = "Deployed"
12+
)
13+
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",
20+
})
21+
}
22+
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...),
29+
})
30+
}

api/v1alpha1/function_types.go

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

2424
// +kubebuilder:object:root=true
2525
// +kubebuilder:subresource:status
26+
// +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"
2629

2730
// Function is the Schema for the functions API.
2831
type Function struct {

config/crd/bases/functions.dev_functions.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@ spec:
1414
singular: function
1515
scope: Namespaced
1616
versions:
17-
- name: v1alpha1
17+
- additionalPrinterColumns:
18+
- jsonPath: .metadata.creationTimestamp
19+
name: Age
20+
type: date
21+
- description: Ready Condition Status
22+
jsonPath: .status.conditions[?(@.type=="Deployed")].status
23+
name: Ready
24+
type: string
25+
- description: Ready Condition Reason
26+
jsonPath: .status.conditions[?(@.type=="Deployed")].reason
27+
name: Reason
28+
type: string
29+
name: v1alpha1
1830
schema:
1931
openAPIV3Schema:
2032
description: Function is the Schema for the functions API.

internal/controller/function_controller.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,46 @@ type FunctionReconciler struct {
6767
// +kubebuilder:rbac:groups=tekton.dev,resources=taskruns,verbs=get;list;watch
6868
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=rolebindings,verbs=get;list;watch;create;update;patch;delete
6969

70-
// Reconcile a Function
70+
// Reconcile a Function with status update on error
7171
func (r *FunctionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
7272
logger := logf.FromContext(ctx)
7373

74+
reconcileRes, reconcileErr := r.reconcile(ctx, req)
75+
76+
function := &v1alpha1.Function{}
77+
if err := r.Get(ctx, req.NamespacedName, function); err != nil {
78+
// we can't get the function to update the status -> return early
79+
if apierrors.IsNotFound(err) {
80+
// function was already deleted -> nothing to update
81+
return ctrl.Result{}, nil
82+
}
83+
logger.Error(err, "Unable to fetch Function to update status condition", "reconcileError", reconcileErr)
84+
return reconcileRes, reconcileErr
85+
}
86+
87+
statusUpdated := false
88+
if reconcileErr != nil {
89+
// update status condition with error
90+
statusUpdated = function.Status.MarkDeployFailed("ReconcileError", "%s", reconcileErr.Error())
91+
} else {
92+
// clear previous errors
93+
statusUpdated = function.Status.MarkDeploySucceeded()
94+
}
95+
96+
if statusUpdated {
97+
// update status
98+
if err := r.Status().Update(ctx, function); err != nil {
99+
logger.Error(err, "Unable to update Function status conditions", "functionName", function.Name, "functionNamespace", function.Namespace)
100+
return reconcileRes, reconcileErr
101+
}
102+
}
103+
104+
return reconcileRes, reconcileErr
105+
}
106+
107+
func (r *FunctionReconciler) reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
108+
logger := logf.FromContext(ctx)
109+
74110
function := &v1alpha1.Function{}
75111
err := r.Get(ctx, req.NamespacedName, function)
76112
if err != nil {

0 commit comments

Comments
 (0)