From 015458f01769e51eefabc99f19baf94333ee2a47 Mon Sep 17 00:00:00 2001 From: Aleksey Pesternikov Date: Tue, 11 Jun 2024 15:00:10 -0700 Subject: [PATCH] tag all targets generated by k8s_deploy --- README.md | 1 + push_oci/push_oci.bzl | 2 ++ skylib/k8s.bzl | 19 ++++++++++++++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e968b650..3c86e823 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ When you run `bazel run ///helloworld:mynamespace.apply`, it applies this file i | ***release_branch_prefix*** | `master` | A git branch name/prefix. Automatically run GitOps while building this branch. See [GitOps and Deployment](#gitops_and_deployment). | ***deployment_branch*** | `None` | Automatic GitOps output will appear in a branch and PR with this name. See [GitOps and Deployment](#gitops_and_deployment). | ***gitops_path*** | `cloud` | Path within the git repo where gitops files get generated into +| ***tags*** | `[]` | A list of tags that will be added to all generated bazel targets. Useful for marking targets as manual. | ***visibility*** | [Default_visibility](https://docs.bazel.build/versions/master/be/functions.html#package.default_visibility) | Changes the visibility of all rules generated by this macro. See [Bazel docs on visibility](https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes). diff --git a/push_oci/push_oci.bzl b/push_oci/push_oci.bzl index 4b01c1d8..eedab56d 100644 --- a/push_oci/push_oci.bzl +++ b/push_oci/push_oci.bzl @@ -108,6 +108,7 @@ def push_oci( image_digest_tag = False, # buildifier: disable=unused-variable either remove parameter or implement tag = None, remote_tags = None, # file with tags to push + tags = [], # bazel tags to add to the push_oci_rule visibility = None): if tag: tags_label = "_{}_write_tags".format(name) @@ -128,5 +129,6 @@ def push_oci( image = image, repository = repository, remote_tags = remote_tags, + tags = tags, visibility = visibility, ) diff --git a/skylib/k8s.bzl b/skylib/k8s.bzl index 847b52cf..597259df 100644 --- a/skylib/k8s.bzl +++ b/skylib/k8s.bzl @@ -65,7 +65,7 @@ show = rule( executable = True, ) -def _image_pushes(name_suffix, images, image_registry, image_repository, image_digest_tag): +def _image_pushes(name_suffix, images, image_registry, image_repository, image_digest_tag, tags = []): image_pushes = [] def process_image(image_label, image_alias = None): @@ -81,6 +81,7 @@ def _image_pushes(name_suffix, images, image_registry, image_repository, image_d image_digest_tag = image_digest_tag, registry = image_registry, repository = image_repository, + tags = tags, visibility = ["//visibility:public"], ) if not image_alias: @@ -92,6 +93,7 @@ def _image_pushes(name_suffix, images, image_registry, image_repository, image_d name = rule_name + "_alias_" + name_suffix, alias = image_alias, pushed_image = rule_name + name_suffix, + tags = tags, visibility = ["//visibility:public"], ) return rule_name + "_alias_" + name_suffix @@ -106,10 +108,6 @@ def _image_pushes(name_suffix, images, image_registry, image_repository, image_d push = process_image(image) image_pushes.append(push) return image_pushes - for image in images: - image_push = process_image(image) - image_pushes.append(image_push) - return image_pushes def k8s_deploy( name, # name of the rule is important for gitops, since it will become a part of the target manifest file name in /cloud @@ -144,6 +142,7 @@ def k8s_deploy( release_branch_prefix = "main", start_tag = "{{", end_tag = "}}", + tags = [], # tags to add to all generated rules. visibility = None): """ k8s_deploy """ @@ -175,6 +174,7 @@ def k8s_deploy( image_registry = image_registry + "/mynamespace", image_repository = image_repository, image_digest_tag = image_digest_tag, + tags = tags, ) kustomize( name = name, @@ -200,6 +200,7 @@ def k8s_deploy( image_name_patches = image_name_patches, image_tag_patches = image_tag_patches, openapi_path = openapi_path, + tags = tags, visibility = visibility, ) kubectl( @@ -208,6 +209,7 @@ def k8s_deploy( cluster = cluster, user = user, namespace = namespace, + tags = tags, visibility = visibility, ) kubectl( @@ -218,12 +220,14 @@ def k8s_deploy( push = False, user = user, namespace = namespace, + tags = tags, visibility = visibility, ) show( name = name + ".show", namespace = namespace, src = name, + tags = tags, visibility = visibility, ) else: @@ -236,6 +240,7 @@ def k8s_deploy( image_registry = image_registry, image_repository = image_repository, image_digest_tag = image_digest_tag, + tags = tags, ) kustomize( name = name, @@ -261,6 +266,7 @@ def k8s_deploy( image_name_patches = image_name_patches, image_tag_patches = image_tag_patches, openapi_path = openapi_path, + tags = tags, ) kubectl( name = name + ".apply", @@ -268,6 +274,7 @@ def k8s_deploy( cluster = cluster, user = user, namespace = namespace, + tags = tags, visibility = visibility, ) kustomize_gitops( @@ -282,12 +289,14 @@ def k8s_deploy( ], deployment_branch = deployment_branch, release_branch_prefix = release_branch_prefix, + tags = tags, visibility = ["//visibility:public"], ) show( name = name + ".show", src = name, namespace = namespace, + tags = tags, visibility = visibility, )