Search in sources :

Example 11 with StackGresClusterSpecAnnotations

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

the class ClusterAnnotationDecoratorTest method allResourcesAnnotations_shouldBePresentInCronJobsPodTemplate.

@Test
void allResourcesAnnotations_shouldBePresentInCronJobsPodTemplate() {
    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));
    annotationDecorator.decorate(context, resources);
    Map<String, String> expected = Map.of(allResourceAnnotationKey, allResourceAnnotationValue);
    resources.stream().filter(CronJob.class::isInstance).map(CronJob.class::cast).forEach(cronJob -> {
        final JobTemplateSpec jobTemplate = cronJob.getSpec().getJobTemplate();
        checkResourceAnnotations(jobTemplate, expected);
        PodTemplateSpec template = jobTemplate.getSpec().getTemplate();
        checkResourceAnnotations(template, expected);
    });
}
Also used : PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) CronJob(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob) JobTemplateSpec(io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec) Test(org.junit.jupiter.api.Test)

Example 12 with StackGresClusterSpecAnnotations

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

the class StackGresDistributedLogsUtil method getStackGresClusterForDistributedLogs.

static StackGresCluster getStackGresClusterForDistributedLogs(StackGresDistributedLogs distributedLogs) {
    final StackGresCluster distributedLogsCluster = new StackGresCluster();
    distributedLogsCluster.getMetadata().setNamespace(distributedLogs.getMetadata().getNamespace());
    distributedLogsCluster.getMetadata().setName(distributedLogs.getMetadata().getName());
    distributedLogsCluster.getMetadata().setUid(distributedLogs.getMetadata().getUid());
    final StackGresClusterSpec spec = new StackGresClusterSpec();
    spec.setPostgres(new StackGresClusterPostgres());
    spec.getPostgres().setVersion(getPostgresVersion());
    spec.setInstances(1);
    final StackGresClusterPod pod = new StackGresClusterPod();
    final StackGresPodPersistentVolume persistentVolume = new StackGresPodPersistentVolume();
    persistentVolume.setSize(distributedLogs.getSpec().getPersistentVolume().getSize());
    persistentVolume.setStorageClass(distributedLogs.getSpec().getPersistentVolume().getStorageClass());
    pod.setPersistentVolume(persistentVolume);
    spec.setPostgresServices(buildPostgresServices(distributedLogs.getSpec()));
    StackGresClusterPodScheduling scheduling = new StackGresClusterPodScheduling();
    Optional.of(distributedLogs).map(StackGresDistributedLogs::getSpec).map(StackGresDistributedLogsSpec::getScheduling).ifPresent(distributedLogsScheduling -> {
        scheduling.setNodeSelector(distributedLogsScheduling.getNodeSelector());
        scheduling.setTolerations(distributedLogsScheduling.getTolerations());
    });
    pod.setScheduling(scheduling);
    spec.setPod(pod);
    final StackGresClusterInitData initData = new StackGresClusterInitData();
    final StackGresClusterScriptEntry script = new StackGresClusterScriptEntry();
    script.setName("distributed-logs-template");
    script.setDatabase("template1");
    script.setScript(Unchecked.supplier(() -> Resources.asCharSource(StackGresDistributedLogsUtil.class.getResource("/distributed-logs-template.sql"), StandardCharsets.UTF_8).read()).get());
    initData.setScripts(ImmutableList.of(script));
    spec.setInitData(initData);
    final StackGresClusterNonProduction nonProduction = new StackGresClusterNonProduction();
    nonProduction.setDisableClusterPodAntiAffinity(Optional.ofNullable(distributedLogs.getSpec().getNonProduction()).map(StackGresDistributedLogsNonProduction::getDisableClusterPodAntiAffinity).orElse(false));
    spec.setNonProduction(nonProduction);
    final StackGresClusterSpecMetadata metadata = new StackGresClusterSpecMetadata();
    final StackGresClusterSpecAnnotations annotations = new StackGresClusterSpecAnnotations();
    Optional.of(distributedLogs).map(StackGresDistributedLogs::getSpec).map(StackGresDistributedLogsSpec::getMetadata).map(StackGresDistributedLogsSpecMetadata::getAnnotations).ifPresent(distributedLogsAnnotations -> {
        annotations.setAllResources(distributedLogsAnnotations.getAllResources());
        annotations.setClusterPods(distributedLogsAnnotations.getPods());
        annotations.setPrimaryService(distributedLogsAnnotations.getServices());
        annotations.setReplicasService(distributedLogsAnnotations.getServices());
    });
    metadata.setAnnotations(annotations);
    spec.setMetadata(metadata);
    spec.setToInstallPostgresExtensions(Optional.ofNullable(distributedLogs.getSpec()).map(StackGresDistributedLogsSpec::getToInstallPostgresExtensions).orElse(null));
    distributedLogsCluster.setSpec(spec);
    return distributedLogsCluster;
}
Also used : StackGresClusterPodScheduling(io.stackgres.common.crd.sgcluster.StackGresClusterPodScheduling) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresDistributedLogsSpec(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) StackGresClusterPostgres(io.stackgres.common.crd.sgcluster.StackGresClusterPostgres) StackGresDistributedLogsNonProduction(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsNonProduction) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterNonProduction(io.stackgres.common.crd.sgcluster.StackGresClusterNonProduction) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata)

