Skip to content

Commit f840e56

Browse files
committed
Make a runnable lifecycle tagger
This takes the bulk tagger and adds bazel wrappers around it. The intention is to query the system and run bulk tagger scripts on cloud run on a cron to tag any older images. Signed-off-by: Appu Goundan <[email protected]>
1 parent faac4a1 commit f840e56

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("//private/oci:defs.bzl", "sign_and_push_all")
2+
load("//private/tools/lifecycle:defs.bzl", "attach_lifecycle_tags")
23
load("//static:config.bzl", "STATIC_ARCHITECTURES", "STATIC_DISTROS")
34
load("//base:config.bzl", "BASE_ARCHITECTURES", "BASE_DISTROS")
45
load("//cc:config.bzl", "CC_ARCHITECTURES", "CC_DISTROS")
@@ -331,3 +332,8 @@ sign_and_push_all(
331332
name = "sign_and_push",
332333
images = ALL,
333334
)
335+
336+
attach_lifecycle_tags(
337+
name = "attach_lifecycle_tags",
338+
images = ALL,
339+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"rules to deal with auto_vm lifecycle tagging"
2+
3+
exports_files(["tag.sh"])

private/tools/lifecycle/defs.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load(":tag.bzl", _attach_lifecycle_tags = "attach_lifecycle_tags")
2+
3+
attach_lifecycle_tags = _attach_lifecycle_tags

private/tools/lifecycle/tag.bzl

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"rules for attaching lifecycle tags to older published images"
2+
3+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
4+
5+
def _tag(ctx):
6+
script_file = ctx.executable.script
7+
8+
runner_script = ctx.actions.declare_file(ctx.label.name + "_runner.sh")
9+
10+
commands = ["#!/usr/bin/env bash", "set -euo pipefail"]
11+
for ref in ctx.attr.targets:
12+
commands.append("{script} update {ref}".format(
13+
script = script_file.short_path,
14+
ref = ref,
15+
))
16+
17+
ctx.actions.write(
18+
output = runner_script,
19+
content = "\n".join(commands),
20+
is_executable = True,
21+
)
22+
23+
return [
24+
DefaultInfo(
25+
executable = runner_script,
26+
runfiles = ctx.runfiles(files = [script_file]),
27+
),
28+
RunEnvironmentInfo(
29+
environment = {"DRY_RUN": "false"},
30+
# maybe inherit this, need cloud build testing
31+
# inherited_environment = ["GOOGLE_APPLICATION_CREDENTIALS"],
32+
),
33+
]
34+
35+
tag = rule(
36+
implementation = _tag,
37+
attrs = {
38+
"targets": attr.string_list(),
39+
"script": attr.label(
40+
mandatory = True,
41+
allow_single_file = True,
42+
executable = True,
43+
cfg = "exec",
44+
),
45+
},
46+
executable = True,
47+
)
48+
49+
def attach_lifecycle_tags(name, images):
50+
"""simple macro to assign lifecycle tags to older images
51+
52+
Args:
53+
name: name of the target
54+
images: a dict where keys are fully qualified image references and values are image label
55+
"""
56+
57+
all_images = dict()
58+
59+
for (ref, _) in images.items():
60+
repository_and_tag = ref.split(":")
61+
62+
# TODO: have these leverage ../stamp.bash
63+
repository = repository_and_tag[0].format(REGISTRY = "gcr.io", PROJECT_ID = "distroless")
64+
all_images[repository] = "ignored"
65+
66+
write_file(
67+
name = name + ".query",
68+
content = sorted(all_images.keys()),
69+
out = name + "_query",
70+
)
71+
72+
tag(
73+
name = name,
74+
targets = sorted(all_images.keys()),
75+
script = Label("//private/tools/lifecycle:tag.sh"),
76+
)
File renamed without changes.

0 commit comments

Comments
 (0)