Skip to content

Commit cf673a1

Browse files
committed
fix: coordinator and shards services should inherit all the sharded cluster's services config
Closes #3008
1 parent f883dcd commit cf673a1

4 files changed

Lines changed: 226 additions & 21 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.fabric8.kubernetes.api.model.ObjectMeta;
1717
import io.stackgres.common.ManagedSqlUtil;
1818
import io.stackgres.common.StackGresShardedClusterUtil;
19+
import io.stackgres.common.crd.CustomServicePortBuilder;
1920
import io.stackgres.common.crd.SecretKeySelector;
2021
import io.stackgres.common.crd.postgres.service.StackGresPostgresService;
2122
import io.stackgres.common.crd.postgres.service.StackGresPostgresServicesBuilder;
@@ -91,6 +92,12 @@ StackGresCluster getCoordinatorCluster(StackGresShardedCluster cluster) {
9192
Optional.ofNullable(postgresServices)
9293
.map(StackGresShardedClusterPostgresServices::getCoordinator)
9394
.map(StackGresShardedClusterPostgresCoordinatorServices::getCustomPorts)
95+
.map(customPorts -> customPorts
96+
.stream()
97+
.map(customPort -> new CustomServicePortBuilder(customPort)
98+
.withNodePort(null)
99+
.build())
100+
.toList())
94101
.orElse(null))
95102
.endPrimary()
96103
.withNewReplicas()
@@ -104,6 +111,12 @@ StackGresCluster getCoordinatorCluster(StackGresShardedCluster cluster) {
104111
Optional.ofNullable(postgresServices)
105112
.map(StackGresShardedClusterPostgresServices::getCoordinator)
106113
.map(StackGresShardedClusterPostgresCoordinatorServices::getCustomPorts)
114+
.map(customPorts -> customPorts
115+
.stream()
116+
.map(customPort -> new CustomServicePortBuilder(customPort)
117+
.withNodePort(null)
118+
.build())
119+
.toList())
107120
.orElse(null))
108121
.endReplicas()
109122
.build());
@@ -155,6 +168,12 @@ StackGresCluster getShardsCluster(StackGresShardedCluster cluster, int index) {
155168
Optional.ofNullable(postgresServices)
156169
.map(StackGresShardedClusterPostgresServices::getShards)
157170
.map(StackGresShardedClusterPostgresShardsServices::getCustomPorts)
171+
.map(customPorts -> customPorts
172+
.stream()
173+
.map(customPort -> new CustomServicePortBuilder(customPort)
174+
.withNodePort(null)
175+
.build())
176+
.toList())
158177
.orElse(null))
159178
.endPrimary()
160179
.withNewReplicas()

stackgres-k8s/src/operator/src/test/java/io/stackgres/operator/conciliation/factory/shardedcluster/StackGresShardedClusterForCitusUtilTest.java

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.List;
1515

1616
import io.stackgres.common.StackGresShardedClusterUtil;
17+
import io.stackgres.common.crd.CustomServicePortBuilder;
1718
import io.stackgres.common.crd.SecretKeySelector;
1819
import io.stackgres.common.crd.postgres.service.StackGresPostgresServiceBuilder;
1920
import io.stackgres.common.crd.sgcluster.StackGresCluster;
@@ -114,7 +115,7 @@ void givedMinimalShardedClusterPrimaryDisabled_shouldGenerateCoordinatorCluster(
114115
0);
115116
Assertions.assertEquals(
116117
new StackGresPostgresServiceBuilder()
117-
.withEnabled(false)
118+
.withEnabled(true)
118119
.build(),
119120
cluster.getSpec().getPostgresServices().getPrimary());
120121
Assertions.assertEquals(
@@ -124,6 +125,42 @@ void givedMinimalShardedClusterPrimaryDisabled_shouldGenerateCoordinatorCluster(
124125
cluster.getSpec().getPostgresServices().getReplicas());
125126
}
126127

