Search in sources :

Example 16 with StackGresDistributedLogs

use of io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs in project stackgres by ongres.

the class DistributedLogsRequiredResourcesGeneratorTest method get095Sts.

@NotNull
private StatefulSet get095Sts(StackGresDistributedLogs distributedLogs095) {
    StatefulSet sts = JsonUtil.readFromJson("statefulset/0.9.5-distributedlogs.json", StatefulSet.class);
    final String namespace = distributedLogs095.getMetadata().getNamespace();
    sts.getMetadata().setNamespace(namespace);
    final String distributedLogsName = distributedLogs095.getMetadata().getName();
    sts.getMetadata().setName(distributedLogsName);
    final String uid = distributedLogs095.getMetadata().getUid();
    fix095Labels(sts, distributedLogs095);
    fix095Labels(sts.getSpec().getTemplate().getMetadata(), distributedLogs095);
    fix095Labels(sts.getSpec().getSelector().getMatchLabels(), distributedLogs095);
    Container patroniContainer = sts.getSpec().getTemplate().getSpec().getContainers().get(0);
    patroniContainer.getEnv().forEach(envVar -> {
        Optional.ofNullable(envVar.getValueFrom()).map(EnvVarSource::getSecretKeyRef).filter(secretKeySelector -> secretKeySelector.getName().equals("distributedlogs")).ifPresent(secretKeySelector -> secretKeySelector.setName(distributedLogsName));
        Optional.ofNullable(envVar.getValueFrom()).map(EnvVarSource::getConfigMapKeyRef).filter(configMapKeySelector -> configMapKeySelector.getName().equals("distributedlogs")).ifPresent(configMapKeySelector -> configMapKeySelector.setName(distributedLogsName));
    });
    patroniContainer.getEnvFrom().stream().map(EnvFromSource::getConfigMapRef).forEach(configMapEnvSource -> configMapEnvSource.setName(distributedLogsName));
    final List<Container> containers = sts.getSpec().getTemplate().getSpec().getContainers();
    final List<Container> initContainers = sts.getSpec().getTemplate().getSpec().getInitContainers();
    var allContainers = Stream.concat(containers.stream(), initContainers.stream());
    allContainers.forEach(container -> container.getVolumeMounts().stream().filter(volumeMount -> volumeMount.getName().startsWith("distributedlogs")).forEach(volumeMount -> {
        String name = volumeMount.getName().replaceAll("distributedlogs", distributedLogsName);
        volumeMount.setName(name);
    }));
    final List<OwnerReference> ownerReferences = List.of(new OwnerReferenceBuilder().withApiVersion(CommonDefinition.GROUP + "/v1beta1").withKind(distributedLogs095.getKind()).withName(distributedLogsName).withUid(uid).withController(true).build());
    sts.getMetadata().setOwnerReferences(ownerReferences);
    sts.getSpec().setServiceName(distributedLogsName);
    sts.getSpec().getTemplate().getSpec().setServiceAccount(distributedLogsName + "-patroni");
    sts.getSpec().getTemplate().getSpec().setServiceAccountName(distributedLogsName + "-patroni");
    sts.getSpec().getTemplate().getSpec().getVolumes().stream().filter(volume -> volume.getName().startsWith("distributedlogs")).forEach(volume -> {
        String name = volume.getName().replaceAll("distributedlogs", distributedLogsName);
        volume.setName(name);
    });
    sts.getSpec().getTemplate().getSpec().getVolumes().stream().map(Volume::getConfigMap).filter(Objects::nonNull).forEach(configMap -> {
        String configMapName = configMap.getName().replaceAll("distributedlogs", distributedLogsName);
        configMap.setName(configMapName);
    });
    sts.getSpec().getVolumeClaimTemplates().forEach(vct -> {
        fix095Labels(vct, distributedLogs095);
        vct.getMetadata().setName(distributedLogsName + "-data");
        vct.getMetadata().setNamespace(namespace);
        vct.getMetadata().setOwnerReferences(ownerReferences);
    });
    return sts;
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Container(io.fabric8.kubernetes.api.model.Container) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterList(io.stackgres.common.crd.sgcluster.StackGresClusterList) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) Seq(org.jooq.lambda.Seq) Mockito.lenient(org.mockito.Mockito.lenient) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Inject(javax.inject.Inject) StringUtils(io.stackgres.testutil.StringUtils) CharStreams(com.google.common.io.CharStreams) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) Volume(io.fabric8.kubernetes.api.model.Volume) InjectMock(io.quarkus.test.junit.mockito.InjectMock) ImmutableMap(com.google.common.collect.ImmutableMap) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) ReconciliationScope(io.stackgres.operator.conciliation.ReconciliationScope) IOException(java.io.IOException) StackGresUtil(io.stackgres.common.StackGresUtil) Reader(java.io.Reader) UUID(java.util.UUID) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StandardCharsets(java.nio.charset.StandardCharsets) DefaultComparator(io.stackgres.operator.conciliation.comparator.DefaultComparator) JsonUtil(io.stackgres.testutil.JsonUtil) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) EnvVarSource(io.fabric8.kubernetes.api.model.EnvVarSource) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Optional(java.util.Optional) Comparator(java.util.Comparator) CommonDefinition(io.stackgres.common.crd.CommonDefinition) NotNull(org.jetbrains.annotations.NotNull) InputStream(java.io.InputStream) CustomResource(io.fabric8.kubernetes.client.CustomResource) Container(io.fabric8.kubernetes.api.model.Container) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) EnvVarSource(io.fabric8.kubernetes.api.model.EnvVarSource) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with StackGresDistributedLogs

