Search in sources :

Example 1 with StackGresClusterSpecAnnotations

use of io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations 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;
}
Also used : StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) ClusterInitData(io.stackgres.apiweb.dto.cluster.ClusterInitData) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) ClusterScriptEntry(io.stackgres.apiweb.dto.cluster.ClusterScriptEntry) StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) ClusterPodSchedulingConverter(io.stackgres.apiweb.transformer.converter.cluster.ClusterPodSchedulingConverter) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices) ClusterPostgresServices(io.stackgres.apiweb.dto.cluster.ClusterPostgresServices) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ClusterSpec(io.stackgres.apiweb.dto.cluster.ClusterSpec) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) ClusterConfiguration(io.stackgres.apiweb.dto.cluster.ClusterConfiguration) ClusterSpecAnnotations(io.stackgres.apiweb.dto.cluster.ClusterSpecAnnotations) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) PostgresService(io.stackgres.apiweb.app.postgres.service.PostgresService) ClusterPod(io.stackgres.apiweb.dto.cluster.ClusterPod) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) StackGresClusterPostgres(io.stackgres.common.crd.sgcluster.StackGresClusterPostgres) ClusterPostgres(io.stackgres.apiweb.dto.cluster.ClusterPostgres) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata) ClusterSpecMetadata(io.stackgres.apiweb.dto.cluster.ClusterSpecMetadata) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresClusterSsl(io.stackgres.common.crd.sgcluster.StackGresClusterSsl) ClusterSsl(io.stackgres.apiweb.dto.cluster.ClusterSsl) StackGresClusterSsl(io.stackgres.common.crd.sgcluster.StackGresClusterSsl) StackGresClusterSpecLabels(io.stackgres.common.crd.sgcluster.StackGresClusterSpecLabels) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) ClusterPodPersistentVolume(io.stackgres.apiweb.dto.cluster.ClusterPodPersistentVolume) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) ClusterScriptFrom(io.stackgres.apiweb.dto.cluster.ClusterScriptFrom) StackGresClusterSpecLabels(io.stackgres.common.crd.sgcluster.StackGresClusterSpecLabels) ClusterSpecLabels(io.stackgres.apiweb.dto.cluster.ClusterSpecLabels) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices)

Example 2 with StackGresClusterSpecAnnotations

use of io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations in project stackgres by ongres.

the class ClusterAnnotationDecoratorTest method services_shouldNotHavePodAnnotations.

@Test
void services_shouldNotHavePodAnnotations() {
    String allResourceAnnotationKey = StringUtil.generateRandom(8);
    String allResourceAnnotationValue = StringUtil.generateRandom(8);
    defaultCluster.getSpec().setPod(null);
    defaultCluster.getSpec().setPostgresServices(null);
    defaultCluster.getSpec().getMetadata().setAnnotations(new StackGresClusterSpecAnnotations());
    defaultCluster.getSpec().getMetadata().getAnnotations().setAllResources(ImmutableMap.of(allResourceAnnotationKey, allResourceAnnotationValue));
    String podAnnotationKey = StringUtil.generateRandom(8);
    String podAnnotationValue = StringUtil.generateRandom(8);
    defaultCluster.getSpec().getMetadata().getAnnotations().setClusterPods(Map.of(podAnnotationKey, podAnnotationValue));
    annotationDecorator.decorate(context, resources);
    resources.stream().filter(r -> r.getKind().equals("Service")).forEach(resource -> {
        assertFalse(resource.getMetadata().getAnnotations().containsKey(podAnnotationKey));
    });
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) StackGresContext(io.stackgres.common.StackGresContext) BeforeEach(org.junit.jupiter.api.BeforeEach) StackGresProperty(io.stackgres.common.StackGresProperty) Mock(org.mockito.Mock) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StringUtil(io.stackgres.common.StringUtil) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) CronJob(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob) ClusterAnnotationDecorator(io.stackgres.operator.conciliation.factory.cluster.ClusterAnnotationDecorator) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ImmutableMap(com.google.common.collect.ImmutableMap) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) Mockito.when(org.mockito.Mockito.when) JobTemplateSpec(io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec) Truth.assertThat(com.google.common.truth.Truth.assertThat) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) JsonUtil(io.stackgres.testutil.JsonUtil) Test(org.junit.jupiter.api.Test) List(java.util.List) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PatroniUtil(io.stackgres.common.PatroniUtil) Optional(java.util.Optional) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) Test(org.junit.jupiter.api.Test)

Example 3 with StackGresClusterSpecAnnotations

use of io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations in project stackgres by ongres.

the class ClusterAnnotationDecoratorTest method pods_shouldNotHaveServiceAnnotations.

