Search in sources :

Example 26 with StackGresClusterSpec

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());
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterPostgres(io.stackgres.common.crd.sgcluster.StackGresClusterPostgres) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 27 with StackGresClusterSpec

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));
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 28 with StackGresClusterSpec

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));
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ValidationFailed(io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 29 with StackGresClusterSpec

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();
}
Also used : StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ImmutableList(com.google.common.collect.ImmutableList) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) JsonPointer(com.github.fge.jackson.jsonpointer.JsonPointer) AddOperation(com.github.fge.jsonpatch.AddOperation) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation)

Example 30 with StackGresClusterSpec

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();
}
Also used : StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) StackGresClusterReview(io.stackgres.operator.common.StackGresClusterReview) Unchecked(org.jooq.lambda.Unchecked) Logger(org.slf4j.Logger) AddOperation(com.github.fge.jsonpatch.AddOperation) Operation(io.stackgres.operatorframework.admissionwebhook.Operation) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) JsonPointer(com.github.fge.jackson.jsonpointer.JsonPointer) ReplaceOperation(com.github.fge.jsonpatch.ReplaceOperation) Seq(org.jooq.lambda.Seq) TextNode(com.fasterxml.jackson.databind.node.TextNode) Inject(javax.inject.Inject) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) List(java.util.List) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation) StackGresClusterPostgres(io.stackgres.common.crd.sgcluster.StackGresClusterPostgres) StackGresExtensionMetadata(io.stackgres.common.extension.StackGresExtensionMetadata) ClusterExtensionMetadataManager(io.stackgres.operator.mutation.ClusterExtensionMetadataManager) ImmutableList(com.google.common.collect.ImmutableList) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) ExtensionUtil(io.stackgres.common.extension.ExtensionUtil) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) AdmissionRequest(io.stackgres.operatorframework.admissionwebhook.AdmissionRequest) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ImmutableList(com.google.common.collect.ImmutableList) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) TextNode(com.fasterxml.jackson.databind.node.TextNode) JsonPointer(com.github.fge.jackson.jsonpointer.JsonPointer) StackGresExtensionMetadata(io.stackgres.common.extension.StackGresExtensionMetadata) AddOperation(com.github.fge.jsonpatch.AddOperation) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ReplaceOperation(com.github.fge.jsonpatch.ReplaceOperation)

Aggregations

StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)46 Test (org.junit.jupiter.api.Test)26 StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)23 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)18 QuarkusTest (io.quarkus.test.junit.QuarkusTest)16 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)15 List (java.util.List)11 Optional (java.util.Optional)11 Inject (javax.inject.Inject)10 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 StackGresClusterPod (io.stackgres.common.crd.sgcluster.StackGresClusterPod)8 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)7 Collectors (java.util.stream.Collectors)7 StackGresClusterInitData (io.stackgres.common.crd.sgcluster.StackGresClusterInitData)6 StackGresClusterPostgres (io.stackgres.common.crd.sgcluster.StackGresClusterPostgres)6 StackGresComponent (io.stackgres.common.StackGresComponent)5 StackGresBackup (io.stackgres.common.crd.sgbackup.StackGresBackup)5 StackGresBackupConfig (io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig)5 ValidationFailed (io.stackgres.operatorframework.admissionwebhook.validating.ValidationFailed)5