Skip to content

Commit 409c7d1

Browse files
committed
Sync WorkloadDeployment replica and Available condition information in status.
1 parent 58fcf96 commit 409c7d1

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

api/v1alpha/workloaddeployment_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type WorkloadDeploymentStatus struct {
5050
Replicas int32 `json:"replicas"`
5151

5252
// The number of instances created by a deployment and have the latest
53-
// deployment generation settings applied.
53+
// deployment template settings applied.
5454
CurrentReplicas int32 `json:"currentReplicas"`
5555

5656
// The desired number of instances to be managed by a deployment.

config/crd/bases/compute.datumapis.com_workloaddeployments.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ spec:
10141014
currentReplicas:
10151015
description: |-
10161016
The number of instances created by a deployment and have the latest
1017-
deployment generation settings applied.
1017+
deployment template settings applied.
10181018
format: int32
10191019
type: integer
10201020
desiredReplicas:

internal/controller/workloaddeployment_controller.go

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,29 +138,63 @@ func (r *WorkloadDeploymentReconciler) Reconcile(ctx context.Context, req mcreco
138138
// their gates removed.
139139
logger.Info("removing scheduling gates from instances")
140140

141+
replicas := len(instances.Items)
142+
currentReplicas := 0
143+
desiredReplicas := deployment.Spec.ScaleSettings.MinReplicas
144+
readyReplicas := 0
141145
for _, instance := range instances.Items {
142-
if len(instance.Spec.Controller.SchedulingGates) == 0 {
143-
continue
146+
if len(instance.Spec.Controller.SchedulingGates) > 0 {
147+
newGates := slices.DeleteFunc(instance.Spec.Controller.SchedulingGates, func(gate computev1alpha.SchedulingGate) bool {
148+
return gate.Name == instancecontrol.NetworkSchedulingGate.String()
149+
})
150+
151+
if len(newGates) == len(instance.Spec.Controller.SchedulingGates) {
152+
// Already been removed, there must be other gates.
153+
continue
154+
}
155+
156+
if _, err := controllerutil.CreateOrPatch(ctx, cl.GetClient(), &instance, func() error {
157+
instance.Spec.Controller.SchedulingGates = newGates
158+
return nil
159+
}); err != nil {
160+
return ctrl.Result{}, fmt.Errorf("failed updating instance: %w", err)
161+
}
144162
}
145163

146-
newGates := slices.DeleteFunc(instance.Spec.Controller.SchedulingGates, func(gate computev1alpha.SchedulingGate) bool {
147-
return gate.Name == instancecontrol.NetworkSchedulingGate.String()
148-
})
164+
if apimeta.IsStatusConditionTrue(instance.Status.Conditions, computev1alpha.InstanceProgrammed) {
165+
if instance.Status.Controller.ObservedTemplateHash == instancecontrol.ComputeHash(deployment.Spec.Template) {
166+
currentReplicas++
167+
}
168+
}
149169

150-
if len(newGates) == len(instance.Spec.Controller.SchedulingGates) {
151-
// Already been removed, there must be other gates.
152-
continue
170+
if apimeta.IsStatusConditionTrue(instance.Status.Conditions, computev1alpha.InstanceReady) {
171+
readyReplicas++
153172
}
173+
}
154174

155-
if _, err := controllerutil.CreateOrPatch(ctx, cl.GetClient(), &instance, func() error {
156-
instance.Spec.Controller.SchedulingGates = newGates
157-
return nil
158-
}); err != nil {
159-
return ctrl.Result{}, fmt.Errorf("failed updating instance: %w", err)
175+
patchResult, err := controllerutil.CreateOrPatch(ctx, cl.GetClient(), &deployment, func() error {
176+
deployment.Status.Replicas = int32(replicas)
177+
deployment.Status.CurrentReplicas = int32(currentReplicas)
178+
deployment.Status.DesiredReplicas = int32(desiredReplicas)
179+
180+
if readyReplicas > 0 {
181+
apimeta.SetStatusCondition(&deployment.Status.Conditions, metav1.Condition{
182+
Type: computev1alpha.WorkloadDeploymentAvailable,
183+
Status: metav1.ConditionTrue,
184+
Reason: "StableInstanceFound",
185+
Message: fmt.Sprintf("%d/%d instances are ready", readyReplicas, replicas),
186+
})
187+
} else {
160188
}
189+
190+
return nil
191+
})
192+
193+
if err != nil {
194+
return ctrl.Result{}, fmt.Errorf("failed updating deployment status: %w", err)
161195
}
162196

163-
// TODO(jreese) update deployment status conditions
197+
logger.Info("deployment status processed", "operation_result", patchResult)
164198

165199
return ctrl.Result{}, nil
166200
}

0 commit comments

Comments
 (0)