Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if we need to add lock here for container.

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