Search in sources :

Example 1 with ClusterDto

use of io.stackgres.apiweb.dto.cluster.ClusterDto 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 2 with ClusterDto

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

the class ClusterResourceQuarkusTest method givenACreationWithInlineScripts_shouldNotFail.

@Test
void givenACreationWithInlineScripts_shouldNotFail() {
    ClusterDto cluster = getClusterInlineScripts();
    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);
}
Also used : ClusterDto(io.stackgres.apiweb.dto.cluster.ClusterDto) Metadata(io.stackgres.apiweb.dto.Metadata) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 3 with ClusterDto

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

the class NamespacedClusterLogsResource method logs.

/**
 * Query distributed logs and return a list of {@code ClusterLogEntry}.
 */
@Operation(responses = { @ApiResponse(responseCode = "200", description = "OK", content = { @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = ClusterLogEntryDto.class))) }) })
@CommonApiResponses
@GET
@Path("{name}/logs")
public List<ClusterLogEntryDto> logs(@PathParam("namespace") String namespace, @PathParam("name") String name, @QueryParam("records") Integer records, @QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("sort") String sort, @QueryParam("text") String text, @QueryParam("logType") List<String> logType, @QueryParam("podName") List<String> podName, @QueryParam("role") List<String> role, @QueryParam("errorLevel") List<String> errorLevel, @QueryParam("userName") List<String> userName, @QueryParam("databaseName") List<String> databaseName, @QueryParam("fromInclusive") Boolean fromInclusive) {
    final ClusterDto cluster = clusterFinder.findByNameAndNamespace(name, namespace).orElseThrow(NotFoundException::new);
    final int calculatedRecords = records != null ? records : 50;
    if (calculatedRecords <= 0) {
        throw new BadRequestException("records should be a positive number");
    }
    final Optional<Tuple2<Instant, Integer>> fromTuple;
    final Optional<Tuple2<Instant, Integer>> toTuple;
    if (!Optional.ofNullable(cluster.getSpec()).map(ClusterSpec::getDistributedLogs).map(ClusterDistributedLogs::getDistributedLogs).isPresent()) {
        throw new BadRequestException("Distributed logs are not configured for specified cluster");
    }
    final var filters = ImmutableMap.<String, ImmutableList<String>>builder();
    addFilter("logType", logType, filters);
    addFilter("podName", podName, filters);
    addFilter("role", role, filters);
    addFilter("errorLevel", errorLevel, filters);
    addFilter("userName", userName, filters);
    addFilter("databaseName", databaseName, filters);
    try {
        fromTuple = Optional.ofNullable(from).map(s -> s.split(",")).map(ss -> Tuple.tuple(ss[0], ss.length > 1 ? ss[1] : String.valueOf(Integer.valueOf(0)))).map(t -> t.map1(Instant::parse)).map(t -> t.map2(Integer::valueOf));
    } catch (Exception ex) {
        throw new BadRequestException("from should be a timestamp" + " or a timestamp and an index separated by character ','", ex);
    }
    try {
        toTuple = Optional.ofNullable(to).map(s -> s.split(",")).map(ss -> Tuple.tuple(ss[0], ss.length > 1 ? ss[1] : String.valueOf(Integer.MAX_VALUE))).map(t -> t.map1(Instant::parse)).map(t -> t.map2(Integer::valueOf));
    } catch (Exception ex) {
        throw new BadRequestException("to should be a timestamp" + " or a timestamp and an index separated by character ','", ex);
    }
    if (sort != null && !sort.equals("asc") && !sort.equals("desc")) {
        throw new BadRequestException("sort only accept asc or desc values");
    }
    DistributedLogsQueryParameters logs = ImmutableDistributedLogsQueryParameters.builder().cluster(cluster).records(calculatedRecords).fromTimeAndIndex(fromTuple).toTimeAndIndex(toTuple).filters(filters.build()).isSortAsc(Objects.equals("asc", sort)).fullTextSearchQuery(Optional.ofNullable(text).map(FullTextSearchQuery::new)).isFromInclusive(fromInclusive != null && fromInclusive).build();
    return distributedLogsFetcher.logs(logs);
}
Also used : Authenticated(io.quarkus.security.Authenticated) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) DistributedLogsFetcher(io.stackgres.apiweb.distributedlogs.DistributedLogsFetcher) Path(javax.ws.rs.Path) FullTextSearchQuery(io.stackgres.apiweb.distributedlogs.FullTextSearchQuery) ClusterLogEntryDto(io.stackgres.apiweb.dto.cluster.ClusterLogEntryDto) Builder(com.google.common.collect.ImmutableMap.Builder) Inject(javax.inject.Inject) ImmutableDistributedLogsQueryParameters(io.stackgres.apiweb.distributedlogs.ImmutableDistributedLogsQueryParameters) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) Tuple2(org.jooq.lambda.tuple.Tuple2) QueryParam(javax.ws.rs.QueryParam) ImmutableList(com.google.common.collect.ImmutableList) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) BadRequestException(javax.ws.rs.BadRequestException) CommonApiResponses(io.stackgres.apiweb.rest.utils.CommonApiResponses) ClusterDistributedLogs(io.stackgres.apiweb.dto.cluster.ClusterDistributedLogs) Schema(io.swagger.v3.oas.annotations.media.Schema) ClusterSpec(io.stackgres.apiweb.dto.cluster.ClusterSpec) ImmutableMap(com.google.common.collect.ImmutableMap) ClusterDto(io.stackgres.apiweb.dto.cluster.ClusterDto) Instant(java.time.Instant) NotFoundException(javax.ws.rs.NotFoundException) Objects(java.util.Objects) CdiUtil(io.stackgres.common.CdiUtil) List(java.util.List) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) Tuple(org.jooq.lambda.tuple.Tuple) RequestScoped(javax.enterprise.context.RequestScoped) Optional(java.util.Optional) DistributedLogsQueryParameters(io.stackgres.apiweb.distributedlogs.DistributedLogsQueryParameters) ClusterDistributedLogs(io.stackgres.apiweb.dto.cluster.ClusterDistributedLogs) ClusterDto(io.stackgres.apiweb.dto.cluster.ClusterDto) ImmutableList(com.google.common.collect.ImmutableList) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) ImmutableDistributedLogsQueryParameters(io.stackgres.apiweb.distributedlogs.ImmutableDistributedLogsQueryParameters) DistributedLogsQueryParameters(io.stackgres.apiweb.distributedlogs.DistributedLogsQueryParameters) Tuple2(org.jooq.lambda.tuple.Tuple2) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) CommonApiResponses(io.stackgres.apiweb.rest.utils.CommonApiResponses) Operation(io.swagger.v3.oas.annotations.Operation)

