"""Functions for generating pull and push targets for third party container images""" load("@io_bazel_rules_docker//container:container.bzl", "container_pull") load("@rules_oci//oci:pull.bzl", "oci_pull") load(":constants.bzl", "COMMON_TAGS") load(":push.bzl", container_push = "container_push2") RULES_DOCKER_OVERRIDES = ["alpine", "ubuntu", "go_image_base"] def pull_third_party_images(imgs): # buildifier: disable=unnamed-macro """Creates targets for pulling all of our third party container images we use. Args: imgs: A dict of image_infos. """ for image_name, image_info in imgs.items(): src_registry = image_info.get("REGISTRY") src_repository = image_info.get("REPO") digest = image_info.get("DIGEST") tag = image_info.get("TAG") registry = "us-east1-docker.pkg.dev" # convert registries to names of GAR remote repos, eg: # index.docker.io -> index-docker-io-proxy. quay.io -> quay-io-proxy repository = "ret-edge-pltf-infra/{0}-proxy/{1}".format(src_registry.replace(".", "-"), src_repository) if digest == None: fail("DIGEST not found for {0}".format(image_name)) # temporary override for images using alpine base if image_name in RULES_DOCKER_OVERRIDES: container_pull( # Snake case repo / target names to align with .bzl style guide name = image_name.replace("-", "_"), digest = digest, registry = registry, repository = repository, tag = tag, import_tags = COMMON_TAGS, ) continue oci_pull( # Snake case repo / target names to align with .bzl style guide name = image_name.replace("-", "_"), digest = digest, registry = registry, repository = repository, ) def push_third_party_images(imgs): # buildifier: disable=unnamed-macro """Creates container push targets for each third-party image Args: imgs: A dict of image_infos. """ for image_name, image_info in imgs.items(): if image_name not in RULES_DOCKER_OVERRIDES: # Snake case repo / target names to align with .bzl style guide image_name = image_name.replace("-", "_") container_push( name = image_name + "_container_push", image = "@{0}//:{0}".format(image_name), digest = "@{0}//:digest".format(image_name), repository_file = "//hack/build/rules/container:thirdparty-repo", image_name = _dst_image_name(image_info), from_third_party = True, tag = image_info.get("TAG") or "", tags = COMMON_TAGS, ) else: image_name = image_name.replace("-", "_") container_push( name = image_name + "_container_push", image = "@{0}//image".format(image_name), repository_file = "//hack/build/rules/container:thirdparty-repo", image_name = _dst_image_name(image_info), from_third_party = True, tag = image_info.get("TAG") or "", rules_docker = True, ) def _dst_image_name(img): """Determines destination path in mirrored repository for third party images Args: img: An image_info dict. Returns: The resolved string. """ if img.get("DESTINATION_REPO") != None: return img.get("DESTINATION_REPO") path = img.get("REPO") if img.get("REGISTRY") != "index.docker.io": path = "{0}/{1}".format(img.get("REGISTRY"), path) return path def third_party_container_dep( name, digest, registry, repository, platforms = None, tag = None, destination_repo = None): # @unused """third_party_container_dep is a macro that wraps an `oci_pull` with strongly typed data. It also enables Gazelle to read these dependencies and create accompanying `container_push` targets that push to the edge-infra third party repo Args: name: The name field will be passed to downstream targets in the form of: oci_pull.name = name container_push.name = name_container_push digest: A digest in the standard sha256@abcde1234 format registry: The name of the registry to pull from i.e. {registry}/cool-repo/an-image repository: The path after registry that includes the image name in the fully qualified URI i.e. registry.io/{repository} The repository path includes the name of the image platforms: A list of OCI standard platforms to pull Ex. ["linux/amd64", "darwin/aarch64"] tag: An optional tag to push up to the thirdparty registry, usually something like a version of the container pulled in destination_repo: An override for the destination path in the thirdparty repo i.e. library/nginx -> nginx This override only affects the corresponding container_push rule, as the external repo pull-through in GAR requries the correct path in order to pull into the mirror repository. """ if "-" in name: fail("name {} cannot contain '_'".format(name)) mirror_registry = "us-east1-docker.pkg.dev" # convert registries to names of GAR remote repos, eg: # index.docker.io -> index-docker-io-proxy. quay.io -> quay-io-proxy mirror_repository = "ret-edge-pltf-infra/{0}-proxy/{1}".format( registry.replace(".", "-"), repository, ) oci_pull( name = name, digest = digest, image = "{registry}/{repository}".format( registry = mirror_registry, repository = mirror_repository, ), tag = tag if tag else None, platforms = platforms, )