container_opts.go: add WithAdditionalContainerLabels

WithAdditionalContainerLabels() preserves the existing entries in c.Labels.
OTOH, WithContainerLabels() clears them.

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
This commit is contained in:
Akihiro Suda 2020-12-14 16:44:47 +09:00
parent 7b0149ac4a
commit 041eb3ac31
No known key found for this signature in database
GPG Key ID: 49524C6F9F638F1A

View File

@ -85,7 +85,9 @@ func WithImageName(n string) NewContainerOpts {
}
}
// WithContainerLabels adds the provided labels to the container
// WithContainerLabels sets the provided labels to the container.
// The existing labels are cleared.
// Use WithAdditionalContainerLabels to preserve the existing labels.
func WithContainerLabels(labels map[string]string) NewContainerOpts {
return func(_ context.Context, _ *Client, c *containers.Container) error {
c.Labels = labels
@ -93,6 +95,21 @@ func WithContainerLabels(labels map[string]string) NewContainerOpts {
}
}
// WithAdditionalContainerLabels adds the provided labels to the container
// The existing labels are preserved as long as they do not conflict with the added labels.
func WithAdditionalContainerLabels(labels map[string]string) NewContainerOpts {
return func(_ context.Context, _ *Client, c *containers.Container) error {
if c.Labels == nil {
c.Labels = labels
return nil
}
for k, v := range labels {
c.Labels[k] = v
}
return nil
}
}
// WithImageStopSignal sets a well-known containerd label (StopSignalLabel)
// on the container for storing the stop signal specified in the OCI image
// config