use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec 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.StackGresClusterSpec 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.StackGresClusterSpec in project stackgres by ongres.
the class ContextUtil method toPostgresContext.
public static PostgresContainerContext toPostgresContext(StackGresClusterContainerContext context) {
final StackGresClusterContext clusterContext = context.getClusterContext();
final StackGresCluster cluster = clusterContext.getSource();
ImmutablePostgresContainerContext.Builder contextBuilder = Optional.of(clusterContext.getSource()).map(StackGresCluster::getStatus).map(StackGresClusterStatus::getDbOps).map(StackGresClusterDbOpsStatus::getMajorVersionUpgrade).map(majorVersionUpgradeStatus -> {
String targetVersion = majorVersionUpgradeStatus.getTargetPostgresVersion();
String sourceVersion = majorVersionUpgradeStatus.getSourcePostgresVersion();
String sourceMajorVersion = StackGresComponent.POSTGRESQL.findMajorVersion(sourceVersion);
return ImmutablePostgresContainerContext.builder().from(context).postgresMajorVersion(StackGresComponent.POSTGRESQL.findMajorVersion(targetVersion)).oldMajorVersion(sourceMajorVersion).imageBuildMajorVersion(StackGresComponent.POSTGRESQL.findBuildMajorVersion(targetVersion)).oldImageBuildMajorVersion(StackGresComponent.POSTGRESQL.findBuildMajorVersion(sourceVersion)).postgresVersion(targetVersion).oldPostgresVersion(sourceVersion);
}).orElseGet(() -> {
final String postgresVersion = cluster.getSpec().getPostgres().getVersion();
final String majorVersion = StackGresComponent.POSTGRESQL.findMajorVersion(postgresVersion);
final String buildMajorVersion = StackGresComponent.POSTGRESQL.findBuildMajorVersion(postgresVersion);
return ImmutablePostgresContainerContext.builder().from(context).postgresVersion(postgresVersion).imageBuildMajorVersion(buildMajorVersion).postgresMajorVersion(majorVersion);
});
final List<StackGresClusterInstalledExtension> installedExtensions = Optional.ofNullable(cluster.getSpec()).map(StackGresClusterSpec::getToInstallPostgresExtensions).stream().flatMap(Collection::stream).collect(Collectors.toUnmodifiableList());
contextBuilder.addAllInstalledExtensions(installedExtensions);
return contextBuilder.build();
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class BackupRequiredResourcesGenerator method getRequiredResources.
@Override
public List<HasMetadata> getRequiredResources(StackGresBackup config) {
final ObjectMeta metadata = config.getMetadata();
final String dbOpsName = metadata.getName();
final String dbOpsNamespace = metadata.getNamespace();
final StackGresBackupSpec spec = config.getSpec();
final StackGresCluster cluster = clusterFinder.findByNameAndNamespace(spec.getSgCluster(), dbOpsNamespace).orElseThrow(() -> new IllegalArgumentException("SGBackup " + dbOpsNamespace + "/" + dbOpsName + " target a non existent SGCluster " + spec.getSgCluster()));
final StackGresBackupConfig backupConfig = Optional.of(cluster.getSpec()).map(StackGresClusterSpec::getConfiguration).map(StackGresClusterConfiguration::getBackupConfig).map(backupConfigName -> backupConfigFinder.findByNameAndNamespace(backupConfigName, dbOpsNamespace).orElseThrow(() -> new IllegalArgumentException("SGBackup " + dbOpsNamespace + "/" + dbOpsName + " target SGCluster " + spec.getSgCluster() + " with a non existent SGBackupConfig " + backupConfigName))).orElseThrow(() -> new IllegalArgumentException("SGBackup " + dbOpsNamespace + "/" + dbOpsName + " target SGCluster " + spec.getSgCluster() + " without a SGBackupConfig"));
StackGresBackupContext context = ImmutableStackGresBackupContext.builder().source(config).cluster(cluster).backupConfig(backupConfig).build();
final List<ResourceGenerator<StackGresBackupContext>> resourceGenerators = generators.getResourceGenerators(context);
final List<HasMetadata> resources = resourceGenerators.stream().flatMap(generator -> generator.generateResource(context)).collect(Collectors.toUnmodifiableList());
List<Decorator<StackGresBackupContext>> decorators = decoratorDiscoverer.discoverDecorator(context);
decorators.forEach(decorator -> decorator.decorate(context, resources));
return resources;
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class ExtensionUtilTest method testExtensionsMetadataMapSameMajorBuild.
@Test
public void testExtensionsMetadataMapSameMajorBuild() {
StackGresCluster cluster = new StackGresCluster();
cluster.setSpec(new StackGresClusterSpec());
cluster.getSpec().setPostgres(new StackGresClusterPostgres());
var extensionMetadataMap = ExtensionUtil.toExtensionsMetadataIndexSameMajorBuilds(repository, extensionsMetadata);
cluster.getSpec().getPostgres().setVersion(firstPgMajorVersion);
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(0))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(0))).size());
Assertions.assertNotEquals(repository, extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(0))).get(0).getExtension().getRepository());
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(1))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(1))).size());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(1))).get(0).getExtension().getRepository());
Assertions.assertNull(extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(2))));
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(3))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(3))).size());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(3))).get(0).getExtension().getRepository());
cluster.getSpec().getPostgres().setVersion(secondPgMajorVersion);
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(0))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(0))).size());
Assertions.assertNotEquals(repository, extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(0))).get(0).getExtension().getRepository());
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(1))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(1))).size());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(1))).get(0).getExtension().getRepository());
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(2))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(2))).size());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(2))).get(0).getExtension().getRepository());
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(3))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(3))).size());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexSameMajorBuild(cluster, extensions.get(3))).get(0).getExtension().getRepository());
}
Aggregations