Skip to content

Commit d117fcf

Browse files
committed
refactor: improve testability of context construction
1 parent a14f72b commit d117fcf

6 files changed

Lines changed: 81 additions & 12 deletions

File tree

stackgres-k8s/src/common/src/main/java/io/stackgres/common/crd/sgshardedcluster/ShardedClusterStatusCondition.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66
package io.stackgres.common.crd.sgshardedcluster;
77

8+
import java.util.Objects;
9+
810
import io.stackgres.common.crd.Condition;
911

1012
public enum ShardedClusterStatusCondition {
1113

12-
CLUSTER_REQUIRES_RESTART(Type.PENDING_RESTART, Status.TRUE, "ClusterRequiresRestart"),
14+
SHARDED_CLUSTER_BOOTSTRAPPED(Type.BOOTSTRAPPED, Status.TRUE, "ShardedClusterBootstrapped"),
15+
SHARDED_CLUSTER_REQUIRES_RESTART(Type.PENDING_RESTART, Status.TRUE, "ShardedClusterRequiresRestart"),
1316
FALSE_PENDING_RESTART(Type.PENDING_RESTART, Status.FALSE, "FalsePendingRestart"),
1417
SHARDED_CLUSTER_REQUIRES_UPGRADE(
1518
Type.PENDING_UPGRADE, Status.TRUE, "ShardedClusterRequiresUpgrade"),
@@ -31,8 +34,15 @@ public Condition getCondition() {
3134
return new Condition(type, status, reason);
3235
}
3336

37+
public boolean isCondition(Condition condition) {
38+
return Objects.equals(condition.getType(), type)
39+
&& Objects.equals(condition.getStatus(), status)
40+
&& Objects.equals(condition.getReason(), reason);
41+
}
42+
3443
public enum Type {
3544

45+
BOOTSTRAPPED("Bootstrapped"),
3646
PENDING_RESTART("PendingRestart"),
3747
PENDING_UPGRADE("PendingUpgrade"),
3848
FAILED("Failed");

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/cluster/context/ClusterRestoreBackupContextAppender.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public ClusterRestoreBackupContextAppender(
5353

5454
@Override
5555
public void appendContext(StackGresCluster cluster, Builder contextBuilder) {
56-
final Optional<StackGresBackup> restoreBackup = findRestoreBackup(
57-
cluster,
58-
cluster.getMetadata().getNamespace());
59-
6056
if (Optional.of(cluster)
6157
.map(StackGresCluster::getStatus)
6258
.map(StackGresClusterStatus::getConditions)
@@ -69,6 +65,10 @@ public void appendContext(StackGresCluster cluster, Builder contextBuilder) {
6965
return;
7066
}
7167

68+
final Optional<StackGresBackup> restoreBackup = findRestoreBackup(
69+
cluster,
70+
cluster.getMetadata().getNamespace());
71+
7272
final Map<String, Secret> restoreSecrets = restoreBackup
7373
.map(StackGresBackup::getStatus)
7474
.map(StackGresBackupStatus::getSgBackupConfig)
@@ -84,7 +84,7 @@ public void appendContext(StackGresCluster cluster, Builder contextBuilder) {
8484
secretFinder
8585
.findByNameAndNamespace(
8686
entry.getKey(),
87-
cluster.getMetadata().getName())
87+
cluster.getMetadata().getNamespace())
8888
.orElseThrow(() -> new IllegalArgumentException(
8989
"Secret " + entry.getKey() + " not found for " + StackGresBackup.KIND + " "
9090
+ restoreBackup.get().getMetadata().getName())),

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/shardedcluster/ShardedClusterStatusManager.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ public StackGresShardedCluster refreshCondition(StackGresShardedCluster source)
6060
}
6161
source.getStatus().setBinding(new StackGresClusterServiceBindingStatus());
6262
source.getStatus().getBinding().setName(ServiceBindingSecret.name(source));
63-
if (isPendingRestart(source)) {
64-
updateCondition(getClusterRequiresRestart(), source);
63+
List<StackGresCluster> clusters = getClusters(source);
64+
if (isPendingRestart(clusters)) {
65+
updateCondition(getShardedClusterRequiresRestart(), source);
6566
} else {
6667
updateCondition(getFalsePendingRestart(), source);
6768
}
@@ -70,14 +71,16 @@ public StackGresShardedCluster refreshCondition(StackGresShardedCluster source)
7071
} else {
7172
updateCondition(getFalsePendingUpgrade(), source);
7273
}
74+
if (isBootstrapped(clusters)) {
75+
updateCondition(getShardedClusterBootstrapped(), source);
76+
}
7377
return source;
7478
}
7579

7680
/**
7781
* Check pending restart status condition.
7882
*/
79-
public boolean isPendingRestart(StackGresShardedCluster shardedCluster) {
80-
List<StackGresCluster> clusters = getClusters(shardedCluster);
83+
public boolean isPendingRestart(List<StackGresCluster> clusters) {
8184
return clusters.stream()
8285
.flatMap(cluster -> Optional.of(cluster)
8386
.map(StackGresCluster::getStatus)
@@ -106,6 +109,19 @@ private boolean isPendingUpgrade(
106109
return false;
107110
}
108111

112+
/**
113+
* Check bootstrapped status condition.
114+
*/
115+
public boolean isBootstrapped(List<StackGresCluster> clusters) {
116+
return clusters.stream()
117+
.flatMap(cluster -> Optional.of(cluster)
118+
.map(StackGresCluster::getStatus)
119+
.map(StackGresClusterStatus::getConditions)
120+
.stream()
121+
.flatMap(List::stream))
122+
.allMatch(ClusterStatusCondition.CLUSTER_BOOTSTRAPPED::isCondition);
123+
}
124+
109125
private List<StackGresCluster> getClusters(StackGresShardedCluster shardedCluster) {
110126
final Map<String, String> clusterLabels =
111127
labelFactory.genericLabels(shardedCluster);
@@ -144,8 +160,8 @@ protected Condition getFalsePendingRestart() {
144160
return ShardedClusterStatusCondition.FALSE_PENDING_RESTART.getCondition();
145161
}
146162

147-
protected Condition getClusterRequiresRestart() {
148-
return ShardedClusterStatusCondition.CLUSTER_REQUIRES_RESTART.getCondition();
163+
protected Condition getShardedClusterRequiresRestart() {
164+
return ShardedClusterStatusCondition.SHARDED_CLUSTER_REQUIRES_RESTART.getCondition();
149165
}
150166

151167
protected Condition getFalsePendingUpgrade() {
@@ -155,4 +171,8 @@ protected Condition getFalsePendingUpgrade() {
155171
protected Condition getShardedClusterRequiresUpgrade() {
156172
return ShardedClusterStatusCondition.SHARDED_CLUSTER_REQUIRES_UPGRADE.getCondition();
157173
}
174+
175+
protected Condition getShardedClusterBootstrapped() {
176+
return ShardedClusterStatusCondition.SHARDED_CLUSTER_BOOTSTRAPPED.getCondition();
177+
}
158178
}

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/shardedcluster/context/ShardedClusterRestoreBackupContextAppender.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.stackgres.common.crd.sgshardedbackup.ShardedBackupStatus;
1414
import io.stackgres.common.crd.sgshardedbackup.StackGresShardedBackup;
1515
import io.stackgres.common.crd.sgshardedbackup.StackGresShardedBackupStatus;
16+
import io.stackgres.common.crd.sgshardedcluster.ShardedClusterStatusCondition;
1617
import io.stackgres.common.crd.sgshardedcluster.StackGresShardedCluster;
1718
import io.stackgres.common.crd.sgshardedcluster.StackGresShardedClusterInitialData;
1819
import io.stackgres.common.crd.sgshardedcluster.StackGresShardedClusterRestore;
@@ -36,6 +37,15 @@ public ShardedClusterRestoreBackupContextAppender(
3637

3738
@Override
3839
public void appendContext(StackGresShardedCluster cluster, Builder contextBuilder) {
40+
if (Optional.of(cluster)
41+
.map(StackGresShardedCluster::getStatus)
42+
.map(StackGresShardedClusterStatus::getConditions)
43+
.stream()
44+
.flatMap(List::stream)
45+
.anyMatch(ShardedClusterStatusCondition.SHARDED_CLUSTER_BOOTSTRAPPED::isCondition)) {
46+
return;
47+
}
48+
3949
findRestoreBackup(
4050
cluster,
4151
cluster.getMetadata().getNamespace());

stackgres-k8s/src/operator/src/test/java/io/stackgres/operator/conciliation/cluster/context/ClusterRestoreBackupContextAppenderTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import io.stackgres.common.crd.sgbackup.BackupStatus;
2121
import io.stackgres.common.crd.sgbackup.StackGresBackup;
2222
import io.stackgres.common.crd.sgbackup.StackGresBackupBuilder;
23+
import io.stackgres.common.crd.sgcluster.ClusterStatusCondition;
2324
import io.stackgres.common.crd.sgcluster.StackGresCluster;
25+
import io.stackgres.common.crd.sgcluster.StackGresClusterStatusBuilder;
2426
import io.stackgres.common.fixture.Fixtures;
2527
import io.stackgres.common.resource.CustomResourceFinder;
2628
import io.stackgres.common.resource.ResourceFinder;
@@ -30,6 +32,7 @@
3032
import org.junit.jupiter.api.Test;
3133
import org.junit.jupiter.api.extension.ExtendWith;
3234
import org.mockito.Mock;
35+
import org.mockito.Mockito;
3336
import org.mockito.Spy;
3437
import org.mockito.junit.jupiter.MockitoExtension;
3538

@@ -66,6 +69,18 @@ void givenClusterWithoutBackup_shouldPass() {
6669
verify(contextBuilder).restoreSecrets(Map.of());
6770
}
6871

72+
@Test
73+
void givenBootstrappedCluster_shouldPass() {
74+
cluster.setStatus(
75+
new StackGresClusterStatusBuilder()
76+
.addToConditions(ClusterStatusCondition.CLUSTER_BOOTSTRAPPED.getCondition())
77+
.build());
78+
contextAppender.appendContext(cluster, contextBuilder);
79+
verify(backupFinder, Mockito.never()).findByNameAndNamespace(any(), any());
80+
verify(contextBuilder).restoreBackup(Optional.empty());
81+
verify(contextBuilder).restoreSecrets(Map.of());
82+
}
83+
6984
@Test
7085
void givenClusterWithBackup_shouldPass() {
7186
final Optional<StackGresBackup> backup = Optional.of(

stackgres-k8s/src/operator/src/test/java/io/stackgres/operator/conciliation/shardedcluster/context/ShardedClusterRestoreBackupContextAppenderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static org.junit.jupiter.api.Assertions.assertNull;
1111
import static org.junit.jupiter.api.Assertions.assertThrows;
1212
import static org.mockito.ArgumentMatchers.any;
13+
import static org.mockito.Mockito.verify;
1314
import static org.mockito.Mockito.when;
1415

1516
import java.time.Instant;
@@ -19,15 +20,18 @@
1920
import io.stackgres.common.crd.sgshardedbackup.ShardedBackupStatus;
2021
import io.stackgres.common.crd.sgshardedbackup.StackGresShardedBackup;
2122
import io.stackgres.common.crd.sgshardedbackup.StackGresShardedBackupBuilder;
23+
import io.stackgres.common.crd.sgshardedcluster.ShardedClusterStatusCondition;
2224
import io.stackgres.common.crd.sgshardedcluster.StackGresShardedCluster;
2325
import io.stackgres.common.crd.sgshardedcluster.StackGresShardedClusterInitialDataBuilder;
26+
import io.stackgres.common.crd.sgshardedcluster.StackGresShardedClusterStatusBuilder;
2427
import io.stackgres.common.fixture.Fixtures;
2528
import io.stackgres.common.resource.CustomResourceFinder;
2629
import io.stackgres.operator.conciliation.shardedcluster.StackGresShardedClusterContext;
2730
import org.junit.jupiter.api.BeforeEach;
2831
import org.junit.jupiter.api.Test;
2932
import org.junit.jupiter.api.extension.ExtendWith;
3033
import org.mockito.Mock;
34+
import org.mockito.Mockito;
3135
import org.mockito.Spy;
3236
import org.mockito.junit.jupiter.MockitoExtension;
3337

@@ -65,6 +69,16 @@ void givenClusterWithoutBackup_shouldPass() {
6569
assertNull(cluster.getStatus());
6670
}
6771

72+
@Test
73+
void givenBootstrappedCluster_shouldPass() {
74+
cluster.setStatus(
75+
new StackGresShardedClusterStatusBuilder()
76+
.addToConditions(ShardedClusterStatusCondition.SHARDED_CLUSTER_BOOTSTRAPPED.getCondition())
77+
.build());
78+
contextAppender.appendContext(cluster, contextBuilder);
79+
verify(backupFinder, Mockito.never()).findByNameAndNamespace(any(), any());
80+
}
81+
6882
@Test
6983
void givenClusterWithBackup_shouldPass() {
7084
final Optional<StackGresShardedBackup> backup = Optional.of(

0 commit comments

Comments
 (0)