Skip to content

Commit

Permalink
Fix #777: Failure in processing generated tarball by old Docker versions
Browse files Browse the repository at this point in the history
Tar files generated during `k8s:build` were getting rejected from Docker
v1.12.1 (API version 1.24). Not setting size of TarArchiveEntry in case
of directories seem to fix this issue

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia authored and manusa committed Jul 27, 2021
1 parent 15bb59f commit d1c7898
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Usage:
* Fix #751: QuarkusGenerator: Multi-layer images for the different Quarkus packaging modes
* Fix #756: Service re-apply error happening during `k8s:watch`
* Fix #758: QuarkusHealthCheckEnricher: default health path value is outdated
* Fix #777: Error processing tar file(archive/tar: missed writing 4096 bytes) on Docker 1.12.1

### 1.3.0 (2021-05-18)
* Fix #497: Assembly descriptor removed but still in documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,7 @@ public static File createTarBall(
String relativeFilePath = inputDirectory.toURI().relativize(
new File(currentFile.getAbsolutePath()).toURI()).getPath();

final TarArchiveEntry tarEntry = new TarArchiveEntry(currentFile, relativeFilePath);
tarEntry.setSize(currentFile.length());
if (fileModeMap.containsKey(currentFile)) {
tarEntry.setMode(Integer.parseInt(fileModeMap.get(currentFile), 8));
} else if (currentFile.isDirectory()) {
tarEntry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE);
}
final TarArchiveEntry tarEntry = createTarArchiveEntry(fileModeMap, currentFile, relativeFilePath);
Optional.ofNullable(tarArchiveEntryCustomizer).ifPresent(tac -> tac.accept(tarEntry));
tarArchiveOutputStream.putArchiveEntry(tarEntry);
if (currentFile.isFile()) {
Expand All @@ -93,4 +87,16 @@ public static File createTarBall(

return outputFile;
}

static TarArchiveEntry createTarArchiveEntry(Map<File, String> fileModeMap, File currentFile, String relativeFilePath) {
final TarArchiveEntry tarEntry = new TarArchiveEntry(currentFile, relativeFilePath);
tarEntry.setSize(currentFile.length());
if (fileModeMap.containsKey(currentFile)) {
tarEntry.setMode(Integer.parseInt(fileModeMap.get(currentFile), 8));
} else if (currentFile.isDirectory()) {
tarEntry.setSize(0L);
tarEntry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE);
}
return tarEntry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.function.Consumer;

import org.eclipse.jkube.kit.common.assertj.ArchiveAssertions;
Expand All @@ -30,6 +31,8 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class JKubeTarArchiverTest {

private static final String LONG_FILE_NAME = "0123456789012345678901234567890123456789012345678901234567890123456789"
Expand Down Expand Up @@ -152,4 +155,32 @@ public void createTarBallOfDirectory_defaultCompressionWithTarCustomizer_creates
"nested/directory/01234567890123456789012345678901234567890123456789012345678901234567890123456789012");
}

}
@Test
public void createTarArchiveEntry_whenUsedForDirectories_shouldCreateEntryWithZeroSize() {
// Given
File tmpDir = new File(getClass().getResource("/jkubetararchiver-directory/").getFile());

// When
TarArchiveEntry tarArchiveEntry = JKubeTarArchiver.createTarArchiveEntry(Collections.emptyMap(), tmpDir, "/tmp/foo");

// Then
assertThat(tarArchiveEntry)
.isNotNull()
.hasFieldOrPropertyWithValue("size", 0L);
}

@Test
public void createTarArchiveEntry_whenUsedForFiles_shouldCreateEntryWithNonZeroSize() {
// Given
File tmpFile = new File(getClass().getResource("/jkubetararchiver-directory/jkubetararchiver-non-empty-file.txt").getFile());

// When
TarArchiveEntry tarArchiveEntry = JKubeTarArchiver.createTarArchiveEntry(Collections.singletonMap(tmpFile, "0644"), tmpFile, "/tmp/foo");

// Then
assertThat(tarArchiveEntry)
.isNotNull()
.hasFieldOrPropertyWithValue("size", 10L)
.hasFieldOrPropertyWithValue("mode", Integer.parseInt("0644", 8));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Not Empty.

0 comments on commit d1c7898

Please sign in to comment.