Skip to content
This repository was archived by the owner on Nov 3, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.LatestUmbrellaChartUpgradeStragey;
import com.deviceinsight.bumbershoot.service.SubChartUpgradeStrategy;
import com.deviceinsight.bumbershoot.service.strategy.LatestUmbrellaChartUpgradeStrategy;
import com.deviceinsight.bumbershoot.service.strategy.SubChartUpgradeStrategy;

import java.util.List;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.deviceinsight.bumbershoot.service;
package com.deviceinsight.bumbershoot.service.strategy;

import java.io.File;
import java.util.Collection;
Expand All @@ -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 LatestUmbrellaChartUpgradeStragey extends SubChartUpgradeStrategy {
public class LatestUmbrellaChartUpgradeStrategy extends SubChartUpgradeStrategy {

private final ChartArchiveModifier archiveModifier;
private static final String SNAPSHOT_VERSION = "SNAPSHOT";

public LatestUmbrellaChartUpgradeStragey(ChartRepositoryClientFactory chartRepositoryClientFactory,
public LatestUmbrellaChartUpgradeStrategy(ChartRepositoryClientFactory chartRepositoryClientFactory,
ChartArchiveModifier archiveModifier) {

super(chartRepositoryClientFactory);
this.archiveModifier = archiveModifier;
super(chartRepositoryClientFactory, archiveModifier);
}

@Override
public boolean canUpgradeUmbrellaChart(Version currentVersion, Collection<Version> 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
Expand All @@ -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);
Expand All @@ -69,28 +79,14 @@ public void upgradeUmbrellaChart(Release release, ChartRepository umbrellaChartR

}
}

private ChartMetaData modifyUmbrellaChart(Collection<Version> 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<Version> 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")) {
Expand All @@ -102,12 +98,33 @@ private Version buildNextVersion(Version currentVersion, Collection<Version> ava
return nextVersion;
}

private Optional<Version> getHighestMajorVersion(Version currentVersion, Collection<Version> availableVersions) {
private Optional<Version> getHighestCompatibleToMajorVersion(Version currentVersion, Collection<Version> availableVersions) {
var compatibleWithExpression = String.format("%d.x", currentVersion.getMajorVersion());

return availableVersions.stream()
.filter(v -> v.satisfies(compatibleWithExpression))
.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<Version> 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();

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.deviceinsight.bumbershoot.service;
package com.deviceinsight.bumbershoot.service.strategy;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -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;
Expand All @@ -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<Version> availableVersions);
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
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)
public class LatestUmbrellaChartUpgradeStrageyTest {
public class LatestUmbrellaChartUpgradeStrategyTest {

@Mock
private Release release;
Expand All @@ -59,7 +61,7 @@ public class LatestUmbrellaChartUpgradeStrageyTest {
private ChartRepositoryClientFactory clientFactory;

@InjectMocks
private LatestUmbrellaChartUpgradeStragey strategy;
private LatestUmbrellaChartUpgradeStrategy strategy;

@Before
public void mockBehavior() throws MalformedURLException, UnsupportedChartRepositoryType {
Expand Down Expand Up @@ -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<Version> 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<Version> availableVersions = Arrays.asList(
Version.valueOf("0.1.0"),
Expand All @@ -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
Expand All @@ -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<Version> availableVersions = Arrays.asList(
Version.valueOf("0.1.0"),
Expand Down Expand Up @@ -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<Version> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down