Example 13 with StackGresClusterSpecAnnotations

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

the class PatroniServicesTest method enablePrimaryService.

private void enablePrimaryService(Map<String, String> annotations) {
    enablePrimaryService(true);
    if (defaultCluster.getSpec().getMetadata() == null) {
        defaultCluster.getSpec().setMetadata(new StackGresClusterSpecMetadata());
    }
    if (defaultCluster.getSpec().getMetadata().getAnnotations() == null) {
        defaultCluster.getSpec().getMetadata().setAnnotations(new StackGresClusterSpecAnnotations());
    }
    defaultCluster.getSpec().getMetadata().getAnnotations().setPrimaryService(annotations);
}
Also used : StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata)

Example 14 with StackGresClusterSpecAnnotations

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

the class PatroniServicesTest method enableReplicaService.

private void enableReplicaService(Map<String, String> annotations) {
    enableReplicaService(true);
    if (defaultCluster.getSpec().getMetadata() == null) {
        defaultCluster.getSpec().setMetadata(new StackGresClusterSpecMetadata());
    }
    if (defaultCluster.getSpec().getMetadata().getAnnotations() == null) {
        defaultCluster.getSpec().getMetadata().setAnnotations(new StackGresClusterSpecAnnotations());
    }
    defaultCluster.getSpec().getMetadata().getAnnotations().setReplicasService(annotations);
}
Also used : StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata)

Example 15 with StackGresClusterSpecAnnotations

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

the class ClusterTransformer method getCustomResourceSpec.

