Search in sources :

Example 1 with StackGresClusterConfiguration

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

the class ClusterConfigurationMutator method ensureConfigurationNode.

static List<JsonPatchOperation> ensureConfigurationNode(StackGresClusterReview review) {
    final StackGresClusterSpec spec = review.getRequest().getObject().getSpec();
    StackGresClusterConfiguration configuration = spec.getConfiguration();
    if (configuration == null) {
        try {
            configuration = new StackGresClusterConfiguration();
            spec.setConfiguration(configuration);
            final JsonPointer confPointer = getTargetPointer();
            final AddOperation configurationAdd = new AddOperation(confPointer, JsonPatchMutator.FACTORY.objectNode());
            return ImmutableList.of(configurationAdd);
        } catch (NoSuchFieldException e) {
            return ImmutableList.of();
        }
    }
    return ImmutableList.of();
}
Also used : StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) JsonPointer(com.github.fge.jackson.jsonpointer.JsonPointer) AddOperation(com.github.fge.jsonpatch.AddOperation)

Example 2 with StackGresClusterConfiguration

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

the class ClusterResourceMockedTest method checkDto.

@Override
protected void checkDto(ClusterDto dto, StackGresCluster resource) {
    if (resource.getMetadata() != null) {
        assertNotNull(dto.getMetadata());
        assertEquals(resource.getMetadata().getNamespace(), dto.getMetadata().getNamespace());
        assertEquals(resource.getMetadata().getName(), dto.getMetadata().getName());
        assertEquals(resource.getMetadata().getUid(), dto.getMetadata().getUid());
    } else {
        assertNull(dto.getMetadata());
    }
    final StackGresClusterSpec resourceSpec = resource.getSpec();
    final ClusterSpec dtoSpec = dto.getSpec();
    if (resourceSpec != null) {
        assertNotNull(dtoSpec);
        assertEquals(resourceSpec.getInstances(), dtoSpec.getInstances());
        assertEquals(resourceSpec.getPostgres().getVersion(), dtoSpec.getPostgres().getVersion());
        assertEquals(resourceSpec.getPrometheusAutobind(), dtoSpec.getPrometheusAutobind());
        assertEquals(resourceSpec.getResourceProfile(), dtoSpec.getSgInstanceProfile());
        final ClusterConfiguration dtoClusterConfigurations = dtoSpec.getConfigurations();
        final StackGresClusterConfiguration resourceClusterConfiguration = resourceSpec.getConfiguration();
        if (resourceClusterConfiguration != null) {
            assertNotNull(dtoClusterConfigurations);
            assertEquals(resourceClusterConfiguration.getBackupConfig(), dtoClusterConfigurations.getSgBackupConfig());
            assertEquals(resourceClusterConfiguration.getConnectionPoolingConfig(), dtoClusterConfigurations.getSgPoolingConfig());
            assertEquals(resourceClusterConfiguration.getPostgresConfig(), dtoClusterConfigurations.getSgPostgresConfig());
        } else {
            assertNull(dtoClusterConfigurations);
        }
        final ClusterPod dtoSpecPods = dtoSpec.getPods();
        final StackGresClusterPod resourcePod = resourceSpec.getPod();
        if (resourcePod != null) {
            assertNotNull(dtoSpecPods);
            assertEquals(resourcePod.getDisableConnectionPooling(), dtoSpecPods.getDisableConnectionPooling());
            assertEquals(resourcePod.getDisableMetricsExporter(), dtoSpecPods.getDisableMetricsExporter());
            assertEquals(resourcePod.getDisablePostgresUtil(), dtoSpecPods.getDisablePostgresUtil());
            final ClusterPodPersistentVolume resourcePV = dtoSpecPods.getPersistentVolume();
            final StackGresPodPersistentVolume dtoPV = resourcePod.getPersistentVolume();
            if (dtoPV != null) {
                assertNotNull(resourcePV);
                assertEquals(dtoPV.getSize(), resourcePV.getSize());
                assertEquals(dtoPV.getStorageClass(), resourcePV.getStorageClass());
            } else {
                assertNull(resourcePV);
            }
            if (resourceSpec.getMetadata() != null) {
                assertNotNull(dtoSpec.getMetadata());
                assertEquals(resourceSpec.getMetadata().getLabels().getClusterPods(), dtoSpec.getMetadata().getLabels().getClusterPods());
            } else {
                assertNull(dtoSpec.getMetadata());
            }
            if (resourcePod.getScheduling() != null) {
                assertNotNull(dtoSpecPods.getScheduling());
                assertEquals(resourcePod.getScheduling().getNodeSelector(), dtoSpecPods.getScheduling().getNodeSelector());
                assertEquals(resourcePod.getScheduling().getNodeAffinity(), dtoSpecPods.getScheduling().getNodeAffinity());
            } else {
                assertNull(dtoSpecPods.getScheduling());
            }
        } else {
            assertNull(dtoSpecPods);
        }
        if (resourceSpec.getDistributedLogs() != null) {
            assertNotNull(dtoSpec.getDistributedLogs());
            assertEquals(resourceSpec.getDistributedLogs().getDistributedLogs(), dtoSpec.getDistributedLogs().getDistributedLogs());
        } else {
            assertNull(dtoSpec.getDistributedLogs());
        }
        final StackGresClusterInitData resourceInitData = resourceSpec.getInitData();
        if (resourceInitData != null) {
            final ClusterInitData dtoInitData = dtoSpec.getInitData();
            assertNotNull(dtoInitData);
            if (resourceInitData.getRestore() != null) {
                assertNotNull(dtoInitData.getRestore());
                assertEquals(resourceInitData.getRestore().getFromBackup().getUid(), dtoInitData.getRestore().getFromBackup().getUid());
                assertEquals(resourceInitData.getRestore().getDownloadDiskConcurrency(), dtoInitData.getRestore().getDownloadDiskConcurrency());
            } else {
                assertNull(dtoInitData.getRestore());
            }
            if (resourceInitData.getScripts() != null) {
                assertNotNull(dtoInitData.getScripts());
                Seq.zip(resourceInitData.getScripts(), dtoInitData.getScripts()).forEach(tuple -> {
                    assertEquals(tuple.v1.getDatabase(), tuple.v2.getDatabase());
                    assertEquals(tuple.v2.getName(), tuple.v2.getName());
                    assertEquals(tuple.v2.getScript(), tuple.v2.getScript());
                    final StackGresClusterScriptFrom resourceScriptFrom = tuple.v1.getScriptFrom();
                    final ClusterScriptFrom dtoScriptFrom = tuple.v2.getScriptFrom();
                    if (resourceScriptFrom != null) {
                        assertNotNull(dtoScriptFrom);
                        if (resourceScriptFrom.getSecretKeyRef() != null) {
                            assertNotNull(dtoScriptFrom.getSecretKeyRef());
                            assertEquals(resourceScriptFrom.getSecretKeyRef().getKey(), dtoScriptFrom.getSecretKeyRef().getKey());
                            assertEquals(resourceScriptFrom.getSecretKeyRef().getName(), dtoScriptFrom.getSecretKeyRef().getName());
                        } else {
                            assertNull(dtoScriptFrom.getSecretKeyRef());
                        }
                        if (resourceScriptFrom.getConfigMapKeyRef() != null) {
                            assertNotNull(dtoScriptFrom.getConfigMapKeyRef());
                            assertEquals(resourceScriptFrom.getConfigMapKeyRef().getKey(), dtoScriptFrom.getConfigMapKeyRef().getKey());
                            assertEquals(resourceScriptFrom.getConfigMapKeyRef().getName(), dtoScriptFrom.getConfigMapKeyRef().getName());
                            assertEquals(configMap.getData().get(resourceScriptFrom.getConfigMapKeyRef().getKey()), dtoScriptFrom.getConfigMapScript());
                        } else {
                            assertNull(dtoScriptFrom.getConfigMapKeyRef());
                        }
                    } else {
                        assertNull(dtoScriptFrom);
                    }
                });
            }
        }
    } else {
        assertNull(dtoSpec);
    }
    if (dto.getPods() != null) {
        assertEquals(1, dto.getPodsReady());
        assertEquals(2, dto.getPods().size());
        assertEquals(4, dto.getPods().get(0).getContainers());
        assertEquals(4, dto.getPods().get(0).getContainersReady());
        assertEquals("10.244.3.23", dto.getPods().get(0).getIp());
        assertEquals("stackgres-0", dto.getPods().get(0).getName());
        assertEquals("stackgres", dto.getPods().get(0).getNamespace());
        assertEquals("primary", dto.getPods().get(0).getRole());
        assertEquals("Active", dto.getPods().get(0).getStatus());
        assertEquals(4, dto.getPods().get(0).getComponentVersions().size());
        assertEquals("12.2", dto.getPods().get(0).getComponentVersions().get("postgresql"));
        assertEquals("1.6.4", dto.getPods().get(0).getComponentVersions().get("patroni"));
        assertEquals("1.13.1", dto.getPods().get(0).getComponentVersions().get("envoy"));
        assertEquals("0.8", dto.getPods().get(0).getComponentVersions().get("prometheus-postgres-exporter"));
        assertEquals(4, dto.getPods().get(1).getContainers());
        assertEquals(0, dto.getPods().get(1).getContainersReady());
        assertNull(dto.getPods().get(1).getIp());
        assertEquals("stackgres-1", dto.getPods().get(1).getName());
        assertEquals("stackgres", dto.getPods().get(1).getNamespace());
        assertNull(dto.getPods().get(1).getRole());
        assertEquals("Pending", dto.getPods().get(1).getStatus());
        assertEquals(4, dto.getPods().get(1).getComponentVersions().size());
        assertEquals("12.2", dto.getPods().get(1).getComponentVersions().get("postgresql"));
        assertEquals("1.6.4", dto.getPods().get(1).getComponentVersions().get("patroni"));
        assertEquals("1.13.1", dto.getPods().get(1).getComponentVersions().get("envoy"));
        assertEquals("0.8", dto.getPods().get(1).getComponentVersions().get("prometheus-postgres-exporter"));
    }
    if (dto.getInfo() != null) {
        String appendDns = "." + resource.getMetadata().getNamespace() + ".svc.cluster.local";
        String expectedPrimaryDns = PatroniUtil.readWriteName(resource.getMetadata().getName()) + appendDns;
        String expectedReplicasDns = "f4611c56942064ed5a468d8ce0a894ec.us-east-1.elb.amazonaws.com";
        assertEquals(expectedPrimaryDns, dto.getInfo().getPrimaryDns());
        assertEquals(expectedReplicasDns, dto.getInfo().getReplicasDns());
        assertEquals("postgres", dto.getInfo().getSuperuserUsername());
        assertEquals("superuser-password", dto.getInfo().getSuperuserPasswordKey());
        assertEquals(resource.getMetadata().getName(), dto.getInfo().getSuperuserSecretName());
    }
}
Also used : StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) ClusterInitData(io.stackgres.apiweb.dto.cluster.ClusterInitData) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) ClusterSpec(io.stackgres.apiweb.dto.cluster.ClusterSpec) ClusterConfiguration(io.stackgres.apiweb.dto.cluster.ClusterConfiguration) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StackGresClusterPod(io.stackgres.common.crd.sgcluster.StackGresClusterPod) ClusterPod(io.stackgres.apiweb.dto.cluster.ClusterPod) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) ClusterPodPersistentVolume(io.stackgres.apiweb.dto.cluster.ClusterPodPersistentVolume) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) ClusterScriptFrom(io.stackgres.apiweb.dto.cluster.ClusterScriptFrom)

