Skip to content

Commit 98e8f0d

Browse files
committed
refactor: improve testeability of context construction
1 parent ae7fe7b commit 98e8f0d

15 files changed

Lines changed: 137 additions & 27 deletions

stackgres-k8s/src/common/src/main/java/io/stackgres/common/WebUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static Optional<Exception> checkUnsecureUri(String uri, Map<String, Object> head
6262
.buildGet()
6363
.invoke();
6464
return Optional.of(response.getStatus())
65-
.filter(statusCode -> statusCode.intValue() < BAD_REQUEST_STATUS_CODE)
65+
.filter(statusCode -> statusCode.intValue() >= BAD_REQUEST_STATUS_CODE)
6666
.map(status -> new Exception("Invalid status code " + status));
6767
}
6868
} catch (IOException | IllegalArgumentException | ProcessingException

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/ResourceKey.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
package io.stackgres.operator.conciliation;
77

8-
import java.util.Objects;
9-
108
import io.fabric8.kubernetes.api.model.HasMetadata;
119

1210
public record ResourceKey(String apiVersion, String kind, String namespace, String name) {
@@ -23,22 +21,4 @@ public static boolean same(HasMetadata resource1, HasMetadata resource2) {
2321
return ResourceKey.create(resource1).equals(ResourceKey.create(resource2));
2422
}
2523

26-
@Override
27-
public int hashCode() {
28-
return Objects.hash(apiVersion, kind, name, namespace);
29-
}
30-
31-
@Override
32-
public boolean equals(Object obj) {
33-
if (this == obj) {
34-
return true;
35-
}
36-
if (!(obj instanceof ResourceKey)) {
37-
return false;
38-
}
39-
ResourceKey other = (ResourceKey) obj;
40-
return Objects.equals(apiVersion, other.apiVersion) && Objects.equals(kind, other.kind)
41-
&& Objects.equals(name, other.name) && Objects.equals(namespace, other.namespace);
42-
}
43-
4424
}

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/factory/cluster/ClusterDefaultInstanceProfile.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package io.stackgres.operator.conciliation.factory.cluster;
77

88
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Optional;
11+
import java.util.Set;
912
import java.util.stream.Stream;
1013

1114
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -37,6 +40,15 @@ public Stream<HasMetadata> generateResource(StackGresClusterContext context) {
3740
.of(true)
3841
.filter(ignored -> context.getProfile().isEmpty()
3942
|| context.getProfile()
43+
.filter(instanceProfile -> labelFactory.defaultConfigLabels(context.getSource())
44+
.entrySet()
45+
.stream()
46+
.allMatch(label -> Optional
47+
.ofNullable(instanceProfile.getMetadata().getLabels())
48+
.stream()
49+
.map(Map::entrySet)
50+
.flatMap(Set::stream)
51+
.anyMatch(label::equals)))
4052
.map(instanceProfile -> instanceProfile.getMetadata().getOwnerReferences())
4153
.stream()
4254
.flatMap(List::stream)

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/factory/cluster/ClusterDefaultPostgresConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package io.stackgres.operator.conciliation.factory.cluster;
77

88
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Optional;
11+
import java.util.Set;
912
import java.util.stream.Stream;
1013

1114
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -37,6 +40,15 @@ public Stream<HasMetadata> generateResource(StackGresClusterContext context) {
3740
.of(true)
3841
.filter(ignored -> context.getPostgresConfig().isEmpty()
3942
|| context.getPostgresConfig()
43+
.filter(postgresConfig -> labelFactory.defaultConfigLabels(context.getSource())
44+
.entrySet()
45+
.stream()
46+
.allMatch(label -> Optional
47+
.ofNullable(postgresConfig.getMetadata().getLabels())
48+
.stream()
49+
.map(Map::entrySet)
50+
.flatMap(Set::stream)
51+
.anyMatch(label::equals)))
4052
.map(postgresConfig -> postgresConfig.getMetadata().getOwnerReferences())
4153
.stream()
4254
.flatMap(List::stream)

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/factory/cluster/sidecars/pooling/PgBouncerDefaultPoolingConfig.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
package io.stackgres.operator.conciliation.factory.cluster.sidecars.pooling;
77

88
import java.util.List;
9+
import java.util.Map;
910
import java.util.Optional;
11+
import java.util.Set;
1012
import java.util.stream.Stream;
1113

1214
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -42,7 +44,16 @@ public Stream<HasMetadata> generateResource(StackGresClusterContext context) {
4244
.filter(disabled -> !disabled)
4345
.filter(ignored -> context.getPoolingConfig().isEmpty()
4446
|| context.getPoolingConfig()
45-
.map(postgresConfig -> postgresConfig.getMetadata().getOwnerReferences())
47+
.filter(poolingConfig -> labelFactory.defaultConfigLabels(context.getSource())
48+
.entrySet()
49+
.stream()
50+
.allMatch(label -> Optional
51+
.ofNullable(poolingConfig.getMetadata().getLabels())
52+
.stream()
53+
.map(Map::entrySet)
54+
.flatMap(Set::stream)
55+
.anyMatch(label::equals)))
56+
.map(poolingConfig -> poolingConfig.getMetadata().getOwnerReferences())
4657
.stream()
4758
.flatMap(List::stream)
4859
.anyMatch(ResourceUtil.getControllerOwnerReference(context.getSource())::equals))

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/factory/distributedlogs/DistributedLogsDefaultInstanceProfile.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package io.stackgres.operator.conciliation.factory.distributedlogs;
77

88
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Optional;
11+
import java.util.Set;
912
import java.util.stream.Stream;
1013

1114
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -37,6 +40,15 @@ public Stream<HasMetadata> generateResource(StackGresDistributedLogsContext cont
3740
.of(true)
3841
.filter(ignored -> context.getProfile().isEmpty()
3942
|| context.getProfile()
43+
.filter(instanceProfile -> labelFactory.defaultConfigLabels(context.getSource())
44+
.entrySet()
45+
.stream()
46+
.allMatch(label -> Optional
47+
.ofNullable(instanceProfile.getMetadata().getLabels())
48+
.stream()
49+
.map(Map::entrySet)
50+
.flatMap(Set::stream)
51+
.anyMatch(label::equals)))
4052
.map(instanceProfile -> instanceProfile.getMetadata().getOwnerReferences())
4153
.stream()
4254
.flatMap(List::stream)

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/factory/distributedlogs/DistributedLogsDefaultPostgresConfig.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package io.stackgres.operator.conciliation.factory.distributedlogs;
77

88
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Optional;
11+
import java.util.Set;
912
import java.util.stream.Stream;
1013

1114
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -38,6 +41,15 @@ public Stream<HasMetadata> generateResource(StackGresDistributedLogsContext cont
3841
.of(true)
3942
.filter(ignored -> context.getPostgresConfig().isEmpty()
4043
|| context.getPostgresConfig()
44+
.filter(postgresConfig -> labelFactory.defaultConfigLabels(context.getSource())
45+
.entrySet()
46+
.stream()
47+
.allMatch(label -> Optional
48+
.ofNullable(postgresConfig.getMetadata().getLabels())
49+
.stream()
50+
.map(Map::entrySet)
51+
.flatMap(Set::stream)
52+
.anyMatch(label::equals)))
4153
.map(postgresConfig -> postgresConfig.getMetadata().getOwnerReferences())
4254
.stream()
4355
.flatMap(List::stream)
@@ -51,7 +63,7 @@ private StackGresPostgresConfig getDefaultConfig(StackGresDistributedLogsContext
5163
.withNewMetadata()
5264
.withNamespace(cluster.getMetadata().getNamespace())
5365
.withName(cluster.getSpec().getConfigurations().getSgPostgresConfig())
54-
.withLabels(labelFactory.genericLabels(cluster))
66+
.withLabels(labelFactory.defaultConfigLabels(cluster))
5567
.endMetadata()
5668
.withNewSpec()
5769
.withPostgresVersion(getPostgresMajorVersion(context))

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package io.stackgres.operator.conciliation.factory.shardedcluster;
77

88
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Optional;
11+
import java.util.Set;
912
import java.util.stream.Stream;
1013

1114
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -38,6 +41,15 @@ public Stream<HasMetadata> generateResource(StackGresShardedClusterContext conte
3841
.of(true)
3942
.filter(ignored -> context.getCoordinatorProfile().isEmpty()
4043
|| context.getCoordinatorProfile()
44+
.filter(instanceProfile -> labelFactory.defaultConfigLabels(context.getSource())
45+
.entrySet()
46+
.stream()
47+
.allMatch(label -> Optional
48+
.ofNullable(instanceProfile.getMetadata().getLabels())
49+
.stream()
50+
.map(Map::entrySet)
51+
.flatMap(Set::stream)
52+
.anyMatch(label::equals)))
4153
.map(instanceProfile -> instanceProfile.getMetadata().getOwnerReferences())
4254
.stream()
4355
.flatMap(List::stream)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package io.stackgres.operator.conciliation.factory.shardedcluster;
77

88
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Optional;
11+
import java.util.Set;
912
import java.util.stream.Stream;
1013

1114
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -38,6 +41,15 @@ public Stream<HasMetadata> generateResource(StackGresShardedClusterContext conte
3841
.of(true)
3942
.filter(ignored -> context.getCoordinatorPostgresConfig().isEmpty()
4043
|| context.getCoordinatorPostgresConfig()
44+
.filter(postgresConfig -> labelFactory.defaultConfigLabels(context.getSource())
45+
.entrySet()
46+
.stream()
47+
.allMatch(label -> Optional
48+
.ofNullable(postgresConfig.getMetadata().getLabels())
49+
.stream()
50+
.map(Map::entrySet)
51+
.flatMap(Set::stream)
52+
.anyMatch(label::equals)))
4153
.map(postgresConfig -> postgresConfig.getMetadata().getOwnerReferences())
4254
.stream()
4355
.flatMap(List::stream)

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
package io.stackgres.operator.conciliation.factory.shardedcluster;
77

88
import java.util.List;
9+
import java.util.Map;
910
import java.util.Optional;
11+
import java.util.Set;
1012
import java.util.stream.Stream;
1113

1214
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -43,7 +45,16 @@ public Stream<HasMetadata> generateResource(StackGresShardedClusterContext conte
4345
.filter(disabled -> !disabled)
4446
.filter(ignored -> context.getCoordinatorPoolingConfig().isEmpty()
4547
|| context.getCoordinatorPoolingConfig()
46-
.map(postgresConfig -> postgresConfig.getMetadata().getOwnerReferences())
48+
.filter(poolingConfig -> labelFactory.defaultConfigLabels(context.getSource())
49+
.entrySet()
50+
.stream()
51+
.allMatch(label -> Optional
52+
.ofNullable(poolingConfig.getMetadata().getLabels())
53+
.stream()
54+
.map(Map::entrySet)
55+
.flatMap(Set::stream)
56+
.anyMatch(label::equals)))
57+
.map(poolingConfig -> poolingConfig.getMetadata().getOwnerReferences())
4758
.stream()
4859
.flatMap(List::stream)
4960
.anyMatch(ResourceUtil.getControllerOwnerReference(context.getSource())::equals))

0 commit comments

Comments
 (0)