use of io.stackgres.common.crd.sgprofile.StackGresProfile in project stackgres by ongres.
the class ProfileReferenceValidator method checkIfProfileExists.
private void checkIfProfileExists(StackGresClusterReview review, String onError) throws ValidationFailed {
StackGresCluster cluster = review.getRequest().getObject();
String resourceProfile = cluster.getSpec().getResourceProfile();
String namespace = cluster.getMetadata().getNamespace();
Optional<StackGresProfile> profileOpt = profileFinder.findByNameAndNamespace(resourceProfile, namespace);
if (!profileOpt.isPresent()) {
fail(onError);
}
}
use of io.stackgres.common.crd.sgprofile.StackGresProfile 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.sgprofile.StackGresProfile in project stackgres by ongres.
the class ProfileReferenceValidatorTest method giveAnAttemptToUpdateToAnKnownProfile_shouldNotFail.
@Test
void giveAnAttemptToUpdateToAnKnownProfile_shouldNotFail() throws ValidationFailed {
final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/profile_config_update.json", StackGresClusterReview.class);
String resourceProfile = review.getRequest().getObject().getSpec().getResourceProfile();
String namespace = review.getRequest().getObject().getMetadata().getNamespace();
StackGresProfile profileSizeS = JsonUtil.readFromJson("stackgres_profiles/size-s.json", StackGresProfile.class);
when(profileFinder.findByNameAndNamespace(resourceProfile, namespace)).thenReturn(Optional.of(profileSizeS));
validator.validate(review);
verify(profileFinder).findByNameAndNamespace(anyString(), anyString());
}
use of io.stackgres.common.crd.sgprofile.StackGresProfile in project stackgres by ongres.
the class DefaultProfileFactory method buildResource.
@Override
StackGresProfile buildResource(String namespace) {
StackGresProfile profile = new StackGresProfile();
profile.getMetadata().setName(generateDefaultName());
profile.getMetadata().setNamespace(namespace);
StackGresProfileSpec spec = buildSpec(StackGresProfileSpec.class);
profile.setSpec(spec);
return profile;
}
use of io.stackgres.common.crd.sgprofile.StackGresProfile in project stackgres by ongres.
the class ProfileTransformer method toCustomResource.
@Override
public StackGresProfile toCustomResource(ProfileDto source, StackGresProfile original) {
StackGresProfile transformation = Optional.ofNullable(original).orElseGet(StackGresProfile::new);
transformation.setMetadata(getCustomResourceMetadata(source, original));
transformation.setSpec(getCustomResourceSpec(source.getSpec()));
return transformation;
}
Aggregations