|
| 1 | +""" config """ |
| 2 | + |
| 3 | +load("@com_github_datadog_rules_oci//oci:image.bzl", "get_descriptor_file") |
| 4 | +load("@com_github_datadog_rules_oci//oci:providers.bzl", "OCIDescriptor", "OCILayout") |
| 5 | + |
| 6 | +def generate_config_file_action(ctx, config_file, image, os, arch): |
| 7 | + """ Generates a run action with that extracts an image's config file. |
| 8 | +
|
| 9 | + In order to use this action, the calling rule _must_ register |
| 10 | + `@com_github_datadog_rules_oci//oci:toolchain` and the image |
| 11 | + must provide the `OCIDescriptor` and `OCILayout` (this should |
| 12 | + not be an issue when using the `oci_image` rule). |
| 13 | +
|
| 14 | + Args: |
| 15 | + ctx: The current rules context |
| 16 | + config_file: The file to write the config to |
| 17 | + image: The image to extract the config from. |
| 18 | + os: The os to extract the config for |
| 19 | + arch: The arch to extract the config for |
| 20 | +
|
| 21 | + Returns: |
| 22 | + The config file named after the rule, os, and arch |
| 23 | + """ |
| 24 | + toolchain = ctx.toolchains["@com_github_datadog_rules_oci//oci:toolchain"] |
| 25 | + |
| 26 | + base_desc = get_descriptor_file(ctx, image[OCIDescriptor]) |
| 27 | + base_layout = image[OCILayout] |
| 28 | + |
| 29 | + ctx.actions.run( |
| 30 | + executable = toolchain.sdk.ocitool, |
| 31 | + arguments = [ |
| 32 | + "--layout={}".format(base_layout.blob_index.path), |
| 33 | + "config", |
| 34 | + "--base={}".format(base_desc.path), |
| 35 | + "--os={}".format(os), |
| 36 | + "--arch={}".format(arch), |
| 37 | + "--out-config={}".format(config_file.path), |
| 38 | + ], |
| 39 | + inputs = [ |
| 40 | + base_desc, |
| 41 | + base_layout.blob_index, |
| 42 | + ] + base_layout.files.to_list(), |
| 43 | + outputs = [ |
| 44 | + config_file, |
| 45 | + ], |
| 46 | + ) |
| 47 | + |
| 48 | + return config_file |
| 49 | + |
| 50 | +def _oci_image_config_impl(ctx): |
| 51 | + config_file = ctx.actions.declare_file("{}.config.json".format(ctx.label.name)) |
| 52 | + |
| 53 | + return DefaultInfo(files = depset([ |
| 54 | + generate_config_file_action(ctx, config_file, ctx.attr.image, ctx.attr.os, ctx.attr.arch), |
| 55 | + ])) |
| 56 | + |
| 57 | +oci_image_config_rule = struct( |
| 58 | + implementation = _oci_image_config_impl, |
| 59 | + attrs = { |
| 60 | + "image": attr.label( |
| 61 | + mandatory = True, |
| 62 | + providers = [OCIDescriptor, OCILayout], |
| 63 | + ), |
| 64 | + "os": attr.string( |
| 65 | + doc = "Used to extract config from image if image is an index", |
| 66 | + ), |
| 67 | + "arch": attr.string( |
| 68 | + doc = "Used to extract config from image if image is an index", |
| 69 | + ), |
| 70 | + }, |
| 71 | + toolchains = ["@com_github_datadog_rules_oci//oci:toolchain"], |
| 72 | +) |
| 73 | + |
| 74 | +oci_image_config = rule( |
| 75 | + implementation = oci_image_config_rule.implementation, |
| 76 | + attrs = oci_image_config_rule.attrs, |
| 77 | + toolchains = oci_image_config_rule.toolchains, |
| 78 | +) |
0 commit comments