@@ -19,8 +19,11 @@ package controller
1919import (
2020 "context"
2121 "fmt"
22+ "time"
2223
2324 corev1 "k8s.io/api/core/v1"
25+ "k8s.io/apimachinery/pkg/api/meta"
26+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2427 "k8s.io/apimachinery/pkg/runtime"
2528 ctrl "sigs.k8s.io/controller-runtime"
2629 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -72,6 +75,11 @@ func (r *ExporterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
7275 return ctrl.Result {}, err
7376 }
7477
78+ result , err := r .reconcileStatusConditionsOnline (ctx , & exporter )
79+ if err != nil {
80+ return ctrl.Result {}, err
81+ }
82+
7583 if err := r .reconcileStatusEndpoint (ctx , & exporter ); err != nil {
7684 return ctrl.Result {}, err
7785 }
@@ -80,7 +88,7 @@ func (r *ExporterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
8088 return RequeueConflict (logger , ctrl.Result {}, err )
8189 }
8290
83- return ctrl. Result {} , nil
91+ return result , nil
8492}
8593
8694func (r * ExporterReconciler ) reconcileStatusCredential (
@@ -144,6 +152,64 @@ func (r *ExporterReconciler) reconcileStatusEndpoint(
144152 return nil
145153}
146154
155+ // nolint:unparam
156+ func (r * ExporterReconciler ) reconcileStatusConditionsOnline (
157+ _ context.Context ,
158+ exporter * jumpstarterdevv1alpha1.Exporter ,
159+ ) (ctrl.Result , error ) {
160+ var requeueAfter time.Duration = 0
161+
162+ if exporter .Status .LastSeen .IsZero () {
163+ meta .SetStatusCondition (& exporter .Status .Conditions , metav1.Condition {
164+ Type : string (jumpstarterdevv1alpha1 .ExporterConditionTypeOnline ),
165+ Status : metav1 .ConditionFalse ,
166+ ObservedGeneration : exporter .Generation ,
167+ Reason : "Seen" ,
168+ Message : "Never seen" ,
169+ })
170+ // marking the exporter offline, no need to requeue
171+ } else if time .Since (exporter .Status .LastSeen .Time ) > time .Minute {
172+ meta .SetStatusCondition (& exporter .Status .Conditions , metav1.Condition {
173+ Type : string (jumpstarterdevv1alpha1 .ExporterConditionTypeOnline ),
174+ Status : metav1 .ConditionFalse ,
175+ ObservedGeneration : exporter .Generation ,
176+ Reason : "Seen" ,
177+ Message : "Last seen more than 1 minute ago" ,
178+ })
179+ // marking the exporter offline, no need to requeue
180+ } else {
181+ meta .SetStatusCondition (& exporter .Status .Conditions , metav1.Condition {
182+ Type : string (jumpstarterdevv1alpha1 .ExporterConditionTypeOnline ),
183+ Status : metav1 .ConditionTrue ,
184+ ObservedGeneration : exporter .Generation ,
185+ Reason : "Seen" ,
186+ Message : "Lase seen less than 1 minute ago" ,
187+ })
188+ // marking the exporter online, requeue after 30 seconds
189+ requeueAfter = time .Second * 30
190+ }
191+
192+ if exporter .Status .Devices == nil {
193+ meta .SetStatusCondition (& exporter .Status .Conditions , metav1.Condition {
194+ Type : string (jumpstarterdevv1alpha1 .ExporterConditionTypeRegistered ),
195+ Status : metav1 .ConditionFalse ,
196+ ObservedGeneration : exporter .Generation ,
197+ Reason : "Unregister" ,
198+ })
199+ } else {
200+ meta .SetStatusCondition (& exporter .Status .Conditions , metav1.Condition {
201+ Type : string (jumpstarterdevv1alpha1 .ExporterConditionTypeRegistered ),
202+ Status : metav1 .ConditionTrue ,
203+ ObservedGeneration : exporter .Generation ,
204+ Reason : "Register" ,
205+ })
206+ }
207+
208+ return ctrl.Result {
209+ RequeueAfter : requeueAfter ,
210+ }, nil
211+ }
212+
147213// SetupWithManager sets up the controller with the Manager.
148214func (r * ExporterReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
149215 return ctrl .NewControllerManagedBy (mgr ).
0 commit comments