Example 4 with ClusterDto

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

the class ClusterResourceQuarkusTest method givenACreationWithConfigMapsScripts_shouldNotFail.

@Test
void givenACreationWithConfigMapsScripts_shouldNotFail() {
    ClusterDto cluster = getClusterInlineScripts();
    ClusterScriptEntry entry = getConfigMapScriptEntry();
    cluster.getSpec().getInitData().setScripts(Collections.singletonList(entry));
    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()) {
        ConfigMap configMap = client.configMaps().inNamespace("test").withName(entry.getScriptFrom().getConfigMapKeyRef().getName()).get();
        assertNotNull(configMap);
        String actualConfigScript = configMap.getData().get(entry.getScriptFrom().getConfigMapKeyRef().getKey());
        assertEquals(entry.getScriptFrom().getConfigMapScript(), actualConfigScript);
    }
}
Also used : ClusterScriptEntry(io.stackgres.apiweb.dto.cluster.ClusterScriptEntry) 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) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 5 with ClusterDto

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

the class ClusterResourceQuarkusTest method givenACreationWithSecretScripts_shouldNotFail.

@Test
void givenACreationWithSecretScripts_shouldNotFail() {
    ClusterDto cluster = getClusterInlineScripts();
    ClusterScriptEntry entry = getSecretScriptEntry();
    cluster.getSpec().getInitData().setScripts(Collections.singletonList(entry));
    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 scriptFrom = entry.getScriptFrom();
        final SecretKeySelector secretKeyRef = scriptFrom.getSecretKeyRef();
        Secret secret = client.secrets().inNamespace("test").withName(secretKeyRef.getName()).get();
        assertNotNull(secret);
        byte[] actualScript = Base64.getDecoder().decode(secret.getData().get(secretKeyRef.getKey()));
        assertEquals(scriptFrom.getSecretScript(), new String(actualScript, StandardCharsets.UTF_8));
    }
}
Also used : ClusterScriptEntry(io.stackgres.apiweb.dto.cluster.ClusterScriptEntry) Secret(io.fabric8.kubernetes.api.model.Secret) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) 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) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

ClusterDto (io.stackgres.apiweb.dto.cluster.ClusterDto)7 QuarkusTest (io.quarkus.test.junit.QuarkusTest)4 Metadata (io.stackgres.apiweb.dto.Metadata)4 ClusterScriptEntry (io.stackgres.apiweb.dto.cluster.ClusterScriptEntry)4 Test (org.junit.jupiter.api.Test)4 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)3 ClusterScriptFrom (io.stackgres.apiweb.dto.cluster.ClusterScriptFrom)3 ImmutableList (com.google.common.collect.ImmutableList)2 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 Secret (io.fabric8.kubernetes.api.model.Secret)2 ClusterDistributedLogs (io.stackgres.apiweb.dto.cluster.ClusterDistributedLogs)2 ClusterSpec (io.stackgres.apiweb.dto.cluster.ClusterSpec)2 CdiUtil (io.stackgres.common.CdiUtil)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Builder (com.google.common.collect.ImmutableMap.Builder)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 Authenticated (io.quarkus.security.Authenticated)1 PostgresService (io.stackgres.apiweb.app.postgres.service.PostgresService)1 WebApiProperty (io.stackgres.apiweb.config.WebApiProperty)1 DistributedLogsFetcher (io.stackgres.apiweb.distributedlogs.DistributedLogsFetcher)1