Skip to content

Commit e9f6205

Browse files
committed
feature: UpdateContainerResources of CRI Manager
Signed-off-by: Starnop <[email protected]>
1 parent f742cf2 commit e9f6205

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

cri/v1alpha1/cri.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,29 @@ func (c *CriManager) ListContainerStats(ctx context.Context, r *runtime.ListCont
689689

690690
// UpdateContainerResources updates ContainerConfig of the container.
691691
func (c *CriManager) UpdateContainerResources(ctx context.Context, r *runtime.UpdateContainerResourcesRequest) (*runtime.UpdateContainerResourcesResponse, error) {
692-
return nil, fmt.Errorf("UpdateContainerResources Not Implemented Yet")
692+
containerID := r.GetContainerId()
693+
container, err := c.ContainerMgr.Get(ctx, containerID)
694+
if err != nil {
695+
return nil, fmt.Errorf("failed to get container %q: %v", containerID, err)
696+
}
697+
container.Lock()
698+
defer container.Unlock()
699+
700+
// cannot update container resource when it is in removing state
701+
if container.State.Status == apitypes.StatusRemoving {
702+
return nil, fmt.Errorf("cannot to update resource for container %q when it is in removing state", containerID)
703+
}
704+
705+
resources := resourceToCriResource(r.GetLinux())
706+
updateConfig := &apitypes.UpdateConfig{
707+
Resources: resources,
708+
}
709+
err = c.ContainerMgr.Update(ctx, containerID, updateConfig)
710+
if err != nil {
711+
return nil, fmt.Errorf("failed to update resource for container %q with error: %v", containerID, err)
712+
}
713+
714+
return &runtime.UpdateContainerResourcesResponse{}, nil
693715
}
694716

695717
// ExecSync executes a command in the container, and returns the stdout output.

cri/v1alpha1/cri_utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,3 +770,27 @@ func parseUserFromImageUser(id string) string {
770770
// no group, just return the id
771771
return id
772772
}
773+
774+
// resourceToCriResource converts OCILinuxResource to apitypes.Resources Object.
775+
func resourceToCriResource(newResource *runtime.LinuxContainerResources) apitypes.Resources {
776+
var resources apitypes.Resources
777+
if newResource.GetCpuPeriod() != 0 {
778+
resources.CPUPeriod = newResource.GetCpuPeriod()
779+
}
780+
if newResource.GetCpuQuota() != 0 {
781+
resources.CPUQuota = newResource.GetCpuQuota()
782+
}
783+
if newResource.GetCpuShares() != 0 {
784+
resources.CPUShares = newResource.GetCpuShares()
785+
}
786+
if newResource.GetMemoryLimitInBytes() != 0 {
787+
resources.Memory = newResource.GetMemoryLimitInBytes()
788+
}
789+
if newResource.GetCpusetCpus() != "" {
790+
resources.CpusetCpus = newResource.GetCpusetCpus()
791+
}
792+
if newResource.GetCpusetMems() != "" {
793+
resources.CpusetMems = newResource.GetCpusetMems()
794+
}
795+
return resources
796+
}

cri/v1alpha2/cri.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,29 @@ func (c *CriManager) ListContainerStats(ctx context.Context, r *runtime.ListCont
686686

687687
// UpdateContainerResources updates ContainerConfig of the container.
688688
func (c *CriManager) UpdateContainerResources(ctx context.Context, r *runtime.UpdateContainerResourcesRequest) (*runtime.UpdateContainerResourcesResponse, error) {
689-
return nil, fmt.Errorf("UpdateContainerResources Not Implemented Yet")
689+
containerID := r.GetContainerId()
690+
container, err := c.ContainerMgr.Get(ctx, containerID)
691+
if err != nil {
692+
return nil, fmt.Errorf("failed to get container %q: %v", containerID, err)
693+
}
694+
container.Lock()
695+
defer container.Unlock()
696+
697+
// cannot update container resource when it is in removing state
698+
if container.State.Status == apitypes.StatusRemoving {
699+
return nil, fmt.Errorf("cannot to update resource for container %q when it is in removing state", containerID)
700+
}
701+
702+
resources := resourceToCriResource(r.GetLinux())
703+
updateConfig := &apitypes.UpdateConfig{
704+
Resources: resources,
705+
}
706+
err = c.ContainerMgr.Update(ctx, containerID, updateConfig)
707+
if err != nil {
708+
return nil, fmt.Errorf("failed to update resource for container %q with error: %v", containerID, err)
709+
}
710+
711+
return &runtime.UpdateContainerResourcesResponse{}, nil
690712
}
691713

692714
// ReopenContainerLog asks runtime to reopen the stdout/stderr log file

cri/v1alpha2/cri_utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,3 +791,27 @@ func (c *CriManager) attachLog(logPath string, containerID string) error {
791791
}
792792
return nil
793793
}
794+
795+
// resourceToCriResource converts OCILinuxResource to apitypes.Resources Object.
796+
func resourceToCriResource(newResource *runtime.LinuxContainerResources) apitypes.Resources {
797+
var resources apitypes.Resources
798+
if newResource.GetCpuPeriod() != 0 {
799+
resources.CPUPeriod = newResource.GetCpuPeriod()
800+
}
801+
if newResource.GetCpuQuota() != 0 {
802+
resources.CPUQuota = newResource.GetCpuQuota()
803+
}
804+
if newResource.GetCpuShares() != 0 {
805+
resources.CPUShares = newResource.GetCpuShares()
806+
}
807+
if newResource.GetMemoryLimitInBytes() != 0 {
808+
resources.Memory = newResource.GetMemoryLimitInBytes()
809+
}
810+
if newResource.GetCpusetCpus() != "" {
811+
resources.CpusetCpus = newResource.GetCpusetCpus()
812+
}
813+
if newResource.GetCpusetMems() != "" {
814+
resources.CpusetMems = newResource.GetCpusetMems()
815+
}
816+
return resources
817+
}

daemon/mgr/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
823823
}
824824

825825
// TODO update restartpolicy when container is running.
826-
if config.RestartPolicy.Name != "" {
826+
if config.RestartPolicy != nil && config.RestartPolicy.Name != "" {
827827
c.HostConfig.RestartPolicy = config.RestartPolicy
828828
}
829829

0 commit comments

Comments
 (0)