Search in sources :

Example 26 with StackGresClusterInstalledExtension

use of io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension in project stackgres by ongres.

the class ContextUtil method toPostgresContext.

public static PostgresContainerContext toPostgresContext(StackGresClusterContainerContext context) {
    final StackGresClusterContext clusterContext = context.getClusterContext();
    final StackGresCluster cluster = clusterContext.getSource();
    ImmutablePostgresContainerContext.Builder contextBuilder = Optional.of(clusterContext.getSource()).map(StackGresCluster::getStatus).map(StackGresClusterStatus::getDbOps).map(StackGresClusterDbOpsStatus::getMajorVersionUpgrade).map(majorVersionUpgradeStatus -> {
        String targetVersion = majorVersionUpgradeStatus.getTargetPostgresVersion();
        String sourceVersion = majorVersionUpgradeStatus.getSourcePostgresVersion();
        String sourceMajorVersion = StackGresComponent.POSTGRESQL.findMajorVersion(sourceVersion);
        return ImmutablePostgresContainerContext.builder().from(context).postgresMajorVersion(StackGresComponent.POSTGRESQL.findMajorVersion(targetVersion)).oldMajorVersion(sourceMajorVersion).imageBuildMajorVersion(StackGresComponent.POSTGRESQL.findBuildMajorVersion(targetVersion)).oldImageBuildMajorVersion(StackGresComponent.POSTGRESQL.findBuildMajorVersion(sourceVersion)).postgresVersion(targetVersion).oldPostgresVersion(sourceVersion);
    }).orElseGet(() -> {
        final String postgresVersion = cluster.getSpec().getPostgres().getVersion();
        final String majorVersion = StackGresComponent.POSTGRESQL.findMajorVersion(postgresVersion);
        final String buildMajorVersion = StackGresComponent.POSTGRESQL.findBuildMajorVersion(postgresVersion);
        return ImmutablePostgresContainerContext.builder().from(context).postgresVersion(postgresVersion).imageBuildMajorVersion(buildMajorVersion).postgresMajorVersion(majorVersion);
    });
    final List<StackGresClusterInstalledExtension> installedExtensions = Optional.ofNullable(cluster.getSpec()).map(StackGresClusterSpec::getToInstallPostgresExtensions).stream().flatMap(Collection::stream).collect(Collectors.toUnmodifiableList());
    contextBuilder.addAllInstalledExtensions(installedExtensions);
    return contextBuilder.build();
}
Also used : StackGresClusterStatus(io.stackgres.common.crd.sgcluster.StackGresClusterStatus) StackGresClusterContainerContext(io.stackgres.operator.conciliation.factory.cluster.StackGresClusterContainerContext) StackGresClusterDbOpsStatus(io.stackgres.common.crd.sgcluster.StackGresClusterDbOpsStatus) StackGresDistributedLogsUtil(io.stackgres.common.StackGresDistributedLogsUtil) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) Collection(java.util.Collection) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresDistributedLogsSpec(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec) DistributedLogsContainerContext(io.stackgres.operator.conciliation.factory.distributedlogs.DistributedLogsContainerContext) Collectors(java.util.stream.Collectors) StackGresComponent(io.stackgres.common.StackGresComponent) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) List(java.util.List) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) Optional(java.util.Optional) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterDbOpsStatus(io.stackgres.common.crd.sgcluster.StackGresClusterDbOpsStatus) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension)

Example 27 with StackGresClusterInstalledExtension

use of io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension in project stackgres by ongres.

the class ExtensionManagerTest method testSetExtensionAsPending.

@Test
void testSetExtensionAsPending() throws Exception {
    StackGresCluster cluster = getCluster();
    StackGresClusterInstalledExtension extension = getInstalledExtension();
    when(webClientFactory.create(anyBoolean())).thenReturn(webClient);
    when(webClient.getJson(any(), any())).thenReturn(getExtensions());
    final String extensionPackageName = ExtensionUtil.getExtensionPackageName(getInstalledExtension());
    ExtensionInstaller extensionInstaller = extensionManager.getExtensionInstaller(context(cluster), extension);
    extensionInstaller.setExtensionAsPending();
    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(any());
    verify(fileSystemHandler, times(0)).newInputStream(any());
    verify(fileSystemHandler, times(1)).createOrReplaceFile(any());
    verify(fileSystemHandler, times(1)).createOrReplaceFile(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve(extensionPackageName + ExtensionManager.PENDING_SUFFIX)));
    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());
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) ExtensionInstaller(io.stackgres.common.extension.ExtensionManager.ExtensionInstaller) Test(org.junit.jupiter.api.Test)

Example 28 with StackGresClusterInstalledExtension

use of io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension in project stackgres by ongres.

the class ExtensionManagerTest method testCheckExtensionWillNotOverwrite.

@Test
void testCheckExtensionWillNotOverwrite() 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(false);
    StackGresClusterInstalledExtension extension = getInstalledExtension();
    Assertions.assertFalse(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(1)).newInputStream(any());
    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());
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) Test(org.junit.jupiter.api.Test)

Example 29 with StackGresClusterInstalledExtension

use of io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension in project stackgres by ongres.

the class ExtensionManagerTest method testUninstallExtension.

