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 12, 2018
1 parent f742cf2 commit 8ab7bfc
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
22 changes: 21 additions & 1 deletion cri/v1alpha1/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,27 @@ 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.State.Status == apitypes.StatusRemoving {
return nil, fmt.Errorf("container %q is in removing state", containerID)
}

resources := resourceToCriResource(r.GetLinux())
updateConfig := &apitypes.UpdateConfig{
Resources: resources,
}
err = c.ContainerMgr.Update(ctx, containerID, updateConfig)
if err != nil {
return nil, fmt.Errorf("failed to update resource for container %q", containerID)
}

return &runtime.UpdateContainerResourcesResponse{}, nil
}

// ExecSync executes a command in the container, and returns the stdout output.
Expand Down
24 changes: 24 additions & 0 deletions cri/v1alpha1/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,3 +770,27 @@ func parseUserFromImageUser(id string) string {
// no group, just return the id
return id
}

// resourceToCriResource converts OCILinuxResource to apitypes.Resources Object.
func resourceToCriResource(newResource *runtime.LinuxContainerResources) apitypes.Resources {
var resources apitypes.Resources
if newResource.GetCpuPeriod() != 0 {
resources.CPUPeriod = newResource.GetCpuPeriod()
}
if newResource.GetCpuQuota() != 0 {
resources.CPUQuota = newResource.GetCpuQuota()
}
if newResource.GetCpuShares() != 0 {
resources.CPUShares = newResource.GetCpuShares()
}
if newResource.GetMemoryLimitInBytes() != 0 {
resources.Memory = newResource.GetMemoryLimitInBytes()
}
if newResource.GetCpusetCpus() != "" {
resources.CpusetCpus = newResource.GetCpusetCpus()
}
if newResource.GetCpusetMems() != "" {
resources.CpusetMems = newResource.GetCpusetMems()
}
return resources
}
22 changes: 21 additions & 1 deletion cri/v1alpha2/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,27 @@ 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.State.Status == apitypes.StatusRemoving {
return nil, fmt.Errorf("container %q is in removing state", containerID)
}

resources := resourceToCriResource(r.GetLinux())
updateConfig := &apitypes.UpdateConfig{
Resources: resources,
}
err = c.ContainerMgr.Update(ctx, containerID, updateConfig)
if err != nil {
return nil, fmt.Errorf("failed to update resource for container %q", containerID)
}

return &runtime.UpdateContainerResourcesResponse{}, nil
}

// ReopenContainerLog asks runtime to reopen the stdout/stderr log file
Expand Down
24 changes: 24 additions & 0 deletions cri/v1alpha2/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,27 @@ func (c *CriManager) attachLog(logPath string, containerID string) error {
}
return nil
}

// resourceToCriResource converts OCILinuxResource to apitypes.Resources Object.
func resourceToCriResource(newResource *runtime.LinuxContainerResources) apitypes.Resources {
var resources apitypes.Resources
if newResource.GetCpuPeriod() != 0 {
resources.CPUPeriod = newResource.GetCpuPeriod()
}
if newResource.GetCpuQuota() != 0 {
resources.CPUQuota = newResource.GetCpuQuota()
}
if newResource.GetCpuShares() != 0 {
resources.CPUShares = newResource.GetCpuShares()
}
if newResource.GetMemoryLimitInBytes() != 0 {
resources.Memory = newResource.GetMemoryLimitInBytes()
}
if newResource.GetCpusetCpus() != "" {
resources.CpusetCpus = newResource.GetCpusetCpus()
}
if newResource.GetCpusetMems() != "" {
resources.CpusetMems = newResource.GetCpusetMems()
}
return resources
}
2 changes: 1 addition & 1 deletion daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
}

// TODO update restartpolicy when container is running.
if config.RestartPolicy.Name != "" {
if config.RestartPolicy != nil && config.RestartPolicy.Name != "" {
c.HostConfig.RestartPolicy = config.RestartPolicy
}

Expand Down

0 comments on commit 8ab7bfc

Please sign in to comment.