From dd9b6b3ab92ca6fd3f66d4306e6baf994fc6e024 Mon Sep 17 00:00:00 2001 From: Kevin Carter Date: Tue, 2 Mar 2021 13:21:23 -0600 Subject: [PATCH] Add label argument for the container image build This change adds a new argument for the container image build CLI, which allows operators to inject specific labels into a build that also supports basic string replacement from user input. This feature will allow maintainers and vendors to appropriately label their container images without running any post validation or injection. Change-Id: Ia1337dcbeceed485eda14733221164529daf0856 Signed-off-by: Kevin Carter (cherry picked from commit 2c5927a5ad3c2066b4aebd8bc1cee9563454eaa7) --- .../image-build-labels-97fda64f693cd8ba.yaml | 12 +++++++ tripleoclient/v1/tripleo_container_image.py | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 releasenotes/notes/image-build-labels-97fda64f693cd8ba.yaml diff --git a/releasenotes/notes/image-build-labels-97fda64f693cd8ba.yaml b/releasenotes/notes/image-build-labels-97fda64f693cd8ba.yaml new file mode 100644 index 000000000..666cf45e4 --- /dev/null +++ b/releasenotes/notes/image-build-labels-97fda64f693cd8ba.yaml @@ -0,0 +1,12 @@ +--- +features: + - The container image build command now has the ability to inject labels + into various images being constructed. To add labels into a container, + the argument `--label` can be specified multiple times. The value is + always a key=value pair and each key must be unique. +other: + - The container image build label agument has the ability to do simple + string replacements following the python standard. Available options + for string replacement are `registry`, `namespace`, `prefix`, + `image`, `tag`, and `name`. Example usage + `--label component="%(prefix)s-%(name)s-container"`. diff --git a/tripleoclient/v1/tripleo_container_image.py b/tripleoclient/v1/tripleo_container_image.py index 7160528b3..d7f717151 100644 --- a/tripleoclient/v1/tripleo_container_image.py +++ b/tripleoclient/v1/tripleo_container_image.py @@ -184,6 +184,18 @@ class Build(command.Command): "(default: %(default)s)" ), ) + parser.add_argument( + "--label", + dest="labels", + metavar="", + default=[], + action="append", + help=_( + "Add labels to the containers. This option can be " + "specified multiple times. Each label is a key=value " + "pair." + ), + ) parser.add_argument( "--volume", dest="volumes", @@ -562,6 +574,26 @@ class Build(command.Command): rhel_modules.update({name: version}) image_config['tcib_rhel_modules'] = rhel_modules + if parsed_args.labels: + _desc = "OpenStack Platform {}".format(image_parsed_name) + label_data = image_config['tcib_labels'] = { + "tcib_managed": True, + "maintainer": "OpenStack TripleO Team", + "description": _desc, + "summary": _desc, + "io.k8s.display-name": _desc, + } + for item in parsed_args.labels: + key, value = item.split("=", 1) + label_data[key] = value % dict( + registry=parsed_args.registry, + namespace=parsed_args.namespace, + prefix=parsed_args.prefix, + image=image_name, + tag=parsed_args.tag, + name=image_parsed_name, + ) + # NOTE(cloudnull): Check if the reference config has a valid # "from" option. If the reference "from" # option is valid, it will be used.