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

chore: more comments in injectForOrigin function #1264

Merged
merged 2 commits into from
May 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/koordlet/runtimehooks/protocol/container_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ func (c *ContainerContext) ReconcilerDone(executor resourceexecutor.ResourceUpda
c.injectForOrigin()
}

// Inject valid parameters in ContainerContext.Response.Resources,
// such as CPUShares, CPUSet, CFSQuota, MemoryLimit...
func (c *ContainerContext) injectForOrigin() {
// If CPUShares is not nil, set container cpu share
if c.Response.Resources.CPUShares != nil {
eventHelper := audit.V(3).Container(c.Request.ContainerMeta.ID).Reason("runtime-hooks").Message(
"set container cpu share to %v", *c.Response.Resources.CPUShares)
Expand All @@ -205,6 +208,7 @@ func (c *ContainerContext) injectForOrigin() {
"set container cpu share to %v", *c.Response.Resources.CPUShares).Do()
}
}
// If CPUSet is not nil and is not an empty string, set container cpuset
if c.Response.Resources.CPUSet != nil && *c.Response.Resources.CPUSet != "" {
eventHelper := audit.V(3).Container(c.Request.ContainerMeta.ID).Reason("runtime-hooks").Message("set container cpuset to %v", *c.Response.Resources.CPUSet)
err := injectCPUSet(c.Request.CgroupParent, *c.Response.Resources.CPUSet, eventHelper, c.executor)
Expand All @@ -220,6 +224,7 @@ func (c *ContainerContext) injectForOrigin() {
*c.Response.Resources.CPUSet, c.Request.CgroupParent)
}
}
// If CFSQuota is not nil, set container cfs quota
if c.Response.Resources.CFSQuota != nil {
eventHelper := audit.V(3).Container(c.Request.ContainerMeta.ID).Reason("runtime-hooks").Message(
"set container cfs quota to %v", *c.Response.Resources.CFSQuota)
Expand All @@ -232,6 +237,7 @@ func (c *ContainerContext) injectForOrigin() {
*c.Response.Resources.CFSQuota, c.Request.CgroupParent)
}
}
// If MemoryLimit is not nil, set container memory limit
if c.Response.Resources.MemoryLimit != nil {
eventHelper := audit.V(3).Container(c.Request.ContainerMeta.ID).Reason("runtime-hooks").Message(
"set container memory limit to %v", *c.Response.Resources.MemoryLimit)
Expand Down
110 changes: 106 additions & 4 deletions pkg/koordlet/runtimehooks/protocol/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ limitations under the License.
package protocol

import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/utils/pointer"

runtimeapi "github.com/koordinator-sh/koordinator/apis/runtime/v1alpha1"
"github.com/koordinator-sh/koordinator/pkg/koordlet/resourceexecutor"
)

func TestResources_IsOriginResSet(t *testing.T) {
Expand Down Expand Up @@ -76,7 +78,11 @@ func TestContainerResponse_ProxyDone(t *testing.T) {
resp *runtimeapi.ContainerResourceHookResponse
}
type wants struct {
CPUSet *string
CPUSet *string
CPUShares *int64
CFSQuota *int64
MemoryLimit *int64
CPUBvt *int64
}
tests := []struct {
name string
Expand All @@ -85,19 +91,28 @@ func TestContainerResponse_ProxyDone(t *testing.T) {
wants wants
}{
{
name: "origin resource is nil",
name: "origin resource is not nil",
fields: fields{
Resources: Resources{
CPUSet: pointer.String("0,1,2"),
CPUShares: pointer.Int64(15),
CFSQuota: pointer.Int64(1000),
CPUSet: pointer.String("0,1,2"),
MemoryLimit: pointer.Int64(1048576),
CPUBvt: pointer.Int64(10),
},
ContainerEnvs: make(map[string]string, 0),
},
args: args{
resp: &runtimeapi.ContainerResourceHookResponse{
ContainerResources: nil,
},
},
wants: wants{
CPUSet: pointer.String("0,1,2"),
CPUSet: pointer.String("0,1,2"),
CPUShares: pointer.Int64(15),
CFSQuota: pointer.Int64(1000),
MemoryLimit: pointer.Int64(1048576),
CPUBvt: pointer.Int64(10),
},
},
}
Expand All @@ -109,6 +124,10 @@ func TestContainerResponse_ProxyDone(t *testing.T) {
}
c.ProxyDone(tt.args.resp)
assert.Equal(t, tt.wants.CPUSet, c.Resources.CPUSet, "cpu set equal")
assert.Equal(t, tt.wants.CPUBvt, c.Resources.CPUBvt, "cpu bvt equal")
assert.Equal(t, tt.wants.CPUShares, c.Resources.CPUShares, "cpu shares equal")
assert.Equal(t, tt.wants.CFSQuota, c.Resources.CFSQuota, "cfs quota equal")
assert.Equal(t, tt.wants.MemoryLimit, c.Resources.MemoryLimit, "memory limit equal")
})
}
}
Expand Down Expand Up @@ -156,3 +175,86 @@ func TestPodResponse_ProxyDone(t *testing.T) {
})
}
}

func TestPodResponse_ReconcilerDone(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please left a blank line between functions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your suggestion.


type wants struct {
CPUSet *string
CPUShares *int64
CFSQuota *int64
MemoryLimit *int64
CPUBvt *int64
}
var tests = []struct {
name string
ex *resourceexecutor.ResourceUpdateExecutorImpl
req ContainerRequest
res ContainerResponse
wants wants
}{
{
name: "req's parent is nil",
ex: &resourceexecutor.ResourceUpdateExecutorImpl{
LeveledUpdateLock: sync.Mutex{},
ResourceCache: nil,
Config: nil,
},
req: ContainerRequest{},
},
{
name: "test injectForExt nil",
ex: &resourceexecutor.ResourceUpdateExecutorImpl{
LeveledUpdateLock: sync.Mutex{},
ResourceCache: nil,
Config: nil,
},
req: ContainerRequest{
CgroupParent: "test",
},
res: ContainerResponse{
Resources: Resources{},
AddContainerEnvs: nil,
},
},
{
name: "test injectForExt",
ex: &resourceexecutor.ResourceUpdateExecutorImpl{
LeveledUpdateLock: sync.Mutex{},
ResourceCache: nil,
Config: nil,
},
req: ContainerRequest{
CgroupParent: "test",
},
res: ContainerResponse{
Resources: Resources{
CPUSet: pointer.String(""),
CPUShares: pointer.Int64(15),
CFSQuota: pointer.Int64(1000),
MemoryLimit: pointer.Int64(1048576),
CPUBvt: pointer.Int64(10),
},
AddContainerEnvs: nil,
},
wants: wants{
CPUSet: pointer.String(""),
CPUShares: pointer.Int64(15),
CFSQuota: pointer.Int64(1000),
MemoryLimit: pointer.Int64(1048576),
CPUBvt: pointer.Int64(10),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

c := ContainerContext{
Request: tt.req,
Response: tt.res,
executor: nil,
}
c.ReconcilerDone(tt.ex)

})
}
}