Skip to content

Commit 5b86408

Browse files
committed
add ready status to MCAD
readiness state just reflects the availability of the deployment Signed-off-by: Kevin <[email protected]>
1 parent 5a132e8 commit 5b86408

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

api/v1alpha1/mcad_types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ type MCADSpec struct {
5555

5656
// MCADStatus defines the observed state of MCAD
5757
type MCADStatus struct {
58-
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
5958
// Important: Run "make" to regenerate code after modifying this file
59+
60+
// Ready indicates whether the application is ready to serve requests
61+
Ready bool `json:"ready"`
6062
}
6163

6264
//+kubebuilder:object:root=true

config/crd/bases/codeflare.codeflare.dev_mcads.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ spec:
7070
type: object
7171
status:
7272
description: MCADStatus defines the observed state of MCAD
73+
properties:
74+
ready:
75+
description: Ready indicates whether the application is ready to serve
76+
requests
77+
type: boolean
78+
required:
79+
- ready
7380
type: object
7481
type: object
7582
served: true

controllers/mcad_controller.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
2223
"github.com/go-logr/logr"
2324
mf "github.com/manifestival/manifestival"
2425
"github.com/project-codeflare/codeflare-operator/controllers/config"
2526

2627
codeflarev1alpha1 "github.com/project-codeflare/codeflare-operator/api/v1alpha1"
28+
appsv1 "k8s.io/api/apps/v1"
2729
corev1 "k8s.io/api/core/v1"
2830
rbacv1 "k8s.io/api/rbac/v1"
2931
apierrs "k8s.io/apimachinery/pkg/api/errors"
3032
"k8s.io/apimachinery/pkg/runtime"
33+
"k8s.io/apimachinery/pkg/types"
3134
ctrl "sigs.k8s.io/controller-runtime"
3235
"sigs.k8s.io/controller-runtime/pkg/client"
3336
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -186,13 +189,43 @@ func (r *MCADReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
186189
return ctrl.Result{}, err
187190
}
188191

192+
err = updateReadyStatus(ctx, r, req, mcadCustomResource)
193+
if err != nil {
194+
return ctrl.Result{}, err
195+
}
196+
err = r.Client.Status().Update(context.Background(), mcadCustomResource)
197+
if err != nil {
198+
return ctrl.Result{}, err
199+
}
200+
189201
return ctrl.Result{}, nil
190202
}
191203

204+
func updateReadyStatus(ctx context.Context, r *MCADReconciler, req ctrl.Request, mcadCustomResource *codeflarev1alpha1.MCAD) error {
205+
deployment := &appsv1.Deployment{}
206+
err := r.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("mcad-controller-%s", req.Name), Namespace: req.Namespace}, deployment)
207+
if err != nil {
208+
return err
209+
}
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
221+
return nil
222+
}
223+
192224
// SetupWithManager sets up the controller with the Manager.
193225
func (r *MCADReconciler) SetupWithManager(mgr ctrl.Manager) error {
194226
return ctrl.NewControllerManagedBy(mgr).
195227
For(&codeflarev1alpha1.MCAD{}).
228+
Owns(&appsv1.Deployment{}).
196229
Owns(&corev1.ConfigMap{}).
197230
Owns(&corev1.Service{}).
198231
Owns(&corev1.ServiceAccount{}).

0 commit comments

Comments
 (0)