From 43f051313e9b39d03354725f03d4d9c0a66bdcac Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Fri, 22 Nov 2019 18:09:00 +0000 Subject: [PATCH 1/2] virtcontainers: update resources after adding container to sandbox Status of container should know prior to calculate the number of CPU and memory Signed-off-by: Julio Montes --- virtcontainers/sandbox.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index 4a6a0d6d52..8dde060492 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -1176,12 +1176,6 @@ func (s *Sandbox) CreateContainer(contConfig ContainerConfig) (VCContainer, erro } }() - // Sandbox is reponsable to update VM resources needed by Containers - err = s.updateResources() - if err != nil { - return nil, err - } - err = c.create() if err != nil { return nil, err @@ -1192,6 +1186,14 @@ func (s *Sandbox) CreateContainer(contConfig ContainerConfig) (VCContainer, erro return nil, err } + // Sandbox is reponsable to update VM resources needed by Containers + // Update resources after having added containers to the sandbox, since + // container status is requiered to know if more resources should be added. + err = s.updateResources() + if err != nil { + return nil, err + } + // Store it. err = c.storeContainer() if err != nil { @@ -1228,7 +1230,13 @@ func (s *Sandbox) StartContainer(containerID string) (VCContainer, error) { } s.Logger().Info("Container is started") - //Fixme Container delete from sandbox, need to update resources + + // Update sandbox resources in case a stopped container + // is started + err = s.updateResources() + if err != nil { + return nil, err + } return c, nil } @@ -1487,10 +1495,6 @@ func (s *Sandbox) createContainers() error { span, _ := s.trace("createContainers") defer span.Finish() - if err := s.updateResources(); err != nil { - return err - } - for _, contConfig := range s.config.Containers { c, err := newContainer(s, &contConfig) @@ -1506,6 +1510,12 @@ func (s *Sandbox) createContainers() error { } } + // Update resources after having added containers to the sandbox, since + // container status is requiered to know if more resources should be added. + if err := s.updateResources(); err != nil { + return err + } + if err := s.cgroupsUpdate(); err != nil { return err } From b7731e97dd280d2777a10b61388f01d32e2f9bf2 Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Fri, 22 Nov 2019 18:08:40 +0000 Subject: [PATCH 2/2] virtcontainers: don't consider non-running container resources Don't hot add again non-running container resources to avoid having extra and useless resources fixes #2186 Signed-off-by: Julio Montes --- virtcontainers/sandbox.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index 8dde060492..7a3bcb811e 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -1961,6 +1961,12 @@ func (s *Sandbox) updateResources() error { func (s *Sandbox) calculateSandboxMemory() int64 { memorySandbox := int64(0) for _, c := range s.config.Containers { + // Do not hot add again non-running containers resources + if cont, ok := s.containers[c.ID]; ok && cont.state.State == types.StateStopped { + s.Logger().WithField("container-id", c.ID).Debug("Do not taking into account memory resources of not running containers") + continue + } + if m := c.Resources.Memory; m != nil && m.Limit != nil { memorySandbox += *m.Limit } @@ -1972,6 +1978,12 @@ func (s *Sandbox) calculateSandboxCPUs() uint32 { mCPU := uint32(0) for _, c := range s.config.Containers { + // Do not hot add again non-running containers resources + if cont, ok := s.containers[c.ID]; ok && cont.state.State == types.StateStopped { + s.Logger().WithField("container-id", c.ID).Debug("Do not taking into account CPU resources of not running containers") + continue + } + if cpu := c.Resources.CPU; cpu != nil { if cpu.Period != nil && cpu.Quota != nil { mCPU += utils.CalculateMilliCPUs(*cpu.Quota, *cpu.Period)