Skip to content

Commit edad960

Browse files
committed
Move status generation to separate procedure.
Currently status generation does not need to be intertwined within the reconcile procedure and can be generated near the end of the loop all together before publishing metrics. As such this change de couples the generation logic into it's own procedure. Signed-off-by: Humair Khan <[email protected]>
1 parent 000bd51 commit edad960

File tree

1 file changed

+67
-38
lines changed

1 file changed

+67
-38
lines changed

controllers/dspipeline_controller.go

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22-
2322
"github.com/go-logr/logr"
2423
mf "github.com/manifestival/manifestival"
2524
dspav1alpha1 "github.com/opendatahub-io/data-science-pipelines-operator/api/v1alpha1"
@@ -119,7 +118,12 @@ func (r *DSPAReconciler) buildCondition(conditionType string, dspa *dspav1alpha1
119118
return condition
120119
}
121120

122-
func (r *DSPAReconciler) isDeploymentAvailable(ctx context.Context, dspa *dspav1alpha1.DataSciencePipelinesApplication, name string) bool {
121+
// isDeploymentInCondition evaluates if condition with "name" is in condition of type "conditionType".
122+
// this procedure is valid only for conditions with bool status type, for conditions of non bool type
123+
// results are undefined.
124+
func (r *DSPAReconciler) isDeploymentInCondition(ctx context.Context,
125+
dspa *dspav1alpha1.DataSciencePipelinesApplication, name string,
126+
conditionType appsv1.DeploymentConditionType) (bool, appsv1.DeploymentCondition) {
123127
found := &appsv1.Deployment{}
124128

125129
// Every Deployment in DSPA is the name followed by the DSPA CR name
@@ -128,15 +132,15 @@ func (r *DSPAReconciler) isDeploymentAvailable(ctx context.Context, dspa *dspav1
128132
err := r.Get(ctx, types.NamespacedName{Name: component, Namespace: dspa.Namespace}, found)
129133
if err == nil {
130134
if found.Spec.Replicas != nil && *found.Spec.Replicas == 0 {
131-
return false
135+
return false, appsv1.DeploymentCondition{}
132136
}
133137
for _, s := range found.Status.Conditions {
134-
if s.Type == "Available" && s.Status == corev1.ConditionTrue {
135-
return true
138+
if s.Type == conditionType && s.Status == corev1.ConditionTrue {
139+
return true, s
136140
}
137141
}
138142
}
139-
return false
143+
return false, appsv1.DeploymentCondition{}
140144
}
141145

142146
//+kubebuilder:rbac:groups=datasciencepipelinesapplications.opendatahub.io,resources=datasciencepipelinesapplications,verbs=get;list;watch;create;update;patch;delete
@@ -222,9 +226,6 @@ func (r *DSPAReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
222226
return ctrl.Result{}, nil
223227
}
224228

225-
// Initialize conditions
226-
var conditions []metav1.Condition
227-
228229
err = r.ReconcileDatabase(ctx, dspa, params)
229230
if err != nil {
230231
return ctrl.Result{}, err
@@ -240,39 +241,21 @@ func (r *DSPAReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
240241
return ctrl.Result{}, err
241242
}
242243

243-
apiServerReady := r.buildCondition(config.APIServerReady, dspa, config.MinimumReplicasAvailable)
244244
err = r.ReconcileAPIServer(ctx, dspa, params)
245245
if err != nil {
246246
return ctrl.Result{}, err
247247
}
248248

249-
if r.isDeploymentAvailable(ctx, dspa, "ds-pipeline") {
250-
apiServerReady.Status = metav1.ConditionTrue
251-
}
252-
conditions = append(conditions, apiServerReady)
253-
254-
persistenceAgentReady := r.buildCondition(config.PersistenceAgentReady, dspa, config.MinimumReplicasAvailable)
255249
err = r.ReconcilePersistenceAgent(dspa, params)
256250
if err != nil {
257251
return ctrl.Result{}, err
258252
}
259253

260-
if r.isDeploymentAvailable(ctx, dspa, "ds-pipeline-persistenceagent") {
261-
persistenceAgentReady.Status = metav1.ConditionTrue
262-
}
263-
conditions = append(conditions, persistenceAgentReady)
264-
265-
scheduledWorkflowReady := r.buildCondition(config.ScheduledWorkflowReady, dspa, config.MinimumReplicasAvailable)
266254
err = r.ReconcileScheduledWorkflow(dspa, params)
267255
if err != nil {
268256
return ctrl.Result{}, err
269257
}
270258

271-
if r.isDeploymentAvailable(ctx, dspa, "ds-pipeline-scheduledworkflow") {
272-
scheduledWorkflowReady.Status = metav1.ConditionTrue
273-
}
274-
conditions = append(conditions, scheduledWorkflowReady)
275-
276259
err = r.ReconcileUI(dspa, params)
277260
if err != nil {
278261
return ctrl.Result{}, err
@@ -291,6 +274,62 @@ func (r *DSPAReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
291274
return ctrl.Result{}, err
292275
}
293276

277+
conditions := r.GenerateStatus(ctx, dspa)
278+
dspa.Status.Conditions = conditions
279+
280+
// Update Status
281+
err = r.Status().Update(ctx, dspa)
282+
if err != nil {
283+
log.Info(err.Error())
284+
return ctrl.Result{}, err
285+
}
286+
287+
r.PublishMetrics(
288+
dspa,
289+
GetConditionByType(config.APIServerReady, conditions),
290+
GetConditionByType(config.PersistenceAgentReady, conditions),
291+
GetConditionByType(config.ScheduledWorkflowReady, conditions),
292+
GetConditionByType(config.CrReady, conditions),
293+
)
294+
295+
return ctrl.Result{}, nil
296+
}
297+
298+
// GetConditionByType returns condition of type T if it exists in conditions, otherwise
299+
// return empty condition struct.
300+
func GetConditionByType(t string, conditions []metav1.Condition) metav1.Condition {
301+
for _, c := range conditions {
302+
if c.Type == t {
303+
return c
304+
}
305+
}
306+
return metav1.Condition{}
307+
}
308+
309+
func (r *DSPAReconciler) GenerateStatus(ctx context.Context, dspa *dspav1alpha1.DataSciencePipelinesApplication) []metav1.Condition {
310+
var conditions []metav1.Condition
311+
312+
apiServerReady := r.buildCondition(config.APIServerReady, dspa, config.MinimumReplicasAvailable)
313+
deploymentAvailable, _ := r.isDeploymentInCondition(ctx, dspa, "ds-pipeline", appsv1.DeploymentAvailable)
314+
if deploymentAvailable {
315+
apiServerReady.Status = metav1.ConditionTrue
316+
}
317+
conditions = append(conditions, apiServerReady)
318+
319+
persistenceAgentReady := r.buildCondition(config.PersistenceAgentReady, dspa, config.MinimumReplicasAvailable)
320+
deploymentAvailable, _ = r.isDeploymentInCondition(ctx, dspa, "ds-pipeline-persistenceagent", appsv1.DeploymentAvailable)
321+
if deploymentAvailable {
322+
persistenceAgentReady.Status = metav1.ConditionTrue
323+
}
324+
conditions = append(conditions, persistenceAgentReady)
325+
326+
scheduledWorkflowReady := r.buildCondition(config.ScheduledWorkflowReady, dspa, config.MinimumReplicasAvailable)
327+
deploymentAvailable, _ = r.isDeploymentInCondition(ctx, dspa, "ds-pipeline-scheduledworkflow", appsv1.DeploymentAvailable)
328+
if deploymentAvailable {
329+
scheduledWorkflowReady.Status = metav1.ConditionTrue
330+
}
331+
conditions = append(conditions, scheduledWorkflowReady)
332+
294333
crReady := r.buildCondition(config.CrReady, dspa, config.MinimumReplicasAvailable)
295334
crReady.Type = config.CrReady
296335

@@ -309,18 +348,8 @@ func (r *DSPAReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
309348
conditions[i].LastTransitionTime = condition.LastTransitionTime
310349
}
311350
}
312-
dspa.Status.Conditions = conditions
313351

314-
// Update Status
315-
err = r.Status().Update(ctx, dspa)
316-
if err != nil {
317-
log.Info(err.Error())
318-
return ctrl.Result{}, err
319-
}
320-
321-
r.PublishMetrics(dspa, apiServerReady, persistenceAgentReady, scheduledWorkflowReady, crReady)
322-
323-
return ctrl.Result{}, nil
352+
return conditions
324353
}
325354

326355
func (r *DSPAReconciler) PublishMetrics(dspa *dspav1alpha1.DataSciencePipelinesApplication,

0 commit comments

Comments
 (0)