aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoaz Shuster <boaz.shuster.github@gmail.com>2021-11-11 11:45:36 +0200
committerBoaz Shuster <boaz.shuster.github@gmail.com>2021-11-11 11:45:36 +0200
commit73e1cdfe9e41a8748b75b9461c087e4cee5d2183 (patch)
treecad9d14e5c451814fcfe035eb52be0f32fb2931a
parent6ee3b33d3825177033763e51f756209102e0a309 (diff)
downloadpodman-73e1cdfe9e41a8748b75b9461c087e4cee5d2183.tar.gz
podman-73e1cdfe9e41a8748b75b9461c087e4cee5d2183.tar.bz2
podman-73e1cdfe9e41a8748b75b9461c087e4cee5d2183.zip
export adding id-specifier code to setContainerNameForTemplate
Signed-off-by: Boaz Shuster <boaz.shuster.github@gmail.com>
-rw-r--r--pkg/systemd/generate/containers.go69
-rw-r--r--pkg/systemd/generate/containers_test.go46
2 files changed, 89 insertions, 26 deletions
diff --git a/pkg/systemd/generate/containers.go b/pkg/systemd/generate/containers.go
index a4b18ec88..95ff13371 100644
--- a/pkg/systemd/generate/containers.go
+++ b/pkg/systemd/generate/containers.go
@@ -206,6 +206,46 @@ func containerServiceName(ctr *libpod.Container, options entities.GenerateSystem
return nameOrID, serviceName
}
+// setContainerNameForTemplate updates startCommand to contain the name argument with
+// a value that includes the identify specifier.
+// In case startCommand doesn't contain that argument it's added after "run" and its
+// value will be set to info.ServiceName concated with the identify specifier %i.
+func setContainerNameForTemplate(startCommand []string, info *containerInfo) ([]string, error) {
+ // find the index of "--name" in the command slice
+ nameIx := -1
+ for argIx, arg := range startCommand {
+ if arg == "--name" {
+ nameIx = argIx + 1
+ break
+ }
+ if strings.HasPrefix(arg, "--name=") {
+ nameIx = argIx
+ break
+ }
+ }
+ switch {
+ case nameIx == -1:
+ // if not found, add --name argument in the command slice before the "run" argument.
+ // it's assumed that the command slice contains this argument.
+ runIx := -1
+ for argIx, arg := range startCommand {
+ if arg == "run" {
+ runIx = argIx
+ break
+ }
+ }
+ if runIx == -1 {
+ return startCommand, fmt.Errorf("\"run\" is missing in the command arguments")
+ }
+ startCommand = append(startCommand[:runIx+1], startCommand[runIx:]...)
+ startCommand[runIx+1] = fmt.Sprintf("--name=%s-%%i", info.ServiceName)
+ default:
+ // append the identity specifier (%i) to the end of the --name value
+ startCommand[nameIx] = fmt.Sprintf("%s-%%i", startCommand[nameIx])
+ }
+ return startCommand, nil
+}
+
// executeContainerTemplate executes the container template on the specified
// containerInfo. Note that the containerInfo is also post processed and
// completed, which allows for an easier unit testing.
@@ -392,32 +432,9 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst
startCommand = escapeSystemdArguments(startCommand)
if options.TemplateUnitFile {
info.IdentifySpecifier = true
- runIx := -1
- nameIx := -1
- // Add systemd identify specifier next to the name value
- // to set a name to the container according to the parameters passed to systemd.
- // In case no --name set for that container, use ServiceName specified
- // in the containerInfo struct.
- for argIx, arg := range startCommand {
- if arg == "run" {
- runIx = argIx
- continue
- }
- if arg == "--name" {
- nameIx = argIx + 1
- break
- }
- if strings.HasPrefix(arg, "--name=") {
- nameIx = argIx
- break
- }
- }
- if nameIx == -1 {
- startCommand = append(startCommand[:runIx+1], startCommand[runIx:]...)
- startCommand[runIx+1] = fmt.Sprintf("--name=%s-%%i", info.ServiceName)
- fmt.Println(startCommand)
- } else {
- startCommand[nameIx] = fmt.Sprintf("%s-%%i", startCommand[nameIx])
+ startCommand, err = setContainerNameForTemplate(startCommand, info)
+ if err != nil {
+ return "", err
}
}
info.ExecStart = strings.Join(startCommand, " ")
diff --git a/pkg/systemd/generate/containers_test.go b/pkg/systemd/generate/containers_test.go
index 85aeb92b8..eab2c2e67 100644
--- a/pkg/systemd/generate/containers_test.go
+++ b/pkg/systemd/generate/containers_test.go
@@ -1,6 +1,7 @@
package generate
import (
+ "fmt"
"testing"
"github.com/containers/podman/v3/pkg/domain/entities"
@@ -1036,3 +1037,48 @@ WantedBy=multi-user.target default.target
})
}
}
+
+func TestSetContainerNameForTemplate(t *testing.T) {
+ tt := []struct {
+ name string
+ startCommand []string
+ info *containerInfo
+ expected []string
+ err error
+ }{
+ {
+ name: "no name argument is set",
+ startCommand: []string{"/usr/bin/podman", "run", "busybox", "top"},
+ info: &containerInfo{ServiceName: "container-122"},
+ expected: []string{"/usr/bin/podman", "run", "--name=container-122-%i", "busybox", "top"},
+ err: nil,
+ },
+ {
+ name: "--name=value is used in arguments",
+ startCommand: []string{"/usr/bin/podman", "run", "--name=lovely_james", "busybox", "top"},
+ info: &containerInfo{},
+ expected: []string{"/usr/bin/podman", "run", "--name=lovely_james-%i", "busybox", "top"},
+ err: nil,
+ },
+ {
+ name: "--name value is used in arguments",
+ startCommand: []string{"/usr/bin/podman", "run", "--name", "lovely_james", "busybox", "top"},
+ info: &containerInfo{},
+ expected: []string{"/usr/bin/podman", "run", "--name", "lovely_james-%i", "busybox", "top"},
+ err: nil,
+ },
+ {
+ name: "--name value is used in arguments",
+ startCommand: []string{"/usr/bin/podman", "create", "busybox", "top"},
+ info: &containerInfo{},
+ expected: []string{"/usr/bin/podman", "create", "busybox", "top"},
+ err: fmt.Errorf("\"run\" is missing in the command arguments"),
+ },
+ }
+
+ for _, te := range tt {
+ res, err := setContainerNameForTemplate(te.startCommand, te.info)
+ assert.Equal(t, te.err, err)
+ assert.Equal(t, te.expected, res)
+ }
+}