use of io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration 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.StackGresClusterConfiguration 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.StackGresClusterConfiguration in project stackgres by ongres.
the class ClusterResourceMockedTest method checkCustomResource.
@Override
protected void checkCustomResource(StackGresCluster resource, ClusterDto resourceDto, Operation operation) {
final Metadata dtoMetadata = resourceDto.getMetadata();
final ObjectMeta resourceMetadata = resource.getMetadata();
if (dtoMetadata != null) {
assertNotNull(resourceMetadata);
assertEquals(dtoMetadata.getName(), resourceMetadata.getName());
assertEquals(dtoMetadata.getNamespace(), resourceMetadata.getNamespace());
assertEquals(dtoMetadata.getUid(), resourceMetadata.getUid());
} else {
assertNull(resourceMetadata);
}
final ClusterSpec dtoSpec = resourceDto.getSpec();
final StackGresClusterSpec resourceSpec = resource.getSpec();
if (dtoSpec != null) {
assertNotNull(resourceSpec);
assertEquals(dtoSpec.getPrometheusAutobind(), resourceSpec.getPrometheusAutobind());
assertEquals(dtoSpec.getInstances(), resourceSpec.getInstances());
assertEquals(dtoSpec.getPostgres().getVersion(), resourceSpec.getPostgres().getVersion());
assertEquals(dtoSpec.getSgInstanceProfile(), resourceSpec.getResourceProfile());
final ClusterConfiguration dtoSpecConfigurations = dtoSpec.getConfigurations();
final StackGresClusterConfiguration resourceSpecConfiguration = resourceSpec.getConfiguration();
if (dtoSpecConfigurations != null) {
assertNotNull(resourceSpecConfiguration);
assertEquals(dtoSpecConfigurations.getSgBackupConfig(), resourceSpecConfiguration.getBackupConfig());
assertEquals(dtoSpecConfigurations.getSgPoolingConfig(), resourceSpecConfiguration.getConnectionPoolingConfig());
assertEquals(dtoSpecConfigurations.getSgPostgresConfig(), resourceSpecConfiguration.getPostgresConfig());
} else {
assertNull(resourceSpecConfiguration);
}
final ClusterPod dtoSpecPods = dtoSpec.getPods();
if (dtoSpecPods != null) {
final StackGresClusterPod resourceSpecPod = resourceSpec.getPod();
assertNotNull(resourceSpecPod);
assertEquals(dtoSpecPods.getDisableConnectionPooling(), resourceSpecPod.getDisableConnectionPooling());
assertEquals(dtoSpecPods.getDisableMetricsExporter(), resourceSpecPod.getDisableMetricsExporter());
assertEquals(dtoSpecPods.getDisableMetricsExporter(), resourceSpecPod.getDisableMetricsExporter());
final ClusterPodPersistentVolume dtoPV = dtoSpecPods.getPersistentVolume();
final StackGresPodPersistentVolume resourcePV = resourceSpecPod.getPersistentVolume();
if (dtoPV != null) {
assertNotNull(resourcePV);
assertEquals(dtoPV.getSize(), resourcePV.getSize());
assertEquals(dtoPV.getStorageClass(), resourcePV.getStorageClass());
} else {
assertNull(resourcePV);
}
final StackGresClusterSpecLabels resourceMetadataLabels = Optional.ofNullable(resourceSpec.getMetadata()).map(StackGresClusterSpecMetadata::getLabels).orElse(null);
final ClusterSpecLabels dtoMetadataLabels = Optional.ofNullable(dtoSpec.getMetadata()).map(ClusterSpecMetadata::getLabels).orElse(null);
if (dtoMetadataLabels != null) {
assertNotNull(resourceMetadataLabels);
assertEquals(dtoMetadataLabels.getClusterPods(), resourceMetadataLabels.getClusterPods());
} else {
assertNull(resourceMetadataLabels);
}
final ClusterPodScheduling podScheduling = dtoSpecPods.getScheduling();
final StackGresClusterPodScheduling resourceScheduling = resourceSpecPod.getScheduling();
if (podScheduling != null) {
assertNotNull(resourceScheduling);
assertEquals(podScheduling.getNodeSelector(), resourceScheduling.getNodeSelector());
assertEquals(podScheduling.getNodeAffinity(), resourceScheduling.getNodeAffinity());
} else {
assertNull(resourceScheduling);
}
}
final ClusterInitData dtoInitData = dtoSpec.getInitData();
final StackGresClusterInitData resourceInitData = resourceSpec.getInitData();
if (dtoInitData != null) {
assertNotNull(resourceInitData);
final ClusterRestore dtoRestore = dtoInitData.getRestore();
final StackGresClusterRestore resourceRestore = resourceInitData.getRestore();
if (dtoRestore != null) {
assertNotNull(resourceRestore);
assertEquals(dtoRestore.getFromBackup().getUid(), resourceRestore.getFromBackup().getUid());
} else {
assertNull(resourceRestore);
}
if (dtoInitData.getScripts() != null) {
assertNotNull(resourceInitData.getScripts());
assertEquals(dtoInitData.getScripts().size(), resourceInitData.getScripts().size());
Seq.zip(dtoInitData.getScripts(), resourceInitData.getScripts()).forEach(entryTuple -> {
ClusterScriptEntry dtoEntry = entryTuple.v1;
StackGresClusterScriptEntry resourceEntry = entryTuple.v2;
assertEquals(dtoEntry.getDatabase(), resourceEntry.getDatabase());
assertEquals(dtoEntry.getName(), resourceEntry.getName());
assertEquals(dtoEntry.getScript(), resourceEntry.getScript());
final ClusterScriptFrom dtoScriptFrom = dtoEntry.getScriptFrom();
final StackGresClusterScriptFrom resourceScriptFrom = resourceEntry.getScriptFrom();
if (dtoScriptFrom != null) {
assertNotNull(resourceScriptFrom);
final SecretKeySelector dtoSecretKeyRef = dtoScriptFrom.getSecretKeyRef();
final SecretKeySelector resourceSecretKeyRef = resourceScriptFrom.getSecretKeyRef();
if (dtoSecretKeyRef != null) {
assertNotNull(resourceSecretKeyRef);
assertEquals(dtoSecretKeyRef.getName(), resourceSecretKeyRef.getName());
assertEquals(dtoSecretKeyRef.getKey(), resourceSecretKeyRef.getKey());
} else {
assertNull(resourceSecretKeyRef);
}
final ConfigMapKeySelector resourceConfigMapKeyRef = resourceScriptFrom.getConfigMapKeyRef();
final ConfigMapKeySelector dtoConfigMapKeyRef = dtoScriptFrom.getConfigMapKeyRef();
if (dtoConfigMapKeyRef != null) {
assertNotNull(resourceConfigMapKeyRef);
assertEquals(dtoConfigMapKeyRef.getName(), resourceConfigMapKeyRef.getName());
assertEquals(dtoConfigMapKeyRef.getKey(), resourceConfigMapKeyRef.getKey());
} else {
assertNull(resourceConfigMapKeyRef);
}
} else {
assertNull(resourceScriptFrom);
}
});
}
} else {
assertNull(resourceInitData);
}
if (dtoSpec.getDistributedLogs() != null) {
assertNotNull(resourceSpec.getDistributedLogs());
assertEquals(dtoSpec.getDistributedLogs().getDistributedLogs(), resourceSpec.getDistributedLogs().getDistributedLogs());
} else {
assertNull(resourceSpec.getDistributedLogs());
}
} else {
assertNull(resourceSpec);
}
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration in project stackgres by ongres.
the class ClusterResourceQuarkusTest method setUp.
@BeforeEach
void setUp() {
try (KubernetesClient client = factory.create()) {
client.secrets().inNamespace("test").delete();
client.configMaps().inNamespace("test").delete();
cluster.getMetadata().setNamespace("test");
cluster.getMetadata().setName(StringUtils.getRandomClusterName());
cluster.getSpec().setConfiguration(new StackGresClusterConfiguration());
client.customResources(StackGresCluster.class, StackGresClusterList.class).inNamespace(cluster.getMetadata().getNamespace()).withName(cluster.getMetadata().getName()).create(cluster);
Service primary = new ServiceBuilder().withNewMetadata().withName(PatroniUtil.readWriteName(cluster.getMetadata().getName())).withNamespace(cluster.getMetadata().getNamespace()).endMetadata().withNewSpec().withType(cluster.getSpec().getPostgresServices().getPrimary().getType() != null ? cluster.getSpec().getPostgresServices().getPrimary().getType() : null).withClusterIP("10.10.100.8").endSpec().build();
Service replicas = new ServiceBuilder().withNewMetadata().withName(PatroniUtil.readOnlyName(cluster.getMetadata().getName())).withNamespace(cluster.getMetadata().getNamespace()).endMetadata().withNewSpec().withType(cluster.getSpec().getPostgresServices().getReplicas().getType() != null ? cluster.getSpec().getPostgresServices().getReplicas().getType() : null).withClusterIP("10.10.100.30").endSpec().build();
client.services().inNamespace(cluster.getMetadata().getNamespace()).create(primary);
client.services().inNamespace(cluster.getMetadata().getNamespace()).create(replicas);
}
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration in project stackgres by ongres.
the class ClusterRequiredResourcesGeneratorTest method givenClusterWithNoPoolingConfig_shouldNotFail.
@Test
void givenClusterWithNoPoolingConfig_shouldNotFail() {
final ObjectMeta metadata = cluster.getMetadata();
final String clusterNamespace = metadata.getNamespace();
final StackGresClusterSpec clusterSpec = cluster.getSpec();
final StackGresClusterConfiguration clusterConfiguration = clusterSpec.getConfiguration();
final String backupConfigName = clusterConfiguration.getBackupConfig();
mockBackupConfig(clusterNamespace, backupConfigName);
final String postgresConfigName = clusterConfiguration.getPostgresConfig();
mockPgConfig(clusterNamespace, postgresConfigName);
final String connectionPoolingConfig = clusterConfiguration.getConnectionPoolingConfig();
when(poolingConfigFinder.findByNameAndNamespace(connectionPoolingConfig, clusterNamespace)).thenReturn(Optional.empty());
final String resourceProfile = clusterSpec.getResourceProfile();
when(profileConfigFinder.findByNameAndNamespace(resourceProfile, clusterNamespace)).thenReturn(Optional.of(instanceProfile));
when(backupScanner.getResources()).thenReturn(backups);
mockSecrets(clusterNamespace);
generator.getRequiredResources(cluster);
verify(backupConfigFinder).findByNameAndNamespace(backupConfigName, clusterNamespace);
verify(postgresConfigFinder).findByNameAndNamespace(postgresConfigName, clusterNamespace);
verify(poolingConfigFinder).findByNameAndNamespace(connectionPoolingConfig, clusterNamespace);
verify(profileConfigFinder).findByNameAndNamespace(resourceProfile, clusterNamespace);
verify(backupScanner, atLeastOnce()).getResources();
}
Aggregations