use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class ExtensionUtilTest method testExtensionsMetadataMapAnyVersion.
@Test
public void testExtensionsMetadataMapAnyVersion() {
StackGresCluster cluster = new StackGresCluster();
cluster.setSpec(new StackGresClusterSpec());
cluster.getSpec().setPostgres(new StackGresClusterPostgres());
cluster.getSpec().setPostgres(new StackGresClusterPostgres());
Map<StackGresExtensionIndexAnyVersion, List<StackGresExtensionMetadata>> extensionMetadataMap = ExtensionUtil.toExtensionsMetadataIndexAnyVersions(repository, extensionsMetadata);
cluster.getSpec().getPostgres().setVersion(firstPgMajorVersion);
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(0))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(0))).size());
Assertions.assertNotEquals(repository, extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(0))).get(0).getExtension().getRepository());
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(1))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(1))).size());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(1))).get(0).getExtension().getRepository());
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(2))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(2))).size());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(2))).get(0).getExtension().getRepository());
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(3))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(3))).size());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(3))).get(0).getExtension().getRepository());
cluster.getSpec().getPostgres().setVersion(secondPgMajorVersion);
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(0))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(0))).size());
Assertions.assertNotEquals(repository, extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(0))).get(0).getExtension().getRepository());
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(1))));
Assertions.assertEquals(2, extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(1))).size());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(1))).get(0).getExtension().getRepository());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(1))).get(1).getExtension().getRepository());
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(2))));
Assertions.assertEquals(2, extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(2))).size());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(2))).get(0).getExtension().getRepository());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(2))).get(1).getExtension().getRepository());
Assertions.assertNotNull(extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(3))));
Assertions.assertEquals(1, extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(3))).size());
Assertions.assertEquals(repository.toASCIIString(), extensionMetadataMap.get(new StackGresExtensionIndexAnyVersion(cluster, extensions.get(3))).get(0).getExtension().getRepository());
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class PostgresVersionValidatorTest method givenInconsistentPostgresVersion_shouldFail.
@Test
void givenInconsistentPostgresVersion_shouldFail() {
final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/invalid_creation_pg_version.json", StackGresClusterReview.class);
review.getRequest().getObject().getSpec().getPostgres().setVersion(secondPgMajorVersion);
postgresConfig.getSpec().setPostgresVersion(firstPgMajorVersion);
StackGresClusterSpec spec = review.getRequest().getObject().getSpec();
String postgresProfile = spec.getConfiguration().getPostgresConfig();
String namespace = review.getRequest().getObject().getMetadata().getNamespace();
when(configFinder.findByNameAndNamespace(eq(postgresProfile), eq(namespace))).thenReturn(Optional.of(postgresConfig));
ValidationFailed exception = assertThrows(ValidationFailed.class, () -> {
validator.validate(review);
});
String resultMessage = exception.getResult().getMessage();
assertEquals("Invalid postgres version, must be " + firstPgMajorVersion + " to use sgPostgresConfig postgresconf", resultMessage);
verify(configFinder).findByNameAndNamespace(eq(postgresProfile), eq(namespace));
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class PostgresVersionValidatorTest method givenInvalidPostgresConfigReference_shouldFail.
@Test
void givenInvalidPostgresConfigReference_shouldFail() {
final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/valid_creation.json", StackGresClusterReview.class);
review.getRequest().getObject().getSpec().getPostgres().setVersion(firstPgMajorVersion);
StackGresClusterSpec spec = review.getRequest().getObject().getSpec();
String postgresProfile = spec.getConfiguration().getPostgresConfig();
String namespace = review.getRequest().getObject().getMetadata().getNamespace();
when(configFinder.findByNameAndNamespace(eq(postgresProfile), eq(namespace))).thenReturn(Optional.empty());
ValidationFailed exception = assertThrows(ValidationFailed.class, () -> {
validator.validate(review);
});
String resultMessage = exception.getResult().getMessage();
assertEquals("Invalid sgPostgresConfig value " + postgresProfile, resultMessage);
verify(configFinder).findByNameAndNamespace(eq(postgresProfile), eq(namespace));
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class DefaultProfileMutator method mutate.
@Override
public List<JsonPatchOperation> mutate(StackGresClusterReview review) {
if (review.getRequest().getOperation() == Operation.CREATE) {
ImmutableList.Builder<JsonPatchOperation> operations = ImmutableList.builder();
final StackGresClusterSpec spec = review.getRequest().getObject().getSpec();
StackGresClusterPod pod = spec.getPod();
final JsonPointer clusterPodPointer = CLUSTER_CONFIG_POINTER.append("pod");
if (pod == null) {
pod = new StackGresClusterPod();
spec.setPod(pod);
operations.add(new AddOperation(clusterPodPointer, FACTORY.objectNode()));
}
if (pod.getPersistentVolume() == null) {
operations.add(new AddOperation(clusterPodPointer.append("persistentVolume"), FACTORY.objectNode()));
}
operations.addAll(super.mutate(review));
return operations.build();
}
return ImmutableList.of();
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class ExtensionsChannelMutator method mutate.
@Override
public List<JsonPatchOperation> mutate(StackGresClusterReview review) {
if (review.getRequest().getOperation() == Operation.CREATE || review.getRequest().getOperation() == Operation.UPDATE) {
ImmutableList.Builder<JsonPatchOperation> operations = ImmutableList.builder();
Optional.of(review).map(StackGresClusterReview::getRequest).map(AdmissionRequest<StackGresCluster>::getObject).ifPresent(cluster -> {
Optional.of(cluster).map(StackGresCluster::getSpec).map(StackGresClusterSpec::getPostgres).map(StackGresClusterPostgres::getExtensions).stream().flatMap(extensions -> Seq.seq(extensions).zipWithIndex()).forEach(Unchecked.consumer(extension -> {
final JsonPointer extensionVersionPointer = CLUSTER_CONFIG_POINTER.append("postgres").append("extensions").append(extension.v2.intValue()).append("version");
final StackGresExtensionMetadata extensionMetadata;
try {
extensionMetadata = extensionMetadataManager.getExtensionCandidateSameMajorBuild(cluster, extension.v1);
} catch (Exception ex) {
LOGGER.warn("Unable to retrieve candidate with same major build for extension", ex);
return;
}
final StackGresClusterInstalledExtension installedExtension = ExtensionUtil.getInstalledExtension(extension.v1, extensionMetadata);
final TextNode extensionVersion = new TextNode(installedExtension.getVersion());
if (extension.v1.getVersion() == null) {
operations.add(new AddOperation(extensionVersionPointer, extensionVersion));
} else if (!installedExtension.getVersion().equals(extension.v1.getVersion())) {
operations.add(new ReplaceOperation(extensionVersionPointer, extensionVersion));
}
}));
});
return operations.build();
}
return ImmutableList.of();
}
Aggregations