@Test
void pods_shouldNotHaveServiceAnnotations() {
    String allResourceAnnotationKey = StringUtil.generateRandom(8);
    String allResourceAnnotationValue = StringUtil.generateRandom(8);
    defaultCluster.getSpec().setPod(null);
    defaultCluster.getSpec().setPostgresServices(null);
    defaultCluster.getSpec().getMetadata().setAnnotations(new StackGresClusterSpecAnnotations());
    defaultCluster.getSpec().getMetadata().getAnnotations().setAllResources(ImmutableMap.of(allResourceAnnotationKey, allResourceAnnotationValue));
    String serviceAnnotationKey = StringUtil.generateRandom(8);
    String serviceAnnotationValue = StringUtil.generateRandom(8);
    defaultCluster.getSpec().getMetadata().getAnnotations().setServices(ImmutableMap.of(serviceAnnotationKey, serviceAnnotationValue));
    annotationDecorator.decorate(context, resources);
    resources.stream().filter(r -> r.getKind().equals("Pod")).forEach(resource -> assertFalse(resource.getMetadata().getAnnotations().containsKey(serviceAnnotationKey)));
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) StackGresContext(io.stackgres.common.StackGresContext) BeforeEach(org.junit.jupiter.api.BeforeEach) StackGresProperty(io.stackgres.common.StackGresProperty) Mock(org.mockito.Mock) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StringUtil(io.stackgres.common.StringUtil) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) CronJob(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob) ClusterAnnotationDecorator(io.stackgres.operator.conciliation.factory.cluster.ClusterAnnotationDecorator) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ImmutableMap(com.google.common.collect.ImmutableMap) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) Mockito.when(org.mockito.Mockito.when) JobTemplateSpec(io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec) Truth.assertThat(com.google.common.truth.Truth.assertThat) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) JsonUtil(io.stackgres.testutil.JsonUtil) Test(org.junit.jupiter.api.Test) List(java.util.List) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PatroniUtil(io.stackgres.common.PatroniUtil) Optional(java.util.Optional) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) Test(org.junit.jupiter.api.Test)

Example 4 with StackGresClusterSpecAnnotations

use of io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations in project stackgres by ongres.

the class ClusterAnnotationDecoratorTest method primaryServices_shouldHavePrimaryServiceAnnotations.

@Test
void primaryServices_shouldHavePrimaryServiceAnnotations() {
    defaultCluster.getSpec().getMetadata().setAnnotations(new StackGresClusterSpecAnnotations());
    String primaryAnnotationKey = "primary-" + StringUtil.generateRandom(8);
    String primaryAnnotationValue = "primary-" + StringUtil.generateRandom(8);
    defaultCluster.getSpec().getMetadata().getAnnotations().setPrimaryService(Map.of(primaryAnnotationKey, primaryAnnotationValue));
    String serviceAnnotationKey = "service-" + StringUtil.generateRandom(8);
    String serviceAnnotationValue = "service-" + StringUtil.generateRandom(8);
    defaultCluster.getSpec().setPod(null);
    defaultCluster.getSpec().getPostgresServices().setReplicas(null);
    defaultCluster.getSpec().getMetadata().getAnnotations().setServices(Map.of(serviceAnnotationKey, serviceAnnotationValue));
    annotationDecorator.decorate(context, resources);
    Map<String, String> expected = Map.of(primaryAnnotationKey, primaryAnnotationValue, serviceAnnotationKey, serviceAnnotationValue);
    resources.stream().filter(r -> r.getKind().equals("Service")).filter(r -> r.getMetadata().getName().endsWith(PatroniUtil.READ_WRITE_SERVICE)).forEach(resource -> checkResourceAnnotations(resource, expected));
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) StackGresContext(io.stackgres.common.StackGresContext) BeforeEach(org.junit.jupiter.api.BeforeEach) StackGresProperty(io.stackgres.common.StackGresProperty) Mock(org.mockito.Mock) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StringUtil(io.stackgres.common.StringUtil) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) CronJob(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob) ClusterAnnotationDecorator(io.stackgres.operator.conciliation.factory.cluster.ClusterAnnotationDecorator) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ImmutableMap(com.google.common.collect.ImmutableMap) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) Mockito.when(org.mockito.Mockito.when) JobTemplateSpec(io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec) Truth.assertThat(com.google.common.truth.Truth.assertThat) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) JsonUtil(io.stackgres.testutil.JsonUtil) Test(org.junit.jupiter.api.Test) List(java.util.List) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PatroniUtil(io.stackgres.common.PatroniUtil) Optional(java.util.Optional) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) Test(org.junit.jupiter.api.Test)

Example 5 with StackGresClusterSpecAnnotations

use of io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations in project stackgres by ongres.

the class ClusterAnnotationDecoratorTest method allResources_shouldBeAppliedToAllResources.

@Test
void allResources_shouldBeAppliedToAllResources() {
    String randomAnnotationKey = StringUtil.generateRandom(8);
    String randomAnnotationValue = StringUtil.generateRandom(8);
    defaultCluster.getSpec().setPod(null);
    defaultCluster.getSpec().setPostgresServices(null);
    defaultCluster.getSpec().getMetadata().setAnnotations(new StackGresClusterSpecAnnotations());
    defaultCluster.getSpec().getMetadata().getAnnotations().setAllResources(Map.of(randomAnnotationKey, randomAnnotationValue));
    annotationDecorator.decorate(context, resources);
    resources.forEach(resource -> checkResourceAnnotations(resource, Map.of(randomAnnotationKey, randomAnnotationValue)));
}
Also used : StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) Test(org.junit.jupiter.api.Test)

Aggregations

StackGresClusterSpecAnnotations (io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations)15 Test (org.junit.jupiter.api.Test)10 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)9 CronJob (io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob)9 JobTemplateSpec (io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec)9 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 Truth.assertThat (com.google.common.truth.Truth.assertThat)8 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)8 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)8 PatroniUtil (io.stackgres.common.PatroniUtil)8 StackGresContext (io.stackgres.common.StackGresContext)8 StackGresProperty (io.stackgres.common.StackGresProperty)8 StringUtil (io.stackgres.common.StringUtil)8 StackGresClusterContext (io.stackgres.operator.conciliation.cluster.StackGresClusterContext)8 ClusterAnnotationDecorator (io.stackgres.operator.conciliation.factory.cluster.ClusterAnnotationDecorator)8 JsonUtil (io.stackgres.testutil.JsonUtil)8 List (java.util.List)8 Map (java.util.Map)8