-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
koordlet: support cri-o container runtime (#1983)
Signed-off-by: ocichina001 <[email protected]> Signed-off-by: george <[email protected]> Signed-off-by: saintube <[email protected]> Signed-off-by: wangjianyu.wjy <[email protected]> Signed-off-by: xulinfei.xlf <[email protected]> Signed-off-by: Siyu Wang <[email protected]> Signed-off-by: Fansong Zeng <[email protected]> Signed-off-by: acejilam <[email protected]> Signed-off-by: lucming <[email protected]> Signed-off-by: sjtufl <[email protected]> Co-authored-by: ocichina001 <[email protected]> Co-authored-by: ocichina <[email protected]> Co-authored-by: Frame <[email protected]> Co-authored-by: wangjianyu <[email protected]> Co-authored-by: wangjianyu.wjy <[email protected]> Co-authored-by: xulinfei1996 <[email protected]> Co-authored-by: xulinfei.xlf <[email protected]> Co-authored-by: Siyu Wang <[email protected]> Co-authored-by: Fansong Zeng <[email protected]> Co-authored-by: ls-2018 <[email protected]> Co-authored-by: lucming <[email protected]> Co-authored-by: Liang Fang <[email protected]>
- Loading branch information
1 parent
3b6167d
commit 9b4c5b1
Showing
8 changed files
with
330 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
Copyright 2022 The Koordinator Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package handler | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" | ||
|
||
"github.com/koordinator-sh/koordinator/pkg/koordlet/util/system" | ||
) | ||
|
||
func GetCrioEndpoint() string { | ||
return filepath.Join(system.Conf.VarRunRootDir, "crio/crio.sock") | ||
} | ||
|
||
func GetCrioEndpoint2() string { | ||
return filepath.Join(system.Conf.VarRunRootDir, "crio.sock") | ||
} | ||
|
||
type CrioRuntimeHandler struct { | ||
runtimeServiceClient runtimeapi.RuntimeServiceClient | ||
timeout time.Duration | ||
endpoint string | ||
} | ||
|
||
func NewCrioRuntimeHandler(endpoint string) (ContainerRuntimeHandler, error) { | ||
ep := strings.TrimPrefix(endpoint, "unix://") | ||
if _, err := os.Stat(ep); err != nil { | ||
return nil, err | ||
} | ||
|
||
client, err := getRuntimeClient(endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &CrioRuntimeHandler{ | ||
runtimeServiceClient: client, | ||
timeout: defaultConnectionTimeout, | ||
endpoint: endpoint, | ||
}, nil | ||
} | ||
|
||
func (c *CrioRuntimeHandler) StopContainer(containerID string, timeout int64) error { | ||
if containerID == "" { | ||
return fmt.Errorf("containerID cannot be empty") | ||
} | ||
t := c.timeout + time.Duration(timeout) | ||
ctx, cancel := context.WithTimeout(context.Background(), t) | ||
defer cancel() | ||
|
||
request := &runtimeapi.StopContainerRequest{ | ||
ContainerId: containerID, | ||
Timeout: timeout, | ||
} | ||
_, err := c.runtimeServiceClient.StopContainer(ctx, request) | ||
return err | ||
} | ||
|
||
func (c *CrioRuntimeHandler) UpdateContainerResources(containerID string, opts UpdateOptions) error { | ||
if containerID == "" { | ||
return fmt.Errorf("containerID cannot be empty") | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), c.timeout) | ||
defer cancel() | ||
request := &runtimeapi.UpdateContainerResourcesRequest{ | ||
ContainerId: containerID, | ||
Linux: &runtimeapi.LinuxContainerResources{ | ||
CpuPeriod: opts.CPUPeriod, | ||
CpuQuota: opts.CPUQuota, | ||
CpuShares: opts.CPUShares, | ||
CpusetCpus: opts.CpusetCpus, | ||
CpusetMems: opts.CpusetMems, | ||
MemoryLimitInBytes: opts.MemoryLimitInBytes, | ||
OomScoreAdj: opts.OomScoreAdj, | ||
}, | ||
} | ||
_, err := c.runtimeServiceClient.UpdateContainerResources(ctx, request) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
Copyright 2022 The Koordinator Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package handler | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/prashantv/gostub" | ||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/grpc" | ||
|
||
mockclient "github.com/koordinator-sh/koordinator/pkg/koordlet/util/runtime/handler/mockclient" | ||
"github.com/koordinator-sh/koordinator/pkg/koordlet/util/system" | ||
) | ||
|
||
func Test_NewCrioRuntimeHandler(t *testing.T) { | ||
stubs := gostub.Stub(&GrpcDial, func(context context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { | ||
return &grpc.ClientConn{}, nil | ||
}) | ||
defer stubs.Reset() | ||
|
||
helper := system.NewFileTestUtil(t) | ||
defer helper.Cleanup() | ||
|
||
helper.WriteFileContents("/var/run/crio/crio.sock", "test") | ||
system.Conf.VarRunRootDir = filepath.Join(helper.TempDir, "/var/run") | ||
CrioEndpoint1 := GetCrioEndpoint() | ||
unixEndPoint := fmt.Sprintf("unix://%s", CrioEndpoint1) | ||
crioRuntime, err := NewCrioRuntimeHandler(unixEndPoint) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, crioRuntime) | ||
|
||
// custom VarRunRootDir | ||
helper.WriteFileContents("/host-var-run/crio/crio.sock", "test1") | ||
system.Conf.VarRunRootDir = filepath.Join(helper.TempDir, "/host-var-run") | ||
CrioEndpoint1 = GetCrioEndpoint() | ||
unixEndPoint = fmt.Sprintf("unix://%s", CrioEndpoint1) | ||
crioRuntime, err = NewCrioRuntimeHandler(unixEndPoint) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, crioRuntime) | ||
} | ||
|
||
func Test_Crio_StopContainer(t *testing.T) { | ||
type args struct { | ||
name string | ||
containerId string | ||
runtimeError error | ||
expectError bool | ||
} | ||
tests := []args{ | ||
{ | ||
name: "test_stopContainer_success", | ||
containerId: "test_container_id", | ||
runtimeError: nil, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "test_stopContainer_fail", | ||
containerId: "test_container_id", | ||
runtimeError: fmt.Errorf("stopContainer error"), | ||
expectError: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctl := gomock.NewController(t) | ||
defer ctl.Finish() | ||
mockRuntimeClient := mockclient.NewMockRuntimeServiceClient(ctl) | ||
mockRuntimeClient.EXPECT().StopContainer(gomock.Any(), gomock.Any()).Return(nil, tt.runtimeError) | ||
|
||
runtimeHandler := ContainerdRuntimeHandler{runtimeServiceClient: mockRuntimeClient, timeout: 1, endpoint: GetCrioEndpoint()} | ||
gotErr := runtimeHandler.StopContainer(tt.containerId, 1) | ||
assert.Equal(t, gotErr != nil, tt.expectError) | ||
|
||
}) | ||
} | ||
} | ||
|
||
func Test_Crio_UpdateContainerResources(t *testing.T) { | ||
type args struct { | ||
name string | ||
containerId string | ||
runtimeError error | ||
expectError bool | ||
} | ||
tests := []args{ | ||
{ | ||
name: "test_UpdateContainerResources_success", | ||
containerId: "test_container_id", | ||
runtimeError: nil, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "test_UpdateContainerResources_fail", | ||
containerId: "test_container_id", | ||
runtimeError: fmt.Errorf("UpdateContainerResources error"), | ||
expectError: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctl := gomock.NewController(t) | ||
defer ctl.Finish() | ||
mockRuntimeClient := mockclient.NewMockRuntimeServiceClient(ctl) | ||
mockRuntimeClient.EXPECT().UpdateContainerResources(gomock.Any(), gomock.Any()).Return(nil, tt.runtimeError) | ||
|
||
runtimeHandler := ContainerdRuntimeHandler{runtimeServiceClient: mockRuntimeClient, timeout: 1, endpoint: GetCrioEndpoint()} | ||
gotErr := runtimeHandler.UpdateContainerResources(tt.containerId, UpdateOptions{}) | ||
assert.Equal(t, tt.expectError, gotErr != nil) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.