diff --git a/cmd/kubelet/app/server.go b/cmd/kubelet/app/server.go index c9534d35067..63d2cbd9615 100644 --- a/cmd/kubelet/app/server.go +++ b/cmd/kubelet/app/server.go @@ -414,7 +414,7 @@ func run(s *options.KubeletServer, kubeDeps *kubelet.Dependencies) (err error) { if kubeDeps.CAdvisorInterface == nil { imageFsInfoProvider := cadvisor.NewImageFsInfoProvider(s.ContainerRuntime, s.RemoteRuntimeEndpoint) - kubeDeps.CAdvisorInterface, err = cadvisor.New(s.Address, uint(s.CAdvisorPort), imageFsInfoProvider, s.RootDirectory) + kubeDeps.CAdvisorInterface, err = cadvisor.New(s.Address, uint(s.CAdvisorPort), imageFsInfoProvider, s.RootDirectory, cadvisor.UsingLegacyCadvisorStats(s.ContainerRuntime, s.RemoteRuntimeEndpoint)) if err != nil { return err } diff --git a/pkg/kubelet/cadvisor/BUILD b/pkg/kubelet/cadvisor/BUILD index adb113dd97d..faa9d5005b2 100644 --- a/pkg/kubelet/cadvisor/BUILD +++ b/pkg/kubelet/cadvisor/BUILD @@ -28,6 +28,7 @@ go_library( deps = [ "//pkg/apis/core/v1/helper:go_default_library", "//pkg/features:go_default_library", + "//pkg/kubelet/types:go_default_library", "//vendor/github.com/google/cadvisor/events:go_default_library", "//vendor/github.com/google/cadvisor/info/v1:go_default_library", "//vendor/github.com/google/cadvisor/info/v2:go_default_library", @@ -36,7 +37,6 @@ go_library( "//vendor/k8s.io/apiserver/pkg/util/feature:go_default_library", ] + select({ "@io_bazel_rules_go//go/platform:linux_amd64": [ - "//pkg/kubelet/types:go_default_library", "//vendor/github.com/golang/glog:go_default_library", "//vendor/github.com/google/cadvisor/cache/memory:go_default_library", "//vendor/github.com/google/cadvisor/container:go_default_library", diff --git a/pkg/kubelet/cadvisor/cadvisor_linux.go b/pkg/kubelet/cadvisor/cadvisor_linux.go index 4749016a49a..948cb651b30 100644 --- a/pkg/kubelet/cadvisor/cadvisor_linux.go +++ b/pkg/kubelet/cadvisor/cadvisor_linux.go @@ -105,11 +105,16 @@ func containerLabels(c *cadvisorapi.ContainerInfo) map[string]string { } // New creates a cAdvisor and exports its API on the specified port if port > 0. -func New(address string, port uint, imageFsInfoProvider ImageFsInfoProvider, rootPath string) (Interface, error) { +func New(address string, port uint, imageFsInfoProvider ImageFsInfoProvider, rootPath string, usingLegacyStats bool) (Interface, error) { sysFs := sysfs.NewRealSysFs() + ignoreMetrics := cadvisormetrics.MetricSet{cadvisormetrics.NetworkTcpUsageMetrics: struct{}{}, cadvisormetrics.NetworkUdpUsageMetrics: struct{}{}} + if !usingLegacyStats { + ignoreMetrics[cadvisormetrics.DiskUsageMetrics] = struct{}{} + } + // Create and start the cAdvisor container manager. - m, err := manager.New(memory.New(statsCacheDuration, nil), sysFs, maxHousekeepingInterval, allowDynamicHousekeeping, cadvisormetrics.MetricSet{cadvisormetrics.NetworkTcpUsageMetrics: struct{}{}, cadvisormetrics.NetworkUdpUsageMetrics: struct{}{}}, http.DefaultClient) + m, err := manager.New(memory.New(statsCacheDuration, nil), sysFs, maxHousekeepingInterval, allowDynamicHousekeeping, ignoreMetrics, http.DefaultClient) if err != nil { return nil, err } diff --git a/pkg/kubelet/cadvisor/util.go b/pkg/kubelet/cadvisor/util.go index 6c679a07208..7937917a89b 100644 --- a/pkg/kubelet/cadvisor/util.go +++ b/pkg/kubelet/cadvisor/util.go @@ -17,6 +17,8 @@ limitations under the License. package cadvisor import ( + goruntime "runtime" + cadvisorapi "github.com/google/cadvisor/info/v1" cadvisorapi2 "github.com/google/cadvisor/info/v2" "k8s.io/api/core/v1" @@ -24,6 +26,7 @@ import ( utilfeature "k8s.io/apiserver/pkg/util/feature" v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper" "k8s.io/kubernetes/pkg/features" + kubetypes "k8s.io/kubernetes/pkg/kubelet/types" ) func CapacityFromMachineInfo(info *cadvisorapi.MachineInfo) v1.ResourceList { @@ -57,3 +60,16 @@ func EphemeralStorageCapacityFromFsInfo(info cadvisorapi2.FsInfo) v1.ResourceLis } return c } + +// 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.RktContainerRuntime || + (runtime == kubetypes.DockerContainerRuntime && goruntime.GOOS == "linux") || + runtimeEndpoint == "/var/run/crio.sock" +} diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 9b6ed93ee9b..3808b701149 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -24,7 +24,6 @@ import ( "net/url" "os" "path" - goruntime "runtime" "sort" "strings" "sync" @@ -681,14 +680,7 @@ func NewMainKubelet(kubeCfg *kubeletconfiginternal.KubeletConfiguration, klet.containerRuntime = runtime klet.runner = runtime - // 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 - if (containerRuntime == kubetypes.DockerContainerRuntime && - goruntime.GOOS == "linux") || remoteRuntimeEndpoint == "/var/run/crio.sock" { + if cadvisor.UsingLegacyCadvisorStats(containerRuntime, remoteRuntimeEndpoint) { klet.StatsProvider = stats.NewCadvisorStatsProvider( klet.cadvisor, klet.resourceAnalyzer, diff --git a/test/e2e_node/environment/conformance.go b/test/e2e_node/environment/conformance.go index 0b887c07bd5..fc53559fd77 100644 --- a/test/e2e_node/environment/conformance.go +++ b/test/e2e_node/environment/conformance.go @@ -99,7 +99,7 @@ func containerRuntime() error { } // Setup cadvisor to check the container environment - c, err := cadvisor.New("", 0 /*don't start the http server*/, cadvisor.NewImageFsInfoProvider("docker", ""), "/var/lib/kubelet") + c, err := cadvisor.New("", 0 /*don't start the http server*/, cadvisor.NewImageFsInfoProvider("docker", ""), "/var/lib/kubelet", false) if err != nil { return printError("Container Runtime Check: %s Could not start cadvisor %v", failed, err) }