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

Fix/Add comments on cadvisor implementations #73829

Merged
merged 1 commit into from Jul 30, 2019
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
2 changes: 0 additions & 2 deletions hack/.golint_failures
Expand Up @@ -148,8 +148,6 @@ pkg/kubelet
pkg/kubelet/apis/config
pkg/kubelet/apis/config/v1beta1
pkg/kubelet/apis/deviceplugin/v1beta1
pkg/kubelet/cadvisor
pkg/kubelet/cadvisor/testing
pkg/kubelet/checkpointmanager/testing/example_checkpoint_formats/v1
pkg/kubelet/client
pkg/kubelet/cm
Expand Down
9 changes: 4 additions & 5 deletions pkg/kubelet/cadvisor/cadvisor_linux.go
Expand Up @@ -82,6 +82,7 @@ func init() {
}
}

// New creates a new cAdvisor Interface for linux systems.
func New(imageFsInfoProvider ImageFsInfoProvider, rootPath string, cgroupRoots []string, usingLegacyStats bool) (Interface, error) {
sysFs := sysfs.NewRealSysFs()

Expand All @@ -98,7 +99,7 @@ func New(imageFsInfoProvider ImageFsInfoProvider, rootPath string, cgroupRoots [
includedMetrics[cadvisormetrics.DiskUsageMetrics] = struct{}{}
}

// Create and start the cAdvisor container manager.
// Create the cAdvisor container manager.
m, err := manager.New(memory.New(statsCacheDuration, nil), sysFs, maxHousekeepingInterval, allowDynamicHousekeeping, includedMetrics, http.DefaultClient, cgroupRoots)
if err != nil {
return nil, err
Expand All @@ -114,13 +115,11 @@ func New(imageFsInfoProvider ImageFsInfoProvider, rootPath string, cgroupRoots [
}
}

cadvisorClient := &cadvisorClient{
return &cadvisorClient{
imageFsInfoProvider: imageFsInfoProvider,
rootPath: rootPath,
Manager: m,
}

return cadvisorClient, nil
}, nil
}

func (cc *cadvisorClient) Start() error {
Expand Down
23 changes: 12 additions & 11 deletions pkg/kubelet/cadvisor/cadvisor_unsupported.go
Expand Up @@ -31,50 +31,51 @@ type cadvisorUnsupported struct {

var _ Interface = new(cadvisorUnsupported)

// New creates a new cAdvisor Interface for unsupported systems.
func New(imageFsInfoProvider ImageFsInfoProvider, rootPath string, cgroupsRoots []string, usingLegacyStats bool) (Interface, error) {
return &cadvisorUnsupported{}, nil
}

var unsupportedErr = errors.New("cAdvisor is unsupported in this build")
var errUnsupported = errors.New("cAdvisor is unsupported in this build")

func (cu *cadvisorUnsupported) Start() error {
return unsupportedErr
return errUnsupported
}

func (cu *cadvisorUnsupported) DockerContainer(name string, req *cadvisorapi.ContainerInfoRequest) (cadvisorapi.ContainerInfo, error) {
return cadvisorapi.ContainerInfo{}, unsupportedErr
return cadvisorapi.ContainerInfo{}, errUnsupported
}

func (cu *cadvisorUnsupported) ContainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
return nil, unsupportedErr
return nil, errUnsupported
}

func (cu *cadvisorUnsupported) ContainerInfoV2(name string, options cadvisorapiv2.RequestOptions) (map[string]cadvisorapiv2.ContainerInfo, error) {
return nil, unsupportedErr
return nil, errUnsupported
}

func (cu *cadvisorUnsupported) SubcontainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (map[string]*cadvisorapi.ContainerInfo, error) {
return nil, unsupportedErr
return nil, errUnsupported
}

func (cu *cadvisorUnsupported) MachineInfo() (*cadvisorapi.MachineInfo, error) {
return nil, unsupportedErr
return nil, errUnsupported
}

func (cu *cadvisorUnsupported) VersionInfo() (*cadvisorapi.VersionInfo, error) {
return nil, unsupportedErr
return nil, errUnsupported
}

func (cu *cadvisorUnsupported) ImagesFsInfo() (cadvisorapiv2.FsInfo, error) {
return cadvisorapiv2.FsInfo{}, unsupportedErr
return cadvisorapiv2.FsInfo{}, errUnsupported
}

func (cu *cadvisorUnsupported) RootFsInfo() (cadvisorapiv2.FsInfo, error) {
return cadvisorapiv2.FsInfo{}, unsupportedErr
return cadvisorapiv2.FsInfo{}, errUnsupported
}

func (cu *cadvisorUnsupported) WatchEvents(request *events.Request) (*events.EventChannel, error) {
return nil, unsupportedErr
return nil, errUnsupported
}

func (cu *cadvisorUnsupported) GetDirFsInfo(path string) (cadvisorapiv2.FsInfo, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/kubelet/cadvisor/cadvisor_windows.go
Expand Up @@ -53,6 +53,7 @@ func (cu *cadvisorClient) ContainerInfo(name string, req *cadvisorapi.ContainerI
return &cadvisorapi.ContainerInfo{}, nil
}

// ContainerInfoV2 is only expected to be used for the root container. Returns info for all containers in the node.
func (cu *cadvisorClient) ContainerInfoV2(name string, options cadvisorapiv2.RequestOptions) (map[string]cadvisorapiv2.ContainerInfo, error) {
return cu.winStatsClient.WinContainerInfos()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/cadvisor/doc.go
Expand Up @@ -14,5 +14,5 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Kubelet interactions with cAdvisor.
// Package cadvisor provides an interface for Kubelet interactions with cAdvisor.
package cadvisor // import "k8s.io/kubernetes/pkg/kubelet/cadvisor"
34 changes: 24 additions & 10 deletions pkg/kubelet/cadvisor/testing/cadvisor_fake.go
Expand Up @@ -23,71 +23,85 @@ import (
"k8s.io/kubernetes/pkg/kubelet/cadvisor"
)

// Fake cAdvisor implementation.
// Fake cadvisor.Interface implementation.
type Fake struct {
NodeName string
}

const (
FakeNumCores = 1
FakeMemoryCapacity = 4026531840
FakeKernelVersion = "3.16.0-0.bpo.4-amd64"
FakeContainerOsVersion = "Debian GNU/Linux 7 (wheezy)"
FakeDockerVersion = "1.13.1"
// FakeKernelVersion is a fake kernel version for testing.
FakeKernelVersion = "3.16.0-0.bpo.4-amd64"
// FakeContainerOSVersion is a fake OS version for testing.
FakeContainerOSVersion = "Debian GNU/Linux 7 (wheezy)"

fakeNumCores = 1
fakeMemoryCapacity = 4026531840
fakeDockerVersion = "1.13.1"
)

var _ cadvisor.Interface = new(Fake)

// Start is a fake implementation of Interface.Start.
func (c *Fake) Start() error {
return nil
}

// ContainerInfo is a fake implementation of Interface.ContainerInfo.
func (c *Fake) ContainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
return new(cadvisorapi.ContainerInfo), nil
}

// ContainerInfoV2 is a fake implementation of Interface.ContainerInfoV2.
func (c *Fake) ContainerInfoV2(name string, options cadvisorapiv2.RequestOptions) (map[string]cadvisorapiv2.ContainerInfo, error) {
return map[string]cadvisorapiv2.ContainerInfo{}, nil
}

// SubcontainerInfo is a fake implementation of Interface.SubcontainerInfo.
func (c *Fake) SubcontainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (map[string]*cadvisorapi.ContainerInfo, error) {
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.
// We set it to non-zero values to make non-zero-capacity machines in Kubemark.
return &cadvisorapi.MachineInfo{
NumCores: FakeNumCores,
NumCores: fakeNumCores,
InstanceID: cadvisorapi.InstanceID(c.NodeName),
MemoryCapacity: FakeMemoryCapacity,
MemoryCapacity: fakeMemoryCapacity,
}, nil
}

// VersionInfo is a fake implementation of Interface.VersionInfo.
func (c *Fake) VersionInfo() (*cadvisorapi.VersionInfo, error) {
return &cadvisorapi.VersionInfo{
KernelVersion: FakeKernelVersion,
ContainerOsVersion: FakeContainerOsVersion,
DockerVersion: FakeDockerVersion,
ContainerOsVersion: FakeContainerOSVersion,
DockerVersion: fakeDockerVersion,
}, nil
}

// ImagesFsInfo is a fake implementation of Interface.ImagesFsInfo.
func (c *Fake) ImagesFsInfo() (cadvisorapiv2.FsInfo, error) {
return cadvisorapiv2.FsInfo{}, nil
}

// RootFsInfo is a fake implementation of Interface.RootFsInfo.
func (c *Fake) RootFsInfo() (cadvisorapiv2.FsInfo, error) {
return cadvisorapiv2.FsInfo{}, nil
}

// WatchEvents is a fake implementation of Interface.WatchEvents.
func (c *Fake) WatchEvents(request *events.Request) (*events.EventChannel, error) {
return new(events.EventChannel), nil
}

// GetDirFsInfo is a fake implementation of Interface.GetDirFsInfo.
func (c *Fake) GetDirFsInfo(path string) (cadvisorapiv2.FsInfo, error) {
return cadvisorapiv2.FsInfo{}, nil
}
8 changes: 8 additions & 0 deletions pkg/kubelet/cadvisor/testing/cadvisor_mock.go
Expand Up @@ -24,12 +24,14 @@ import (
"k8s.io/kubernetes/pkg/kubelet/cadvisor"
)

// Mock cadvisor.Interface implementation.
type Mock struct {
mock.Mock
}

var _ cadvisor.Interface = new(Mock)

// Start is a mock implementation of Interface.Start.
func (c *Mock) Start() error {
args := c.Called()
return args.Error(0)
Expand All @@ -47,6 +49,7 @@ func (c *Mock) ContainerInfoV2(name string, options cadvisorapiv2.RequestOptions
return args.Get(0).(map[string]cadvisorapiv2.ContainerInfo), args.Error(1)
}

// SubcontainerInfo is a mock implementation of Interface.SubcontainerInfo.
func (c *Mock) SubcontainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (map[string]*cadvisorapi.ContainerInfo, error) {
args := c.Called(name, req)
return args.Get(0).(map[string]*cadvisorapi.ContainerInfo), args.Error(1)
Expand All @@ -64,26 +67,31 @@ func (c *Mock) MachineInfo() (*cadvisorapi.MachineInfo, error) {
return args.Get(0).(*cadvisorapi.MachineInfo), args.Error(1)
}

// VersionInfo is a mock implementation of Interface.VersionInfo.
func (c *Mock) VersionInfo() (*cadvisorapi.VersionInfo, error) {
args := c.Called()
return args.Get(0).(*cadvisorapi.VersionInfo), args.Error(1)
}

// ImagesFsInfo is a mock implementation of Interface.ImagesFsInfo.
func (c *Mock) ImagesFsInfo() (cadvisorapiv2.FsInfo, error) {
args := c.Called()
return args.Get(0).(cadvisorapiv2.FsInfo), args.Error(1)
}

// RootFsInfo is a mock implementation of Interface.RootFsInfo.
func (c *Mock) RootFsInfo() (cadvisorapiv2.FsInfo, error) {
args := c.Called()
return args.Get(0).(cadvisorapiv2.FsInfo), args.Error(1)
}

// WatchEvents is a mock implementation of Interface.WatchEvents.
func (c *Mock) WatchEvents(request *events.Request) (*events.EventChannel, error) {
args := c.Called()
return args.Get(0).(*events.EventChannel), args.Error(1)
}

// GetDirFsInfo is a mock implementation of Interface.GetDirFsInfo.
func (c *Mock) GetDirFsInfo(path string) (cadvisorapiv2.FsInfo, error) {
args := c.Called(path)
return args.Get(0).(cadvisorapiv2.FsInfo), args.Error(1)
Expand Down
5 changes: 4 additions & 1 deletion pkg/kubelet/cadvisor/util.go
Expand Up @@ -28,11 +28,13 @@ import (
)

const (
// CrioSocket is the path to the CRI-O socket.
// Please keep this in sync with the one in:
// github.com/google/cadvisor/container/crio/client.go
CrioSocket = "/var/run/crio/crio.sock"
)

// CapacityFromMachineInfo returns the capacity of the resources from the machine info.
func CapacityFromMachineInfo(info *cadvisorapi.MachineInfo) v1.ResourceList {
c := v1.ResourceList{
v1.ResourceCPU: *resource.NewMilliQuantity(
Expand All @@ -54,6 +56,7 @@ func CapacityFromMachineInfo(info *cadvisorapi.MachineInfo) v1.ResourceList {
return c
}

// EphemeralStorageCapacityFromFsInfo returns the capacity of the ephemeral storage from the FsInfo.
func EphemeralStorageCapacityFromFsInfo(info cadvisorapi2.FsInfo) v1.ResourceList {
c := v1.ResourceList{
v1.ResourceEphemeralStorage: *resource.NewQuantity(
Expand All @@ -63,13 +66,13 @@ func EphemeralStorageCapacityFromFsInfo(info cadvisorapi2.FsInfo) v1.ResourceLis
return c
}

// UsingLegacyCadvisorStats returns true if container stats are provided by cadvisor instead of through the CRI.
// CRI integrations should get container metrics via CRI. Docker
// uses the built-in cadvisor to gather such metrics on Linux for
// historical reasons.
// cri-o relies on cadvisor as a temporary workaround. The code should
// be removed. Related issue:
// https://github.com/kubernetes/kubernetes/issues/51798
// UsingLegacyCadvisorStats returns true if container stats are provided by cadvisor instead of through the CRI
func UsingLegacyCadvisorStats(runtime, runtimeEndpoint string) bool {
return (runtime == kubetypes.DockerContainerRuntime && goruntime.GOOS == "linux") ||
runtimeEndpoint == CrioSocket || runtimeEndpoint == "unix://"+CrioSocket
Expand Down
8 changes: 4 additions & 4 deletions pkg/kubelet/kubelet_node_status_test.go
Expand Up @@ -271,7 +271,7 @@ func TestUpdateNewNodeStatus(t *testing.T) {
SystemUUID: "abc",
BootID: "1b3",
KernelVersion: cadvisortest.FakeKernelVersion,
OSImage: cadvisortest.FakeContainerOsVersion,
OSImage: cadvisortest.FakeContainerOSVersion,
OperatingSystem: goruntime.GOOS,
Architecture: goruntime.GOARCH,
ContainerRuntimeVersion: "test://1.5.0",
Expand Down Expand Up @@ -449,7 +449,7 @@ func TestUpdateExistingNodeStatus(t *testing.T) {
SystemUUID: "abc",
BootID: "1b3",
KernelVersion: cadvisortest.FakeKernelVersion,
OSImage: cadvisortest.FakeContainerOsVersion,
OSImage: cadvisortest.FakeContainerOSVersion,
OperatingSystem: goruntime.GOOS,
Architecture: goruntime.GOARCH,
ContainerRuntimeVersion: "test://1.5.0",
Expand Down Expand Up @@ -647,7 +647,7 @@ func TestUpdateNodeStatusWithRuntimeStateError(t *testing.T) {
SystemUUID: "abc",
BootID: "1b3",
KernelVersion: cadvisortest.FakeKernelVersion,
OSImage: cadvisortest.FakeContainerOsVersion,
OSImage: cadvisortest.FakeContainerOSVersion,
OperatingSystem: goruntime.GOOS,
Architecture: goruntime.GOARCH,
ContainerRuntimeVersion: "test://1.5.0",
Expand Down Expand Up @@ -877,7 +877,7 @@ func TestUpdateNodeStatusWithLease(t *testing.T) {
SystemUUID: "abc",
BootID: "1b3",
KernelVersion: cadvisortest.FakeKernelVersion,
OSImage: cadvisortest.FakeContainerOsVersion,
OSImage: cadvisortest.FakeContainerOSVersion,
OperatingSystem: goruntime.GOOS,
Architecture: goruntime.GOARCH,
ContainerRuntimeVersion: "test://1.5.0",
Expand Down