pod config: add a `CreateCommand` field

Add a `CreateCommand` field to the pod config which includes the entire
`os.Args` at pod-creation.  Similar to the already existing field in a
container config, we need this information to properly generate generic
systemd unit files for pods.  It's a prerequisite to support the `--new`
flag for pods.

Also add the `CreateCommand` to the pod-inspect data, which can come in
handy for debugging, general inspection and certainly for the tests that
are added along with the other changes.

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
Valentin Rothberg 2020-05-28 10:54:17 +02:00
parent 7b85d5c6d2
commit 636881ece5
6 changed files with 43 additions and 0 deletions

View File

@ -18,6 +18,9 @@ type InspectPodData struct {
Namespace string `json:"Namespace,omitempty"`
// Created is the time when the pod was created.
Created time.Time
// CreateCommand is the full command plus arguments of the process the
// container has been created with.
CreateCommand []string `json:"CreateCommand,omitempty"`
// State represents the current state of the pod.
State string `json:"State"`
// Hostname is the hostname that the pod will set.

View File

@ -1538,6 +1538,18 @@ func WithPodHostname(hostname string) PodCreateOption {
}
}
// WithPodCreateCommand adds the full command plus arguments of the current
// process to the pod config.
func WithPodCreateCommand() PodCreateOption {
return func(pod *Pod) error {
if pod.valid {
return define.ErrPodFinalized
}
pod.config.CreateCommand = os.Args
return nil
}
}
// WithPodLabels sets the labels of a pod.
func WithPodLabels(labels map[string]string) PodCreateOption {
return func(pod *Pod) error {

View File

@ -64,6 +64,10 @@ type PodConfig struct {
// Time pod was created
CreatedTime time.Time `json:"created"`
// CreateCommand is the full command plus arguments of the process the
// container has been created with.
CreateCommand []string `json:"CreateCommand,omitempty"`
// ID of the pod's lock
LockID uint32 `json:"lockID"`
}

View File

@ -489,6 +489,7 @@ func (p *Pod) Inspect() (*define.InspectPodData, error) {
Name: p.Name(),
Namespace: p.Namespace(),
Created: p.CreatedTime(),
CreateCommand: p.config.CreateCommand,
State: podState,
Hostname: p.config.Hostname,
Labels: p.Labels(),

View File

@ -93,5 +93,6 @@ func createPodOptions(p *specgen.PodSpecGenerator) ([]libpod.PodCreateOption, er
options = append(options, libpod.WithInfraContainerPorts(ports))
}
options = append(options, libpod.WithPodCgroups())
options = append(options, libpod.WithPodCreateCommand())
return options, nil
}

View File

@ -57,4 +57,26 @@ var _ = Describe("Podman pod inspect", func() {
podData := inspect.InspectPodToJSON()
Expect(podData.ID).To(Equal(podid))
})
It("podman pod inspect (CreateCommand)", func() {
podName := "myTestPod"
createCommand := []string{"pod", "create", "--name", podName, "--hostname", "rudolph", "--share", "net"}
// Create the pod.
session := podmanTest.Podman(createCommand)
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
// Inspect the pod and make sure that the create command is
// exactly how we created the pod.
inspect := podmanTest.Podman([]string{"pod", "inspect", podName})
inspect.WaitWithDefaultTimeout()
Expect(inspect.ExitCode()).To(Equal(0))
Expect(inspect.IsJSONOutputValid()).To(BeTrue())
podData := inspect.InspectPodToJSON()
// Let's get the last len(createCommand) items in the command.
inspectCreateCommand := podData.CreateCommand
index := len(inspectCreateCommand) - len(createCommand)
Expect(inspectCreateCommand[index:]).To(Equal(createCommand))
})
})