@@ -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