Skip to content

Commit 43fd06f

Browse files
Roger Lehmannnishakm
Roger Lehmann
authored andcommitted
Add subdir for tar files in the working dir
The safeguard of adding a subdir based on the first part of the file name is necessary for docker-like image tar archives, which don't have a folder for each layer but rather another tar.gz file or similar. This is the case for kaniko built docker image archives. Without it, tar archives will overwrite the untar_dir on every new layer analyzed. This will result in the os-release file not being found as the os analyzer expects the files of the first layer in the dir but it's actually the files of the last dir. This patch is enabling a way for using Tern with kaniko images. See tern-tools#361 Signed-off-by: Roger Lehmann <[email protected]>
1 parent 048ae80 commit 43fd06f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

tern/classes/image_layer.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,18 @@ def get_untar_dir(self):
318318
"""Get the directory where contents of the image layer are untarred"""
319319
# the untar directory is based on the image layout
320320
if self.image_layout == 'docker':
321+
# Images built with Kaniko and its tarPath parameter identify
322+
# as Docker image based on the metadata format, but keep their
323+
# layer tar files in the root of the main tar file.
324+
# So we will make sure if there's no subdir for the layer tar
325+
# files, we use the first part of the file name as dir name.
326+
# This is to prevent overwriting the extracted layers on every
327+
# subsequent layer inspection.
328+
subdir_name = os.path.dirname(self.tar_file)
329+
if not subdir_name:
330+
subdir_name = self.tar_file.split('.', 1)[0]
321331
return os.path.join(rootfs.get_working_dir(),
322-
os.path.dirname(self.tar_file),
332+
subdir_name,
323333
constants.untar_dir)
324334
# For OCI layouts, the tar file may be at the root of the directory
325335
# So we will return a path one level deeper

tests/test_class_image_layer.py

+6
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ def testGetUntarDir(self):
165165
expected_path = os.path.join(rootfs.get_working_dir(),
166166
'path/to/contents')
167167
self.assertEqual(self.layer.get_untar_dir(), expected_path)
168+
# Kaniko image format test
169+
self.layer = ImageLayer('123abc', 'some_layer_tar_file.tar.gz')
170+
self.layer.image_layout = "docker"
171+
expected_path = os.path.join(rootfs.get_working_dir(),
172+
'some_layer_tar_file/contents')
173+
self.assertEqual(self.layer.get_untar_dir(), expected_path)
168174

169175

170176
if __name__ == '__main__':

0 commit comments

Comments
 (0)