Skip to content

Commit

Permalink
Merge pull request #104 from ferenc-hechler/master
Browse files Browse the repository at this point in the history
BUGFIX: identity mapping with EXACT and REPLACE is ignored
  • Loading branch information
phenixblue authored Aug 6, 2023
2 parents 8bd5d06 + 665fa1d commit bbd2773
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/imageswap/imageswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,21 @@ def swap_image(container_spec):
app.logger.debug(f"Exact Maps:\n{exact_maps}")
app.logger.debug(f"Replace Maps:\n{replace_maps}")

found = False
if image in exact_maps:
app.logger.debug("found exact mapping")
new_image = exact_maps[image]
found = True
else:
# Check to see if a replacement pattern matches
for pattern in replace_maps.keys():
if fnmatch.fnmatch(image, pattern):
new_image = os.path.join(replace_maps[pattern], image.split("/")[-1])
found = True
break

# Fallback to standard checks if the image has not been changed
if new_image == image:
# Fallback to standard checks if the image has not been found
if not found:
# Check if Registry portion includes a ":<port_number>"
if ":" in image_registry:
image_registry_noport = image_registry.partition(":")[0]
Expand Down
16 changes: 16 additions & 0 deletions app/imageswap/test/test_exact_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ def test_map_exact_nvcr(self):
self.assertTrue(result)
self.assertEqual(container_spec["image"], expected_image)

def test_map_exact_unchanged(self):

"""Method to test exact mapping for entries which should not be changed, like [EXACT]auto::auto"""

imageswap.imageswap_maps_file = "./testing/map_files/map_file_exact.conf"

container_spec = {}
container_spec["name"] = "test-container"
container_spec["image"] = "auto"

expected_image = "auto"
result = imageswap.swap_image(container_spec)

self.assertTrue(result)
self.assertEqual(container_spec["image"], expected_image)


if __name__ == "__main__":
unittest.main()
16 changes: 16 additions & 0 deletions app/imageswap/test/test_replace_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ def test_map_replace_single_char(self):
self.assertTrue(result)
self.assertEqual(container_spec["image"], expected_image)

def test_map_replace_unchanged(self):

"""Method to test replace when the result has not changed from the original"""

imageswap.imageswap_maps_file = "./testing/map_files/map_file_replace.conf"

container_spec = {}
container_spec["name"] = "test-container"
container_spec["image"] = "auto"

expected_image = "auto"
result = imageswap.swap_image(container_spec)

self.assertTrue(result)
self.assertEqual(container_spec["image"], expected_image)


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions testing/map_files/map_file_exact.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ nvcr.io:harbor.example.com
[EXACT]mysql/mysql-server::myownrepo.example.com/base/public-image-cache:mysql_mysql-server
[EXACT]mysql/mysql-server:5.6::myownrepo.example.com/base/public-image-cache:mysql_mysql-server_5.6
[EXACT]nvcr.io/nvidia:k8s-device-plugin_v0.9.0::myownrepo.example.com/base/private-image-cache:nvcr.io_nvidia_k8s-device-plugin_v0.9.0
[EXACT]auto::auto
1 change: 1 addition & 0 deletions testing/map_files/map_file_replace.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ nvcr.io:harbor.example.com
[REPLACE]*mysql-server:5.6::myownrepo.example.com/base/public-image-cache
[REPLACE]*nvidia:k8s-device-plugin_*::myownrepo.example.com/base/
[REPLACE]hello:v1.?::myownrepo.example.com/base/public-image-cache
[REPLACE]auto::

0 comments on commit bbd2773

Please sign in to comment.