Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module Publication: fix the way to handle docker image information #148

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Change the way to handle docker image information when publishing a module

## 1.17.0 - 2024-11-04

### Added
Expand Down
37 changes: 25 additions & 12 deletions sekoia_automation/scripts/sync_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@
from base64 import b64encode
from pathlib import Path
from posixpath import join as urljoin
from typing import Any
from typing import Any, NamedTuple

import requests
import typer
from rich import print


class DockerInfo(NamedTuple):
name: str
version: str

@property
def image(self):
return f"{self.name}:{self.version}"


class SyncLibrary:
DOCKER_REGISTRY = "ghcr.io"
DOCKER_NAMESPACE = "sekoia-io"
Expand Down Expand Up @@ -260,10 +269,13 @@ def set_docker(self, manifests: list, module: dict) -> list:
Returns:
list: Modified version of the manifests received as parameter
"""
module_docker_name = self._get_module_docker_name(module)
module_docker_info = self._get_module_docker_info(module)
for manifest in manifests:
if "docker" not in manifest or manifest["docker"] == module_docker_name:
manifest["docker"] = f"{module_docker_name}:{module['version']}"
if (
"docker" not in manifest
or manifest["docker"] == module_docker_info.name
):
manifest["docker"] = module_docker_info.image

return manifests

Expand Down Expand Up @@ -362,14 +374,13 @@ def load_module(self, module_path: Path):
with manifest_path.open() as fd:
module_info = json.load(fd)

docker_name = self._get_module_docker_name(module_info)
module_info["docker"] = f"{docker_name}:{module_info['version']}"
docker_info = self._get_module_docker_info(module_info)
module_info["docker"] = docker_info.image
if self.registry_check and not self.check_image_on_registry(
docker_name, module_info["version"]
docker_info.name, docker_info.version
):
print(
f"[bold red][!] Image {docker_name}:{module_info['version']} "
f"not available on registry"
f"[bold red][!] Image {docker_info.image} " f"not available on registry"
)
raise typer.Exit(code=1)

Expand Down Expand Up @@ -452,9 +463,11 @@ def execute(self):
module_path=self.modules_path.joinpath(self.module).absolute(),
)

def _get_module_docker_name(self, manifest: dict) -> str:
def _get_module_docker_info(self, manifest: dict) -> DockerInfo:
if docker := manifest.get("docker"):
return docker
(name, version, *extra) = docker.split(":")
return DockerInfo(name, version)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have manifests with the docker attribute ?

Maybe we should stop supporting it since it may lead to a lot of issues:

  • Registry not being set
  • version in the attribute not the same as the "real" module version

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took in account your comment with this PR

if slug := manifest.get("slug"):
return f"{self.registry}/{self.namespace}/{self.DOCKER_PREFIX}-{slug}"
name = f"{self.registry}/{self.namespace}/{self.DOCKER_PREFIX}-{slug}"
return DockerInfo(name, manifest["version"])
raise ValueError("Impossible to generate image name")
Loading