Skip to content

Commit 74305c5

Browse files
committed
Move utility functions to utility package.
Signed-off-by: Humair Khan <[email protected]>
1 parent becc65c commit 74305c5

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

controllers/dspipeline_controller.go

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
mf "github.com/manifestival/manifestival"
2424
dspav1alpha1 "github.com/opendatahub-io/data-science-pipelines-operator/api/v1alpha1"
2525
"github.com/opendatahub-io/data-science-pipelines-operator/controllers/config"
26+
"github.com/opendatahub-io/data-science-pipelines-operator/controllers/util"
2627
routev1 "github.com/openshift/api/route/v1"
2728
appsv1 "k8s.io/api/apps/v1"
2829
corev1 "k8s.io/api/core/v1"
@@ -121,16 +122,6 @@ func (r *DSPAReconciler) buildCondition(conditionType string, dspa *dspav1alpha1
121122
return condition
122123
}
123124

124-
func GetDeploymentCondition(status appsv1.DeploymentStatus, condType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
125-
for i := range status.Conditions {
126-
c := status.Conditions[i]
127-
if c.Type == condType {
128-
return &c
129-
}
130-
}
131-
return nil
132-
}
133-
134125
//+kubebuilder:rbac:groups=datasciencepipelinesapplications.opendatahub.io,resources=datasciencepipelinesapplications,verbs=get;list;watch;create;update;patch;delete
135126
//+kubebuilder:rbac:groups=datasciencepipelinesapplications.opendatahub.io,resources=datasciencepipelinesapplications/status,verbs=get;update;patch
136127
//+kubebuilder:rbac:groups=datasciencepipelinesapplications.opendatahub.io,resources=datasciencepipelinesapplications/finalizers,verbs=update
@@ -278,26 +269,15 @@ func (r *DSPAReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
278269

279270
r.PublishMetrics(
280271
dspa,
281-
GetConditionByType(config.APIServerReady, conditions),
282-
GetConditionByType(config.PersistenceAgentReady, conditions),
283-
GetConditionByType(config.ScheduledWorkflowReady, conditions),
284-
GetConditionByType(config.CrReady, conditions),
272+
util.GetConditionByType(config.APIServerReady, conditions),
273+
util.GetConditionByType(config.PersistenceAgentReady, conditions),
274+
util.GetConditionByType(config.ScheduledWorkflowReady, conditions),
275+
util.GetConditionByType(config.CrReady, conditions),
285276
)
286277

287278
return ctrl.Result{}, nil
288279
}
289280

290-
// GetConditionByType returns condition of type T if it exists in conditions, otherwise
291-
// return empty condition struct.
292-
func GetConditionByType(t string, conditions []metav1.Condition) metav1.Condition {
293-
for _, c := range conditions {
294-
if c.Type == t {
295-
return c
296-
}
297-
}
298-
return metav1.Condition{}
299-
}
300-
301281
// isDeploymentInCondition evaluates if condition with "name" is in condition of type "conditionType".
302282
// this procedure is valid only for conditions with bool status type, for conditions of non bool type
303283
// results are undefined.
@@ -331,9 +311,9 @@ func (r *DSPAReconciler) handleReadyCondition(
331311
// 2. Component is still deploying
332312
// We check for (1), and if no errors are found we presume (2)
333313

334-
progressingCond := GetDeploymentCondition(deployment.Status, appsv1.DeploymentProgressing)
335-
availableCond := GetDeploymentCondition(deployment.Status, appsv1.DeploymentAvailable)
336-
replicaFailureCond := GetDeploymentCondition(deployment.Status, appsv1.DeploymentReplicaFailure)
314+
progressingCond := util.GetDeploymentCondition(deployment.Status, appsv1.DeploymentProgressing)
315+
availableCond := util.GetDeploymentCondition(deployment.Status, appsv1.DeploymentAvailable)
316+
replicaFailureCond := util.GetDeploymentCondition(deployment.Status, appsv1.DeploymentReplicaFailure)
337317

338318
if availableCond != nil && availableCond.Status == corev1.ConditionTrue {
339319
// If this DSPA component is minimally available, we are done.

controllers/util/util.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2023.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
appsv1 "k8s.io/api/apps/v1"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
)
23+
24+
// GetConditionByType returns condition of type T if it exists in conditions, otherwise
25+
// return empty condition struct.
26+
func GetConditionByType(t string, conditions []metav1.Condition) metav1.Condition {
27+
for _, c := range conditions {
28+
if c.Type == t {
29+
return c
30+
}
31+
}
32+
return metav1.Condition{}
33+
}
34+
35+
func GetDeploymentCondition(status appsv1.DeploymentStatus, condType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
36+
for i := range status.Conditions {
37+
c := status.Conditions[i]
38+
if c.Type == condType {
39+
return &c
40+
}
41+
}
42+
return nil
43+
}

0 commit comments

Comments
 (0)