use of io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs in project stackgres by ongres.

the class DistributedLogsRequiredResourcesGeneratorTest method givenADistributedLogsIn095_shouldGenerateATemplatesConfigmapCompatibleWithThatVersion.

@Test
void givenADistributedLogsIn095_shouldGenerateATemplatesConfigmapCompatibleWithThatVersion() {
    StackGresDistributedLogs distributedLogs = get095Cluster();
    ConfigMap expectedConfigMap = get095TemplatesConfigMap(distributedLogs);
    ConfigMap generatedConfigMap = generator.getRequiredResources(distributedLogs).stream().filter(r -> r.getKind().equals("ConfigMap")).filter(r -> r.getMetadata().getName().equals(expectedConfigMap.getMetadata().getName())).map(r -> (ConfigMap) r).findFirst().orElseThrow();
    assertTrue(configMapComparator.isTheSameResource(generatedConfigMap, expectedConfigMap));
    assertTrue(configMapComparator.isResourceContentEqual(generatedConfigMap, expectedConfigMap));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Container(io.fabric8.kubernetes.api.model.Container) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterList(io.stackgres.common.crd.sgcluster.StackGresClusterList) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) Seq(org.jooq.lambda.Seq) Mockito.lenient(org.mockito.Mockito.lenient) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Inject(javax.inject.Inject) StringUtils(io.stackgres.testutil.StringUtils) CharStreams(com.google.common.io.CharStreams) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) Volume(io.fabric8.kubernetes.api.model.Volume) InjectMock(io.quarkus.test.junit.mockito.InjectMock) ImmutableMap(com.google.common.collect.ImmutableMap) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) ReconciliationScope(io.stackgres.operator.conciliation.ReconciliationScope) IOException(java.io.IOException) StackGresUtil(io.stackgres.common.StackGresUtil) Reader(java.io.Reader) UUID(java.util.UUID) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StandardCharsets(java.nio.charset.StandardCharsets) DefaultComparator(io.stackgres.operator.conciliation.comparator.DefaultComparator) JsonUtil(io.stackgres.testutil.JsonUtil) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) EnvVarSource(io.fabric8.kubernetes.api.model.EnvVarSource) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Optional(java.util.Optional) Comparator(java.util.Comparator) CommonDefinition(io.stackgres.common.crd.CommonDefinition) NotNull(org.jetbrains.annotations.NotNull) InputStream(java.io.InputStream) CustomResource(io.fabric8.kubernetes.client.CustomResource) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 18 with StackGresDistributedLogs

use of io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs in project stackgres by ongres.

the class DistributedLogsRequiredResourcesGeneratorTest method givenADistributedLogsIn095_shouldGenerateAPatroniConfigMapCompatibleWithThatVersion.

@Test
void givenADistributedLogsIn095_shouldGenerateAPatroniConfigMapCompatibleWithThatVersion() {
    StackGresDistributedLogs distributedLogs = get095Cluster();
    ConfigMap expectedPatroniConfigMap = get095PatroniConfigMap(distributedLogs);
    ConfigMap generatedConfigMap = generator.getRequiredResources(distributedLogs).stream().filter(r -> r.getKind().equals("ConfigMap")).filter(r -> r.getMetadata().getName().equals(expectedPatroniConfigMap.getMetadata().getName())).map(r -> (ConfigMap) r).findFirst().orElseThrow();
    assertTrue(configMapComparator.isTheSameResource(generatedConfigMap, expectedPatroniConfigMap));
    assertTrue(configMapComparator.isResourceContentEqual(generatedConfigMap, expectedPatroniConfigMap));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Container(io.fabric8.kubernetes.api.model.Container) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterList(io.stackgres.common.crd.sgcluster.StackGresClusterList) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) Seq(org.jooq.lambda.Seq) Mockito.lenient(org.mockito.Mockito.lenient) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Inject(javax.inject.Inject) StringUtils(io.stackgres.testutil.StringUtils) CharStreams(com.google.common.io.CharStreams) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) Volume(io.fabric8.kubernetes.api.model.Volume) InjectMock(io.quarkus.test.junit.mockito.InjectMock) ImmutableMap(com.google.common.collect.ImmutableMap) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) ReconciliationScope(io.stackgres.operator.conciliation.ReconciliationScope) IOException(java.io.IOException) StackGresUtil(io.stackgres.common.StackGresUtil) Reader(java.io.Reader) UUID(java.util.UUID) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StandardCharsets(java.nio.charset.StandardCharsets) DefaultComparator(io.stackgres.operator.conciliation.comparator.DefaultComparator) JsonUtil(io.stackgres.testutil.JsonUtil) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) EnvVarSource(io.fabric8.kubernetes.api.model.EnvVarSource) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Optional(java.util.Optional) Comparator(java.util.Comparator) CommonDefinition(io.stackgres.common.crd.CommonDefinition) NotNull(org.jetbrains.annotations.NotNull) InputStream(java.io.InputStream) CustomResource(io.fabric8.kubernetes.client.CustomResource) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 19 with StackGresDistributedLogs

