Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/v1alpha1/provider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ type ProviderSpec struct {

// ProviderStatus defines the observed state of Provider
type ProviderStatus struct {
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"`
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"`
LastHealthCheckTime *metav1.Time `json:"lastHealthCheckTime,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
4 changes: 4 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ spec:
- type
type: object
type: array
lastHealthCheckTime:
format: date-time
type: string
type: object
type: object
served: true
Expand Down
22 changes: 17 additions & 5 deletions internal/controller/provider_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,21 @@ func (r *ProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
logger.Error(err, "failed to get provider")
return ctrl.Result{}, err
}
logger.V(2).Info("Checking availability of provider", "provider", req.NamespacedName)

// TODO: Schedule checks instead of doing it every time something happens
lastHealthCheckTime := metav1.NewTime(time.Now())
provider.Status.LastHealthCheckTime = &lastHealthCheckTime

interval := time.Duration(provider.Spec.Healthcheck.IntervalSeconds) * time.Second
var next time.Time
if provider.Status.LastHealthCheckTime != nil {
next = provider.Status.LastHealthCheckTime.Time.Add(interval)
} else {
next = provider.ObjectMeta.CreationTimestamp.Time
}

if time.Until(next) > 0 {
return ctrl.Result{RequeueAfter: time.Until(next)}, nil
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should set LastHealthCheckTime on the provider resource here at the start, so that we have it set even on failures.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LastHealthCheckTime placed in the beginning of the function.

// We don't check the availability of JSONTas as it is not yet running as a service we can check.
if provider.Spec.JSONTas == nil {
Expand All @@ -76,7 +88,7 @@ func (r *ProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, err
}
logger.Info("Provider did not respond", "provider", req.NamespacedName)
return ctrl.Result{RequeueAfter: time.Duration(provider.Spec.Healthcheck.IntervalSeconds) * time.Second}, nil
return ctrl.Result{RequeueAfter: interval}, nil
}
if resp.StatusCode != 204 {
meta.SetStatusCondition(&provider.Status.Conditions, metav1.Condition{Type: StatusAvailable, Status: metav1.ConditionFalse, Reason: "Error", Message: fmt.Sprintf("Wrong status code (%d) from health check endpoint", resp.StatusCode)})
Expand All @@ -85,7 +97,7 @@ func (r *ProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, err
}
logger.Info("Provider responded with a bad status code", "provider", req.NamespacedName, "status", resp.StatusCode)
return ctrl.Result{RequeueAfter: time.Duration(provider.Spec.Healthcheck.IntervalSeconds) * time.Second}, nil
return ctrl.Result{RequeueAfter: interval}, nil
}
}
meta.SetStatusCondition(&provider.Status.Conditions, metav1.Condition{Type: StatusAvailable, Status: metav1.ConditionTrue, Reason: "OK", Message: "Provider is up and running"})
Expand All @@ -94,7 +106,7 @@ func (r *ProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, err
}
logger.V(2).Info("Provider is available", "provider", req.NamespacedName)
return ctrl.Result{RequeueAfter: time.Duration(provider.Spec.Healthcheck.IntervalSeconds) * time.Second}, nil
return ctrl.Result{RequeueAfter: interval}, nil
}

// SetupWithManager sets up the controller with the Manager.
Expand Down