use of io.stackgres.common.crd.sgcluster.StackGresClusterInitData in project stackgres by ongres.
the class ClusterTransformer method getResourceSpec.
private ClusterSpec getResourceSpec(StackGresClusterSpec source) {
if (source == null) {
return null;
}
ClusterSpec transformation = new ClusterSpec();
transformation.setPostgres(new ClusterPostgres());
transformation.getPostgres().setVersion(source.getPostgres().getVersion());
transformation.getPostgres().setExtensions(Optional.ofNullable(source.getPostgres().getExtensions()).stream().flatMap(List::stream).map(this::getResourceExtension).collect(ImmutableList.toImmutableList()));
final StackGresClusterSsl sourceClusterSsl = source.getPostgres().getSsl();
if (sourceClusterSsl != null) {
transformation.getPostgres().setSsl(new ClusterSsl());
transformation.getPostgres().getSsl().setEnabled(sourceClusterSsl.getEnabled());
transformation.getPostgres().getSsl().setCertificateSecretKeySelector(sourceClusterSsl.getCertificateSecretKeySelector());
transformation.getPostgres().getSsl().setPrivateKeySecretKeySelector(sourceClusterSsl.getPrivateKeySecretKeySelector());
}
transformation.setConfigurations(new ClusterConfiguration());
transformation.getConfigurations().setSgBackupConfig(source.getConfiguration().getBackupConfig());
transformation.getConfigurations().setSgPoolingConfig(source.getConfiguration().getConnectionPoolingConfig());
transformation.setInstances(source.getInstances());
transformation.setNonProduction(getResourceNonProduction(source.getNonProduction()));
transformation.getConfigurations().setSgPostgresConfig(source.getConfiguration().getPostgresConfig());
transformation.setPrometheusAutobind(source.getPrometheusAutobind());
transformation.setSgInstanceProfile(source.getResourceProfile());
final StackGresClusterInitData sourceInitData = source.getInitData();
if (sourceInitData != null) {
ClusterInitData targetInitData = new ClusterInitData();
transformation.setInitData(targetInitData);
final StackGresClusterRestore sourceRestore = sourceInitData.getRestore();
if (sourceRestore != null) {
targetInitData.setRestore(getResourceRestore(sourceRestore));
}
if (sourceInitData.getScripts() != null) {
targetInitData.setScripts(sourceInitData.getScripts().stream().map(sourceEntry -> {
ClusterScriptEntry targetEntry = new ClusterScriptEntry();
targetEntry.setScript(sourceEntry.getScript());
targetEntry.setDatabase(sourceEntry.getDatabase());
targetEntry.setName(sourceEntry.getName());
if (sourceEntry.getScriptFrom() != null) {
targetEntry.setScriptFrom(new ClusterScriptFrom());
targetEntry.getScriptFrom().setSecretKeyRef(sourceEntry.getScriptFrom().getSecretKeyRef());
targetEntry.getScriptFrom().setConfigMapKeyRef(sourceEntry.getScriptFrom().getConfigMapKeyRef());
}
return targetEntry;
}).collect(ImmutableList.toImmutableList()));
}
}
final ClusterPod targetPod = new ClusterPod();
final StackGresClusterPod sourcePod = source.getPod();
transformation.setPods(targetPod);
targetPod.setPersistentVolume(new ClusterPodPersistentVolume());
targetPod.getPersistentVolume().setStorageClass(sourcePod.getPersistentVolume().getStorageClass());
targetPod.getPersistentVolume().setSize(sourcePod.getPersistentVolume().getSize());
targetPod.setDisableConnectionPooling(sourcePod.getDisableConnectionPooling());
targetPod.setDisableMetricsExporter(sourcePod.getDisableMetricsExporter());
targetPod.setDisablePostgresUtil(sourcePod.getDisablePostgresUtil());
final StackGresClusterSpecMetadata specMetadata = source.getMetadata();
if (specMetadata != null) {
transformation.setMetadata(new ClusterSpecMetadata());
final StackGresClusterSpecAnnotations sourceAnnotations = specMetadata.getAnnotations();
if (specMetadata.getAnnotations() != null) {
ClusterSpecAnnotations targetAnnotations = new ClusterSpecAnnotations();
targetAnnotations.setAllResources(sourceAnnotations.getAllResources());
targetAnnotations.setClusterPods(sourceAnnotations.getClusterPods());
targetAnnotations.setServices(sourceAnnotations.getServices());
targetAnnotations.setPrimaryService(sourceAnnotations.getPrimaryService());
targetAnnotations.setReplicasService(sourceAnnotations.getReplicasService());
transformation.getMetadata().setAnnotations(targetAnnotations);
}
final StackGresClusterSpecLabels sourceLabels = specMetadata.getLabels();
if (sourceLabels != null) {
ClusterSpecLabels targetLabels = new ClusterSpecLabels();
targetLabels.setClusterPods(sourceLabels.getClusterPods());
transformation.getMetadata().setLabels(targetLabels);
}
}
final StackGresClusterPostgresServices sourcePostgresServices = source.getPostgresServices();
if (sourcePostgresServices != null) {
transformation.setPostgresServices(new ClusterPostgresServices());
final ClusterPostgresServices targetPostgresService = transformation.getPostgresServices();
final StackGresPostgresService sourcePrimaryService = sourcePostgresServices.getPrimary();
if (sourcePrimaryService != null) {
targetPostgresService.setPrimary(new PostgresService());
targetPostgresService.getPrimary().setType(sourcePrimaryService.getType());
targetPostgresService.getPrimary().setEnabled(sourcePrimaryService.getEnabled());
}
final StackGresPostgresService sourceReplicaService = sourcePostgresServices.getReplicas();
if (sourceReplicaService != null) {
targetPostgresService.setReplicas(new PostgresService());
targetPostgresService.getReplicas().setEnabled(sourceReplicaService.getEnabled());
targetPostgresService.getReplicas().setType(sourceReplicaService.getType());
}
}
targetPod.setScheduling(Optional.ofNullable(sourcePod.getScheduling()).map(sourcePodScheduling -> {
return new ClusterPodSchedulingConverter().from(sourcePodScheduling);
}).orElse(null));
transformation.setDistributedLogs(getResourceDistributedLogs(source.getDistributedLogs()));
if (source.getToInstallPostgresExtensions() != null) {
transformation.setToInstallPostgresExtensions(source.getToInstallPostgresExtensions().stream().map(this::getClusterInstalledExtension).collect(ImmutableList.toImmutableList()));
}
return transformation;
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterInitData in project stackgres by ongres.
the class ClusterRequiredResourcesGenerator method getRequiredResources.
@Override
public List<HasMetadata> getRequiredResources(StackGresCluster config) {
final ObjectMeta metadata = config.getMetadata();
final String clusterName = metadata.getName();
final String clusterNamespace = metadata.getNamespace();
final StackGresClusterSpec spec = config.getSpec();
final StackGresClusterConfiguration clusterConfiguration = spec.getConfiguration();
final StackGresPostgresConfig clusterPgConfig = postgresConfigFinder.findByNameAndNamespace(clusterConfiguration.getPostgresConfig(), clusterNamespace).orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have a non existent SGPostgresConfig postgresconf"));
final StackGresProfile clusterProfile = profileFinder.findByNameAndNamespace(spec.getResourceProfile(), clusterNamespace).orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have a non existent " + StackGresProfile.KIND + " " + spec.getResourceProfile()));
final Optional<StackGresBackupConfig> backupConfig = Optional.ofNullable(clusterConfiguration.getBackupConfig()).flatMap(backupConfigName -> backupConfigFinder.findByNameAndNamespace(backupConfigName, clusterNamespace));
final Optional<StackGresPoolingConfig> clusterPooling = Optional.ofNullable(clusterConfiguration.getConnectionPoolingConfig()).flatMap(poolingConfigName -> poolingConfigFinder.findByNameAndNamespace(poolingConfigName, clusterNamespace));
Optional<StackGresClusterRestore> restoreConfig = Optional.ofNullable(config.getSpec().getInitData()).map(StackGresClusterInitData::getRestore);
final Optional<StackGresBackup> restoreBackup;
if (restoreConfig.isEmpty()) {
restoreBackup = Optional.empty();
} else {
restoreBackup = restoreConfig.map(restore -> {
final List<StackGresBackup> backups = backupScanner.getResources();
return backups.stream().filter(backup -> backup.getMetadata().getUid().equals(restore.getFromBackup().getUid())).peek(backup -> {
Preconditions.checkNotNull(backup.getStatus(), "Backup is " + BackupPhase.RUNNING.label());
Preconditions.checkNotNull(backup.getStatus().getProcess(), "Backup is " + BackupPhase.RUNNING.label());
Preconditions.checkArgument(backup.getStatus().getProcess().getStatus().equals(BackupPhase.COMPLETED.label()), "Backup is " + backup.getStatus().getProcess().getStatus());
}).findFirst().orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have an invalid restore backup Uid"));
});
}
StackGresClusterContext context = ImmutableStackGresClusterContext.builder().source(config).postgresConfig(clusterPgConfig).stackGresProfile(clusterProfile).backupConfig(backupConfig).poolingConfig(clusterPooling).restoreBackup(restoreBackup).prometheus(getPrometheus(config)).internalScripts(List.of(getPostgresExporterInitScript())).databaseCredentials(secretFinder.findByNameAndNamespace(clusterName, clusterNamespace)).build();
final List<ResourceGenerator<StackGresClusterContext>> resourceGenerators = generators.getResourceGenerators(context);
final List<HasMetadata> resources = resourceGenerators.stream().flatMap(generator -> generator.generateResource(context)).collect(Collectors.toUnmodifiableList());
List<Decorator<StackGresClusterContext>> decorators = decoratorDiscoverer.discoverDecorator(context);
decorators.forEach(decorator -> decorator.decorate(context, resources));
return resources;
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterInitData in project stackgres by ongres.
the class RestoreConfigMap method buildSource.
@NotNull
public Optional<HasMetadata> buildSource(StackGresClusterContext context) {
return context.getRestoreBackup().map(restoreBackup -> {
final Map<String, String> data = new HashMap<>();
data.put("BACKUP_RESOURCE_VERSION", restoreBackup.getMetadata().getResourceVersion());
data.put("RESTORE_BACKUP_ID", restoreBackup.getStatus().getInternalName());
data.putAll(getBackupEnvVars(context, restoreBackup.getMetadata().getNamespace(), restoreBackup.getSpec().getSgCluster(), restoreBackup.getStatus().getBackupConfig()));
final StackGresCluster cluster = context.getSource();
Optional.ofNullable(cluster.getSpec()).map(StackGresClusterSpec::getInitData).map(StackGresClusterInitData::getRestore).map(StackGresClusterRestore::getDownloadDiskConcurrency).ifPresent(downloadDiskConcurrency -> data.put("WALG_DOWNLOAD_CONCURRENCY", convertEnvValue(downloadDiskConcurrency)));
return new ConfigMapBuilder().withNewMetadata().withNamespace(cluster.getMetadata().getNamespace()).withName(name(context)).withLabels(labelFactory.patroniClusterLabels(cluster)).endMetadata().withData(StackGresUtil.addMd5Sum(data)).build();
});
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterInitData in project stackgres by ongres.
the class ClusterConstraintValidatorTest method validScriptFromConfigMapAndSecret_shouldFail.
@Test
void validScriptFromConfigMapAndSecret_shouldFail() {
StackGresClusterReview review = getValidReview();
review.getRequest().getObject().getSpec().setInitData(new StackGresClusterInitData());
review.getRequest().getObject().getSpec().getInitData().setScripts(new ArrayList<>());
review.getRequest().getObject().getSpec().getInitData().getScripts().add(new StackGresClusterScriptEntry());
review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).setScriptFrom(new StackGresClusterScriptFrom());
review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().setConfigMapKeyRef(new ConfigMapKeySelector());
review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getConfigMapKeyRef().setName("test");
review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getConfigMapKeyRef().setKey("test");
review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().setSecretKeyRef(new SecretKeySelector());
review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getSecretKeyRef().setName("test");
review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getSecretKeyRef().setKey("test");
checkErrorCause(StackGresClusterScriptFrom.class, new String[] { "spec.initData.scripts[0].scriptFrom.secretKeyRef", "spec.initData.scripts[0].scriptFrom.configMapKeyRef" }, "isSecretKeySelectorAndConfigMapKeySelectorMutuallyExclusiveAndRequired", review, AssertTrue.class);
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterInitData in project stackgres by ongres.
the class ClusterConstraintValidatorTest method scriptFromSecretWithEmptyKey_shouldFail.
@Test
void scriptFromSecretWithEmptyKey_shouldFail() {
StackGresClusterReview review = getValidReview();
review.getRequest().getObject().getSpec().setInitData(new StackGresClusterInitData());
review.getRequest().getObject().getSpec().getInitData().setScripts(new ArrayList<>());
review.getRequest().getObject().getSpec().getInitData().getScripts().add(new StackGresClusterScriptEntry());
review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).setScriptFrom(new StackGresClusterScriptFrom());
review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().setSecretKeyRef(new SecretKeySelector());
review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getSecretKeyRef().setName("test");
review.getRequest().getObject().getSpec().getInitData().getScripts().get(0).getScriptFrom().getSecretKeyRef().setKey("");
checkErrorCause(SecretKeySelector.class, "spec.initData.scripts[0].scriptFrom.secretKeyRef.key", "isKeyNotEmpty", review, AssertTrue.class);
}
Aggregations