Skip to content

Commit 041eb3a

Browse files
committed
container_opts.go: add WithAdditionalContainerLabels
WithAdditionalContainerLabels() preserves the existing entries in c.Labels. OTOH, WithContainerLabels() clears them. Signed-off-by: Akihiro Suda <[email protected]>
1 parent 7b0149a commit 041eb3a

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

container_opts.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,31 @@ func WithImageName(n string) NewContainerOpts {
8585
}
8686
}
8787

88-
// WithContainerLabels adds the provided labels to the container
88+
// WithContainerLabels sets the provided labels to the container.
89+
// The existing labels are cleared.
90+
// Use WithAdditionalContainerLabels to preserve the existing labels.
8991
func WithContainerLabels(labels map[string]string) NewContainerOpts {
9092
return func(_ context.Context, _ *Client, c *containers.Container) error {
9193
c.Labels = labels
9294
return nil
9395
}
9496
}
9597

98+
// WithAdditionalContainerLabels adds the provided labels to the container
99+
// The existing labels are preserved as long as they do not conflict with the added labels.
100+
func WithAdditionalContainerLabels(labels map[string]string) NewContainerOpts {
101+
return func(_ context.Context, _ *Client, c *containers.Container) error {
102+
if c.Labels == nil {
103+
c.Labels = labels
104+
return nil
105+
}
106+
for k, v := range labels {
107+
c.Labels[k] = v
108+
}
109+
return nil
110+
}
111+
}
112+
96113
// WithImageStopSignal sets a well-known containerd label (StopSignalLabel)
97114
// on the container for storing the stop signal specified in the OCI image
98115
// config

0 commit comments

Comments
 (0)