github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/libpod/pod_internal.go (about) 1 package libpod 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "time" 7 8 "github.com/containers/common/pkg/config" 9 "github.com/containers/libpod/libpod/define" 10 "github.com/containers/storage/pkg/stringid" 11 "github.com/pkg/errors" 12 "github.com/sirupsen/logrus" 13 ) 14 15 // Creates a new, empty pod 16 func newPod(runtime *Runtime) (*Pod, error) { 17 pod := new(Pod) 18 pod.config = new(PodConfig) 19 pod.config.ID = stringid.GenerateNonCryptoID() 20 pod.config.Labels = make(map[string]string) 21 pod.config.CreatedTime = time.Now() 22 pod.config.InfraContainer = new(InfraContainerConfig) 23 pod.state = new(podState) 24 pod.runtime = runtime 25 26 return pod, nil 27 } 28 29 // Update pod state from database 30 func (p *Pod) updatePod() error { 31 if err := p.runtime.state.UpdatePod(p); err != nil { 32 return err 33 } 34 35 return nil 36 } 37 38 // Save pod state to database 39 func (p *Pod) save() error { 40 if err := p.runtime.state.SavePod(p); err != nil { 41 return errors.Wrapf(err, "error saving pod %s state", p.ID()) 42 } 43 44 return nil 45 } 46 47 // Refresh a pod's state after restart 48 // This cannot lock any other pod, but may lock individual containers, as those 49 // will have refreshed by the time pod refresh runs. 50 func (p *Pod) refresh() error { 51 // Need to to an update from the DB to pull potentially-missing state 52 if err := p.runtime.state.UpdatePod(p); err != nil { 53 return err 54 } 55 56 if !p.valid { 57 return define.ErrPodRemoved 58 } 59 60 // Retrieve the pod's lock 61 lock, err := p.runtime.lockManager.AllocateAndRetrieveLock(p.config.LockID) 62 if err != nil { 63 return errors.Wrapf(err, "error retrieving lock %d for pod %s", p.config.LockID, p.ID()) 64 } 65 p.lock = lock 66 67 // We need to recreate the pod's cgroup 68 if p.config.UsePodCgroup { 69 switch p.runtime.config.Engine.CgroupManager { 70 case config.SystemdCgroupsManager: 71 cgroupPath, err := systemdSliceFromPath(p.config.CgroupParent, fmt.Sprintf("libpod_pod_%s", p.ID())) 72 if err != nil { 73 logrus.Errorf("Error creating CGroup for pod %s: %v", p.ID(), err) 74 } 75 p.state.CgroupPath = cgroupPath 76 case config.CgroupfsCgroupsManager: 77 p.state.CgroupPath = filepath.Join(p.config.CgroupParent, p.ID()) 78 79 logrus.Debugf("setting pod cgroup to %s", p.state.CgroupPath) 80 default: 81 return errors.Wrapf(define.ErrInvalidArg, "unknown cgroups manager %s specified", p.runtime.config.Engine.CgroupManager) 82 } 83 } 84 85 // Save changes 86 return p.save() 87 }