@Test
void testUninstallExtension() throws Exception {
    StackGresCluster cluster = getCluster();
    StackGresClusterExtension extension = getClusterExtension();
    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"));
    StackGresClusterInstalledExtension installedExtension = getInstalledExtension();
    extensionManager.getExtensionUninstaller(context(cluster), installedExtension).uninstallExtension();
    verify(webClientFactory, times(0)).create(anyBoolean());
    verify(webClient, times(0)).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.INSTALLED_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(7)).deleteIfExists(any());
    verify(fileSystemHandler, times(1)).deleteIfExists(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve("usr/share/postgresql/12/extension").resolve("test.control")));
    verify(fileSystemHandler, times(1)).deleteIfExists(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve("usr/share/postgresql/12/extension").resolve("test.sql")));
    verify(fileSystemHandler, times(1)).deleteIfExists(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve(extensionPackageName + ExtensionManager.TGZ_SUFFIX)));
    verify(fileSystemHandler, times(1)).deleteIfExists(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve(extensionPackageName + ExtensionManager.SHA256_SUFFIX)));
    verify(fileSystemHandler, times(1)).deleteIfExists(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve(extensionPackageName + ExtensionManager.PENDING_SUFFIX)));
    verify(fileSystemHandler, times(1)).deleteIfExists(eq(Paths.get(ClusterStatefulSetPath.PG_RELOCATED_LIB_PATH.path(context(cluster))).resolve(extensionPackageName + ExtensionManager.LINKS_CREATED_SUFFIX)));
    verify(fileSystemHandler, times(1)).deleteIfExists(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve(extensionPackageName + ExtensionManager.INSTALLED_SUFFIX)));
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) StackGresClusterExtension(io.stackgres.common.crd.sgcluster.StackGresClusterExtension) Test(org.junit.jupiter.api.Test)

Example 30 with StackGresClusterInstalledExtension

use of io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension in project stackgres by ongres.

the class ExtensionManagerTest method testDownloadAndExtractExtension.

@Test
void testDownloadAndExtractExtension() throws Exception {
    when(webClientFactory.create(anyBoolean())).thenReturn(webClient);
    when(webClient.getJson(any(), any())).thenReturn(getExtensions());
    when(webClient.getInputStream(any())).then(invocation -> getClass().getResourceAsStream("/test.tar"));
    StackGresCluster cluster = getCluster();
    StackGresClusterInstalledExtension extension = getInstalledExtension();
    extensionManager.getExtensionInstaller(context(cluster), extension).downloadAndExtract();
    verify(webClientFactory, times(2)).create(anyBoolean());
    verify(webClient, times(1)).getJson(any(), any());
    verify(webClient, times(1)).getJson(eq(ExtensionUtil.getIndexUri(REPOSITORY)), eq(StackGresExtensions.class));
    verify(webClient, times(1)).getInputStream(eq(ExtensionUtil.getExtensionPackageUri(REPOSITORY, extension, getExtensionMetadata())));
    verify(fileSystemHandler, times(0)).newInputStream(any());
    verify(fileSystemHandler, times(0)).createOrReplaceFile(any());
    verify(fileSystemHandler, times(2)).createDirectories(any());
    verify(fileSystemHandler, times(2)).createDirectories(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster)))));
    verify(fileSystemHandler, times(0)).createOrReplaceSymbolicLink(any(), any());
    verify(fileSystemHandler, times(2)).copyOrReplace(any(), any());
    verify(fileSystemHandler, times(1)).copyOrReplace(any(), eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve("test.tgz.sha256")));
    verify(fileSystemHandler, times(1)).copyOrReplace(any(), eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve("test.tgz")));
    verify(fileSystemHandler, times(2)).setPosixFilePermissions(any(), any());
    verify(fileSystemHandler, times(1)).setPosixFilePermissions(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve("test.tgz.sha256")), eq(ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_READ, PosixFilePermission.OTHERS_READ)));
    verify(fileSystemHandler, times(1)).setPosixFilePermissions(eq(Paths.get(ClusterStatefulSetPath.PG_EXTENSIONS_PATH.path(context(cluster))).resolve("test.tgz")), eq(ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_READ, PosixFilePermission.OTHERS_READ)));
    verify(fileSystemHandler, times(0)).deleteIfExists(any());
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) Test(org.junit.jupiter.api.Test)

Aggregations

StackGresClusterInstalledExtension (io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension)52 Test (org.junit.jupiter.api.Test)35 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)24 List (java.util.List)15 Optional (java.util.Optional)15 JsonPatchOperation (com.github.fge.jsonpatch.JsonPatchOperation)14 ImmutableList (com.google.common.collect.ImmutableList)14 StackGresClusterExtension (io.stackgres.common.crd.sgcluster.StackGresClusterExtension)14 ExtensionInstaller (io.stackgres.common.extension.ExtensionManager.ExtensionInstaller)14 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)13 StackGresClusterStatus (io.stackgres.common.crd.sgcluster.StackGresClusterStatus)13 BeforeEach (org.junit.jupiter.api.BeforeEach)13 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)12 StackGresComponent (io.stackgres.common.StackGresComponent)12 StackGresClusterPodStatus (io.stackgres.common.crd.sgcluster.StackGresClusterPodStatus)12 ExtensionUninstaller (io.stackgres.common.extension.ExtensionManager.ExtensionUninstaller)12 ArrayList (java.util.ArrayList)12 ReplaceOperation (com.github.fge.jsonpatch.ReplaceOperation)11 StackGresClusterList (io.stackgres.common.crd.sgcluster.StackGresClusterList)11 JsonUtil (io.stackgres.testutil.JsonUtil)11