Skip to content

Commit a14f72b

Browse files
committed
feat: generate default configs using reconciliation cycle
1 parent 98e8f0d commit a14f72b

12 files changed

Lines changed: 152 additions & 40 deletions

File tree

stackgres-k8s/ci/build/build-functions.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,8 @@ show_image_hashes() {
634634
}
635635
636636
find_image_digests() {
637+
(! ls stackgres-k8s/ci/build/target/image-digests.* > /dev/null 2>&1 \
638+
|| rm -rf stackgres-k8s/ci/build/target/image-digests.*)
637639
sort "$1" | uniq \
638640
| xargs -I @ -P 16 sh $(! echo $- | grep -q x || printf %s "-x") \
639641
stackgres-k8s/ci/build/build-functions.sh find_image_digest @

stackgres-k8s/src/common/src/main/java/io/stackgres/common/crd/sgcluster/ClusterStatusCondition.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
public enum ClusterStatusCondition {
1313

14+
CLUSTER_BOOTSTRAPPED(Type.BOOTSTRAPPED, Status.TRUE, "ClusterBootstrapped"),
1415
POD_REQUIRES_RESTART(Type.PENDING_RESTART, Status.TRUE, "PodRequiresRestart"),
1516
FALSE_PENDING_RESTART(Type.PENDING_RESTART, Status.FALSE, "FalsePendingRestart"),
1617
CLUSTER_REQUIRES_UPGRADE(Type.PENDING_UPGRADE, Status.TRUE, "ClusterRequiresUpgrade"),
@@ -39,6 +40,7 @@ public boolean isCondition(Condition condition) {
3940
}
4041

4142
public enum Type {
43+
BOOTSTRAPPED("Bootstrapped"),
4244
PENDING_RESTART("PendingRestart"),
4345
PENDING_UPGRADE("PendingUpgrade"),
4446
FAILED("Failed");

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public StackGresCluster refreshCondition(StackGresCluster source) {
7777
} else {
7878
updateCondition(getFalsePendingUpgrade(), source);
7979
}
80+
if (source.getStatus() != null
81+
&& source.getStatus().getArch() != null
82+
&& source.getStatus().getOs() != null) {
83+
updateCondition(getClusterBootstrapped(), source);
84+
}
8085
if (source.getStatus() != null
8186
&& source.getStatus().getArch() != null
8287
&& source.getStatus().getOs() != null
@@ -225,6 +230,10 @@ protected Condition getClusterRequiresUpgrade() {
225230
return ClusterStatusCondition.CLUSTER_REQUIRES_UPGRADE.getCondition();
226231
}
227232

233+
protected Condition getClusterBootstrapped() {
234+
return ClusterStatusCondition.CLUSTER_BOOTSTRAPPED.getCondition();
235+
}
236+
228237
record StatusContext(
229238
List<StackGresClusterPodStatus> clusterPodStatuses,
230239
Optional<StatefulSet> clusterStatefulSet,

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static io.stackgres.common.StackGresUtil.getPostgresFlavorComponent;
99

10+
import java.util.List;
1011
import java.util.Map;
1112
import java.util.Optional;
1213
import java.util.function.Function;
@@ -18,10 +19,12 @@
1819
import io.stackgres.common.crd.sgbackup.StackGresBackup;
1920
import io.stackgres.common.crd.sgbackup.StackGresBackupConfigSpec;
2021
import io.stackgres.common.crd.sgbackup.StackGresBackupStatus;
22+
import io.stackgres.common.crd.sgcluster.ClusterStatusCondition;
2123
import io.stackgres.common.crd.sgcluster.StackGresCluster;
2224
import io.stackgres.common.crd.sgcluster.StackGresClusterInitialData;
2325
import io.stackgres.common.crd.sgcluster.StackGresClusterRestore;
2426
import io.stackgres.common.crd.sgcluster.StackGresClusterRestoreFromBackup;
27+
import io.stackgres.common.crd.sgcluster.StackGresClusterStatus;
2528
import io.stackgres.common.resource.CustomResourceFinder;
2629
import io.stackgres.common.resource.ResourceFinder;
2730
import io.stackgres.operator.conciliation.ContextAppender;
@@ -54,6 +57,18 @@ public void appendContext(StackGresCluster cluster, Builder contextBuilder) {
5457
cluster,
5558
cluster.getMetadata().getNamespace());
5659

60+
if (Optional.of(cluster)
61+
.map(StackGresCluster::getStatus)
62+
.map(StackGresClusterStatus::getConditions)
63+
.stream()
64+
.flatMap(List::stream)
65+
.anyMatch(ClusterStatusCondition.CLUSTER_BOOTSTRAPPED::isCondition)) {
66+
contextBuilder
67+
.restoreBackup(Optional.empty())
68+
.restoreSecrets(Map.of());
69+
return;
70+
}
71+
5772
final Map<String, Secret> restoreSecrets = restoreBackup
5873
.map(StackGresBackup::getStatus)
5974
.map(StackGresBackupStatus::getSgBackupConfig)

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

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import java.util.Objects;
1111
import java.util.Optional;
12+
import java.util.Set;
1213
import java.util.stream.Stream;
1314

1415
import io.fabric8.kubernetes.api.model.ConfigMap;
@@ -147,9 +148,17 @@ protected void onPostReconciliation(StackGresDistributedLogs config) {
147148
cluster.getMetadata().getName(),
148149
config.getMetadata().getName()))
149150
.findFirst();
151+
LOGGER.debug("{} {} was {}",
152+
StackGresCluster.KIND,
153+
config.getMetadata().getName(),
154+
foundCluster.isPresent() ? "found" : "not found");
150155
Optional<StackGresPostgresConfig> foundPostgresConfig = postgresConfigFinder.findByNameAndNamespace(
151156
config.getSpec().getConfigurations().getSgPostgresConfig(),
152157
config.getMetadata().getNamespace());
158+
LOGGER.debug("{} {} was {}",
159+
StackGresPostgresConfig.KIND,
160+
config.getSpec().getConfigurations().getSgPostgresConfig(),
161+
foundPostgresConfig.isPresent() ? "found" : "not found");
153162
foundCluster.ifPresent(cluster -> distributedLogsScheduler
154163
.update(config, foundConfig -> {
155164
setVersionFromCluster(cluster, foundConfig);
@@ -222,24 +231,50 @@ protected void onPostReconciliation(StackGresDistributedLogs config) {
222231
(currentDistributedLogs) -> currentDistributedLogs.setStatus(config.getStatus()));
223232
}
224233

225-
private void setVersionFromCluster(StackGresCluster cluster, StackGresDistributedLogs foundConfig) {
226-
foundConfig.getMetadata().setAnnotations(
234+
private void setVersionFromCluster(StackGresCluster cluster, StackGresDistributedLogs config) {
235+
String currentVersion = Optional.ofNullable(config.getMetadata().getAnnotations())
236+
.stream()
237+
.map(Map::entrySet)
238+
.flatMap(Set::stream)
239+
.filter(entry -> entry.getKey().equals(StackGresContext.VERSION_KEY))
240+
.map(Map.Entry::getValue)
241+
.findFirst()
242+
.orElse(null);
243+
config.getMetadata().setAnnotations(
227244
Seq.of(
228245
Optional.ofNullable(cluster.getMetadata().getAnnotations())
229246
.orElse(Map.of()))
230247
.flatMap(annotations -> Seq.seq(annotations)
231248
.filter(annotation -> annotation.v1.equals(StackGresContext.VERSION_KEY))
232249
.append(Stream.of(
233-
Optional.ofNullable(foundConfig.getMetadata().getAnnotations())
250+
Optional.ofNullable(config.getMetadata().getAnnotations())
234251
.orElse(Map.of()))
235252
.flatMap(existingAnnotations -> Seq.seq(existingAnnotations)
236253
.filter(annotation -> !annotations.containsKey(StackGresContext.VERSION_KEY)
237254
|| !annotation.v1.equals(StackGresContext.VERSION_KEY)))))
238255
.toMap(Tuple2::v1, Tuple2::v2));
256+
String updatedVersion = Optional.ofNullable(config.getMetadata().getAnnotations())
257+
.stream()
258+
.map(Map::entrySet)
259+
.flatMap(Set::stream)
260+
.filter(entry -> entry.getKey().equals(StackGresContext.VERSION_KEY))
261+
.map(Map.Entry::getValue)
262+
.findFirst()
263+
.orElse(null);
264+
if (!Objects.equals(currentVersion, updatedVersion)) {
265+
LOGGER.debug("{} {} {} annotation was updated from {} to {}",
266+
StackGresDistributedLogs.KIND,
267+
config.getMetadata().getName(),
268+
StackGresContext.VERSION_KEY,
269+
currentVersion,
270+
updatedVersion);
271+
}
239272
}
240273

241-
private void setClusterConfigurationIfMajorVersionMismatch(Optional<StackGresPostgresConfig> foundPostgresConfig,
242-
StackGresCluster cluster, StackGresDistributedLogs foundConfig) {
274+
private void setClusterConfigurationIfMajorVersionMismatch(
275+
Optional<StackGresPostgresConfig> foundPostgresConfig,
276+
StackGresCluster cluster,
277+
StackGresDistributedLogs config) {
243278
if (foundPostgresConfig
244279
.map(StackGresPostgresConfig::getSpec)
245280
.map(StackGresPostgresConfigSpec::getPostgresVersion)
@@ -254,7 +289,16 @@ private void setClusterConfigurationIfMajorVersionMismatch(Optional<StackGresPos
254289
.map(StackGresClusterSpec::getConfigurations)
255290
.map(StackGresClusterConfigurations::getSgPostgresConfig)
256291
.isPresent()) {
257-
foundConfig.getSpec().getConfigurations().setSgPostgresConfig(
292+
LOGGER.debug("Postgres configuration {} (version {}) was updated to {}"
293+
+ " (cluster postgres version {})",
294+
config.getSpec().getConfigurations().getSgPostgresConfig(),
295+
foundPostgresConfig
296+
.map(StackGresPostgresConfig::getSpec)
297+
.map(StackGresPostgresConfigSpec::getPostgresVersion)
298+
.orElse(null),
299+
cluster.getSpec().getConfigurations().getSgPostgresConfig(),
300+
cluster.getSpec().getPostgres().getVersion());
301+
config.getSpec().getConfigurations().setSgPostgresConfig(
258302
cluster.getSpec().getConfigurations().getSgPostgresConfig());
259303
}
260304
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import java.util.stream.Stream;
1313

1414
import io.fabric8.kubernetes.api.model.HasMetadata;
15-
import io.stackgres.common.crd.sgcluster.StackGresCluster;
1615
import io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs;
1716
import io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig;
1817
import io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfigBuilder;
1918
import io.stackgres.common.labels.LabelFactoryForDistributedLogs;
19+
import io.stackgres.operator.common.StackGresDistributedLogsUtil;
2020
import io.stackgres.operator.conciliation.OperatorVersionBinder;
2121
import io.stackgres.operator.conciliation.ResourceGenerator;
2222
import io.stackgres.operator.conciliation.distributedlogs.StackGresDistributedLogsContext;
@@ -66,15 +66,14 @@ private StackGresPostgresConfig getDefaultConfig(StackGresDistributedLogsContext
6666
.withLabels(labelFactory.defaultConfigLabels(cluster))
6767
.endMetadata()
6868
.withNewSpec()
69-
.withPostgresVersion(getPostgresMajorVersion(context))
69+
.withPostgresVersion(
70+
getPostgresMajorVersion(
71+
StackGresDistributedLogsUtil.getPostgresVersion(cluster)))
7072
.endSpec()
7173
.build();
7274
}
7375

74-
private String getPostgresMajorVersion(StackGresDistributedLogsContext context) {
75-
StackGresCluster cluster = DistributedLogsCluster.getCluster(
76-
labelFactory, context.getSource(), context.getCluster());
77-
String version = cluster.getSpec().getPostgres().getVersion();
76+
private String getPostgresMajorVersion(String version) {
7877
return version.split("\\.")[0];
7978
}
8079

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.stackgres.operator.conciliation.factory.distributedlogs;
77

8-
import java.util.Optional;
98
import java.util.stream.Stream;
109

1110
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -50,7 +49,7 @@ public DistributedLogsService(LabelFactoryForDistributedLogs labelFactory) {
5049
public Stream<HasMetadata> generateResource(StackGresDistributedLogsContext context) {
5150
return Seq.of(getService(context));
5251
}
53-
52+
5453
private Service getService(StackGresDistributedLogsContext context) {
5554
StackGresDistributedLogs distributedLogs = context.getSource();
5655
return new ServiceBuilder()
@@ -63,7 +62,7 @@ private Service getService(StackGresDistributedLogsContext context) {
6362
.withType("ExternalName")
6463
.withExternalName(
6564
PatroniUtil.readWriteName(DistributedLogsCluster.getCluster(
66-
labelFactory, distributedLogs, Optional.empty()))
65+
labelFactory, distributedLogs, context.getCluster()))
6766
+ "." + distributedLogs.getMetadata().getNamespace()
6867
+ StackGresUtil.domainSearchPath())
6968
.endSpec()

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public Stream<HasMetadata> generateResource(StackGresDistributedLogsContext cont
9393
return Stream.of(cluster);
9494
}
9595

96-
private StackGresCluster getCluster(
96+
public static StackGresCluster getCluster(
9797
final LabelFactoryForDistributedLogs labelFactory,
9898
final StackGresDistributedLogs distributedLogs,
9999
final Optional<StackGresCluster> previousCluster) {
@@ -161,6 +161,8 @@ private StackGresCluster getCluster(
161161
.toList())
162162
.endPostgres()
163163
.editConfigurations()
164+
.withSgPostgresConfig(
165+
DistributedLogsPostgresConfig.configName(distributedLogs))
164166
.withNewCredentials()
165167
.withNewUsers()
166168
.withNewSuperuser()
@@ -183,8 +185,6 @@ private StackGresCluster getCluster(
183185
.endAuthenticator()
184186
.endUsers()
185187
.endCredentials()
186-
.withSgPostgresConfig(
187-
DistributedLogsPostgresConfig.configName(distributedLogs))
188188
.endConfigurations()
189189
.withPostgresServices(
190190
Optional.of(distributedLogs.getSpec())
@@ -380,8 +380,4 @@ private StackGresCluster getCluster(
380380
return cluster;
381381
}
382382

383-
public static void main(String[] args) {
384-
System.out.println(StackGresVolume.CUSTOM.getName(
385-
StackGresVolume.FLUENTD_CONFIG.getName()));
386-
}
387383
}

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/initialization/DefaultClusterPostgresConfigFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.stackgres.operator.initialization;
77

8+
import static io.stackgres.common.StackGresUtil.getPostgresFlavorComponent;
9+
810
import java.util.Map;
911
import java.util.Properties;
1012
import java.util.Set;
@@ -62,7 +64,8 @@ public String getDefaultResourceName(StackGresCluster resource) {
6264
}
6365

6466
private String getPostgresMajorVersion(StackGresCluster resource) {
65-
String version = resource.getSpec().getPostgres().getVersion();
67+
String version = getPostgresFlavorComponent(resource).get(resource)
68+
.getVersion(resource.getSpec().getPostgres().getVersion());
6669
return version.split("\\.")[0];
6770
}
6871

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/initialization/DefaultShardedClusterPostgresConfigFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.stackgres.operator.initialization;
77

8+
import static io.stackgres.common.StackGresUtil.getPostgresFlavorComponent;
9+
810
import java.util.Map;
911
import java.util.Properties;
1012
import java.util.Set;
@@ -62,7 +64,8 @@ public String getDefaultResourceName(StackGresShardedCluster resource) {
6264
}
6365

6466
private String getPostgresMajorVersion(StackGresShardedCluster resource) {
65-
String version = resource.getSpec().getPostgres().getVersion();
67+
String version = getPostgresFlavorComponent(resource).get(resource)
68+
.getVersion(resource.getSpec().getPostgres().getVersion());
6669
return version.split("\\.")[0];
6770
}
6871

0 commit comments

Comments
 (0)