mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-24 19:12:02 +00:00
Remove unused code in kubelet - take 1
spotted `DockerContainer` in types.go and pulling on whether we really needed it found that a bunch of code is not really required. Signed-off-by: Davanum Srinivas <davanum@gmail.com>
This commit is contained in:
parent
3ca88dc1d0
commit
e2cf9692fc
@ -43,10 +43,6 @@ func (cu *cadvisorUnsupported) Start() error {
|
||||
return errUnsupported
|
||||
}
|
||||
|
||||
func (cu *cadvisorUnsupported) DockerContainer(name string, req *cadvisorapi.ContainerInfoRequest) (cadvisorapi.ContainerInfo, error) {
|
||||
return cadvisorapi.ContainerInfo{}, errUnsupported
|
||||
}
|
||||
|
||||
func (cu *cadvisorUnsupported) ContainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
|
||||
return nil, errUnsupported
|
||||
}
|
||||
|
@ -46,10 +46,6 @@ func (cu *cadvisorClient) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cu *cadvisorClient) DockerContainer(name string, req *cadvisorapi.ContainerInfoRequest) (cadvisorapi.ContainerInfo, error) {
|
||||
return cadvisorapi.ContainerInfo{}, nil
|
||||
}
|
||||
|
||||
func (cu *cadvisorClient) ContainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
|
||||
return &cadvisorapi.ContainerInfo{}, nil
|
||||
}
|
||||
|
@ -66,11 +66,6 @@ func (c *Fake) SubcontainerInfo(name string, req *cadvisorapi.ContainerInfoReque
|
||||
return map[string]*cadvisorapi.ContainerInfo{}, nil
|
||||
}
|
||||
|
||||
// DockerContainer is a fake implementation of Interface.DockerContainer.
|
||||
func (c *Fake) DockerContainer(name string, req *cadvisorapi.ContainerInfoRequest) (cadvisorapi.ContainerInfo, error) {
|
||||
return cadvisorapi.ContainerInfo{}, nil
|
||||
}
|
||||
|
||||
// MachineInfo is a fake implementation of Interface.MachineInfo.
|
||||
func (c *Fake) MachineInfo() (*cadvisorapi.MachineInfo, error) {
|
||||
// Simulate a machine with 1 core and 3.75GB of memory.
|
||||
|
@ -97,21 +97,6 @@ func (mr *MockInterfaceMockRecorder) ContainerInfoV2(name, options interface{})
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ContainerInfoV2", reflect.TypeOf((*MockInterface)(nil).ContainerInfoV2), name, options)
|
||||
}
|
||||
|
||||
// DockerContainer mocks base method.
|
||||
func (m *MockInterface) DockerContainer(name string, req *v1.ContainerInfoRequest) (v1.ContainerInfo, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "DockerContainer", name, req)
|
||||
ret0, _ := ret[0].(v1.ContainerInfo)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// DockerContainer indicates an expected call of DockerContainer.
|
||||
func (mr *MockInterfaceMockRecorder) DockerContainer(name, req interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DockerContainer", reflect.TypeOf((*MockInterface)(nil).DockerContainer), name, req)
|
||||
}
|
||||
|
||||
// GetDirFsInfo mocks base method.
|
||||
func (m *MockInterface) GetDirFsInfo(path string) (v2.FsInfo, error) {
|
||||
m.ctrl.T.Helper()
|
||||
|
@ -26,7 +26,6 @@ import (
|
||||
// Interface is an abstract interface for testability. It abstracts the interface to cAdvisor.
|
||||
type Interface interface {
|
||||
Start() error
|
||||
DockerContainer(name string, req *cadvisorapi.ContainerInfoRequest) (cadvisorapi.ContainerInfo, error)
|
||||
ContainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error)
|
||||
ContainerInfoV2(name string, options cadvisorapiv2.RequestOptions) (map[string]cadvisorapiv2.ContainerInfo, error)
|
||||
GetRequestedContainersInfo(containerName string, options cadvisorapiv2.RequestOptions) (map[string]*cadvisorapi.ContainerInfo, error)
|
||||
|
@ -1361,16 +1361,6 @@ func (kl *Kubelet) RootFsStats() (*statsapi.FsStats, error) {
|
||||
return kl.StatsProvider.RootFsStats()
|
||||
}
|
||||
|
||||
// GetContainerInfo is delegated to StatsProvider, which implements stats.Provider interface
|
||||
func (kl *Kubelet) GetContainerInfo(ctx context.Context, podFullName string, uid types.UID, containerName string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
|
||||
return kl.StatsProvider.GetContainerInfo(ctx, podFullName, uid, containerName, req)
|
||||
}
|
||||
|
||||
// GetRawContainerInfo is delegated to StatsProvider, which implements stats.Provider interface
|
||||
func (kl *Kubelet) GetRawContainerInfo(containerName string, req *cadvisorapi.ContainerInfoRequest, subcontainers bool) (map[string]*cadvisorapi.ContainerInfo, error) {
|
||||
return kl.StatsProvider.GetRawContainerInfo(containerName, req, subcontainers)
|
||||
}
|
||||
|
||||
// RlimitStats is delegated to StatsProvider, which implements stats.Provider interface
|
||||
func (kl *Kubelet) RlimitStats() (*statsapi.RlimitStats, error) {
|
||||
return kl.StatsProvider.RlimitStats()
|
||||
|
@ -69,15 +69,6 @@ type Provider interface {
|
||||
// RootFsStats returns the stats of the node root filesystem.
|
||||
RootFsStats() (*statsapi.FsStats, error)
|
||||
|
||||
// The following stats are provided by cAdvisor for legacy usage.
|
||||
//
|
||||
// GetContainerInfo returns the information of the container with the
|
||||
// containerName managed by the pod with the uid.
|
||||
GetContainerInfo(ctx context.Context, podFullName string, uid types.UID, containerName string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error)
|
||||
// GetRawContainerInfo returns the information of the container with the
|
||||
// containerName. If subcontainers is true, this function will return the
|
||||
// information of all the sub-containers as well.
|
||||
GetRawContainerInfo(containerName string, req *cadvisorapi.ContainerInfoRequest, subcontainers bool) (map[string]*cadvisorapi.ContainerInfo, error)
|
||||
// GetRequestedContainersInfo returns the information of the container with
|
||||
// the containerName, and with the specified cAdvisor options.
|
||||
GetRequestedContainersInfo(containerName string, options cadvisorv2.RequestOptions) (map[string]*cadvisorapi.ContainerInfo, error)
|
||||
|
@ -165,29 +165,6 @@ func (p *Provider) RootFsStats() (*statsapi.FsStats, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetContainerInfo returns stats (from cAdvisor) for a container.
|
||||
func (p *Provider) GetContainerInfo(ctx context.Context, podFullName string, podUID types.UID, containerName string, req *cadvisorapiv1.ContainerInfoRequest) (*cadvisorapiv1.ContainerInfo, error) {
|
||||
// Resolve and type convert back again.
|
||||
// We need the static pod UID but the kubecontainer API works with types.UID.
|
||||
podUID = types.UID(p.podManager.TranslatePodUID(podUID))
|
||||
|
||||
pods, err := p.runtimeCache.GetPods(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pod := kubecontainer.Pods(pods).FindPod(podFullName, podUID)
|
||||
container := pod.FindContainerByName(containerName)
|
||||
if container == nil {
|
||||
return nil, kubecontainer.ErrContainerNotFound
|
||||
}
|
||||
|
||||
ci, err := p.cadvisor.DockerContainer(container.ID.ID, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ci, nil
|
||||
}
|
||||
|
||||
// GetRawContainerInfo returns the stats (from cadvisor) for a non-Kubernetes
|
||||
// container.
|
||||
func (p *Provider) GetRawContainerInfo(containerName string, req *cadvisorapiv1.ContainerInfoRequest, subcontainers bool) (map[string]*cadvisorapiv1.ContainerInfo, error) {
|
||||
|
@ -33,11 +33,9 @@ import (
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
|
||||
cadvisortest "k8s.io/kubernetes/pkg/kubelet/cadvisor/testing"
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
kubecontainertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
|
||||
kubepodtest "k8s.io/kubernetes/pkg/kubelet/pod/testing"
|
||||
serverstats "k8s.io/kubernetes/pkg/kubelet/server/stats"
|
||||
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
|
||||
"k8s.io/kubernetes/pkg/volume"
|
||||
)
|
||||
|
||||
@ -179,182 +177,6 @@ func TestRootFsStats(t *testing.T) {
|
||||
assert.Equal(*rootFsInfo.Inodes-*rootFsInfo.InodesFree, *stats.InodesUsed)
|
||||
}
|
||||
|
||||
func TestGetContainerInfo(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
cadvisorAPIFailure := fmt.Errorf("cAdvisor failure")
|
||||
runtimeError := fmt.Errorf("List containers error")
|
||||
tests := []struct {
|
||||
name string
|
||||
containerID string
|
||||
containerPath string
|
||||
cadvisorContainerInfo cadvisorapiv1.ContainerInfo
|
||||
runtimeError error
|
||||
podList []*kubecontainer.Pod
|
||||
requestedPodFullName string
|
||||
requestedPodUID types.UID
|
||||
requestedContainerName string
|
||||
expectDockerContainerCall bool
|
||||
mockError error
|
||||
expectedError error
|
||||
expectStats bool
|
||||
}{
|
||||
{
|
||||
name: "get container info",
|
||||
containerID: "ab2cdf",
|
||||
containerPath: "/docker/ab2cdf",
|
||||
cadvisorContainerInfo: cadvisorapiv1.ContainerInfo{
|
||||
ContainerReference: cadvisorapiv1.ContainerReference{
|
||||
Name: "/docker/ab2cdf",
|
||||
},
|
||||
},
|
||||
runtimeError: nil,
|
||||
podList: []*kubecontainer.Pod{
|
||||
{
|
||||
ID: "12345678",
|
||||
Name: "qux",
|
||||
Namespace: "ns",
|
||||
Containers: []*kubecontainer.Container{
|
||||
{
|
||||
Name: "foo",
|
||||
ID: kubecontainer.ContainerID{Type: "test", ID: "ab2cdf"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
requestedPodFullName: "qux_ns",
|
||||
requestedPodUID: "",
|
||||
requestedContainerName: "foo",
|
||||
expectDockerContainerCall: true,
|
||||
mockError: nil,
|
||||
expectedError: nil,
|
||||
expectStats: true,
|
||||
},
|
||||
{
|
||||
name: "get container info when cadvisor failed",
|
||||
containerID: "ab2cdf",
|
||||
containerPath: "/docker/ab2cdf",
|
||||
cadvisorContainerInfo: cadvisorapiv1.ContainerInfo{},
|
||||
runtimeError: nil,
|
||||
podList: []*kubecontainer.Pod{
|
||||
{
|
||||
ID: "uuid",
|
||||
Name: "qux",
|
||||
Namespace: "ns",
|
||||
Containers: []*kubecontainer.Container{
|
||||
{
|
||||
Name: "foo",
|
||||
ID: kubecontainer.ContainerID{Type: "test", ID: "ab2cdf"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
requestedPodFullName: "qux_ns",
|
||||
requestedPodUID: "uuid",
|
||||
requestedContainerName: "foo",
|
||||
expectDockerContainerCall: true,
|
||||
mockError: cadvisorAPIFailure,
|
||||
expectedError: cadvisorAPIFailure,
|
||||
expectStats: false,
|
||||
},
|
||||
{
|
||||
name: "get container info on non-existent container",
|
||||
containerID: "",
|
||||
containerPath: "",
|
||||
cadvisorContainerInfo: cadvisorapiv1.ContainerInfo{},
|
||||
runtimeError: nil,
|
||||
podList: []*kubecontainer.Pod{},
|
||||
requestedPodFullName: "qux",
|
||||
requestedPodUID: "",
|
||||
requestedContainerName: "foo",
|
||||
expectDockerContainerCall: false,
|
||||
mockError: nil,
|
||||
expectedError: kubecontainer.ErrContainerNotFound,
|
||||
expectStats: false,
|
||||
},
|
||||
{
|
||||
name: "get container info when container runtime failed",
|
||||
containerID: "",
|
||||
containerPath: "",
|
||||
cadvisorContainerInfo: cadvisorapiv1.ContainerInfo{},
|
||||
runtimeError: runtimeError,
|
||||
podList: []*kubecontainer.Pod{},
|
||||
requestedPodFullName: "qux",
|
||||
requestedPodUID: "",
|
||||
requestedContainerName: "foo",
|
||||
mockError: nil,
|
||||
expectedError: runtimeError,
|
||||
expectStats: false,
|
||||
},
|
||||
{
|
||||
name: "get container info with no containers",
|
||||
containerID: "",
|
||||
containerPath: "",
|
||||
cadvisorContainerInfo: cadvisorapiv1.ContainerInfo{},
|
||||
runtimeError: nil,
|
||||
podList: []*kubecontainer.Pod{},
|
||||
requestedPodFullName: "qux_ns",
|
||||
requestedPodUID: "",
|
||||
requestedContainerName: "foo",
|
||||
mockError: nil,
|
||||
expectedError: kubecontainer.ErrContainerNotFound,
|
||||
expectStats: false,
|
||||
},
|
||||
{
|
||||
name: "get container info with no matching containers",
|
||||
containerID: "",
|
||||
containerPath: "",
|
||||
cadvisorContainerInfo: cadvisorapiv1.ContainerInfo{},
|
||||
runtimeError: nil,
|
||||
podList: []*kubecontainer.Pod{
|
||||
{
|
||||
ID: "12345678",
|
||||
Name: "qux",
|
||||
Namespace: "ns",
|
||||
Containers: []*kubecontainer.Container{
|
||||
{
|
||||
Name: "bar",
|
||||
ID: kubecontainer.ContainerID{Type: "test", ID: "fakeID"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
requestedPodFullName: "qux_ns",
|
||||
requestedPodUID: "",
|
||||
requestedContainerName: "foo",
|
||||
mockError: nil,
|
||||
expectedError: kubecontainer.ErrContainerNotFound,
|
||||
expectStats: false,
|
||||
},
|
||||
}
|
||||
|
||||
mockCtrl := gomock.NewController(t)
|
||||
defer mockCtrl.Finish()
|
||||
|
||||
for _, tc := range tests {
|
||||
var (
|
||||
mockCadvisor = cadvisortest.NewMockInterface(mockCtrl)
|
||||
mockPodManager = kubepodtest.NewMockManager(mockCtrl)
|
||||
mockRuntimeCache = kubecontainertest.NewMockRuntimeCache(mockCtrl)
|
||||
|
||||
cadvisorReq = &cadvisorapiv1.ContainerInfoRequest{}
|
||||
)
|
||||
|
||||
mockPodManager.EXPECT().TranslatePodUID(tc.requestedPodUID).Return(kubetypes.ResolvedPodUID(tc.requestedPodUID))
|
||||
mockRuntimeCache.EXPECT().GetPods(ctx).Return(tc.podList, tc.runtimeError)
|
||||
if tc.expectDockerContainerCall {
|
||||
mockCadvisor.EXPECT().DockerContainer(tc.containerID, cadvisorReq).Return(tc.cadvisorContainerInfo, tc.mockError)
|
||||
}
|
||||
|
||||
provider := newStatsProvider(mockCadvisor, mockPodManager, mockRuntimeCache, fakeContainerStatsProvider{})
|
||||
stats, err := provider.GetContainerInfo(ctx, tc.requestedPodFullName, tc.requestedPodUID, tc.requestedContainerName, cadvisorReq)
|
||||
assert.Equal(t, tc.expectedError, err)
|
||||
|
||||
if tc.expectStats {
|
||||
require.NotNil(t, stats)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRawContainerInfoRoot(t *testing.T) {
|
||||
mockCtrl := gomock.NewController(t)
|
||||
defer mockCtrl.Finish()
|
||||
|
Loading…
Reference in New Issue
Block a user