Search in sources :

Example 1 with ClusterScriptEntry

use of io.stackgres.apiweb.dto.cluster.ClusterScriptEntry in project stackgres by ongres.

the class ClusterResourceMockedTest method buildSecretScriptEntry.

private ClusterScriptEntry buildSecretScriptEntry() {
    ClusterScriptEntry entry = new ClusterScriptEntry();
    entry.setScript(null);
    final ClusterScriptFrom scriptFrom = new ClusterScriptFrom();
    entry.setScriptFrom(scriptFrom);
    scriptFrom.setSecretScript("CREATE DATABASE test");
    final SecretKeySelector secretKeyRef = new SecretKeySelector();
    scriptFrom.setSecretKeyRef(secretKeyRef);
    final String randomKey = StringUtil.generateRandom(30);
    final String randomSecretName = StringUtil.generateRandom(30);
    secretKeyRef.setKey(randomKey);
    secretKeyRef.setName(randomSecretName);
    return entry;
}
Also used : StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) ClusterScriptEntry(io.stackgres.apiweb.dto.cluster.ClusterScriptEntry) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) ClusterScriptFrom(io.stackgres.apiweb.dto.cluster.ClusterScriptFrom) SecretKeySelector(io.stackgres.common.crd.SecretKeySelector)

Example 2 with ClusterScriptEntry

use of io.stackgres.apiweb.dto.cluster.ClusterScriptEntry in project stackgres by ongres.

the class ClusterResourceMockedTest method buildConfigMapScriptEntry.

private ClusterScriptEntry buildConfigMapScriptEntry() {
    ClusterScriptEntry entry = new ClusterScriptEntry();
    entry.setScript(null);
    final ClusterScriptFrom scriptFrom = new ClusterScriptFrom();
    entry.setScriptFrom(scriptFrom);
    scriptFrom.setConfigMapScript("CREATE DATABASE test");
    final ConfigMapKeySelector configMapKeyRef = new ConfigMapKeySelector();
    scriptFrom.setConfigMapKeyRef(configMapKeyRef);
    final String randomKey = StringUtil.generateRandom(30);
    final String randomSecretName = StringUtil.generateRandom(30);
    configMapKeyRef.setKey(randomKey);
    configMapKeyRef.setName(randomSecretName);
    return entry;
}
Also used : StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) ClusterScriptEntry(io.stackgres.apiweb.dto.cluster.ClusterScriptEntry) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StackGresClusterScriptFrom(io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom) ClusterScriptFrom(io.stackgres.apiweb.dto.cluster.ClusterScriptFrom) ConfigMapKeySelector(io.stackgres.common.crd.ConfigMapKeySelector)

Example 3 with ClusterScriptEntry

use of io.stackgres.apiweb.dto.cluster.ClusterScriptEntry in project stackgres by ongres.

the class ClusterResourceQuarkusTest method getConfigMapScriptEntry.

private ClusterScriptEntry getConfigMapScriptEntry() {
    ClusterScriptEntry entry = new ClusterScriptEntry();
    entry.setName("init");
    final ClusterScriptFrom scriptFrom = new ClusterScriptFrom();
    scriptFrom.setConfigMapScript("CREATE DATABASE test");
    final ConfigMapKeySelector configMapKeyRef = new ConfigMapKeySelector();
    scriptFrom.setConfigMapKeyRef(configMapKeyRef);
    configMapKeyRef.setKey("script");
    configMapKeyRef.setName("initScript");
    entry.setScriptFrom(scriptFrom);
    return entry;
}
Also used : ClusterScriptEntry(io.stackgres.apiweb.dto.cluster.ClusterScriptEntry) ClusterScriptFrom(io.stackgres.apiweb.dto.cluster.ClusterScriptFrom) ConfigMapKeySelector(io.stackgres.common.crd.ConfigMapKeySelector)

Example 4 with ClusterScriptEntry

use of io.stackgres.apiweb.dto.cluster.ClusterScriptEntry in project stackgres by ongres.

the class ClusterResourceQuarkusTest method givenACreationWithSecretAndConfigMapScripts_shouldNotFail.

