Skip to content

Commit

Permalink
feature: UpdateContainerResources of CRI Manager
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed Jun 13, 2018
1 parent 3f0c62f commit 390d95e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
29 changes: 28 additions & 1 deletion cri/v1alpha1/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,34 @@ func (c *CriManager) ListContainerStats(ctx context.Context, r *runtime.ListCont

// UpdateContainerResources updates ContainerConfig of the container.
func (c *CriManager) UpdateContainerResources(ctx context.Context, r *runtime.UpdateContainerResourcesRequest) (*runtime.UpdateContainerResourcesResponse, error) {
return nil, fmt.Errorf("UpdateContainerResources Not Implemented Yet")
containerID := r.GetContainerId()
container, err := c.ContainerMgr.Get(ctx, containerID)
if err != nil {
return nil, fmt.Errorf("failed to get container %q: %v", containerID, err)
}

// cannot update container resource when it is in removing state
if container.IsRemoving() {
return nil, fmt.Errorf("cannot to update resource for container %q when it is in removing state", containerID)
}

resources := r.GetLinux()
updateConfig := &apitypes.UpdateConfig{
Resources: apitypes.Resources{
CPUPeriod: resources.GetCpuPeriod(),
CPUQuota: resources.GetCpuQuota(),
CPUShares: resources.GetCpuShares(),
Memory: resources.GetMemoryLimitInBytes(),
CpusetCpus: resources.GetCpusetCpus(),
CpusetMems: resources.GetCpusetMems(),
},
}
err = c.ContainerMgr.Update(ctx, containerID, updateConfig)
if err != nil {
return nil, fmt.Errorf("failed to update resource for container %q: %v", containerID, err)
}

return &runtime.UpdateContainerResourcesResponse{}, nil
}

// ExecSync executes a command in the container, and returns the stdout output.
Expand Down
29 changes: 28 additions & 1 deletion cri/v1alpha2/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,34 @@ func (c *CriManager) ListContainerStats(ctx context.Context, r *runtime.ListCont

// UpdateContainerResources updates ContainerConfig of the container.
func (c *CriManager) UpdateContainerResources(ctx context.Context, r *runtime.UpdateContainerResourcesRequest) (*runtime.UpdateContainerResourcesResponse, error) {
return nil, fmt.Errorf("UpdateContainerResources Not Implemented Yet")
containerID := r.GetContainerId()
container, err := c.ContainerMgr.Get(ctx, containerID)
if err != nil {
return nil, fmt.Errorf("failed to get container %q: %v", containerID, err)
}

// cannot update container resource when it is in removing state
if container.IsRemoving() {
return nil, fmt.Errorf("cannot to update resource for container %q when it is in removing state", containerID)
}

resources := r.GetLinux()
updateConfig := &apitypes.UpdateConfig{
Resources: apitypes.Resources{
CPUPeriod: resources.GetCpuPeriod(),
CPUQuota: resources.GetCpuQuota(),
CPUShares: resources.GetCpuShares(),
Memory: resources.GetMemoryLimitInBytes(),
CpusetCpus: resources.GetCpusetCpus(),
CpusetMems: resources.GetCpusetMems(),
},
}
err = c.ContainerMgr.Update(ctx, containerID, updateConfig)
if err != nil {
return nil, fmt.Errorf("failed to update resource for container %q: %v", containerID, err)
}

return &runtime.UpdateContainerResourcesResponse{}, nil
}

// ReopenContainerLog asks runtime to reopen the stdout/stderr log file
Expand Down
2 changes: 1 addition & 1 deletion daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty

c.Lock()
// TODO update restartpolicy when container is running.
if config.RestartPolicy.Name != "" {
if config.RestartPolicy != nil && config.RestartPolicy.Name != "" {
c.HostConfig.RestartPolicy = config.RestartPolicy
}
c.Unlock()
Expand Down
7 changes: 7 additions & 0 deletions daemon/mgr/container_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ func (c *Container) IsPaused() bool {
return c.State.Status == types.StatusPaused
}

// IsRemoving returns container is removing or not.
func (c *Container) IsRemoving() bool {
c.Lock()
defer c.Unlock()
return c.State.Status == types.StatusRemoving
}

// IsDead returns container is dead or not.
func (c *Container) IsDead() bool {
c.Lock()
Expand Down

0 comments on commit 390d95e

Please sign in to comment.