From 519dee759dab0df9276fbffcd854ffe19b2fb9d2 Mon Sep 17 00:00:00 2001 From: baixinsui Date: Tue, 10 Dec 2024 16:08:42 +0800 Subject: [PATCH] Add config to avoid always loading all available versions of opentofu at boot (#90) --- .github/workflows/ci.yml | 4 ++-- Dockerfile | 2 +- .../opentofu/tool/OpenTofuVersionsCache.java | 6 ++++++ .../tool/OpenTofuVersionsFetcher.java | 2 +- src/main/resources/application.properties | 2 +- .../opentofu/tool/OpenTofuInstallerTest.java | 19 ++++++++++++++----- 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index df3f02b..ee0b76a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,8 +24,8 @@ jobs: java-version: 21 distribution: 'temurin' - name: Set up OpenTofu - uses: opentofu/setup-opentofu@v1 + uses: opentofu/setup-opentofu@v1.0.4 with: - tofu_version: 1.6.0 + tofu_version: 1.6.1 - name: Build run: mvn --batch-mode --update-snapshots --no-transfer-progress verify diff --git a/Dockerfile b/Dockerfile index 58c9c7c..6372581 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,4 +14,4 @@ RUN echo "Downloading and installing OpenTofu with multiple versions $OPENTOFU_V COPY target/tofu-maker-*.jar tofu-maker.jar USER tofu-maker -ENTRYPOINT ["java", "-Dopentofu.install.dir=${OPENTOFU_INSTALL_PATH}", "-Dopentofu.version=${OPENTOFU_VERSIONS}", "-jar", "tofu-maker.jar"] \ No newline at end of file +ENTRYPOINT ["java", "-Dopentofu.install.dir=${OPENTOFU_INSTALL_PATH}", "-Dopentofu.default.supported.versions=${OPENTOFU_VERSIONS}", "-jar", "tofu-maker.jar"] \ No newline at end of file diff --git a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsCache.java b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsCache.java index bf0e452..ff57271 100644 --- a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsCache.java +++ b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsCache.java @@ -11,6 +11,7 @@ import jakarta.annotation.Resource; import java.util.Set; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; @@ -21,6 +22,8 @@ @Component public class OpenTofuVersionsCache { + @Value("${support.default.opentofu.versions.only:true}") + private boolean getDefaultVersionsOnly; @Resource private OpenTofuVersionsFetcher versionsFetcher; @@ -31,6 +34,9 @@ public class OpenTofuVersionsCache { */ @Cacheable(value = OPENTOFU_VERSIONS_CACHE_NAME, key = "'all'") public Set getAvailableVersions() { + if (getDefaultVersionsOnly) { + return versionsFetcher.getDefaultVersionsFromConfig(); + } try { return versionsFetcher.fetchAvailableVersionsFromOpenTofuWebsite(); } catch (Exception e) { diff --git a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsFetcher.java b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsFetcher.java index 8132d58..e9079b9 100644 --- a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsFetcher.java +++ b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsFetcher.java @@ -44,7 +44,7 @@ public class OpenTofuVersionsFetcher { private String openTofuGithubApiEndpoint; @Value("${opentofu.github.repository:opentofu/opentofu}") private String openTofuGithubRepository; - @Value("${opentofu.versions}") + @Value("${opentofu.default.supported.versions}") private String defaultVersionsString; /** diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1a5ab79..765f8a5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -19,7 +19,7 @@ spring.retry.delay-millions=1000 support.default.opentofu.versions.only=true opentofu.install.dir=/opt/opentofu opentofu.download.base.url=https://github.com/opentofu/opentofu/releases -opentofu.versions=1.6.0,1.7.0,1.8.0 +opentofu.default.supported.versions=1.6.0,1.7.0,1.8.0 opentofu.github.api.endpoint=https://api.github.com opentofu.github.repository=opentofu/opentofu failed.callback.response.store.location=/tmp \ No newline at end of file diff --git a/src/test/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuInstallerTest.java b/src/test/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuInstallerTest.java index 57e9b6f..7cc92db 100644 --- a/src/test/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuInstallerTest.java +++ b/src/test/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuInstallerTest.java @@ -8,25 +8,34 @@ import java.io.File; +import java.util.Set; import org.eclipse.xpanse.tofu.maker.models.exceptions.InvalidOpenTofuToolException; import org.eclipse.xpanse.tofu.maker.opentofu.utils.SystemCmd; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; -@SpringBootTest(classes = {OpenTofuInstaller.class, - OpenTofuVersionsHelper.class, - OpenTofuVersionsCache.class, - OpenTofuVersionsFetcher.class, - SystemCmd.class}) +@SpringBootTest(classes = {OpenTofuInstaller.class, OpenTofuVersionsHelper.class, + OpenTofuVersionsCache.class, OpenTofuVersionsFetcher.class, SystemCmd.class}, + properties = {"support.default.opentofu.versions.only=false"} +) class OpenTofuInstallerTest { + @Value("${opentofu.default.supported.versions:1.6.0,1.7.0,1.8.0}") + private String opentofuVersions; @Resource private OpenTofuInstaller installer; @Resource private OpenTofuVersionsHelper versionHelper; + @Resource + private OpenTofuVersionsCache versionsCache; @Test void testGetExecutableOpenTofuByVersion() { + Set defaultVersions = Set.of(opentofuVersions.split(",")); + Set cachedVersions = versionsCache.getAvailableVersions(); + assertTrue(cachedVersions.containsAll(defaultVersions)); + assertTrue(cachedVersions.size() >= defaultVersions.size()); String requiredVersion = ""; String openTofuPath = installer.getExecutorPathThatMatchesRequiredVersion(requiredVersion);