mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-26 15:32:30 +00:00
Merge pull request #2251 from devimc/topic/k8s/fixWrongNumberCPUs
k8s: fix wrong number cpus after killing a container
This commit is contained in:
commit
d054556f60
@ -1174,12 +1174,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()
|
err = c.create()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -1190,6 +1184,14 @@ func (s *Sandbox) CreateContainer(contConfig ContainerConfig) (VCContainer, erro
|
|||||||
return nil, err
|
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.
|
// Store it.
|
||||||
err = c.storeContainer()
|
err = c.storeContainer()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1226,7 +1228,13 @@ func (s *Sandbox) StartContainer(containerID string) (VCContainer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.Logger().Info("Container is started")
|
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
|
return c, nil
|
||||||
}
|
}
|
||||||
@ -1484,10 +1492,6 @@ func (s *Sandbox) createContainers() error {
|
|||||||
span, _ := s.trace("createContainers")
|
span, _ := s.trace("createContainers")
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
if err := s.updateResources(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, contConfig := range s.config.Containers {
|
for _, contConfig := range s.config.Containers {
|
||||||
|
|
||||||
c, err := newContainer(s, &contConfig)
|
c, err := newContainer(s, &contConfig)
|
||||||
@ -1503,6 +1507,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 {
|
if err := s.cgroupsUpdate(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -1961,6 +1971,12 @@ func (s *Sandbox) updateResources() error {
|
|||||||
func (s *Sandbox) calculateSandboxMemory() int64 {
|
func (s *Sandbox) calculateSandboxMemory() int64 {
|
||||||
memorySandbox := int64(0)
|
memorySandbox := int64(0)
|
||||||
for _, c := range s.config.Containers {
|
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 {
|
if m := c.Resources.Memory; m != nil && m.Limit != nil {
|
||||||
memorySandbox += *m.Limit
|
memorySandbox += *m.Limit
|
||||||
}
|
}
|
||||||
@ -1972,6 +1988,12 @@ func (s *Sandbox) calculateSandboxCPUs() uint32 {
|
|||||||
mCPU := uint32(0)
|
mCPU := uint32(0)
|
||||||
|
|
||||||
for _, c := range s.config.Containers {
|
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 := c.Resources.CPU; cpu != nil {
|
||||||
if cpu.Period != nil && cpu.Quota != nil {
|
if cpu.Period != nil && cpu.Quota != nil {
|
||||||
mCPU += utils.CalculateMilliCPUs(*cpu.Quota, *cpu.Period)
|
mCPU += utils.CalculateMilliCPUs(*cpu.Quota, *cpu.Period)
|
||||||
|
Loading…
Reference in New Issue
Block a user