Example 3 with StackGresClusterConfiguration

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

the class ClusterRequiredResourcesGeneratorTest method givenAPrometheusInstallationWithNoServiceMonitorSelector_shouldGenerateServiceMonitors.

@Test
void givenAPrometheusInstallationWithNoServiceMonitorSelector_shouldGenerateServiceMonitors() {
    final ObjectMeta metadata = cluster.getMetadata();
    final String clusterNamespace = metadata.getNamespace();
    final StackGresClusterSpec clusterSpec = cluster.getSpec();
    final StackGresClusterConfiguration clusterConfiguration = clusterSpec.getConfiguration();
    final String backupConfigName = clusterConfiguration.getBackupConfig();
    mockBackupConfig(clusterNamespace, backupConfigName);
    final String postgresConfigName = clusterConfiguration.getPostgresConfig();
    mockPgConfig(clusterNamespace, postgresConfigName);
    final String connectionPoolingConfig = clusterConfiguration.getConnectionPoolingConfig();
    mockPoolingConfig(clusterNamespace, connectionPoolingConfig);
    final String resourceProfile = clusterSpec.getResourceProfile();
    when(profileConfigFinder.findByNameAndNamespace(resourceProfile, clusterNamespace)).thenReturn(Optional.of(instanceProfile));
    when(backupScanner.getResources()).thenReturn(backups);
    mockSecrets(clusterNamespace);
    when(prometheusScanner.findResources()).thenReturn(Optional.of(JsonUtil.readFromJson("prometheus/prometheus_list.json", PrometheusConfigList.class).getItems().stream().peek(pc -> pc.getSpec().setServiceMonitorSelector(null)).collect(Collectors.toUnmodifiableList())));
    List<HasMetadata> generatedResources = generator.getRequiredResources(cluster);
    var serviceMonitors = generatedResources.stream().filter(r -> r.getKind().equals(ServiceMonitor.KIND)).collect(Collectors.toUnmodifiableList());
    assertEquals(2, serviceMonitors.size());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) StackGresBackupList(io.stackgres.common.crd.sgbackup.StackGresBackupList) BeforeEach(org.junit.jupiter.api.BeforeEach) PoolingConfigFinder(io.stackgres.common.resource.PoolingConfigFinder) StackGresPostgresConfigStatus(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfigStatus) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresPoolingConfigStatus(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigStatus) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig) PrometheusConfigList(io.stackgres.operator.customresource.prometheus.PrometheusConfigList) QuarkusTest(io.quarkus.test.junit.QuarkusTest) ServiceMonitor(io.stackgres.operator.customresource.prometheus.ServiceMonitor) Inject(javax.inject.Inject) StackGresClusterRestoreFromBackup(io.stackgres.common.crd.sgcluster.StackGresClusterRestoreFromBackup) ProfileConfigFinder(io.stackgres.common.resource.ProfileConfigFinder) SecretFinder(io.stackgres.common.resource.SecretFinder) BackupScanner(io.stackgres.common.resource.BackupScanner) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) InjectMock(io.quarkus.test.junit.mockito.InjectMock) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) PgBouncerDefaultValues(io.stackgres.operator.conciliation.factory.cluster.sidecars.pooling.parameters.PgBouncerDefaultValues) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresProfile(io.stackgres.common.crd.sgprofile.StackGresProfile) JsonUtil(io.stackgres.testutil.JsonUtil) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) StackGresComponent(io.stackgres.common.StackGresComponent) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) Mockito.never(org.mockito.Mockito.never) List(java.util.List) BackupConfigFinder(io.stackgres.common.resource.BackupConfigFinder) StackGresPoolingConfigPgBouncerStatus(io.stackgres.common.crd.sgpooling.StackGresPoolingConfigPgBouncerStatus) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PostgresDefaultValues(io.stackgres.operator.conciliation.factory.cluster.patroni.parameters.PostgresDefaultValues) Optional(java.util.Optional) Secret(io.fabric8.kubernetes.api.model.Secret) PostgresConfigFinder(io.stackgres.common.resource.PostgresConfigFinder) PrometheusScanner(io.stackgres.operator.resource.PrometheusScanner) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 4 with StackGresClusterConfiguration

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

