Skip to content

Commit eb5d72f

Browse files
KPostOfficeopenshift-merge-robot
authored andcommitted
refactor deployment redady into its own function and update instascale ready status
Signed-off-by: Kevin <[email protected]>
1 parent 1d4dca2 commit eb5d72f

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

api/v1alpha1/instascale_types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ type InstaScaleSpec struct {
3838

3939
// InstaScaleStatus defines the observed state of InstaScale
4040
type InstaScaleStatus struct {
41-
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
4241
// Important: Run "make" to regenerate code after modifying this file
42+
43+
// +kubebuilder:default=false
44+
Ready bool `json:"ready"`
4345
}
4446

4547
//+kubebuilder:object:root=true

config/crd/bases/codeflare.codeflare.dev_instascales.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ spec:
9595
type: object
9696
status:
9797
description: InstaScaleStatus defines the observed state of InstaScale
98+
properties:
99+
ready:
100+
default: false
101+
type: boolean
102+
required:
103+
- ready
98104
type: object
99105
type: object
100106
served: true

controllers/instascale_controller.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
mf "github.com/manifestival/manifestival"
3939
codeflarev1alpha1 "github.com/project-codeflare/codeflare-operator/api/v1alpha1"
4040
"github.com/project-codeflare/codeflare-operator/controllers/config"
41+
"github.com/project-codeflare/codeflare-operator/controllers/util"
4142
)
4243

4344
// InstaScaleReconciler reconciles a InstaScale object
@@ -160,9 +161,29 @@ func (r *InstaScaleReconciler) Reconcile(ctx context.Context, req ctrl.Request)
160161
return ctrl.Result{}, err
161162
}
162163

164+
err = updateInstascaleReadyStatus(ctx, r, req, instascaleCustomResource)
165+
if err != nil {
166+
return ctrl.Result{}, err
167+
}
168+
err = r.Client.Status().Update(context.Background(), instascaleCustomResource)
169+
if err != nil {
170+
return ctrl.Result{}, err
171+
}
172+
163173
return ctrl.Result{}, nil
164174
}
165175

176+
func updateInstascaleReadyStatus(ctx context.Context, r *InstaScaleReconciler, req ctrl.Request, instascaleCustomResource *codeflarev1alpha1.InstaScale) error {
177+
deployment := &appsv1.Deployment{}
178+
err := r.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("instascale-%s", req.Name), Namespace: req.Namespace}, deployment)
179+
if err != nil {
180+
return err
181+
}
182+
r.Log.Info("Checking if deployment is ready.")
183+
instascaleCustomResource.Status.Ready = util.IsDeploymentReady(deployment)
184+
return nil
185+
}
186+
166187
// SetupWithManager sets up the controller with the Manager.
167188
func (r *InstaScaleReconciler) SetupWithManager(mgr ctrl.Manager) error {
168189
crFromLabels := handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request {

controllers/mcad_controller.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/go-logr/logr"
2424
mf "github.com/manifestival/manifestival"
2525
"github.com/project-codeflare/codeflare-operator/controllers/config"
26+
"github.com/project-codeflare/codeflare-operator/controllers/util"
2627

2728
codeflarev1alpha1 "github.com/project-codeflare/codeflare-operator/api/v1alpha1"
2829
appsv1 "k8s.io/api/apps/v1"
@@ -192,7 +193,7 @@ func (r *MCADReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
192193
return ctrl.Result{}, err
193194
}
194195

195-
err = updateReadyStatus(ctx, r, req, mcadCustomResource)
196+
err = updateMCADReadyStatus(ctx, r, req, mcadCustomResource)
196197
if err != nil {
197198
return ctrl.Result{}, err
198199
}
@@ -204,23 +205,14 @@ func (r *MCADReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
204205
return ctrl.Result{}, nil
205206
}
206207

207-
func updateReadyStatus(ctx context.Context, r *MCADReconciler, req ctrl.Request, mcadCustomResource *codeflarev1alpha1.MCAD) error {
208+
func updateMCADReadyStatus(ctx context.Context, r *MCADReconciler, req ctrl.Request, mcadCustomResource *codeflarev1alpha1.MCAD) error {
208209
deployment := &appsv1.Deployment{}
209210
err := r.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("mcad-controller-%s", req.Name), Namespace: req.Namespace}, deployment)
210211
if err != nil {
211212
return err
212213
}
213-
r.Log.Info("Checking if deployment is ready.")
214-
isDeploymentReady := false
215-
for _, condition := range deployment.Status.Conditions {
216-
r.Log.Info(fmt.Sprintf("%v: %v", condition.Type, condition.Status))
217-
if condition.Type == appsv1.DeploymentAvailable && condition.Status == corev1.ConditionTrue {
218-
isDeploymentReady = true
219-
r.Log.Info("Deployment ready")
220-
break
221-
}
222-
}
223-
mcadCustomResource.Status.Ready = isDeploymentReady
214+
r.Log.Info("Checking if MCAD deployment is ready.")
215+
mcadCustomResource.Status.Ready = util.IsDeploymentReady(deployment)
224216
return nil
225217
}
226218

controllers/util/util.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,12 @@ func ClusterRoleBindingsAreEqual(crb1 rbacv1.ClusterRoleBinding, crb2 rbacv1.Clu
172172
}
173173
return true
174174
}
175+
176+
func IsDeploymentReady(deployment *appsv1.Deployment) bool {
177+
for _, condition := range deployment.Status.Conditions {
178+
if condition.Type == appsv1.DeploymentAvailable && condition.Status == corev1.ConditionTrue {
179+
return true
180+
}
181+
}
182+
return false
183+
}

0 commit comments

Comments
 (0)