Merge pull request #53 from Random-Liu/add-other-small-functions
Add other small functions
This commit is contained in:
@@ -19,25 +19,25 @@ package server
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/pkg/truncindex"
|
||||
"github.com/kubernetes-incubator/cri-o/pkg/ocicni"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
contentapi "github.com/containerd/containerd/api/services/content"
|
||||
"github.com/containerd/containerd/api/services/execution"
|
||||
imagesapi "github.com/containerd/containerd/api/services/images"
|
||||
rootfsapi "github.com/containerd/containerd/api/services/rootfs"
|
||||
versionapi "github.com/containerd/containerd/api/services/version"
|
||||
"github.com/containerd/containerd/content"
|
||||
"github.com/containerd/containerd/images"
|
||||
contentservice "github.com/containerd/containerd/services/content"
|
||||
imagesservice "github.com/containerd/containerd/services/images"
|
||||
"github.com/docker/docker/pkg/truncindex"
|
||||
"github.com/kubernetes-incubator/cri-o/pkg/ocicni"
|
||||
"google.golang.org/grpc"
|
||||
healthapi "google.golang.org/grpc/health/grpc_health_v1"
|
||||
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
|
||||
|
||||
"github.com/kubernetes-incubator/cri-containerd/pkg/metadata"
|
||||
"github.com/kubernetes-incubator/cri-containerd/pkg/metadata/store"
|
||||
osinterface "github.com/kubernetes-incubator/cri-containerd/pkg/os"
|
||||
"github.com/kubernetes-incubator/cri-containerd/pkg/registrar"
|
||||
|
||||
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
|
||||
)
|
||||
|
||||
// TODO remove the underscores from the following imports as the services are
|
||||
@@ -86,13 +86,17 @@ type criContainerdService struct {
|
||||
containerNameIndex *registrar.Registrar
|
||||
// containerService is containerd container service client.
|
||||
containerService execution.ContainerServiceClient
|
||||
// contentStoreService is the containerd content service client..
|
||||
// contentStoreService is the containerd content service client.
|
||||
contentStoreService content.Store
|
||||
// rootfsService is the containerd rootfs service client.
|
||||
rootfsService rootfsapi.RootFSClient
|
||||
// imageStoreService is the containerd service to store and track
|
||||
// image metadata.
|
||||
imageStoreService images.Store
|
||||
// versionService is the containerd version service client.
|
||||
versionService versionapi.VersionClient
|
||||
// healthService is the healthcheck service of containerd grpc server.
|
||||
healthService healthapi.HealthClient
|
||||
// netPlugin is used to setup and teardown network when run/stop pod sandbox.
|
||||
netPlugin ocicni.CNIPlugin
|
||||
}
|
||||
@@ -117,6 +121,8 @@ func NewCRIContainerdService(conn *grpc.ClientConn, rootDir, networkPluginBinDir
|
||||
imageStoreService: imagesservice.NewStoreFromClient(imagesapi.NewImagesClient(conn)),
|
||||
contentStoreService: contentservice.NewStoreFromClient(contentapi.NewContentClient(conn)),
|
||||
rootfsService: rootfsapi.NewRootFSClient(conn),
|
||||
versionService: versionapi.NewVersionClient(conn),
|
||||
healthService: healthapi.NewHealthClient(conn),
|
||||
}
|
||||
|
||||
netPlugin, err := ocicni.InitCNI(networkPluginBinDir, networkPluginConfDir)
|
||||
|
||||
@@ -17,14 +17,52 @@ limitations under the License.
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
healthapi "google.golang.org/grpc/health/grpc_health_v1"
|
||||
|
||||
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
|
||||
)
|
||||
|
||||
const (
|
||||
// runtimeNotReadyReason is the reason reported when runtime is not ready.
|
||||
runtimeNotReadyReason = "ContainerdNotReady"
|
||||
// networkNotReadyReason is the reason reported when network is not ready.
|
||||
networkNotReadyReason = "NetworkPluginNotReady"
|
||||
)
|
||||
|
||||
// Status returns the status of the runtime.
|
||||
func (c *criContainerdService) Status(ctx context.Context, r *runtime.StatusRequest) (*runtime.StatusResponse, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
runtimeCondition := &runtime.RuntimeCondition{
|
||||
Type: runtime.RuntimeReady,
|
||||
Status: true,
|
||||
}
|
||||
// Use containerd grpc server healthcheck service to check its readiness.
|
||||
resp, err := c.healthService.Check(ctx, &healthapi.HealthCheckRequest{})
|
||||
if err != nil || resp.Status != healthapi.HealthCheckResponse_SERVING {
|
||||
runtimeCondition.Status = false
|
||||
runtimeCondition.Reason = runtimeNotReadyReason
|
||||
if err != nil {
|
||||
runtimeCondition.Message = fmt.Sprintf("Containerd healthcheck returns error: %v", err)
|
||||
} else {
|
||||
runtimeCondition.Message = "Containerd grpc server is not serving"
|
||||
}
|
||||
}
|
||||
|
||||
networkCondition := &runtime.RuntimeCondition{
|
||||
Type: runtime.NetworkReady,
|
||||
Status: true,
|
||||
}
|
||||
if err := c.netPlugin.Status(); err != nil {
|
||||
networkCondition.Status = false
|
||||
networkCondition.Reason = networkNotReadyReason
|
||||
networkCondition.Message = fmt.Sprintf("Network plugin returns error: %v", err)
|
||||
}
|
||||
return &runtime.StatusResponse{
|
||||
Status: &runtime.RuntimeStatus{Conditions: []*runtime.RuntimeCondition{
|
||||
runtimeCondition,
|
||||
networkCondition,
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
|
||||
97
pkg/server/status_test.go
Normal file
97
pkg/server/status_test.go
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes 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 server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/net/context"
|
||||
healthapi "google.golang.org/grpc/health/grpc_health_v1"
|
||||
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
|
||||
|
||||
servertesting "github.com/kubernetes-incubator/cri-containerd/pkg/server/testing"
|
||||
)
|
||||
|
||||
func TestStatus(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
for desc, test := range map[string]struct {
|
||||
containerdCheckRes *healthapi.HealthCheckResponse
|
||||
containerdCheckErr error
|
||||
networkStatusErr error
|
||||
|
||||
expectRuntimeNotReady bool
|
||||
expectNetworkNotReady bool
|
||||
}{
|
||||
"runtime should not be ready when containerd is not serving": {
|
||||
containerdCheckRes: &healthapi.HealthCheckResponse{
|
||||
Status: healthapi.HealthCheckResponse_NOT_SERVING,
|
||||
},
|
||||
expectRuntimeNotReady: true,
|
||||
},
|
||||
"runtime should not be ready when containerd healthcheck returns error": {
|
||||
containerdCheckErr: errors.New("healthcheck error"),
|
||||
expectRuntimeNotReady: true,
|
||||
},
|
||||
"network should not be ready when network plugin status returns error": {
|
||||
containerdCheckRes: &healthapi.HealthCheckResponse{
|
||||
Status: healthapi.HealthCheckResponse_SERVING,
|
||||
},
|
||||
networkStatusErr: errors.New("status error"),
|
||||
expectNetworkNotReady: true,
|
||||
},
|
||||
"runtime should be ready when containerd is serving": {
|
||||
containerdCheckRes: &healthapi.HealthCheckResponse{
|
||||
Status: healthapi.HealthCheckResponse_SERVING,
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Logf("TestCase %q", desc)
|
||||
c := newTestCRIContainerdService()
|
||||
ctx := context.Background()
|
||||
mock := servertesting.NewMockHealthClient(ctrl)
|
||||
mock.EXPECT().Check(ctx, &healthapi.HealthCheckRequest{}).Return(
|
||||
test.containerdCheckRes, test.containerdCheckErr)
|
||||
c.healthService = mock
|
||||
if test.networkStatusErr != nil {
|
||||
c.netPlugin.(*servertesting.FakeCNIPlugin).InjectError(
|
||||
"Status", test.networkStatusErr)
|
||||
}
|
||||
|
||||
resp, err := c.Status(ctx, &runtime.StatusRequest{})
|
||||
assert.NoError(t, err)
|
||||
require.NotNil(t, resp)
|
||||
runtimeCondition := resp.Status.Conditions[0]
|
||||
networkCondition := resp.Status.Conditions[1]
|
||||
assert.Equal(t, runtime.RuntimeReady, runtimeCondition.Type)
|
||||
assert.Equal(t, test.expectRuntimeNotReady, !runtimeCondition.Status)
|
||||
if test.expectRuntimeNotReady {
|
||||
assert.Equal(t, runtimeNotReadyReason, runtimeCondition.Reason)
|
||||
assert.NotEmpty(t, runtimeCondition.Message)
|
||||
}
|
||||
assert.Equal(t, runtime.NetworkReady, networkCondition.Type)
|
||||
assert.Equal(t, test.expectNetworkNotReady, !networkCondition.Status)
|
||||
if test.expectNetworkNotReady {
|
||||
assert.Equal(t, networkNotReadyReason, networkCondition.Reason)
|
||||
assert.NotEmpty(t, networkCondition.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -168,7 +168,10 @@ func (f *FakeCNIPlugin) GetContainerNetworkStatus(netnsPath string, namespace st
|
||||
|
||||
// Status get the status of the plugin.
|
||||
func (f *FakeCNIPlugin) Status() error {
|
||||
return nil
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("Status", nil)
|
||||
return f.getError("Status")
|
||||
}
|
||||
|
||||
func generateIP() string {
|
||||
|
||||
64
pkg/server/testing/mock_health_client.go
Normal file
64
pkg/server/testing/mock_health_client.go
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes 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.
|
||||
*/
|
||||
|
||||
// Automatically generated by MockGen. DO NOT EDIT!
|
||||
// Source: google.golang.org/grpc/health/grpc_health_v1 (interfaces: HealthClient)
|
||||
|
||||
package testing
|
||||
|
||||
import (
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
context "golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
grpc_health_v1 "google.golang.org/grpc/health/grpc_health_v1"
|
||||
)
|
||||
|
||||
// Mock of HealthClient interface
|
||||
type MockHealthClient struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *_MockHealthClientRecorder
|
||||
}
|
||||
|
||||
// Recorder for MockHealthClient (not exported)
|
||||
type _MockHealthClientRecorder struct {
|
||||
mock *MockHealthClient
|
||||
}
|
||||
|
||||
func NewMockHealthClient(ctrl *gomock.Controller) *MockHealthClient {
|
||||
mock := &MockHealthClient{ctrl: ctrl}
|
||||
mock.recorder = &_MockHealthClientRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
func (_m *MockHealthClient) EXPECT() *_MockHealthClientRecorder {
|
||||
return _m.recorder
|
||||
}
|
||||
|
||||
func (_m *MockHealthClient) Check(_param0 context.Context, _param1 *grpc_health_v1.HealthCheckRequest, _param2 ...grpc.CallOption) (*grpc_health_v1.HealthCheckResponse, error) {
|
||||
_s := []interface{}{_param0, _param1}
|
||||
for _, _x := range _param2 {
|
||||
_s = append(_s, _x)
|
||||
}
|
||||
ret := _m.ctrl.Call(_m, "Check", _s...)
|
||||
ret0, _ := ret[0].(*grpc_health_v1.HealthCheckResponse)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
func (_mr *_MockHealthClientRecorder) Check(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call {
|
||||
_s := append([]interface{}{arg0, arg1}, arg2...)
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Check", _s...)
|
||||
}
|
||||
65
pkg/server/testing/mock_version_client.go
Normal file
65
pkg/server/testing/mock_version_client.go
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes 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.
|
||||
*/
|
||||
|
||||
// Automatically generated by MockGen. DO NOT EDIT!
|
||||
// Source: github.com/containerd/containerd/api/services/version (interfaces: VersionClient)
|
||||
|
||||
package testing
|
||||
|
||||
import (
|
||||
version "github.com/containerd/containerd/api/services/version"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
empty "github.com/golang/protobuf/ptypes/empty"
|
||||
context "golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// Mock of VersionClient interface
|
||||
type MockVersionClient struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *_MockVersionClientRecorder
|
||||
}
|
||||
|
||||
// Recorder for MockVersionClient (not exported)
|
||||
type _MockVersionClientRecorder struct {
|
||||
mock *MockVersionClient
|
||||
}
|
||||
|
||||
func NewMockVersionClient(ctrl *gomock.Controller) *MockVersionClient {
|
||||
mock := &MockVersionClient{ctrl: ctrl}
|
||||
mock.recorder = &_MockVersionClientRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
func (_m *MockVersionClient) EXPECT() *_MockVersionClientRecorder {
|
||||
return _m.recorder
|
||||
}
|
||||
|
||||
func (_m *MockVersionClient) Version(_param0 context.Context, _param1 *empty.Empty, _param2 ...grpc.CallOption) (*version.VersionResponse, error) {
|
||||
_s := []interface{}{_param0, _param1}
|
||||
for _, _x := range _param2 {
|
||||
_s = append(_s, _x)
|
||||
}
|
||||
ret := _m.ctrl.Call(_m, "Version", _s...)
|
||||
ret0, _ := ret[0].(*version.VersionResponse)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
func (_mr *_MockVersionClientRecorder) Version(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call {
|
||||
_s := append([]interface{}{arg0, arg1}, arg2...)
|
||||
return _mr.mock.ctrl.RecordCall(_mr.mock, "Version", _s...)
|
||||
}
|
||||
@@ -17,14 +17,13 @@ limitations under the License.
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
|
||||
)
|
||||
|
||||
// UpdateRuntimeConfig updates the runtime config. Currently only handles podCIDR updates.
|
||||
// TODO(random-liu): Figure out how to handle pod cidr in cri-containerd.
|
||||
func (c *criContainerdService) UpdateRuntimeConfig(ctx context.Context, r *runtime.UpdateRuntimeConfigRequest) (*runtime.UpdateRuntimeConfigResponse, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
return &runtime.UpdateRuntimeConfigResponse{}, nil
|
||||
}
|
||||
|
||||
@@ -17,14 +17,35 @@ limitations under the License.
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/protobuf/ptypes/empty"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
|
||||
)
|
||||
|
||||
const (
|
||||
containerName = "containerd"
|
||||
containerdAPIVersion = "0.0.0"
|
||||
containerdVersion = "0.0.0"
|
||||
// kubeAPIVersion is the api version of kubernetes.
|
||||
kubeAPIVersion = "0.1.0"
|
||||
)
|
||||
|
||||
// Version returns the runtime name, runtime version and runtime API version.
|
||||
func (c *criContainerdService) Version(ctx context.Context, r *runtime.VersionRequest) (*runtime.VersionResponse, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
_, err := c.versionService.Version(ctx, &empty.Empty{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get containerd version: %v", err)
|
||||
}
|
||||
return &runtime.VersionResponse{
|
||||
Version: kubeAPIVersion,
|
||||
RuntimeName: containerName,
|
||||
// Containerd doesn't return semver because of a bug.
|
||||
// TODO(random-liu): Replace this with version from containerd.
|
||||
RuntimeVersion: containerdVersion,
|
||||
// Containerd doesn't have an api version now.
|
||||
RuntimeApiVersion: containerdAPIVersion,
|
||||
}, nil
|
||||
}
|
||||
|
||||
61
pkg/server/version_test.go
Normal file
61
pkg/server/version_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes 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 server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
versionapi "github.com/containerd/containerd/api/services/version"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/golang/protobuf/ptypes/empty"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"golang.org/x/net/context"
|
||||
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
|
||||
|
||||
servertesting "github.com/kubernetes-incubator/cri-containerd/pkg/server/testing"
|
||||
)
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
// TODO(random-liu): Check containerd version after containerd fixes its version.
|
||||
for desc, test := range map[string]struct {
|
||||
versionRes *versionapi.VersionResponse
|
||||
versionErr error
|
||||
expectErr bool
|
||||
}{
|
||||
"should return error if containerd version returns error": {
|
||||
versionErr: errors.New("random error"),
|
||||
expectErr: true,
|
||||
},
|
||||
"should not return error if containerd version returns successfully": {
|
||||
versionRes: &versionapi.VersionResponse{Version: "1.1.1"},
|
||||
expectErr: false,
|
||||
},
|
||||
} {
|
||||
t.Logf("TestCase %q", desc)
|
||||
c := newTestCRIContainerdService()
|
||||
ctx := context.Background()
|
||||
mock := servertesting.NewMockVersionClient(ctrl)
|
||||
mock.EXPECT().Version(ctx, &empty.Empty{}).Return(test.versionRes, test.versionErr)
|
||||
|
||||
c.versionService = mock
|
||||
_, err := c.Version(ctx, &runtime.VersionRequest{})
|
||||
assert.Equal(t, test.expectErr, err != nil)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user