use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.
the class ExtensionReconciliationTest method testReconciliationWithExtensionAlreadyInstalled_installIsSkipped.
@Test
void testReconciliationWithExtensionAlreadyInstalled_installIsSkipped() throws Exception {
StackGresClusterInstalledExtension installedExtension = getInstalledExtension();
ExtensionReconciliatorContext context = getContext(cluster -> {
cluster.getSpec().getPostgres().setExtensions(null);
cluster.getSpec().setToInstallPostgresExtensions(new ArrayList<>());
cluster.getSpec().getToInstallPostgresExtensions().add(installedExtension);
cluster.setStatus(new StackGresClusterStatus());
cluster.getStatus().setPodStatuses(new ArrayList<>());
StackGresClusterPodStatus podStatus = new StackGresClusterPodStatus();
podStatus.setName("test-0");
podStatus.setInstalledPostgresExtensions(new ArrayList<>());
podStatus.getInstalledPostgresExtensions().add(installedExtension);
cluster.getStatus().getPodStatuses().add(podStatus);
});
when(extensionManager.getExtensionInstaller(any(), any(StackGresClusterInstalledExtension.class))).thenReturn(extensionInstaller);
when(extensionInstaller.isExtensionInstalled()).thenReturn(true);
when(extensionInstaller.isLinksCreated()).thenReturn(true);
Assertions.assertFalse(reconciliator.reconcile(null, context).result().get());
Assertions.assertTrue(Optional.of(context.getCluster()).map(StackGresCluster::getStatus).map(StackGresClusterStatus::getPodStatuses).stream().flatMap(List::stream).anyMatch(podStatus -> podStatus.getName().equals("test-0")));
Assertions.assertFalse(context.getCluster().getStatus().getPodStatuses().stream().filter(podStatus -> podStatus.getName().equals("test-0")).findAny().map(StackGresClusterPodStatus::getPendingRestart).orElse(false));
Assertions.assertIterableEquals(ImmutableList.of(installedExtension), context.getCluster().getStatus().getPodStatuses().stream().filter(podStatus -> podStatus.getName().equals("test-0")).findAny().map(StackGresClusterPodStatus::getInstalledPostgresExtensions).stream().flatMap(List::stream).collect(ImmutableList.toImmutableList()));
verify(extensionInstaller, times(1)).isExtensionInstalled();
verify(extensionInstaller, times(1)).isLinksCreated();
verify(extensionUninstaller, times(0)).isExtensionInstalled();
verify(extensionInstaller, times(0)).downloadAndExtract();
verify(extensionInstaller, times(0)).verify();
verify(extensionInstaller, times(0)).installExtension();
verify(extensionInstaller, times(0)).createExtensionLinks();
verify(extensionInstaller, times(0)).createExtensionLinks();
verify(extensionInstaller, times(0)).doesInstallOverwriteAnySharedLibrary();
verify(extensionInstaller, times(0)).setExtensionAsPending();
verify(extensionUninstaller, times(0)).uninstallExtension();
}
use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.
the class ExtensionReconciliationTest method testReconciliationWithExtension_installIsPerformed.
@Test
void testReconciliationWithExtension_installIsPerformed() throws Exception {
StackGresClusterInstalledExtension installedExtension = getInstalledExtension();
ExtensionReconciliatorContext context = getContext(cluster -> {
cluster.getSpec().getPostgres().setExtensions(null);
cluster.getSpec().setToInstallPostgresExtensions(new ArrayList<>());
cluster.getSpec().getToInstallPostgresExtensions().add(installedExtension);
});
when(extensionManager.getExtensionInstaller(any(), any(StackGresClusterInstalledExtension.class))).thenReturn(extensionInstaller);
when(extensionInstaller.isExtensionInstalled()).thenReturn(false);
when(extensionInstaller.isExtensionPendingOverwrite()).thenReturn(false);
doNothing().when(eventEmitter).emitExtensionDeployed(installedExtension);
Assertions.assertTrue(reconciliator.reconcile(null, context).result().get());
Assertions.assertTrue(Optional.of(context.getCluster()).map(StackGresCluster::getStatus).map(StackGresClusterStatus::getPodStatuses).stream().flatMap(List::stream).anyMatch(podStatus -> podStatus.getName().equals("test-0")));
Assertions.assertFalse(context.getCluster().getStatus().getPodStatuses().stream().filter(podStatus -> podStatus.getName().equals("test-0")).findAny().map(StackGresClusterPodStatus::getPendingRestart).orElse(false));
Assertions.assertIterableEquals(ImmutableList.of(installedExtension), context.getCluster().getStatus().getPodStatuses().stream().filter(podStatus -> podStatus.getName().equals("test-0")).findAny().map(StackGresClusterPodStatus::getInstalledPostgresExtensions).stream().flatMap(List::stream).collect(ImmutableList.toImmutableList()));
verify(extensionInstaller, times(1)).isExtensionInstalled();
verify(extensionInstaller, times(0)).isLinksCreated();
verify(extensionUninstaller, times(0)).isExtensionInstalled();
verify(extensionInstaller, times(1)).downloadAndExtract();
verify(extensionInstaller, times(1)).verify();
verify(extensionInstaller, times(1)).installExtension();
verify(extensionInstaller, times(0)).createExtensionLinks();
verify(extensionInstaller, times(1)).doesInstallOverwriteAnySharedLibrary();
verify(extensionInstaller, times(0)).setExtensionAsPending();
verify(extensionUninstaller, times(0)).uninstallExtension();
verify(eventEmitter).emitExtensionDeployed(installedExtension);
}
use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.
the class ExtensionManagerTest method testCheckExtensionWillOverwrite.
@Test
void testCheckExtensionWillOverwrite() throws Exception {
StackGresCluster cluster = getCluster();
when(webClientFactory.create(anyBoolean())).thenReturn(webClient);
when(webClient.getJson(any(), any())).thenReturn(getExtensions());
final String extensionPackageName = ExtensionUtil.getExtensionPackageName(getInstalledExtension());
when(fileSystemHandler.newInputStream(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve(extensionPackageName + ExtensionManager.TGZ_SUFFIX)))).then(invocation -> getClass().getResourceAsStream("/test.tgz"));
when(fileSystemHandler.exists(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve("usr/lib/postgresql/12/lib").resolve("test.so")))).thenReturn(true);
StackGresClusterInstalledExtension extension = getInstalledExtension();
Assertions.assertTrue(extensionManager.getExtensionInstaller(context(cluster), extension).doesInstallOverwriteAnySharedLibrary());
verify(webClientFactory, times(1)).create(anyBoolean());
verify(webClient, times(1)).getJson(any(), any());
verify(webClient, times(1)).getJson(eq(ExtensionUtil.getIndexUri(REPOSITORY)), eq(StackGresExtensions.class));
verify(webClient, times(0)).getInputStream(eq(ExtensionUtil.getExtensionPackageUri(REPOSITORY, extension, getExtensionMetadata())));
verify(fileSystemHandler, times(0)).newInputStream(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve(extensionPackageName + ExtensionManager.SHA256_SUFFIX)));
verify(fileSystemHandler, times(1)).newInputStream(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve(extensionPackageName + ExtensionManager.TGZ_SUFFIX)));
verify(fileSystemHandler, times(0)).createOrReplaceFile(any());
verify(fileSystemHandler, times(0)).createDirectories(any());
verify(fileSystemHandler, times(0)).createOrReplaceSymbolicLink(any(), any());
verify(fileSystemHandler, times(0)).copyOrReplace(any(), any());
verify(fileSystemHandler, times(0)).setPosixFilePermissions(any(), any());
verify(fileSystemHandler, times(0)).deleteIfExists(any());
}
use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.
the class DistributedLogsClusterReconciliatorTest method getStackGresCLusterForDistributedLogs.
private StackGresCluster getStackGresCLusterForDistributedLogs(StackGresDistributedLogs distributedLogs) {
final StackGresCluster distributedLogsCluster = new StackGresCluster();
distributedLogsCluster.getMetadata().setNamespace(distributedLogs.getMetadata().getNamespace());
distributedLogsCluster.getMetadata().setName(distributedLogs.getMetadata().getName());
distributedLogsCluster.getMetadata().setUid(distributedLogs.getMetadata().getUid());
return distributedLogsCluster;
}
use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.
the class AbstractEnvoy method buildSource.
@NotNull
public HasMetadata buildSource(StackGresClusterContext context) {
final StackGresCluster stackGresCluster = context.getSource();
boolean disablePgBouncer = Optional.ofNullable(stackGresCluster.getSpec()).map(StackGresClusterSpec::getPod).map(StackGresClusterPod::getDisableConnectionPooling).orElse(false);
final String envoyConfPath = getEnvoyConfigPath(stackGresCluster, disablePgBouncer);
YAMLMapper yamlMapper = yamlMapperProvider.yamlMapper();
final ObjectNode envoyConfig;
try {
envoyConfig = (ObjectNode) yamlMapper.readTree(Envoy.class.getResource(envoyConfPath));
} catch (Exception ex) {
throw new IllegalStateException("couldn't read envoy config file", ex);
}
Seq.seq(envoyConfig.get("static_resources").get("listeners")).map(listener -> listener.get("address").get("socket_address")).cast(ObjectNode.class).forEach(socketAddress -> socketAddress.put("port_value", LISTEN_SOCKET_ADDRESS_PORT_MAPPING.get(socketAddress.get("port_value").asText())));
Seq.seq(envoyConfig.get("static_resources").get("clusters")).flatMap(cluster -> Seq.seq(cluster.get("load_assignment").get("endpoints").elements())).flatMap(endpoint -> Seq.seq(endpoint.get("lb_endpoints").elements())).map(endpoint -> endpoint.get("endpoint").get("address").get("socket_address")).cast(ObjectNode.class).forEach(socketAddress -> socketAddress.put("port_value", CLUSTER_SOCKET_ADDRESS_PORT_MAPPING.get(socketAddress.get("port_value").asText())));
final Map<String, String> data;
try {
data = ImmutableMap.of("default_envoy.yaml", yamlMapper.writeValueAsString(envoyConfig));
} catch (Exception ex) {
throw new IllegalStateException("couldn't parse envoy config file", ex);
}
String namespace = stackGresCluster.getMetadata().getNamespace();
String configMapName = AbstractEnvoy.configName(context);
return new ConfigMapBuilder().withNewMetadata().withNamespace(namespace).withName(configMapName).withLabels(labelFactory.clusterLabels(stackGresCluster)).endMetadata().withData(data).build();
}
Aggregations