Skip to content

Commit 0ddcf64

Browse files
committed
feat(internal): allow users to add labels when cloning container or running in context
1 parent d72c90c commit 0ddcf64

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

cli/tools/container_clone.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type ContainerCloneCommand struct {
3535
AddHost []string
3636
Env []string
3737
Entrypoint string
38+
ForkLabels []string
39+
Labels []string
3840
}
3941

4042
// NewContainerCloneCommand creates a new container clone command
@@ -62,6 +64,8 @@ func NewContainerCloneCommand(ctx cli.Cli) *cobra.Command {
6264
cmd.Flags().BoolVar(&command.WaitForExit, "wait-for-exit", false, "Wait for the container to stop/exit before updating")
6365
cmd.Flags().BoolVar(&command.CheckForUpdate, "check", false, "Only check if an update is necessary, don't perform the update")
6466
cmd.Flags().StringVar(&command.Entrypoint, "entrypoint", "", "Change the container entrypoint when cloning the container")
67+
cmd.Flags().StringSliceVarP(&command.Labels, "label", "l", []string{}, "Set meta data on the new container")
68+
cmd.Flags().StringSliceVar(&command.ForkLabels, "fork-label", []string{}, "Set meta data on a the forked container")
6569

6670
command.Command = cmd
6771
return cmd
@@ -180,6 +184,10 @@ func (c *ContainerCloneCommand) RunE(cmd *cobra.Command, args []string) error {
180184
forkCmd = append(forkCmd, "--rm")
181185
}
182186

187+
for _, v := range c.Labels {
188+
forkCmd = append(forkCmd, "--label", v)
189+
}
190+
183191
for _, v := range c.AddHost {
184192
forkCmd = append(forkCmd, "--add-host", v)
185193
}
@@ -210,6 +218,9 @@ func (c *ContainerCloneCommand) RunE(cmd *cobra.Command, args []string) error {
210218

211219
// Ensure ports don't conflict with any other containers
212220
IgnorePorts: true,
221+
222+
// Labels for the forked container
223+
Labels: container.FormatLabels(c.ForkLabels),
213224
}
214225

215226
return containerCli.Fork(context.Background(), currentContainer, cloneOptions)
@@ -231,5 +242,6 @@ func (c *ContainerCloneCommand) RunE(cmd *cobra.Command, args []string) error {
231242
ExtraHosts: c.AddHost,
232243
Entrypoint: entrypoint,
233244
Cmd: containerCmd,
245+
Labels: container.FormatLabels(c.Labels),
234246
})
235247
}

cli/tools/run_context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type ContainerRunInContextCommand struct {
2828
Env []string
2929
AutoRemove bool
3030
Entrypoint string
31+
Labels []string
3132
}
3233

3334
// NewRunRemoteAccessCommand creates a new c8y remote access command
@@ -47,6 +48,7 @@ func NewContainerRunInContextCommand(ctx cli.Cli) *cobra.Command {
4748
cmd.Flags().StringVar(&command.Entrypoint, "entrypoint", "", "Overwrite the default ENTRYPOINT of the image")
4849
cmd.Flags().StringSliceVarP(&command.Env, "env", "e", []string{}, "Set environment variables")
4950
cmd.Flags().BoolVar(&command.AutoRemove, "rm", false, "Auto remove the closed container on exit")
51+
cmd.Flags().StringSliceVarP(&command.Labels, "label", "l", []string{}, "Set meta data on a container")
5052

5153
command.Command = cmd
5254
return cmd
@@ -140,6 +142,7 @@ func (c *ContainerRunInContextCommand) RunE(cmd *cobra.Command, args []string) e
140142
Entrypoint: entrypoint,
141143
Cmd: containerCmd,
142144
IgnorePorts: true,
145+
Labels: container.FormatLabels(c.Labels),
143146
}
144147

145148
// Container config

pkg/container/container.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ type CloneOptions struct {
971971
Cmd strslice.StrSlice
972972
Entrypoint strslice.StrSlice
973973
IgnorePorts bool
974+
Labels map[string]string
974975
}
975976

976977
func FormatContainerName(v string) string {
@@ -1268,6 +1269,11 @@ func CloneContainerConfig(ref *container.Config, opts CloneOptions) *container.C
12681269
if opts.Image != "" {
12691270
clonedConfig.Image = opts.Image
12701271
}
1272+
1273+
for label, value := range opts.Labels {
1274+
clonedConfig.Labels[label] = value
1275+
}
1276+
12711277
clonedConfig.Env = append(clonedConfig.Env, opts.Env...)
12721278
return clonedConfig
12731279
}
@@ -1330,3 +1336,13 @@ func CloneNetworkConfig(ref *types.NetworkSettings) *network.NetworkingConfig {
13301336
}
13311337
return networkConfig
13321338
}
1339+
1340+
func FormatLabels(labels []string) map[string]string {
1341+
labelMap := make(map[string]string)
1342+
for _, label := range labels {
1343+
if name, value, found := strings.Cut(label, "="); found {
1344+
labelMap[name] = value
1345+
}
1346+
}
1347+
return labelMap
1348+
}

tests/operations-clone.robot

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ Clone Existing Container
2727
Create Container app2 docker.io/library/httpd:2.4
2828
${prev_container_id}= DeviceLibrary.Execute Command cmd=sudo tedge-container engine docker container inspect app2 --format "{{.Id}}"
2929

30-
DeviceLibrary.Execute Command sudo tedge-container tools container-clone --container app2 --force
30+
DeviceLibrary.Execute Command cmd=sudo tedge-container tools container-clone --container app2 --force --label io.thin-edge.last_id=${prev_container_id}
3131

3232
${next_container_id}= DeviceLibrary.Execute Command cmd=sudo tedge-container engine docker inspect app2 --format "{{.Id}}"
3333
Should Not Be Equal ${next_container_id} ${prev_container_id}
3434

35+
${label_prev_id}= DeviceLibrary.Execute Command cmd=sudo tedge-container engine docker inspect app2 --format '{{ index .Config.Labels "io.thin-edge.last_id" }}'
36+
Should Be Equal ${label_prev_id} ${prev_container_id}
37+
3538
Clone Existing Container by Timeout Whilst Waiting For Exit
3639
Create Container app3 docker.io/library/httpd:2.4
3740
${prev_container_id}= DeviceLibrary.Execute Command cmd=sudo tedge-container engine docker container inspect app3 --format "{{.Id}}"

0 commit comments

Comments
 (0)