Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support memory swap by CRI #2700

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/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ const (

// LxcfsEnabled whether to enable lxcfs for a container
LxcfsEnabled = "io.kubernetes.lxcfs.enabled"

// ExtendAnnotationPrefix is the extend annotation prefix
ExtendAnnotationPrefix = "io.alibaba.pouch"

// MemorySwapExtendAnnotation is the extend annotation of memory swap
MemorySwapExtendAnnotation = "io.alibaba.pouch.resources.memory_swap"
)
25 changes: 25 additions & 0 deletions cri/v1alpha2/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,13 @@ 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 {
return fmt.Errorf("failed to apply container annotation for container %q: %v", config.Metadata.Name, err)
}
}

// Apply cgroupsParent derived from the sandbox config.
if lc := sandboxConfig.GetLinux(); lc != nil {
// Apply Cgroup options.
Expand Down Expand Up @@ -1258,3 +1265,21 @@ func toCNIPortMappings(criPortMappings []*runtime.PortMapping) []ocicni.PortMapp
}
return portMappings
}

// applyContainerConfigByAnnotation updates pouch container config according to annotation.
func applyContainerConfigByAnnotation(annotations map[string]string, config *apitypes.ContainerConfig, hc *apitypes.HostConfig) error {
if len(annotations) == 0 {
return nil
}

if memorySwap, ok := annotations[anno.MemorySwapExtendAnnotation]; ok {
ms, err := strconv.ParseInt(memorySwap, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse resources.memory_swap: %v", err)
}

hc.MemorySwap = ms
}

return nil
}
56 changes: 56 additions & 0 deletions cri/v1alpha2/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

apitypes "github.com/alibaba/pouch/apis/types"
anno "github.com/alibaba/pouch/cri/annotations"
runtime "github.com/alibaba/pouch/cri/apis/v1alpha2"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/utils"
Expand Down Expand Up @@ -1854,3 +1855,58 @@ func Test_toCNIPortMappings(t *testing.T) {
})
}
}

// CRI test: apply container config by annotation
func Test_applyContainerConfigByAnnotation(t *testing.T) {
wangforthinker marked this conversation as resolved.
Show resolved Hide resolved
tests := []struct {
name string
annotation map[string]string
checkFn func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig) bool
wangforthinker marked this conversation as resolved.
Show resolved Hide resolved
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
}

return false
},
errMsg: "",
},
{
name: "errorMemorySwapTest",
annotation: map[string]string{
anno.MemorySwapExtendAnnotation: "1g",
},
checkFn: func(config *apitypes.ContainerConfig, hc *apitypes.HostConfig) bool {
return false
},
errMsg: "failed to parse resources.memory_swap",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := &apitypes.ContainerConfig{}
hc := &apitypes.HostConfig{}

err := applyContainerConfigByAnnotation(tt.annotation, config, hc)
if tt.errMsg != "" {
assert.NotNil(t, err, "error should be %v", tt.errMsg)
if err != nil {
assert.Contains(t, err.Error(), tt.errMsg)
}
}

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