summaryrefslogtreecommitdiff
path: root/libpod/pod.go
diff options
context:
space:
mode:
authorBrent Baude <bbaude@redhat.com>2021-07-14 16:03:55 -0500
committerBrent Baude <bbaude@redhat.com>2021-08-04 14:14:36 -0500
commit3c3fa6fac4d0f8e89181ea2d4e1fe0318d24b6f4 (patch)
treef087d0a772797a9028df514d8d0369835724b3a2 /libpod/pod.go
parente93661f5e765d84893e2ad5a488682c0a67412d0 (diff)
downloadpodman-3c3fa6fac4d0f8e89181ea2d4e1fe0318d24b6f4.tar.gz
podman-3c3fa6fac4d0f8e89181ea2d4e1fe0318d24b6f4.tar.bz2
podman-3c3fa6fac4d0f8e89181ea2d4e1fe0318d24b6f4.zip
implement init containers in podman
this is the first pass at implementing init containers for podman pods. init containersare made popular by k8s as a way to run setup for pods before the pods standard containers run. unlike k8s, we support two styles of init containers: always and oneshot. always means the container stays in the pod and starts whenever a pod is started. this does not apply to pods restarting. oneshot means the container runs onetime when the pod starts and then is removed. Signed-off-by: Brent Baude <bbaude@redhat.com>
Diffstat (limited to 'libpod/pod.go')
-rw-r--r--libpod/pod.go37
1 files changed, 30 insertions, 7 deletions
diff --git a/libpod/pod.go b/libpod/pod.go
index 62f5c9e5b..0fef7f6f3 100644
--- a/libpod/pod.go
+++ b/libpod/pod.go
@@ -3,6 +3,7 @@ package libpod
import (
"context"
"net"
+ "sort"
"time"
"github.com/containers/podman/v3/libpod/define"
@@ -332,17 +333,20 @@ func (p *Pod) SharesNamespaces() bool {
return p.SharesPID() || p.SharesIPC() || p.SharesNet() || p.SharesMount() || p.SharesUser() || p.SharesUTS()
}
+// infraContainerID returns the infra ID without a lock
+func (p *Pod) infraContainerID() (string, error) {
+ if err := p.updatePod(); err != nil {
+ return "", err
+ }
+ return p.state.InfraContainerID, nil
+}
+
// InfraContainerID returns the infra container ID for a pod.
// If the container returned is "", the pod has no infra container.
func (p *Pod) InfraContainerID() (string, error) {
p.lock.Lock()
defer p.lock.Unlock()
-
- if err := p.updatePod(); err != nil {
- return "", err
- }
-
- return p.state.InfraContainerID, nil
+ return p.infraContainerID()
}
// InfraContainer returns the infra container.
@@ -350,7 +354,6 @@ func (p *Pod) InfraContainer() (*Container, error) {
if !p.HasInfraContainer() {
return nil, errors.Wrap(define.ErrNoSuchCtr, "pod has no infra container")
}
-
id, err := p.InfraContainerID()
if err != nil {
return nil, err
@@ -420,3 +423,23 @@ func (p *Pod) ProcessLabel() (string, error) {
}
return ctr.ProcessLabel(), nil
}
+
+// initContainers returns the list of initcontainers
+// in a pod sorted by create time
+func (p *Pod) initContainers() ([]*Container, error) {
+ initCons := make([]*Container, 0)
+ // the pod is already locked when this is called
+ cons, err := p.allContainers()
+ if err != nil {
+ return nil, err
+ }
+ // Sort the pod containers by created time
+ sort.Slice(cons, func(i, j int) bool { return cons[i].CreatedTime().Before(cons[j].CreatedTime()) })
+ // Iterate sorted containers and add ids for any init containers
+ for _, c := range cons {
+ if len(c.config.InitContainerType) > 0 {
+ initCons = append(initCons, c)
+ }
+ }
+ return initCons, nil
+}