Skip to content

Commit

Permalink
support memory swap by cri
Browse files Browse the repository at this point in the history
Signed-off-by: allen.wang <allen.wq@alipay.com>
  • Loading branch information
wangforthinker committed Jan 29, 2019
1 parent 9df7eab commit c10af73
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
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, sandboxMeta.ID, &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, podSandboxID 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
}
48 changes: 48 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,50 @@ func Test_toCNIPortMappings(t *testing.T) {
})
}
}

// CRI test: apply container config by annotation
func Test_applyContainerConfigByAnnotation(t *testing.T) {
tests := []struct {
name string
memorySwapStr string
memorySwap int64
errMsg string
}{
{
name: "normalMemorySwapTest",
memorySwapStr: "200000000",
memorySwap: 200000000,
errMsg: "",
},
{
name: "errorMemorySwapTest",
memorySwapStr: "1g",
memorySwap: 0,
errMsg: "failed to parse resources.memory_swap",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
annotations := map[string]string{
anno.MemorySwapExtendAnnotation: tt.memorySwapStr,
}

config := &apitypes.ContainerConfig{}
hc := &apitypes.HostConfig{}

err := applyContainerConfigByAnnotation(annotations, "", 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.Equal(t, tt.memorySwap, hc.MemorySwap)
}
})
}
}

0 comments on commit c10af73

Please sign in to comment.