Skip to content

Commit e545e54

Browse files
committed
feat(internal): support user specified container name prefix when starting a container in context
1 parent a5e3645 commit e545e54

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

cli/tools/run_context.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/spf13/cobra"
1616
"github.com/thin-edge/tedge-container-plugin/pkg/cli"
1717
"github.com/thin-edge/tedge-container-plugin/pkg/container"
18+
"github.com/thin-edge/tedge-container-plugin/pkg/random"
1819
)
1920

2021
type ContainerRunInContextCommand struct {
@@ -29,6 +30,7 @@ type ContainerRunInContextCommand struct {
2930
AutoRemove bool
3031
Entrypoint string
3132
Labels []string
33+
NamePrefix string
3234
}
3335

3436
// NewRunRemoteAccessCommand creates a new c8y remote access command
@@ -46,6 +48,7 @@ func NewContainerRunInContextCommand(ctx cli.Cli) *cobra.Command {
4648
cmd.Flags().StringVar(&command.ContainerID, "container", "", "Container to clone. Either container id or name. By default the container id of the current container will be used")
4749
cmd.Flags().StringVar(&command.Image, "image", "", "Container image")
4850
cmd.Flags().StringVar(&command.Entrypoint, "entrypoint", "", "Overwrite the default ENTRYPOINT of the image")
51+
cmd.Flags().StringVar(&command.NamePrefix, "name-prefix", "", "Prefix to be added when generating the name. If left blank then the default will be used")
4952
cmd.Flags().StringSliceVarP(&command.Env, "env", "e", []string{}, "Set environment variables")
5053
cmd.Flags().BoolVar(&command.AutoRemove, "rm", false, "Auto remove the closed container on exit")
5154
cmd.Flags().StringSliceVarP(&command.Labels, "label", "l", []string{}, "Set meta data on a container")
@@ -155,7 +158,13 @@ func (c *ContainerRunInContextCommand) RunE(cmd *cobra.Command, args []string) e
155158
// Copy network config
156159
networkConfig := container.CloneNetworkConfig(currentContainer.NetworkSettings)
157160

158-
nextContainer, createErr := containerCli.Client.ContainerCreate(ctx, clonedConfig, hostConfig, networkConfig, nil, "")
161+
// container name
162+
containerName := "" // default. let docker create a random name
163+
if c.NamePrefix != "" {
164+
containerName = c.NamePrefix + "_" + random.String(8)
165+
}
166+
167+
nextContainer, createErr := containerCli.Client.ContainerCreate(ctx, clonedConfig, hostConfig, networkConfig, nil, containerName)
159168

160169
if createErr != nil {
161170
slog.Warn("Failed to create container.", "err", createErr)

pkg/random/random.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package random
2+
3+
import (
4+
"math/rand"
5+
"time"
6+
)
7+
8+
const charset = "abcdefghijklmnopqrstuvwxyz" +
9+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
10+
11+
var seededRand *rand.Rand = rand.New(
12+
rand.NewSource(time.Now().UnixNano()))
13+
14+
func StringWithCharset(length int, charset string) string {
15+
b := make([]byte, length)
16+
for i := range b {
17+
b[i] = charset[seededRand.Intn(len(charset))]
18+
}
19+
return string(b)
20+
}
21+
22+
func String(length int) string {
23+
return StringWithCharset(length, charset)
24+
}

0 commit comments

Comments
 (0)