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;
}
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));
}
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));
}
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());
}
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());
}
Aggregations