Skip to content

Commit 5091017

Browse files
shawkinsmanusa
authored andcommitted
fix #5102: accounting for null replicas
1 parent 5eb8bf5 commit 5091017

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### 6.7-SNAPSHOT
44

55
#### Bugs
6+
* Fix #5102: wait on scale to 0 was not completing
67

78
#### Improvements
89

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/HasMetadataOperation.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.slf4j.LoggerFactory;
3535

3636
import java.io.IOException;
37-
import java.util.Objects;
37+
import java.util.Optional;
3838
import java.util.concurrent.CompletableFuture;
3939
import java.util.concurrent.TimeUnit;
4040
import java.util.concurrent.atomic.AtomicReference;
@@ -309,7 +309,7 @@ public Scale scale(Scale scaleParam) {
309309
/**
310310
* Let's wait until there are enough Ready pods.
311311
*/
312-
protected void waitUntilScaled(final Integer count) {
312+
protected void waitUntilScaled(final int count) {
313313
final AtomicReference<Integer> replicasRef = new AtomicReference<>(0);
314314

315315
final String name = checkName(getItem());
@@ -319,11 +319,13 @@ protected void waitUntilScaled(final Integer count) {
319319
Utils.scheduleWithVariableRate(completion, getOperationContext().getExecutor(), () -> {
320320
try {
321321
Scale scale = scale();
322-
if (Objects.equals(count, scale.getStatus().getReplicas()) && Objects.equals(count, scale.getSpec().getReplicas())) {
322+
int statusReplicas = Optional.ofNullable(scale.getStatus().getReplicas()).orElse(0);
323+
int specReplicas = Optional.ofNullable(scale.getSpec().getReplicas()).orElse(0);
324+
if (count == statusReplicas && count == specReplicas) {
323325
completion.complete(null);
324326
} else {
325327
LOGGER.debug("Only {}/{} replicas scheduled for {}: {} in namespace: {} seconds so waiting...",
326-
scale.getSpec().getReplicas(), count, getKind(), getName(), namespace);
328+
specReplicas, count, getKind(), getName(), namespace);
327329
}
328330
} catch (KubernetesClientException e) {
329331
completion.completeExceptionally(e);

kubernetes-itests/src/test/java/io/fabric8/kubernetes/DeploymentIT.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.junit.jupiter.api.Test;
2929

3030
import java.util.List;
31+
import java.util.Optional;
3132
import java.util.concurrent.TimeUnit;
3233

3334
import static org.assertj.core.api.Assertions.assertThat;
@@ -78,6 +79,9 @@ void scale() {
7879
assertEquals(5, deployment1.getStatus().getReplicas());
7980
deployment1 = client.apps().deployments().withName("deployment-standard").scale(1, true);
8081
assertEquals(1, deployment1.getStatus().getReplicas());
82+
// ensure scale to 0 as some replica fields can flip to null
83+
deployment1 = client.apps().deployments().withName("deployment-standard").scale(0, true);
84+
assertEquals(0, Optional.ofNullable(deployment1.getStatus().getReplicas()).orElse(0));
8185

8286
// will only work on clusters older than 1.16
8387
if (client.supports(io.fabric8.kubernetes.api.model.extensions.Deployment.class)) {

0 commit comments

Comments
 (0)