Skip to content

Commit

Permalink
fix (jkube-kit/helm) : k8s:helm generated chart tar archive contain…
Browse files Browse the repository at this point in the history
…s reference to tar archive

Filter chart archive if it exists before passing outputDir files for
creating new chart archive

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia authored and manusa committed Feb 9, 2024
1 parent ca209f8 commit 3b2ab1a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -42,6 +42,7 @@ Usage:
* Fix #2532: Bump version.kubernetes-client from 6.9.2 to 6.10.0
* Fix #2613: Added new helm lint goal task (k8s:helm-lint / k8sHelmLint)
* Fix #2541: Container image names can now be set as IPv6 addresses
* Fix #2622: `k8s:helm` generated chart tar archive contains reference to tar archive

_**Note**_:
Kubernetes manifests generated by JKube would now contain the following labels by default:
Expand Down
Expand Up @@ -16,6 +16,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -124,8 +125,12 @@ public void generateHelmCharts(HelmConfig helmConfig) throws IOException {
logger.debug("Creating Helm configuration Tarball: '%s'", tarballFile.getAbsolutePath());
final Consumer<TarArchiveEntry> prependNameAsDirectory = tae ->
tae.setName(String.format("%s/%s", helmConfig.getChart(), tae.getName()));
// outputDir might contain tarball already from previous run, filter out tarball file outputDir from recursive listing
List<File> helmTarballContents = FileUtil.listFilesAndDirsRecursivelyInDirectory(outputDir).stream()
.filter(f -> !f.equals(tarballFile))
.collect(Collectors.toList());
JKubeTarArchiver.createTarBall(
tarballFile, outputDir, FileUtil.listFilesAndDirsRecursivelyInDirectory(outputDir), Collections.emptyMap(),
tarballFile, outputDir, helmTarballContents, Collections.emptyMap(),
ArchiveCompression.fromFileName(tarballFile.getName()), null, prependNameAsDirectory);
Optional.ofNullable(helmConfig.getGeneratedChartListeners()).orElse(Collections.emptyList())
.forEach(listener -> listener.chartFileGenerated(helmConfig, helmType, tarballFile));
Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.jkube.kit.common.util.Serialization;
import org.eclipse.jkube.kit.config.resource.ResourceServiceConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand Down Expand Up @@ -113,6 +114,28 @@ void generateHelmChartsTest() throws Exception {
assertThat(generatedChartCount).hasValue(2);
}

@Test
@DisplayName("when chart generated subsequent runs, then tar archive generated in previous run not present in updated archive")
void generateHelmCharts_whenInvokedTwice_thenFinalChartTarballDoesNotContainPreviousGeneratedHelmChartTarballReference() throws IOException {
// Given
helmConfig.setChart("ITChart");
helmConfig.setVersion("1.33.7");
helmConfig.setChartExtension("tar.gz");
helmConfig.setTypes(Collections.singletonList(HelmConfig.HelmType.KUBERNETES));

// When
helmService.generateHelmCharts(helmConfig);
helmService.generateHelmCharts(helmConfig);

// Then
ArchiveAssertions.assertThat(new File(helmOutputDir, "kubernetes/ITChart-1.33.7.tar.gz"))
.exists().isNotEmpty().fileTree().containsExactlyInAnyOrder(
"ITChart/templates/",
"ITChart/templates/kubernetes.yaml",
"ITChart/Chart.yaml",
"ITChart/values.yaml");
}

@Test
void generateHelmChartsTest_withInvalidParameters_throwsException() {
// Given
Expand Down

0 comments on commit 3b2ab1a

Please sign in to comment.