Skip to content

Commit

Permalink
Merge pull request #16 from SethHollandsworth/bugfix/caching-template…
Browse files Browse the repository at this point in the history
…-wide

making layer caching work across multiple container groups
  • Loading branch information
SethHollandsworth authored Mar 8, 2023
2 parents 694fb41 + f3d1646 commit 0a11154
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
15 changes: 11 additions & 4 deletions src/confcom/azext_confcom/rootfs_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@


class SecurityPolicyProxy: # pylint: disable=too-few-public-methods
# static variable to cache layer hashes between container groups
layer_cache = {}

def __init__(self):
script_directory = os.path.dirname(os.path.realpath(__file__))
DEFAULT_LIB = "./bin/dmverity-vhd"
Expand Down Expand Up @@ -49,9 +52,12 @@ def __init__(self):
def get_policy_image_layers(
self, image: str, tag: str, tar_location: str = ""
) -> List[str]:
policy_bin_str = str(self.policy_bin)
image_name = f"{image}:{tag}"
# populate layer info
if self.layer_cache.get(image_name):
return self.layer_cache.get(image_name)

img = image + ":" + tag
policy_bin_str = str(self.policy_bin)

arg_list = [
f"{policy_bin_str}",
Expand All @@ -64,7 +70,7 @@ def get_policy_image_layers(
arg_list += ["-d"]

# add the image to the end of the parameter list
arg_list += ["roothash", "-i", f"{img}"]
arg_list += ["roothash", "-i", f"{image_name}"]

outputlines = None
err = None
Expand Down Expand Up @@ -93,5 +99,6 @@ def get_policy_image_layers(
if err.decode("utf8") != "":
output = []
# eprint(err.decode("utf8"))

# cache output layers
self.layer_cache[image_name] = output
return output
12 changes: 4 additions & 8 deletions src/confcom/azext_confcom/security_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ def populate_policy_content_for_all_images(
)

tar_location = ""
layer_cache = {}
if isinstance(tar_mapping, str):
tar_location = tar_mapping
proxy = self._get_rootfs_proxy()
Expand Down Expand Up @@ -475,13 +474,10 @@ def populate_policy_content_for_all_images(
if isinstance(tar_mapping, dict):
tar_location = get_tar_location_from_mapping(tar_mapping, image_name)
# populate layer info
if layer_cache.get(image_name):
image.set_layers(layer_cache.get(image_name))
else:
image.set_layers(proxy.get_policy_image_layers(
image.base, image.tag, tar_location=tar_location if tar else ""
))
layer_cache[image_name] = image.get_layers()
image.set_layers(proxy.get_policy_image_layers(
image.base, image.tag, tar_location=tar_location if tar else ""
))

progress.update()
progress.close()
self.close()
Expand Down

0 comments on commit 0a11154

Please sign in to comment.