load("//build:helm.bzl", "helm_pkg", "helm_tmpl")
load("//build:files.bzl", "concat_files")

crds = [
    "certificaterequests",
    "certificates",
    "challenges",
    "clusterissuers",
    "issuers",
    "orders",
]

# A single file containing all the CRD templates concatenated together
concat_files(
    name = "templates",
    srcs = [":crd-%s.yaml" % crd for crd in crds],
    separator = "---",
    visibility = ["//visibility:public"],
)

# Create Helm packages that contain only the CRDs named in this directory
# as the contents of 'templates/'.
# All other chart assets (e.g. Chart.yaml, values.yaml), will be sourced from
# main cert-manager Helm chart.
# This allows us to easily render the CRDs as they would be in the Helm chart
# without rendering the entirety of the cert-manager installation.
# This means we can install the CRDs during integration tests, and also publish
# the 'static manifest' variants of the CRDs.
helm_pkg(
    name = "package",
    srcs = [":templates"],
    chart_name = "cert-manager",
    chart_yaml = "//deploy/charts/cert-manager:Chart.yaml",
    readme_file = "//deploy/charts/cert-manager:README.md",
    tpl_files = [
        "//deploy/charts/cert-manager/templates:_helpers.tpl",
    ],
    values_yaml = "//deploy/charts/cert-manager:values.yaml",
    visibility = ["//visibility:private"],
)

# Run 'helm template' against the chart containing only the CRDs that is
# constructed in the above 'helm_pkg' rule.
helm_tmpl(
    name = "crds",
    helm_pkg = ":package",
    release_name = "cert-manager",
    release_namespace = "cert-manager",
    values = {
        # Set creator to "static", so the Helm chart does
        # not add Helm-specific labels to the resources.
        "creator": "static",
        "startupapicheck.enabled": "false",
    },
    visibility = ["//visibility:public"],
)

[helm_pkg(
    name = "crd-%s.package" % crd,
    srcs = [":crd-%s.yaml" % crd],
    chart_name = "cert-manager",
    chart_yaml = "//deploy/charts/cert-manager:Chart.yaml",
    readme_file = "//deploy/charts/cert-manager:README.md",
    tpl_files = [
        "//deploy/charts/cert-manager/templates:_helpers.tpl",
    ],
    values_yaml = "//deploy/charts/cert-manager:values.yaml",
    visibility = ["//visibility:private"],
) for crd in crds]

[helm_tmpl(
    name = "crd-%s.templated" % crd,
    helm_pkg = ":crd-%s.package" % crd,
    release_name = "cert-manager",
    release_namespace = "cert-manager",
    values = {
        # Set creator to "static", so the Helm chart does
        # not add Helm-specific labels to the resources.
        "creator": "static",
        "startupapicheck.enabled": "false",
    },
    visibility = ["//visibility:public"],
) for crd in crds]

filegroup(
    name = "templated_files",
    srcs = ["crd-%s.templated.yaml" % crd for crd in crds],
    visibility = ["//visibility:public"],
)

filegroup(
    name = "package-srcs",
    srcs = glob(["**"]),
    tags = ["automanaged"],
    visibility = ["//visibility:private"],
)

filegroup(
    name = "all-srcs",
    srcs = [":package-srcs"],
    tags = ["automanaged"],
    visibility = ["//visibility:public"],
)
