Skip to content

Commit 90f8ad6

Browse files
committed
refactor deployment redady into its own function and update instascale ready status
Signed-off-by: Kevin <[email protected]>
1 parent 5b86408 commit 90f8ad6

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

api/v1alpha1/instascale_types.go

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

3535
// InstaScaleStatus defines the observed state of InstaScale
3636
type InstaScaleStatus struct {
37-
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
3837
// Important: Run "make" to regenerate code after modifying this file
38+
39+
// +kubebuilder:default=false
40+
Ready bool `json:"ready"`
3941
}
4042

4143
//+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
@@ -48,6 +48,12 @@ spec:
4848
type: object
4949
status:
5050
description: InstaScaleStatus defines the observed state of InstaScale
51+
properties:
52+
ready:
53+
default: false
54+
type: boolean
55+
required:
56+
- ready
5157
type: object
5258
type: object
5359
served: true

controllers/instascale_controller.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
authv1 "k8s.io/api/rbac/v1"
2626
apierrs "k8s.io/apimachinery/pkg/api/errors"
2727
"k8s.io/apimachinery/pkg/runtime"
28+
"k8s.io/apimachinery/pkg/types"
2829
ctrl "sigs.k8s.io/controller-runtime"
2930
"sigs.k8s.io/controller-runtime/pkg/client"
3031
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -33,6 +34,7 @@ import (
3334
mf "github.com/manifestival/manifestival"
3435
codeflarev1alpha1 "github.com/project-codeflare/codeflare-operator/api/v1alpha1"
3536
"github.com/project-codeflare/codeflare-operator/controllers/config"
37+
"github.com/project-codeflare/codeflare-operator/controllers/util"
3638
)
3739

3840
// InstaScaleReconciler reconciles a InstaScale object
@@ -155,9 +157,29 @@ func (r *InstaScaleReconciler) Reconcile(ctx context.Context, req ctrl.Request)
155157
return ctrl.Result{}, err
156158
}
157159

160+
err = updateInstascaleReadyStatus(ctx, r, req, instascaleCustomResource)
161+
if err != nil {
162+
return ctrl.Result{}, err
163+
}
164+
err = r.Client.Status().Update(context.Background(), instascaleCustomResource)
165+
if err != nil {
166+
return ctrl.Result{}, err
167+
}
168+
158169
return ctrl.Result{}, nil
159170
}
160171

172+
func updateInstascaleReadyStatus(ctx context.Context, r *InstaScaleReconciler, req ctrl.Request, instascaleCustomResource *codeflarev1alpha1.InstaScale) error {
173+
deployment := &appsv1.Deployment{}
174+
err := r.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("instascale-%s", req.Name), Namespace: req.Namespace}, deployment)
175+
if err != nil {
176+
return err
177+
}
178+
r.Log.Info("Checking if deployment is ready.")
179+
instascaleCustomResource.Status.Ready = util.IsDeploymentReady(deployment)
180+
return nil
181+
}
182+
161183
// SetupWithManager sets up the controller with the Manager.
162184
func (r *InstaScaleReconciler) SetupWithManager(mgr ctrl.Manager) error {
163185
return ctrl.NewControllerManagedBy(mgr).

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"
@@ -189,7 +190,7 @@ func (r *MCADReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
189190
return ctrl.Result{}, err
190191
}
191192

192-
err = updateReadyStatus(ctx, r, req, mcadCustomResource)
193+
err = updateMCADReadyStatus(ctx, r, req, mcadCustomResource)
193194
if err != nil {
194195
return ctrl.Result{}, err
195196
}
@@ -201,23 +202,14 @@ func (r *MCADReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
201202
return ctrl.Result{}, nil
202203
}
203204

204-
func updateReadyStatus(ctx context.Context, r *MCADReconciler, req ctrl.Request, mcadCustomResource *codeflarev1alpha1.MCAD) error {
205+
func updateMCADReadyStatus(ctx context.Context, r *MCADReconciler, req ctrl.Request, mcadCustomResource *codeflarev1alpha1.MCAD) error {
205206
deployment := &appsv1.Deployment{}
206207
err := r.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("mcad-controller-%s", req.Name), Namespace: req.Namespace}, deployment)
207208
if err != nil {
208209
return err
209210
}
210-
r.Log.Info("Checking if deployment is ready.")
211-
isDeploymentReady := false
212-
for _, condition := range deployment.Status.Conditions {
213-
r.Log.Info(fmt.Sprintf("%v: %v", condition.Type, condition.Status))
214-
if condition.Type == appsv1.DeploymentAvailable && condition.Status == corev1.ConditionTrue {
215-
isDeploymentReady = true
216-
r.Log.Info("Deployment ready")
217-
break
218-
}
219-
}
220-
mcadCustomResource.Status.Ready = isDeploymentReady
211+
r.Log.Info("Checking if MCAD deployment is ready.")
212+
mcadCustomResource.Status.Ready = util.IsDeploymentReady(deployment)
221213
return nil
222214
}
223215

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)