use of io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs in project stackgres by ongres.

the class DistributedLogsConciliatorTest method shouldNotIgnoreChangesMarkedPauseUntilRestartAnnotationIfClusterIsNotPendingToRestart.

@Test
@DisplayName("Conciliation Should Not Ignore Changes On Resources Marked With Reconciliation " + "Pause Until Restart Annotation If The Cluster Is Not Pending To Restart")
void shouldNotIgnoreChangesMarkedPauseUntilRestartAnnotationIfClusterIsNotPendingToRestart() {
    final List<HasMetadata> requiredResources = KubernetessMockResourceGenerationUtil.buildResources("test", "test");
    final List<HasMetadata> deployedResources = deepCopy(requiredResources);
    deployedResources.stream().findAny().orElseThrow().getMetadata().setAnnotations(Map.of(StackGresContext.RECONCILIATION_PAUSE_UNTIL_RESTART_KEY, Boolean.TRUE.toString()));
    Conciliator<StackGresDistributedLogs> conciliator = buildConciliator(requiredResources, deployedResources);
    when(statusManager.isPendingRestart(distributedLogs)).thenReturn(false);
    ReconciliationResult result = conciliator.evalReconciliationState(distributedLogs);
    assertEquals(1, result.getPatches().size());
    assertFalse(result.isUpToDate());
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) ReconciliationResult(io.stackgres.operator.conciliation.ReconciliationResult) ConciliatorTest(io.stackgres.operator.conciliation.ConciliatorTest) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 20 with StackGresDistributedLogs

use of io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs in project stackgres by ongres.

the class DistributedLogsConciliatorTest method shouldIgnoreDeletionsMarkedWithPauseUntilRestartAnnotationIfTheClusterIsPendingToRestart.

@Test
@DisplayName("Conciliation Should Ignore Deletions On Resources Marked With Reconciliation " + "Pause Until Restart Annotation If The Cluster Is Pending To Restart")
void shouldIgnoreDeletionsMarkedWithPauseUntilRestartAnnotationIfTheClusterIsPendingToRestart() {
    final List<HasMetadata> requiredResources = KubernetessMockResourceGenerationUtil.buildResources("test", "test");
    final List<HasMetadata> deployedResources = new ArrayList<>(requiredResources);
    int indexToRemove = new Random().nextInt(requiredResources.size());
    deployedResources.get(indexToRemove).getMetadata().setAnnotations(Map.of(StackGresContext.RECONCILIATION_PAUSE_UNTIL_RESTART_KEY, Boolean.TRUE.toString()));
    requiredResources.remove(indexToRemove);
    Conciliator<StackGresDistributedLogs> conciliator = buildConciliator(requiredResources, deployedResources);
    reset(statusManager);
    when(statusManager.isPendingRestart(distributedLogs)).thenReturn(true);
    ReconciliationResult result = conciliator.evalReconciliationState(getConciliationResource());
    assertEquals(0, result.getDeletions().size());
    assertTrue(result.isUpToDate());
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Random(java.util.Random) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) ReconciliationResult(io.stackgres.operator.conciliation.ReconciliationResult) ArrayList(java.util.ArrayList) ConciliatorTest(io.stackgres.operator.conciliation.ConciliatorTest) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

StackGresDistributedLogs (io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs)48 NotNull (org.jetbrains.annotations.NotNull)14 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)13 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)12 Inject (javax.inject.Inject)11 Test (org.junit.jupiter.api.Test)11 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)10 List (java.util.List)9 Map (java.util.Map)9 Volume (io.fabric8.kubernetes.api.model.Volume)8 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)8 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)7 Container (io.fabric8.kubernetes.api.model.Container)7 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)7 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)7 StackGresUtil (io.stackgres.common.StackGresUtil)7 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ImmutableMap (com.google.common.collect.ImmutableMap)6