From adba95a9891f5f73e2d185944e98eb98599a3d93 Mon Sep 17 00:00:00 2001 From: Brendan Miller <6900229+bhmiller@users.noreply.github.com> Date: Wed, 6 Sep 2023 20:45:53 -0400 Subject: [PATCH 1/2] Add support of bazel tags to rules_gitops Add ability to use bazel tags on k8s_deploy targets so can use bazel tag filters on these targets to include or exclude from building. --- README.md | 1 + examples/helloworld/BUILD | 1 + skylib/k8s.bzl | 14 +++++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4db33521..0494cbd8 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*** | `[]` | See [Bazel docs on tags](https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes). | ***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/examples/helloworld/BUILD b/examples/helloworld/BUILD index 924f8a40..a54d7bd1 100644 --- a/examples/helloworld/BUILD +++ b/examples/helloworld/BUILD @@ -100,6 +100,7 @@ k8s_deploy( ], namespace = NAMESPACE, user = USER, + tags = [ "release" ], ) k8s_deploy( diff --git a/skylib/k8s.bzl b/skylib/k8s.bzl index 5280e10c..68b1287f 100644 --- a/skylib/k8s.bzl +++ b/skylib/k8s.bzl @@ -70,7 +70,7 @@ show = rule( executable = True, ) -def _image_pushes(name_suffix, images, image_registry, image_repository, image_repository_prefix, image_digest_tag): +def _image_pushes(name_suffix, images, image_registry, image_repository, image_repository_prefix, image_digest_tag, tags): image_pushes = [] def process_image(image_label, legacy_name = None): @@ -91,6 +91,7 @@ def _image_pushes(name_suffix, images, image_registry, image_repository, image_r registry = image_registry, repository = image_repository, repository_prefix = image_repository_prefix, + tags = tags, ) return rule_name + name_suffix @@ -138,6 +139,7 @@ def k8s_deploy( release_branch_prefix = "main", start_tag = "{{", end_tag = "}}", + tags = [], visibility = None): """ k8s_deploy """ @@ -170,6 +172,7 @@ def k8s_deploy( image_repository = image_repository, image_repository_prefix = "{BUILD_USER}", image_digest_tag = image_digest_tag, + tags = tags, ) kustomize( name = name, @@ -194,6 +197,7 @@ def k8s_deploy( objects = objects, image_name_patches = image_name_patches, image_tag_patches = image_tag_patches, + tags = tags, visibility = visibility, ) kubectl( @@ -202,6 +206,7 @@ def k8s_deploy( cluster = cluster, user = user, namespace = namespace, + tags = tags, visibility = visibility, ) kubectl( @@ -212,12 +217,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: @@ -231,6 +238,7 @@ def k8s_deploy( image_repository = image_repository, image_repository_prefix = image_repository_prefix, image_digest_tag = image_digest_tag, + tags = tags, ) kustomize( name = name, @@ -255,6 +263,7 @@ def k8s_deploy( patches = patches, image_name_patches = image_name_patches, image_tag_patches = image_tag_patches, + tags = tags, ) kubectl( name = name + ".apply", @@ -262,6 +271,7 @@ def k8s_deploy( cluster = cluster, user = user, namespace = namespace, + tags = tags, visibility = visibility, ) kustomize_gitops( @@ -276,12 +286,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, ) From b1b18fdce2fbbd15337372e4141a7fc333a19b3a Mon Sep 17 00:00:00 2001 From: Brendan Miller <6900229+bhmiller@users.noreply.github.com> Date: Thu, 7 Sep 2023 08:10:31 -0400 Subject: [PATCH 2/2] Run buildifier --- examples/helloworld/BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/helloworld/BUILD b/examples/helloworld/BUILD index a54d7bd1..32edb945 100644 --- a/examples/helloworld/BUILD +++ b/examples/helloworld/BUILD @@ -99,8 +99,8 @@ k8s_deploy( "service.yaml", ], namespace = NAMESPACE, + tags = ["release"], user = USER, - tags = [ "release" ], ) k8s_deploy(