128+
@Test
129+
void givedMinimalShardedClusterPrimaryDisabledAndAnyDisabled_shouldGenerateCoordinatorCluster() {
130+
var shardedCluster = getMinimalShardedCluster();
131+
shardedCluster.getSpec().setPostgresServices(
132+
new StackGresShardedClusterPostgresServicesBuilder()
133+
.withNewCoordinator()
134+
.withNewPrimary()
135+
.withEnabled(false)
136+
.endPrimary()
137+
.withNewAny()
138+
.withEnabled(false)
139+
.endAny()
140+
.endCoordinator()
141+
.build());
142+
var coordinatorPrimary =
143+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getPrimary();
144+
coordinatorPrimary.setEnabled(false);
145+
var cluster = getCoordinatorCluster(JsonUtil.copy(shardedCluster));
146+
checkCoordinatorWithGlobalSettings(
147+
shardedCluster,
148+
shardedCluster.getSpec().getCoordinator(),
149+
shardedCluster.getSpec().getCoordinator().getConfigurationsForCoordinator(),
150+
cluster,
151+
0);
152+
Assertions.assertEquals(
153+
new StackGresPostgresServiceBuilder()
154+
.withEnabled(false)
155+
.build(),
156+
cluster.getSpec().getPostgresServices().getPrimary());
157+
Assertions.assertEquals(
158+
new StackGresPostgresServiceBuilder()
159+
.withEnabled(false)
160+
.build(),
161+
cluster.getSpec().getPostgresServices().getReplicas());
162+
}
163+
127164
@Test
128165
void givedMinimalShardedCluster_shouldGenerateShardCluster() {
129166
var shardedCluster = getMinimalShardedCluster();
@@ -243,11 +280,28 @@ void givedShardedClusterWithMinimalCoordinator_shouldCopyGlobalSettings() {
243280
cluster,
244281
0);
245282
Assertions.assertEquals(
246-
shardedCluster.getSpec().getPostgresServices().getCoordinator().getPrimary(),
247-
cluster.getSpec().getPostgresServices().getPrimary());
283+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getPrimary().getEnabled()
284+
|| shardedCluster.getSpec().getPostgresServices().getCoordinator().getAny().getEnabled(),
285+
cluster.getSpec().getPostgresServices().getPrimary().getEnabled());
248286
Assertions.assertEquals(
249-
shardedCluster.getSpec().getPostgresServices().getCoordinator().getAny(),
250-
cluster.getSpec().getPostgresServices().getReplicas());
287+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getCustomPorts()
288+
.stream()
289+
.map(customPort -> new CustomServicePortBuilder(customPort)
290+
.withNodePort(null)
291+
.build())
292+
.toList(),
293+
cluster.getSpec().getPostgresServices().getPrimary().getCustomPorts());
294+
Assertions.assertEquals(
295+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getAny().getEnabled(),
296+
cluster.getSpec().getPostgresServices().getReplicas().getEnabled());
297+
Assertions.assertEquals(
298+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getCustomPorts()
299+
.stream()
300+
.map(customPort -> new CustomServicePortBuilder(customPort)
301+
.withNodePort(null)
302+
.build())
303+
.toList(),
304+
cluster.getSpec().getPostgresServices().getReplicas().getCustomPorts());
251305
}
252306