@Test
void givenACreationWithSecretAndConfigMapScripts_shouldNotFail() {
    ClusterDto cluster = getClusterInlineScripts();
    ClusterScriptEntry secretScriptEntry = getSecretScriptEntry();
    ClusterScriptEntry configMapScriptEntry = getConfigMapScriptEntry();
    cluster.getSpec().getInitData().setScripts(ImmutableList.of(secretScriptEntry, configMapScriptEntry));
    final Metadata metadata = cluster.getMetadata();
    metadata.setNamespace("test");
    given().header(AUTHENTICATION_HEADER).body(cluster).contentType(ContentType.JSON).accept(ContentType.JSON).post("/stackgres/sgclusters").then().statusCode(204);
    try (KubernetesClient client = factory.create()) {
        final ClusterScriptFrom secretScriptFrom = secretScriptEntry.getScriptFrom();
        final SecretKeySelector secretKeyRef = secretScriptFrom.getSecretKeyRef();
        Secret secret = client.secrets().inNamespace("test").withName(secretKeyRef.getName()).get();
        assertNotNull(secret);
        byte[] actualScript = Base64.getDecoder().decode(secret.getData().get(secretKeyRef.getKey()));
        assertEquals(secretScriptFrom.getSecretScript(), new String(actualScript, StandardCharsets.UTF_8));
        final ClusterScriptFrom configMapScriptFrom = configMapScriptEntry.getScriptFrom();
        final ConfigMapKeySelector configMapKeyRef = configMapScriptFrom.getConfigMapKeyRef();
        ConfigMap configMap = client.configMaps().inNamespace("test").withName(configMapKeyRef.getName()).get();
        assertNotNull(configMap);
        assertEquals(configMapScriptFrom.getConfigMapScript(), configMap.getData().get(configMapKeyRef.getKey()));
    }
}
Also used : ClusterScriptEntry(io.stackgres.apiweb.dto.cluster.ClusterScriptEntry) Secret(io.fabric8.kubernetes.api.model.Secret) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ClusterDto(io.stackgres.apiweb.dto.cluster.ClusterDto) Metadata(io.stackgres.apiweb.dto.Metadata) ClusterScriptFrom(io.stackgres.apiweb.dto.cluster.ClusterScriptFrom) SecretKeySelector(io.stackgres.common.crd.SecretKeySelector) ConfigMapKeySelector(io.stackgres.common.crd.ConfigMapKeySelector) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 5 with ClusterScriptEntry

use of io.stackgres.apiweb.dto.cluster.ClusterScriptEntry 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)

Aggregations

ClusterScriptEntry (io.stackgres.apiweb.dto.cluster.ClusterScriptEntry)12 ClusterScriptFrom (io.stackgres.apiweb.dto.cluster.ClusterScriptFrom)11 SecretKeySelector (io.stackgres.common.crd.SecretKeySelector)7 StackGresClusterScriptEntry (io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry)7 StackGresClusterScriptFrom (io.stackgres.common.crd.sgcluster.StackGresClusterScriptFrom)7 Test (org.junit.jupiter.api.Test)6 ConfigMapKeySelector (io.stackgres.common.crd.ConfigMapKeySelector)5 Secret (io.fabric8.kubernetes.api.model.Secret)4 Metadata (io.stackgres.apiweb.dto.Metadata)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)3 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)3 QuarkusTest (io.quarkus.test.junit.QuarkusTest)3 ClusterDto (io.stackgres.apiweb.dto.cluster.ClusterDto)3 ClusterConfiguration (io.stackgres.apiweb.dto.cluster.ClusterConfiguration)2 ClusterInitData (io.stackgres.apiweb.dto.cluster.ClusterInitData)2 ClusterPod (io.stackgres.apiweb.dto.cluster.ClusterPod)2 ClusterPodPersistentVolume (io.stackgres.apiweb.dto.cluster.ClusterPodPersistentVolume)2 ClusterSpec (io.stackgres.apiweb.dto.cluster.ClusterSpec)2 ClusterSpecLabels (io.stackgres.apiweb.dto.cluster.ClusterSpecLabels)2