private StackGresClusterSpec getCustomResourceSpec(ClusterSpec source) {
    if (source == null) {
        return null;
    }
    StackGresClusterSpec transformation = new StackGresClusterSpec();
    transformation.setPostgres(new StackGresClusterPostgres());
    transformation.getPostgres().setVersion(source.getPostgres().getVersion());
    transformation.getPostgres().setExtensions(Optional.ofNullable(source.getPostgres().getExtensions()).stream().flatMap(List::stream).map(this::getCustomResourceExtension).collect(ImmutableList.toImmutableList()));
    final ClusterSsl sourceClusterSsl = source.getPostgres().getSsl();
    if (sourceClusterSsl != null) {
        transformation.getPostgres().setSsl(new StackGresClusterSsl());
        transformation.getPostgres().getSsl().setEnabled(sourceClusterSsl.getEnabled());
        transformation.getPostgres().getSsl().setCertificateSecretKeySelector(sourceClusterSsl.getCertificateSecretKeySelector());
        transformation.getPostgres().getSsl().setPrivateKeySecretKeySelector(sourceClusterSsl.getPrivateKeySecretKeySelector());
    }
    final ClusterConfiguration sourceClusterConfiguration = source.getConfigurations();
    if (sourceClusterConfiguration != null) {
        transformation.setConfiguration(new StackGresClusterConfiguration());
        transformation.getConfiguration().setBackupConfig(source.getConfigurations().getSgBackupConfig());
        transformation.getConfiguration().setConnectionPoolingConfig(source.getConfigurations().getSgPoolingConfig());
        transformation.getConfiguration().setPostgresConfig(source.getConfigurations().getSgPostgresConfig());
    }
    transformation.setInstances(source.getInstances());
    transformation.setNonProduction(getCustomResourceNonProduction(source.getNonProduction()));
    transformation.setPrometheusAutobind(source.getPrometheusAutobind());
    transformation.setResourceProfile(source.getSgInstanceProfile());
    final ClusterSpecMetadata specMetadata = source.getMetadata();
    if (specMetadata != null) {
        transformation.setMetadata(new StackGresClusterSpecMetadata());
        final ClusterSpecAnnotations sourceAnnotations = specMetadata.getAnnotations();
        if (sourceAnnotations != null) {
            StackGresClusterSpecAnnotations targetAnnotations = new StackGresClusterSpecAnnotations();
            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 ClusterSpecLabels sourceLabels = specMetadata.getLabels();
        if (sourceLabels != null) {
            StackGresClusterSpecLabels targetLabels = new StackGresClusterSpecLabels();
            targetLabels.setClusterPods(sourceLabels.getClusterPods());
            transformation.getMetadata().setLabels(targetLabels);
        }
    }
    final ClusterPostgresServices sourcePostgresServices = source.getPostgresServices();
    if (sourcePostgresServices != null) {
        transformation.setPostgresServices(new StackGresClusterPostgresServices());
        final StackGresClusterPostgresServices targetPostgresService = transformation.getPostgresServices();
        final PostgresService sourcePrimaryService = sourcePostgresServices.getPrimary();
        if (sourcePrimaryService != null) {
            targetPostgresService.setPrimary(new StackGresPostgresService());
            final StackGresPostgresService targetPrimaryService = targetPostgresService.getPrimary();
            targetPrimaryService.setType(sourcePrimaryService.getType());
            targetPrimaryService.setEnabled(sourcePrimaryService.getEnabled());
        }
        final PostgresService sourceReplicaService = sourcePostgresServices.getReplicas();
        if (sourceReplicaService != null) {
            targetPostgresService.setReplicas(new StackGresPostgresService());
            final StackGresPostgresService targetReplicaService = targetPostgresService.getReplicas();
            targetReplicaService.setEnabled(sourceReplicaService.getEnabled());
            targetReplicaService.setType(sourceReplicaService.getType());
        }
    }
    final ClusterInitData sourceInitData = source.getInitData();
    if (sourceInitData != null) {
        final StackGresClusterInitData targetInitData = new StackGresClusterInitData();
        transformation.setInitData(targetInitData);
        if (sourceInitData.getRestore() != null) {
            targetInitData.setRestore(getCustomResourceRestore(sourceInitData.getRestore()));
        }
        if (sourceInitData.getScripts() != null) {
            targetInitData.setScripts(getCustomResourceScripts(sourceInitData.getScripts()));
        }
    }
    final StackGresClusterPod targetPod = new StackGresClusterPod();
    transformation.setPod(targetPod);
    targetPod.setPersistentVolume(new StackGresPodPersistentVolume());
    targetPod.getPersistentVolume().setStorageClass(source.getPods().getPersistentVolume().getStorageClass());
    targetPod.getPersistentVolume().setSize(source.getPods().getPersistentVolume().getSize());
    targetPod.setDisableConnectionPooling(source.getPods().getDisableConnectionPooling());
    targetPod.setDisableMetricsExporter(source.getPods().getDisableMetricsExporter());
    targetPod.setDisablePostgresUtil(source.getPods().getDisablePostgresUtil());
    targetPod.setScheduling(Optional.ofNullable(source.getPods().getScheduling()).map(sourceScheduling -> {
        return new ClusterPodSchedulingConverter().to(sourceScheduling);
    }).orElse(null));
    transformation.setDistributedLogs(getCustomResourceDistributedLogs(source.getDistributedLogs()));
    return transformation;
}
Also used : StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) ClusterInitData(io.stackgres.apiweb.dto.cluster.ClusterInitData) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) 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) StackGresClusterPostgres(io.stackgres.common.crd.sgcluster.StackGresClusterPostgres) 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) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata) ClusterSpecMetadata(io.stackgres.apiweb.dto.cluster.ClusterSpecMetadata) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) ClusterSsl(io.stackgres.apiweb.dto.cluster.ClusterSsl) StackGresClusterSsl(io.stackgres.common.crd.sgcluster.StackGresClusterSsl) StackGresClusterSsl(io.stackgres.common.crd.sgcluster.StackGresClusterSsl) StackGresClusterSpecLabels(io.stackgres.common.crd.sgcluster.StackGresClusterSpecLabels) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) StackGresClusterSpecMetadata(io.stackgres.common.crd.sgcluster.StackGresClusterSpecMetadata) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) StackGresClusterSpecLabels(io.stackgres.common.crd.sgcluster.StackGresClusterSpecLabels) ClusterSpecLabels(io.stackgres.apiweb.dto.cluster.ClusterSpecLabels) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices)

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