253307
@Test
@@ -270,8 +324,16 @@ void givedShardedClusterWithMinimalShards_shouldCopyGlobalSettings() {
270324
cluster,
271325
1);
272326
Assertions.assertEquals(
273-
shardedCluster.getSpec().getPostgresServices().getShards().getPrimaries(),
274-
cluster.getSpec().getPostgresServices().getPrimary());
327+
shardedCluster.getSpec().getPostgresServices().getShards().getPrimaries().getEnabled(),
328+
cluster.getSpec().getPostgresServices().getPrimary().getEnabled());
329+
Assertions.assertEquals(
330+
shardedCluster.getSpec().getPostgresServices().getShards().getCustomPorts()
331+
.stream()
332+
.map(customPort -> new CustomServicePortBuilder(customPort)
333+
.withNodePort(null)
334+
.build())
335+
.toList(),
336+
cluster.getSpec().getPostgresServices().getPrimary().getCustomPorts());
275337
Assertions.assertEquals(
276338
new StackGresPostgresServiceBuilder()
277339
.withEnabled(false)

stackgres-k8s/src/operator/src/test/java/io/stackgres/operator/conciliation/factory/shardedcluster/StackGresShardedClusterForDdpUtilTest.java

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.List;
1414

1515
import io.stackgres.common.StackGresShardedClusterUtil;
16+
import io.stackgres.common.crd.CustomServicePortBuilder;
1617
import io.stackgres.common.crd.SecretKeySelector;
1718
import io.stackgres.common.crd.postgres.service.StackGresPostgresServiceBuilder;
1819
import io.stackgres.common.crd.sgcluster.StackGresCluster;
@@ -111,7 +112,7 @@ void givedMinimalShardedClusterPrimaryDisabled_shouldGenerateCoordinatorCluster(
111112
0);
112113
Assertions.assertEquals(
113114
new StackGresPostgresServiceBuilder()
114-
.withEnabled(false)
115+
.withEnabled(true)
115116
.build(),
116117
cluster.getSpec().getPostgresServices().getPrimary());
117118
Assertions.assertEquals(
@@ -121,6 +122,42 @@ void givedMinimalShardedClusterPrimaryDisabled_shouldGenerateCoordinatorCluster(
121122
cluster.getSpec().getPostgresServices().getReplicas());
122123
}
123124

125+
@Test
126+
void givedMinimalShardedClusterPrimaryDisabledAndAnyDisabled_shouldGenerateCoordinatorCluster() {
127+
var shardedCluster = getMinimalShardedCluster();
128+
shardedCluster.getSpec().setPostgresServices(
129+
new StackGresShardedClusterPostgresServicesBuilder()
130+
.withNewCoordinator()
131+
.withNewPrimary()
132+
.withEnabled(false)
133+
.endPrimary()
134+
.withNewAny()
135+
.withEnabled(false)
136+
.endAny()
137+
.endCoordinator()
138+
.build());
139+
var coordinatorPrimary =
140+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getPrimary();
141+
coordinatorPrimary.setEnabled(false);
142+
var cluster = getCoordinatorCluster(JsonUtil.copy(shardedCluster));
143+
checkClusterWithGlobalSettings(
144+
shardedCluster,
145+
shardedCluster.getSpec().getCoordinator(),
146+
shardedCluster.getSpec().getCoordinator().getConfigurationsForCoordinator(),
147+
cluster,
148+
0);
149+
Assertions.assertEquals(
150+
new StackGresPostgresServiceBuilder()
151+
.withEnabled(false)
152+
.build(),
153+
cluster.getSpec().getPostgresServices().getPrimary());
154+
Assertions.assertEquals(
155+
new StackGresPostgresServiceBuilder()
156+
.withEnabled(false)
157+
.build(),
158+
cluster.getSpec().getPostgresServices().getReplicas());
159+
}
160+
124161
@Test
125162
void givedMinimalShardedCluster_shouldGenerateShardCluster() {
126163
var shardedCluster = getMinimalShardedCluster();
@@ -240,11 +277,28 @@ void givedShardedClusterWithMinimalCoordinator_shouldCopyGlobalSettings() {
240277
cluster,
241278
0);
242279
Assertions.assertEquals(
243-
shardedCluster.getSpec().getPostgresServices().getCoordinator().getPrimary(),
244-
cluster.getSpec().getPostgresServices().getPrimary());
280+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getPrimary().getEnabled()
281+
|| shardedCluster.getSpec().getPostgresServices().getCoordinator().getAny().getEnabled(),
282+
cluster.getSpec().getPostgresServices().getPrimary().getEnabled());
245283
Assertions.assertEquals(
246-
shardedCluster.getSpec().getPostgresServices().getCoordinator().getAny(),
247-
cluster.getSpec().getPostgresServices().getReplicas());
284+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getCustomPorts()
285+
.stream()
286+
.map(customPort -> new CustomServicePortBuilder(customPort)
287+
.withNodePort(null)
288+
.build())
289+
.toList(),
290+
cluster.getSpec().getPostgresServices().getPrimary().getCustomPorts());
291+
Assertions.assertEquals(
292+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getAny().getEnabled(),
293+
cluster.getSpec().getPostgresServices().getReplicas().getEnabled());
294+
Assertions.assertEquals(
295+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getCustomPorts()
296+
.stream()
297+
.map(customPort -> new CustomServicePortBuilder(customPort)
298+
.withNodePort(null)
299+
.build())
300+
.toList(),
301+
cluster.getSpec().getPostgresServices().getReplicas().getCustomPorts());
248302
}
249303

