Skip to content

Commit d814407

Browse files
wangforthinkerzhuangqh
authored andcommitted
support to update memory swap by cri
Signed-off-by: allen.wang <[email protected]>
1 parent baf8555 commit d814407

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

cri/v1alpha2/cri.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,12 @@ func (c *CriManager) UpdateContainerResources(ctx context.Context, r *runtime.Up
10681068
DiskQuota: resources.GetDiskQuota(),
10691069
SpecAnnotation: r.GetSpecAnnotations(),
10701070
}
1071+
1072+
err = applyContainerConfigByAnnotation(updateConfig.SpecAnnotation, nil, nil, updateConfig)
1073+
if err != nil {
1074+
return nil, fmt.Errorf("failed to apply annotation to update config: %v", err)
1075+
}
1076+
10711077
err = c.ContainerMgr.Update(ctx, containerID, updateConfig)
10721078
if err != nil {
10731079
return nil, fmt.Errorf("failed to update resource for container %q: %v", containerID, err)

cri/v1alpha2/cri_utils.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ func (c *CriManager) updateCreateConfig(createConfig *apitypes.ContainerCreateCo
846846

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

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

1287-
hc.MemorySwap = ms
1290+
if uc != nil {
1291+
uc.MemorySwap = ms
1292+
}
12881293
}
12891294

12901295
return nil

cri/v1alpha2/cri_utils_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,20 +1892,24 @@ func Test_applyContainerConfigByAnnotation(t *testing.T) {
18921892
tests := []struct {
18931893
name string
18941894
annotation map[string]string
1895-
checkFn func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig) bool
1895+
checkFn func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig, uc *apitypes.UpdateConfig) bool
18961896
errMsg string
18971897
}{
18981898
{
18991899
name: "normalMemorySwapTest",
19001900
annotation: map[string]string{
19011901
anno.MemorySwapExtendAnnotation: "200000000",
19021902
},
1903-
checkFn: func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig) bool {
1904-
if hc.MemorySwap == 200000000 {
1905-
return true
1903+
checkFn: func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig, uc *apitypes.UpdateConfig) bool {
1904+
if hc.MemorySwap != 200000000 {
1905+
return false
19061906
}
19071907

1908-
return false
1908+
if uc.MemorySwap != 200000000 {
1909+
return false
1910+
}
1911+
1912+
return true
19091913
},
19101914
errMsg: "",
19111915
},
@@ -1914,7 +1918,7 @@ func Test_applyContainerConfigByAnnotation(t *testing.T) {
19141918
annotation: map[string]string{
19151919
anno.MemorySwapExtendAnnotation: "1g",
19161920
},
1917-
checkFn: func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig) bool {
1921+
checkFn: func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig, uc *apitypes.UpdateConfig) bool {
19181922
return false
19191923
},
19201924
errMsg: "failed to parse resources.memory_swap",
@@ -1925,8 +1929,9 @@ func Test_applyContainerConfigByAnnotation(t *testing.T) {
19251929
t.Run(tt.name, func(t *testing.T) {
19261930
config := &apitypes.ContainerConfig{}
19271931
hc := &apitypes.HostConfig{}
1932+
uc := &apitypes.UpdateConfig{}
19281933

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

19371942
if tt.errMsg == "" {
19381943
assert.Nil(t, err)
1939-
assert.True(t, tt.checkFn(config, hc))
1944+
assert.True(t, tt.checkFn(config, hc, uc))
19401945
}
19411946
})
19421947
}

0 commit comments

Comments
 (0)