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
6 changes: 6 additions & 0 deletions cri/v1alpha2/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,12 @@ func (c *CriManager) UpdateContainerResources(ctx context.Context, r *runtime.Up
DiskQuota: resources.GetDiskQuota(),
SpecAnnotation: r.GetSpecAnnotations(),
}

err = applyContainerConfigByAnnotation(updateConfig.SpecAnnotation, nil, nil, updateConfig)
if err != nil {
return nil, fmt.Errorf("failed to apply annotation to update config: %v", err)
}

err = c.ContainerMgr.Update(ctx, containerID, updateConfig)
if err != nil {
return nil, fmt.Errorf("failed to update resource for container %q: %v", containerID, err)
Expand Down
11 changes: 8 additions & 3 deletions cri/v1alpha2/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ func (c *CriManager) updateCreateConfig(createConfig *apitypes.ContainerCreateCo

if len(config.Annotations) > 0 {
// Apply container config by annotation
if err := applyContainerConfigByAnnotation(config.Annotations, &createConfig.ContainerConfig, createConfig.HostConfig); err != nil {
if err := applyContainerConfigByAnnotation(config.Annotations, &createConfig.ContainerConfig, createConfig.HostConfig, nil); err != nil {
return fmt.Errorf("failed to apply container annotation for container %q: %v", config.Metadata.Name, err)
}
}
Expand Down Expand Up @@ -1273,7 +1273,7 @@ func toCNIPortMappings(criPortMappings []*runtime.PortMapping) []ocicni.PortMapp
}

// applyContainerConfigByAnnotation updates pouch container config according to annotation.
func applyContainerConfigByAnnotation(annotations map[string]string, config *apitypes.ContainerConfig, hc *apitypes.HostConfig) error {
func applyContainerConfigByAnnotation(annotations map[string]string, config *apitypes.ContainerConfig, hc *apitypes.HostConfig, uc *apitypes.UpdateConfig) error {
if len(annotations) == 0 {
return nil
}
Expand All @@ -1283,8 +1283,13 @@ func applyContainerConfigByAnnotation(annotations map[string]string, config *api
if err != nil {
return fmt.Errorf("failed to parse resources.memory_swap: %v", err)
}
if hc != nil {
hc.MemorySwap = ms
}

hc.MemorySwap = ms
if uc != nil {
uc.MemorySwap = ms
}
}

return nil
Expand Down
21 changes: 13 additions & 8 deletions cri/v1alpha2/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1892,20 +1892,24 @@ func Test_applyContainerConfigByAnnotation(t *testing.T) {
tests := []struct {
name string
annotation map[string]string
checkFn func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig) bool
checkFn func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig, uc *apitypes.UpdateConfig) bool
errMsg string
}{
{
name: "normalMemorySwapTest",
annotation: map[string]string{
anno.MemorySwapExtendAnnotation: "200000000",
},
checkFn: func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig) bool {
if hc.MemorySwap == 200000000 {
return true
checkFn: func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig, uc *apitypes.UpdateConfig) bool {
if hc.MemorySwap != 200000000 {
return false
}

return false
if uc.MemorySwap != 200000000 {
return false
}

return true
},
errMsg: "",
},
Expand All @@ -1914,7 +1918,7 @@ func Test_applyContainerConfigByAnnotation(t *testing.T) {
annotation: map[string]string{
anno.MemorySwapExtendAnnotation: "1g",
},
checkFn: func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig) bool {
checkFn: func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig, uc *apitypes.UpdateConfig) bool {
return false
},
errMsg: "failed to parse resources.memory_swap",
Expand All @@ -1925,8 +1929,9 @@ func Test_applyContainerConfigByAnnotation(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
config := &apitypes.ContainerConfig{}
hc := &apitypes.HostConfig{}
uc := &apitypes.UpdateConfig{}

err := applyContainerConfigByAnnotation(tt.annotation, config, hc)
err := applyContainerConfigByAnnotation(tt.annotation, config, hc, uc)
if tt.errMsg != "" {
assert.NotNil(t, err, "error should be %v", tt.errMsg)
if err != nil {
Expand All @@ -1936,7 +1941,7 @@ func Test_applyContainerConfigByAnnotation(t *testing.T) {

if tt.errMsg == "" {
assert.Nil(t, err)
assert.True(t, tt.checkFn(config, hc))
assert.True(t, tt.checkFn(config, hc, uc))
}
})
}
Expand Down