250304
@Test
@@ -267,8 +321,16 @@ void givedShardedClusterWithMinimalShards_shouldCopyGlobalSettings() {
267321
cluster,
268322
1);
269323
Assertions.assertEquals(
270-
shardedCluster.getSpec().getPostgresServices().getShards().getPrimaries(),
271-
cluster.getSpec().getPostgresServices().getPrimary());
324+
shardedCluster.getSpec().getPostgresServices().getShards().getPrimaries().getEnabled(),
325+
cluster.getSpec().getPostgresServices().getPrimary().getEnabled());
326+
Assertions.assertEquals(
327+
shardedCluster.getSpec().getPostgresServices().getShards().getCustomPorts()
328+
.stream()
329+
.map(customPort -> new CustomServicePortBuilder(customPort)
330+
.withNodePort(null)
331+
.build())
332+
.toList(),
333+
cluster.getSpec().getPostgresServices().getPrimary().getCustomPorts());
272334
Assertions.assertEquals(
273335
new StackGresPostgresServiceBuilder()
274336
.withEnabled(false)

stackgres-k8s/src/operator/src/test/java/io/stackgres/operator/conciliation/factory/shardedcluster/StackGresShardedClusterForShardingSphereUtilTest.java

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.List;
1414

1515
import io.stackgres.common.StackGresShardedClusterUtil;
16+
import io.stackgres.common.crd.CustomServicePortBuilder;
1617
import io.stackgres.common.crd.SecretKeySelector;
1718
import io.stackgres.common.crd.postgres.service.StackGresPostgresServiceBuilder;
1819
import io.stackgres.common.crd.sgcluster.StackGresCluster;
@@ -111,7 +112,7 @@ void givedMinimalShardedClusterPrimaryDisabled_shouldGenerateCoordinatorCluster(
111112
0);
112113
Assertions.assertEquals(
113114
new StackGresPostgresServiceBuilder()
114-
.withEnabled(false)
115+
.withEnabled(true)
115116
.build(),
116117
cluster.getSpec().getPostgresServices().getPrimary());
117118
Assertions.assertEquals(
@@ -121,6 +122,42 @@ void givedMinimalShardedClusterPrimaryDisabled_shouldGenerateCoordinatorCluster(
121122
cluster.getSpec().getPostgresServices().getReplicas());
122123
}
123124

125+
@Test
126+
void givedMinimalShardedClusterPrimaryDisabledAndAnyDisabled_shouldGenerateCoordinatorCluster() {
127+
var shardedCluster = getMinimalShardedCluster();
128+
shardedCluster.getSpec().setPostgresServices(
129+
new StackGresShardedClusterPostgresServicesBuilder()
130+
.withNewCoordinator()
131+
.withNewPrimary()
132+
.withEnabled(false)
133+
.endPrimary()
134+
.withNewAny()
135+
.withEnabled(false)
136+
.endAny()
137+
.endCoordinator()
138+
.build());
139+
var coordinatorPrimary =
140+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getPrimary();
141+
coordinatorPrimary.setEnabled(false);
142+
var cluster = getCoordinatorCluster(JsonUtil.copy(shardedCluster));
143+
checkClusterWithGlobalSettings(
144+
shardedCluster,
145+
shardedCluster.getSpec().getCoordinator(),
146+
shardedCluster.getSpec().getCoordinator().getConfigurationsForCoordinator(),
147+
cluster,
148+
0);
149+
Assertions.assertEquals(
150+
new StackGresPostgresServiceBuilder()
151+
.withEnabled(false)
152+
.build(),
153+
cluster.getSpec().getPostgresServices().getPrimary());
154+
Assertions.assertEquals(
155+
new StackGresPostgresServiceBuilder()
156+
.withEnabled(false)
157+
.build(),
158+
cluster.getSpec().getPostgresServices().getReplicas());
159+
}
160+
124161
@Test
125162
void givedMinimalShardedCluster_shouldGenerateShardCluster() {
126163
var shardedCluster = getMinimalShardedCluster();
@@ -240,11 +277,28 @@ void givedShardedClusterWithMinimalCoordinator_shouldCopyGlobalSettings() {
240277
cluster,
241278
0);
242279
Assertions.assertEquals(
243-
shardedCluster.getSpec().getPostgresServices().getCoordinator().getPrimary(),
244-
cluster.getSpec().getPostgresServices().getPrimary());
280+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getPrimary().getEnabled()
281+
|| shardedCluster.getSpec().getPostgresServices().getCoordinator().getAny().getEnabled(),
282+
cluster.getSpec().getPostgresServices().getPrimary().getEnabled());
245283
Assertions.assertEquals(
246-
shardedCluster.getSpec().getPostgresServices().getCoordinator().getAny(),
247-
cluster.getSpec().getPostgresServices().getReplicas());
284+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getCustomPorts()
285+
.stream()
286+
.map(customPort -> new CustomServicePortBuilder(customPort)
287+
.withNodePort(null)
288+
.build())
289+
.toList(),
290+
cluster.getSpec().getPostgresServices().getPrimary().getCustomPorts());
291+
Assertions.assertEquals(
292+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getAny().getEnabled(),
293+
cluster.getSpec().getPostgresServices().getReplicas().getEnabled());
294+
Assertions.assertEquals(
295+
shardedCluster.getSpec().getPostgresServices().getCoordinator().getCustomPorts()
296+
.stream()
297+
.map(customPort -> new CustomServicePortBuilder(customPort)
298+
.withNodePort(null)
299+
.build())
300+
.toList(),
301+
cluster.getSpec().getPostgresServices().getReplicas().getCustomPorts());
248302
}
249303

250304
@Test
@@ -267,8 +321,16 @@ void givedShardedClusterWithMinimalShards_shouldCopyGlobalSettings() {
267321
cluster,
268322
1);
269323
Assertions.assertEquals(
270-
shardedCluster.getSpec().getPostgresServices().getShards().getPrimaries(),
271-
cluster.getSpec().getPostgresServices().getPrimary());
324+
shardedCluster.getSpec().getPostgresServices().getShards().getPrimaries().getEnabled(),
325+
cluster.getSpec().getPostgresServices().getPrimary().getEnabled());
326+
Assertions.assertEquals(
327+
shardedCluster.getSpec().getPostgresServices().getShards().getCustomPorts()
328+
.stream()
329+
.map(customPort -> new CustomServicePortBuilder(customPort)
330+
.withNodePort(null)
331+
.build())
332+
.toList(),
333+
cluster.getSpec().getPostgresServices().getPrimary().getCustomPorts());
272334
Assertions.assertEquals(
273335
new StackGresPostgresServiceBuilder()
274336
.withEnabled(false)

0 commit comments

Comments
 (0)