the class ClusterRequiredResourcesGeneratorTest method givenValidClusterWithoutRestoreData_getRequiredResourcesShouldNotScanForBackups.

@Test
void givenValidClusterWithoutRestoreData_getRequiredResourcesShouldNotScanForBackups() {
    cluster.getSpec().getInitData().setRestore(null);
    final ObjectMeta metadata = cluster.getMetadata();
    final String clusterNamespace = metadata.getNamespace();
    final StackGresClusterSpec clusterSpec = cluster.getSpec();
    final StackGresClusterConfiguration clusterConfiguration = clusterSpec.getConfiguration();
    final String backupConfigName = clusterConfiguration.getBackupConfig();
    mockBackupConfig(clusterNamespace, backupConfigName);
    final String postgresConfigName = clusterConfiguration.getPostgresConfig();
    mockPgConfig(clusterNamespace, postgresConfigName);
    final String connectionPoolingConfig = clusterConfiguration.getConnectionPoolingConfig();
    mockPoolingConfig(clusterNamespace, connectionPoolingConfig);
    final String resourceProfile = clusterSpec.getResourceProfile();
    when(profileConfigFinder.findByNameAndNamespace(resourceProfile, clusterNamespace)).thenReturn(Optional.of(instanceProfile));
    mockSecrets(clusterNamespace);
    generator.getRequiredResources(cluster);
    verify(backupConfigFinder).findByNameAndNamespace(backupConfigName, clusterNamespace);
    verify(postgresConfigFinder).findByNameAndNamespace(postgresConfigName, clusterNamespace);
    verify(poolingConfigFinder).findByNameAndNamespace(connectionPoolingConfig, clusterNamespace);
    verify(profileConfigFinder).findByNameAndNamespace(resourceProfile, clusterNamespace);
    verify(backupScanner, never()).getResources();
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 5 with StackGresClusterConfiguration

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

the class BackupRequiredResourcesGeneratorTest method givenValidCluster_getRequiredResourcesShouldNotFail.

@Test
void givenValidCluster_getRequiredResourcesShouldNotFail() {
    final String backupNamespace = backup.getMetadata().getNamespace();
    final String clusterName = backup.getSpec().getSgCluster();
    final StackGresClusterSpec clusterSpec = cluster.getSpec();
    final StackGresClusterConfiguration clusterConfiguration = clusterSpec.getConfiguration();
    final String backupConfigName = clusterConfiguration.getBackupConfig();
    when(clusterFinder.findByNameAndNamespace(any(), any())).thenReturn(Optional.of(cluster));
    when(backupConfigFinder.findByNameAndNamespace(backupConfigName, backupNamespace)).thenReturn(Optional.of(this.backupConfig));
    generator.getRequiredResources(backup);
    verify(clusterFinder, times(1)).findByNameAndNamespace(any(), any());
    verify(clusterFinder).findByNameAndNamespace(eq(clusterName), eq(backupNamespace));
    verify(backupConfigFinder, times(1)).findByNameAndNamespace(any(), any());
    verify(backupConfigFinder).findByNameAndNamespace(eq(backupConfigName), eq(backupNamespace));
}
Also used : StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

StackGresClusterConfiguration (io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration)23 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)22 QuarkusTest (io.quarkus.test.junit.QuarkusTest)16 Test (org.junit.jupiter.api.Test)16 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)15 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)7 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)6 StackGresBackup (io.stackgres.common.crd.sgbackup.StackGresBackup)6 StackGresBackupConfig (io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig)5 List (java.util.List)5 Optional (java.util.Optional)5 Inject (javax.inject.Inject)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Secret (io.fabric8.kubernetes.api.model.Secret)4 InjectMock (io.quarkus.test.junit.mockito.InjectMock)4 StackGresComponent (io.stackgres.common.StackGresComponent)4 StackGresClusterInitData (io.stackgres.common.crd.sgcluster.StackGresClusterInitData)4 Collectors (java.util.stream.Collectors)4 StackGresClusterRestoreFromBackup (io.stackgres.common.crd.sgcluster.StackGresClusterRestoreFromBackup)3 StackGresPostgresConfig (io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig)3