From f422f8871db3f25cb457c04c4445505f0488e411 Mon Sep 17 00:00:00 2001 From: Simon Flandergan Date: Mon, 23 Jul 2018 15:21:12 +0200 Subject: [PATCH 1/2] fixed typo in strategy name --- .../bumbershoot/config/service/ServiceConfiguration.java | 6 +++--- ...Stragey.java => LatestUmbrellaChartUpgradeStrategy.java} | 4 ++-- ...est.java => LatestUmbrellaChartUpgradeStrategyTest.java} | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) rename src/main/java/com/deviceinsight/bumbershoot/service/{LatestUmbrellaChartUpgradeStragey.java => LatestUmbrellaChartUpgradeStrategy.java} (95%) rename src/test/java/com/deviceinsight/bumbershoot/service/{LatestUmbrellaChartUpgradeStrageyTest.java => LatestUmbrellaChartUpgradeStrategyTest.java} (98%) diff --git a/src/main/java/com/deviceinsight/bumbershoot/config/service/ServiceConfiguration.java b/src/main/java/com/deviceinsight/bumbershoot/config/service/ServiceConfiguration.java index d6907ed..8f99f5e 100644 --- a/src/main/java/com/deviceinsight/bumbershoot/config/service/ServiceConfiguration.java +++ b/src/main/java/com/deviceinsight/bumbershoot/config/service/ServiceConfiguration.java @@ -6,7 +6,7 @@ import com.deviceinsight.bumbershoot.service.ChartArchiveModifier; import com.deviceinsight.bumbershoot.service.ReleaseUpgradeCheckService; import com.deviceinsight.bumbershoot.service.ReleaseUpgradePerformService; -import com.deviceinsight.bumbershoot.service.LatestUmbrellaChartUpgradeStragey; +import com.deviceinsight.bumbershoot.service.LatestUmbrellaChartUpgradeStrategy; import com.deviceinsight.bumbershoot.service.SubChartUpgradeStrategy; import java.util.List; @@ -33,10 +33,10 @@ public ReleaseUpgradeCheckService chartUpgradeCheckService(BumbershootConfigurat } @Bean - public LatestUmbrellaChartUpgradeStragey latestUmbrellaChartUpgradeStragey( + public LatestUmbrellaChartUpgradeStrategy latestUmbrellaChartUpgradeStragey( ChartRepositoryClientFactory chartRepositoryClientFactory) { - return new LatestUmbrellaChartUpgradeStragey(chartRepositoryClientFactory, chartArchiveModifier()); + return new LatestUmbrellaChartUpgradeStrategy(chartRepositoryClientFactory, chartArchiveModifier()); } @Bean diff --git a/src/main/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStragey.java b/src/main/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategy.java similarity index 95% rename from src/main/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStragey.java rename to src/main/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategy.java index 391d117..b5a477b 100644 --- a/src/main/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStragey.java +++ b/src/main/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategy.java @@ -24,11 +24,11 @@ * is deployed. */ @Slf4j -public class LatestUmbrellaChartUpgradeStragey extends SubChartUpgradeStrategy { +public class LatestUmbrellaChartUpgradeStrategy extends SubChartUpgradeStrategy { private final ChartArchiveModifier archiveModifier; - public LatestUmbrellaChartUpgradeStragey(ChartRepositoryClientFactory chartRepositoryClientFactory, + public LatestUmbrellaChartUpgradeStrategy(ChartRepositoryClientFactory chartRepositoryClientFactory, ChartArchiveModifier archiveModifier) { super(chartRepositoryClientFactory); diff --git a/src/test/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrageyTest.java b/src/test/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategyTest.java similarity index 98% rename from src/test/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrageyTest.java rename to src/test/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategyTest.java index a46c352..2ef5e33 100644 --- a/src/test/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrageyTest.java +++ b/src/test/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategyTest.java @@ -33,7 +33,7 @@ import com.github.zafarkhaja.semver.Version; @RunWith(MockitoJUnitRunner.Silent.class) -public class LatestUmbrellaChartUpgradeStrageyTest { +public class LatestUmbrellaChartUpgradeStrategyTest { @Mock private Release release; @@ -59,7 +59,7 @@ public class LatestUmbrellaChartUpgradeStrageyTest { private ChartRepositoryClientFactory clientFactory; @InjectMocks - private LatestUmbrellaChartUpgradeStragey strategy; + private LatestUmbrellaChartUpgradeStrategy strategy; @Before public void mockBehavior() throws MalformedURLException, UnsupportedChartRepositoryType { From 209f1814cc0a3cf84f369f58715d538891711263 Mon Sep 17 00:00:00 2001 From: Simon Flandergan Date: Mon, 23 Jul 2018 16:23:52 +0200 Subject: [PATCH 2/2] make latest upgrade strategy upgrade snapshots --- .../config/service/ServiceConfiguration.java | 4 +- .../service/ReleaseUpgradePerformService.java | 2 + .../LatestUmbrellaChartUpgradeStrategy.java | 73 ++++++++++++------- .../SubChartUpgradeStrategy.java | 12 ++- ...atestUmbrellaChartUpgradeStrategyTest.java | 43 ++++++++++- .../ReleaseUpgradePerformServiceTest.java | 2 + 6 files changed, 100 insertions(+), 36 deletions(-) rename src/main/java/com/deviceinsight/bumbershoot/service/{ => strategy}/LatestUmbrellaChartUpgradeStrategy.java (69%) rename src/main/java/com/deviceinsight/bumbershoot/service/{ => strategy}/SubChartUpgradeStrategy.java (91%) diff --git a/src/main/java/com/deviceinsight/bumbershoot/config/service/ServiceConfiguration.java b/src/main/java/com/deviceinsight/bumbershoot/config/service/ServiceConfiguration.java index 8f99f5e..baa4f32 100644 --- a/src/main/java/com/deviceinsight/bumbershoot/config/service/ServiceConfiguration.java +++ b/src/main/java/com/deviceinsight/bumbershoot/config/service/ServiceConfiguration.java @@ -6,8 +6,8 @@ import com.deviceinsight.bumbershoot.service.ChartArchiveModifier; import com.deviceinsight.bumbershoot.service.ReleaseUpgradeCheckService; import com.deviceinsight.bumbershoot.service.ReleaseUpgradePerformService; -import com.deviceinsight.bumbershoot.service.LatestUmbrellaChartUpgradeStrategy; -import com.deviceinsight.bumbershoot.service.SubChartUpgradeStrategy; +import com.deviceinsight.bumbershoot.service.strategy.LatestUmbrellaChartUpgradeStrategy; +import com.deviceinsight.bumbershoot.service.strategy.SubChartUpgradeStrategy; import java.util.List; diff --git a/src/main/java/com/deviceinsight/bumbershoot/service/ReleaseUpgradePerformService.java b/src/main/java/com/deviceinsight/bumbershoot/service/ReleaseUpgradePerformService.java index e27f96f..5e980be 100644 --- a/src/main/java/com/deviceinsight/bumbershoot/service/ReleaseUpgradePerformService.java +++ b/src/main/java/com/deviceinsight/bumbershoot/service/ReleaseUpgradePerformService.java @@ -23,6 +23,8 @@ import com.deviceinsight.bumbershoot.model.tiller.Chart; import com.deviceinsight.bumbershoot.model.tiller.ChartUpgrade; import com.deviceinsight.bumbershoot.model.tiller.Release; +import com.deviceinsight.bumbershoot.service.strategy.SubChartUpgradeStrategy; + import com.github.zafarkhaja.semver.Version; import feign.FeignException; diff --git a/src/main/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategy.java b/src/main/java/com/deviceinsight/bumbershoot/service/strategy/LatestUmbrellaChartUpgradeStrategy.java similarity index 69% rename from src/main/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategy.java rename to src/main/java/com/deviceinsight/bumbershoot/service/strategy/LatestUmbrellaChartUpgradeStrategy.java index b5a477b..e81dfa1 100644 --- a/src/main/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategy.java +++ b/src/main/java/com/deviceinsight/bumbershoot/service/strategy/LatestUmbrellaChartUpgradeStrategy.java @@ -1,4 +1,4 @@ -package com.deviceinsight.bumbershoot.service; +package com.deviceinsight.bumbershoot.service.strategy; import java.io.File; import java.util.Collection; @@ -15,32 +15,36 @@ import com.deviceinsight.bumbershoot.model.ChartUpdateNotification; import com.deviceinsight.bumbershoot.model.tiller.ChartMetaData; import com.deviceinsight.bumbershoot.model.tiller.Release; +import com.deviceinsight.bumbershoot.service.ChartArchiveModifier; + import com.github.zafarkhaja.semver.Version; import lombok.extern.slf4j.Slf4j; /** - * Strategy upgrading releases, when latest minor version of an umbrella chart - * is deployed. + * Strategy upgrading releases, when latest version of an umbrella chart is deployed. */ @Slf4j public class LatestUmbrellaChartUpgradeStrategy extends SubChartUpgradeStrategy { - private final ChartArchiveModifier archiveModifier; + private static final String SNAPSHOT_VERSION = "SNAPSHOT"; public LatestUmbrellaChartUpgradeStrategy(ChartRepositoryClientFactory chartRepositoryClientFactory, ChartArchiveModifier archiveModifier) { - super(chartRepositoryClientFactory); - this.archiveModifier = archiveModifier; + super(chartRepositoryClientFactory, archiveModifier); } @Override public boolean canUpgradeUmbrellaChart(Version currentVersion, Collection availableVersions) { - var highestVersion = getHighestMajorVersion(currentVersion, availableVersions); - return highestVersion - .map(v -> v.lessThanOrEqualTo(currentVersion)) - .orElse(true); + var highestVersion = getHighestCompatibleToMajorVersion(currentVersion, availableVersions); + + if (highestVersion.isPresent()) { + return currentVersion.greaterThanOrEqualTo(highestVersion.get()) + || isCompatibleSnapshotTo(currentVersion, highestVersion.get()); + } + + return true; } @Override @@ -49,6 +53,12 @@ public void upgradeUmbrellaChart(Release release, ChartRepository umbrellaChartR throws ChartUpgradeException { var umbrellaChart = release.getChart().getMetaData(); + Version currentVersion = Version.valueOf(umbrellaChart.getVersion()); + var highestVersion = getHighestCompatibleToMajorVersion(currentVersion, availableVersions); + if (highestVersion.isPresent() && highestVersion.get().greaterThan(currentVersion)) { + umbrellaChart.setVersion(highestVersion.get().toString()); + } + File umbrellaArchive; try { umbrellaArchive = downloadChart(umbrellaChart, umbrellaChartRepository); @@ -69,28 +79,14 @@ public void upgradeUmbrellaChart(Release release, ChartRepository umbrellaChartR } } - - private ChartMetaData modifyUmbrellaChart(Collection availableVersions, ChartMetaData umbrellaChart, - File umbrellaArchive, - ChartMetaData newChartMetaData, File newChartArchive) throws ChartInvalidException { - - archiveModifier.updateChartDependency(umbrellaChart, newChartMetaData, umbrellaArchive, newChartArchive); - Version newVersion = buildNextVersion(Version.valueOf(umbrellaChart.getVersion()), availableVersions); - archiveModifier.setChartVersion(umbrellaChart, umbrellaArchive, newVersion); - - return umbrellaChart.toBuilder() - .version(newVersion.toString()) - .build(); - - } - + private Version buildNextVersion(Version currentVersion, Collection availableVersions) { - var nextVersion = getHighestMajorVersion(currentVersion, availableVersions) + var nextVersion = getHighestCompatibleToMajorVersion(currentVersion, availableVersions) .orElse(currentVersion); if (nextVersion.getPreReleaseVersion().isEmpty()) { nextVersion = nextVersion.incrementMinorVersion(); - nextVersion = nextVersion.setPreReleaseVersion("SNAPSHOT"); + nextVersion = nextVersion.setPreReleaseVersion(SNAPSHOT_VERSION); } if (nextVersion.getPreReleaseVersion().contains("bumbershoot")) { @@ -102,7 +98,7 @@ private Version buildNextVersion(Version currentVersion, Collection ava return nextVersion; } - private Optional getHighestMajorVersion(Version currentVersion, Collection availableVersions) { + private Optional getHighestCompatibleToMajorVersion(Version currentVersion, Collection availableVersions) { var compatibleWithExpression = String.format("%d.x", currentVersion.getMajorVersion()); return availableVersions.stream() @@ -110,4 +106,25 @@ private Optional getHighestMajorVersion(Version currentVersion, Collect .max(Comparator.naturalOrder()); } + private boolean isCompatibleSnapshotTo(Version currentVersion, Version latestVersion) { + return currentVersion.getPreReleaseVersion().toUpperCase().startsWith(SNAPSHOT_VERSION) + && latestVersion.getPreReleaseVersion().toUpperCase().startsWith(SNAPSHOT_VERSION) + && Version.valueOf(currentVersion.getNormalVersion()).equals( + Version.valueOf(latestVersion.getNormalVersion())); + } + + private ChartMetaData modifyUmbrellaChart(Collection availableVersions, ChartMetaData umbrellaChart, + File umbrellaArchive, + ChartMetaData newChartMetaData, File newChartArchive) throws ChartInvalidException { + + archiveModifier.updateChartDependency(umbrellaChart, newChartMetaData, umbrellaArchive, newChartArchive); + Version newVersion = buildNextVersion(Version.valueOf(umbrellaChart.getVersion()), availableVersions); + archiveModifier.setChartVersion(umbrellaChart, umbrellaArchive, newVersion); + + return umbrellaChart.toBuilder() + .version(newVersion.toString()) + .build(); + + } + } diff --git a/src/main/java/com/deviceinsight/bumbershoot/service/SubChartUpgradeStrategy.java b/src/main/java/com/deviceinsight/bumbershoot/service/strategy/SubChartUpgradeStrategy.java similarity index 91% rename from src/main/java/com/deviceinsight/bumbershoot/service/SubChartUpgradeStrategy.java rename to src/main/java/com/deviceinsight/bumbershoot/service/strategy/SubChartUpgradeStrategy.java index 29b29cf..ca977b5 100644 --- a/src/main/java/com/deviceinsight/bumbershoot/service/SubChartUpgradeStrategy.java +++ b/src/main/java/com/deviceinsight/bumbershoot/service/strategy/SubChartUpgradeStrategy.java @@ -1,4 +1,4 @@ -package com.deviceinsight.bumbershoot.service; +package com.deviceinsight.bumbershoot.service.strategy; import java.io.File; import java.io.FileOutputStream; @@ -18,6 +18,8 @@ import com.deviceinsight.bumbershoot.model.ChartUpdateNotification; import com.deviceinsight.bumbershoot.model.tiller.ChartMetaData; import com.deviceinsight.bumbershoot.model.tiller.Release; +import com.deviceinsight.bumbershoot.service.ChartArchiveModifier; + import com.github.zafarkhaja.semver.Version; import lombok.extern.slf4j.Slf4j; @@ -26,9 +28,13 @@ public abstract class SubChartUpgradeStrategy { private final ChartRepositoryClientFactory chartRepositoryClientFactory; + protected final ChartArchiveModifier archiveModifier; - public SubChartUpgradeStrategy(ChartRepositoryClientFactory chartRepositoryClientFactory) { + public SubChartUpgradeStrategy(ChartRepositoryClientFactory chartRepositoryClientFactory, + ChartArchiveModifier archiveModifier) { + this.chartRepositoryClientFactory = chartRepositoryClientFactory; + this.archiveModifier = archiveModifier; } public abstract boolean canUpgradeUmbrellaChart(Version currentVersion, Collection availableVersions); @@ -83,7 +89,7 @@ private File generateTempFileForChartArchive(ChartMetaData chartMetaData) throws @FunctionalInterface - static interface ChartDeployFunction { + public static interface ChartDeployFunction { void deployChart(String chartUrl, Release release) throws ChartUpgradeException; diff --git a/src/test/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategyTest.java b/src/test/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategyTest.java index 2ef5e33..ffab97f 100644 --- a/src/test/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategyTest.java +++ b/src/test/java/com/deviceinsight/bumbershoot/service/LatestUmbrellaChartUpgradeStrategyTest.java @@ -30,6 +30,8 @@ import com.deviceinsight.bumbershoot.model.tiller.Chart; import com.deviceinsight.bumbershoot.model.tiller.ChartMetaData; import com.deviceinsight.bumbershoot.model.tiller.Release; +import com.deviceinsight.bumbershoot.service.strategy.LatestUmbrellaChartUpgradeStrategy; + import com.github.zafarkhaja.semver.Version; @RunWith(MockitoJUnitRunner.Silent.class) @@ -107,7 +109,19 @@ public void test_check_can_upgrade_umbrella_chart_should_return_false_if_there_i } @Test - public void test_check_can_upgrade_umbrella_chart_should_return_false_if_there_is_newer_snapshot() { + public void test_check_can_upgrade_umbrella_chart_should_return_false_if_there_is_newer_minor_snapshot() { + Version currentVersion = Version.valueOf("0.2.0"); + Collection availableVersions = Arrays.asList( + Version.valueOf("0.1.0"), + Version.valueOf("0.2.0"), + Version.valueOf("0.3.0-SNAPSHOT") + ); + + assertThat(strategy.canUpgradeUmbrellaChart(currentVersion, availableVersions)).isFalse(); + } + + @Test + public void test_check_can_upgrade_umbrella_chart_should_return_true_if_there_is_newer_snapshot_build() { Version currentVersion = Version.valueOf("0.3.0-SNAPSHOT"); Collection availableVersions = Arrays.asList( Version.valueOf("0.1.0"), @@ -116,7 +130,7 @@ public void test_check_can_upgrade_umbrella_chart_should_return_false_if_there_i Version.valueOf("0.3.0-SNAPSHOT-bumbershoot.1") ); - assertThat(strategy.canUpgradeUmbrellaChart(currentVersion, availableVersions)).isFalse(); + assertThat(strategy.canUpgradeUmbrellaChart(currentVersion, availableVersions)).isTrue(); } @Test @@ -133,7 +147,7 @@ public void test_check_can_upgrade_umbrella_chart_should_return_false_if_there_i } @Test - public void test_upgrade_umbrella_chart_should_use_bumbershoot_snapshot() throws ChartUpgradeException, ChartInvalidException { + public void test_upgrade_umbrella_chart_should_append_bumbershoot_snapshot() throws ChartUpgradeException, ChartInvalidException { Version currentVersion = Version.valueOf("0.3.0-SNAPSHOT"); Collection availableVersions = Arrays.asList( Version.valueOf("0.1.0"), @@ -173,5 +187,28 @@ public void test_upgrade_umbrella_chart_for_released_version_should_bump_minor_v assertThat(deployed.get()).isTrue(); } + + @Test + public void test_upgrade_umbrella_chart_should_upgrade_latest_snapshot() throws ChartUpgradeException, ChartInvalidException { + Version currentVersion = Version.valueOf("0.3.0-SNAPSHOT-bumbershoot.1"); + Collection availableVersions = Arrays.asList( + Version.valueOf("0.1.0"), + Version.valueOf("0.2.0"), + Version.valueOf("0.3.0-SNAPSHOT"), + Version.valueOf("0.3.0-SNAPSHOT-bumbershoot.1"), + Version.valueOf("0.3.0-SNAPSHOT-bumbershoot.2") + ); + + when(chartMeta.getVersion()).thenReturn(currentVersion.toString()); + ChartIdentifier identifier = new ChartIdentifier("sub", "0.2.0"); + when(update.getChart()).thenReturn(identifier); + + AtomicBoolean deployed = new AtomicBoolean(false); + strategy.upgradeUmbrellaChart(release, repository, update, availableVersions, (url, r) -> deployed.set(true)); + + verify(chartModifier).setChartVersion(any(), any(), eq(Version.valueOf("0.3.0-SNAPSHOT-bumbershoot.3"))); + + assertThat(deployed.get()).isTrue(); + } } diff --git a/src/test/java/com/deviceinsight/bumbershoot/service/ReleaseUpgradePerformServiceTest.java b/src/test/java/com/deviceinsight/bumbershoot/service/ReleaseUpgradePerformServiceTest.java index 31b25de..3ff4cb1 100644 --- a/src/test/java/com/deviceinsight/bumbershoot/service/ReleaseUpgradePerformServiceTest.java +++ b/src/test/java/com/deviceinsight/bumbershoot/service/ReleaseUpgradePerformServiceTest.java @@ -36,6 +36,8 @@ import com.deviceinsight.bumbershoot.model.tiller.ReleaseContent; import com.deviceinsight.bumbershoot.model.tiller.ReleaseInfo; import com.deviceinsight.bumbershoot.model.tiller.ReleaseStatus; +import com.deviceinsight.bumbershoot.service.strategy.SubChartUpgradeStrategy; + import com.google.common.collect.Lists; @RunWith(MockitoJUnitRunner.Silent.class)