Skip to content

Commit

Permalink
Add docker label with original docker image tag if interpolated
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnPreston committed Aug 30, 2023
1 parent 2b3a154 commit 987fb3a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
21 changes: 12 additions & 9 deletions ecs_composex/compose/compose_services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def __init__(
import_env_variables(self.environment) if self.environment else NoValue
)
self.depends_on = set_else_none("depends_on", self.definition, [], False)
self.docker_labels: dict = {}
self.import_docker_labels(self._definition)

if not keyisset("image", self.definition):
raise KeyError("You must specify the image to use for", self.name)
Expand Down Expand Up @@ -877,21 +879,22 @@ def define_port_mappings(self) -> list:
self.handle_expose_ports(service_port_mappings)
return service_port_mappings

def import_docker_labels(self):
def import_docker_labels(self, definition: dict):
"""
Import the Docker labels if defined
"""
labels = {}
if not keyisset("labels", self.definition):
if not keyisset("labels", definition):
return labels
else:
if isinstance(self.definition["labels"], dict):
return self.definition["labels"]
elif isinstance(self.definition["labels"], list):
for label in self.definition["labels"]:
if isinstance(definition["labels"], dict):
self.docker_labels.update(definition["labels"])
elif isinstance(definition["labels"], list):
for label in definition["labels"]:
splits = label.split("=")
labels.update({splits[0]: splits[1] if len(splits) == 2 else ""})
return labels
self.docker_labels.update(
{splits[0]: splits[1] if len(splits) == 2 else ""}
)

def set_container_definition(self):
"""
Expand Down Expand Up @@ -929,7 +932,7 @@ def set_container_definition(self):
If(USE_WINDOWS_OS_T, NoValue, keyisset("Privileged", self.definition)),
),
WorkingDirectory=self.working_dir,
DockerLabels=self.import_docker_labels(),
DockerLabels=self.docker_labels,
ReadonlyRootFilesystem=If(
USE_WINDOWS_OS_T, NoValue, keyisset("read_only", self.definition)
),
Expand Down
17 changes: 17 additions & 0 deletions ecs_composex/compose/compose_services/service_image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

import copy
import re
from typing import TYPE_CHECKING, Union

Expand Down Expand Up @@ -129,6 +130,10 @@ def private_ecr_digest(self, settings: ComposeXSettings):
)
else:
self.service.definition["image"] = self.image_uri
LOG.debug("ECR - ADDING IMAGE TAG TO LABELS")
self.service.docker_labels.update(
{"com.docker.image_tag": service_image["imageTag"]}
)

def interpolate_image_digest(self, settings: ComposeXSettings = None):
"""
Expand Down Expand Up @@ -165,6 +170,7 @@ def retrieve_image_digest(self):
"application/vnd.docker.distribution.manifest.list.v2+json",
]
try:
original_image = self.image
dkr_client = docker.APIClient()
image_details = dkr_client.inspect_distribution(self.image)
if not keyisset("Descriptor", image_details):
Expand Down Expand Up @@ -192,6 +198,17 @@ def retrieve_image_digest(self):
)
else:
self.service.definition["image"] = self.image_uri
try:
image_tag_re = re.compile(r"(?<=[:@])(?P<tag_digest>[\w.\-_]+$)")
original_tag_digest = image_tag_re.search(original_image).group(
"tag_digest"
)
self.service.docker_labels.update(
{"com.compose.image_tag": original_tag_digest}
)
except AttributeError:
pass

else:
LOG.warning(
"No digest found. This might be due to Registry API prior to V2"
Expand Down

0 comments on commit 987fb3a

Please sign in to comment.