Support remote runtimes with native cAdvisor support

This commit is contained in:
Derek Carr 2017-08-31 14:17:34 -04:00
parent 721923924d
commit 566f411b08
8 changed files with 54 additions and 23 deletions

View File

@ -392,7 +392,8 @@ func run(s *options.KubeletServer, kubeDeps *kubelet.Dependencies) (err error) {
} }
if kubeDeps.CAdvisorInterface == nil { if kubeDeps.CAdvisorInterface == nil {
kubeDeps.CAdvisorInterface, err = cadvisor.New(s.Address, uint(s.CAdvisorPort), s.ContainerRuntime, s.RootDirectory) imageFsInfoProvider := cadvisor.NewImageFsInfoProvider(s.ContainerRuntime, s.RemoteRuntimeEndpoint)
kubeDeps.CAdvisorInterface, err = cadvisor.New(s.Address, uint(s.CAdvisorPort), imageFsInfoProvider, s.RootDirectory)
if err != nil { if err != nil {
return err return err
} }

View File

@ -24,6 +24,7 @@ go_library(
}), }),
deps = [ deps = [
"//vendor/github.com/google/cadvisor/events:go_default_library", "//vendor/github.com/google/cadvisor/events:go_default_library",
"//vendor/github.com/google/cadvisor/fs:go_default_library",
"//vendor/github.com/google/cadvisor/info/v1:go_default_library", "//vendor/github.com/google/cadvisor/info/v1:go_default_library",
"//vendor/github.com/google/cadvisor/info/v2:go_default_library", "//vendor/github.com/google/cadvisor/info/v2:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library", "//vendor/k8s.io/api/core/v1:go_default_library",
@ -34,7 +35,6 @@ go_library(
"//vendor/github.com/golang/glog: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/cache/memory:go_default_library",
"//vendor/github.com/google/cadvisor/container:go_default_library", "//vendor/github.com/google/cadvisor/container:go_default_library",
"//vendor/github.com/google/cadvisor/fs:go_default_library",
"//vendor/github.com/google/cadvisor/http:go_default_library", "//vendor/github.com/google/cadvisor/http:go_default_library",
"//vendor/github.com/google/cadvisor/manager:go_default_library", "//vendor/github.com/google/cadvisor/manager:go_default_library",
"//vendor/github.com/google/cadvisor/metrics:go_default_library", "//vendor/github.com/google/cadvisor/metrics:go_default_library",

View File

@ -32,7 +32,6 @@ import (
"github.com/google/cadvisor/cache/memory" "github.com/google/cadvisor/cache/memory"
cadvisormetrics "github.com/google/cadvisor/container" cadvisormetrics "github.com/google/cadvisor/container"
"github.com/google/cadvisor/events" "github.com/google/cadvisor/events"
cadvisorfs "github.com/google/cadvisor/fs"
cadvisorhttp "github.com/google/cadvisor/http" cadvisorhttp "github.com/google/cadvisor/http"
cadvisorapi "github.com/google/cadvisor/info/v1" cadvisorapi "github.com/google/cadvisor/info/v1"
cadvisorapiv2 "github.com/google/cadvisor/info/v2" cadvisorapiv2 "github.com/google/cadvisor/info/v2"
@ -44,7 +43,7 @@ import (
) )
type cadvisorClient struct { type cadvisorClient struct {
runtime string imageFsInfoProvider ImageFsInfoProvider
rootPath string rootPath string
manager.Manager manager.Manager
} }
@ -106,7 +105,7 @@ func containerLabels(c *cadvisorapi.ContainerInfo) map[string]string {
} }
// New creates a cAdvisor and exports its API on the specified port if port > 0. // New creates a cAdvisor and exports its API on the specified port if port > 0.
func New(address string, port uint, runtime string, rootPath string) (Interface, error) { func New(address string, port uint, imageFsInfoProvider ImageFsInfoProvider, rootPath string) (Interface, error) {
sysFs := sysfs.NewRealSysFs() sysFs := sysfs.NewRealSysFs()
// Create and start the cAdvisor container manager. // Create and start the cAdvisor container manager.
@ -126,7 +125,7 @@ func New(address string, port uint, runtime string, rootPath string) (Interface,
} }
cadvisorClient := &cadvisorClient{ cadvisorClient := &cadvisorClient{
runtime: runtime, imageFsInfoProvider: imageFsInfoProvider,
rootPath: rootPath, rootPath: rootPath,
Manager: m, Manager: m,
} }
@ -208,17 +207,10 @@ func (cc *cadvisorClient) MachineInfo() (*cadvisorapi.MachineInfo, error) {
} }
func (cc *cadvisorClient) ImagesFsInfo() (cadvisorapiv2.FsInfo, error) { func (cc *cadvisorClient) ImagesFsInfo() (cadvisorapiv2.FsInfo, error) {
var label string label, err := cc.imageFsInfoProvider.ImageFsInfoLabel()
if err != nil {
switch cc.runtime { return cadvisorapiv2.FsInfo{}, err
case "docker":
label = cadvisorfs.LabelDockerImages
case "rkt":
label = cadvisorfs.LabelRktImages
default:
return cadvisorapiv2.FsInfo{}, fmt.Errorf("ImagesFsInfo: unknown runtime: %v", cc.runtime)
} }
return cc.getFsInfo(label) return cc.getFsInfo(label)
} }

View File

@ -31,7 +31,7 @@ type cadvisorUnsupported struct {
var _ Interface = new(cadvisorUnsupported) var _ Interface = new(cadvisorUnsupported)
func New(address string, port uint, runtime string, rootPath string) (Interface, error) { func New(address string, port uint, imageFsInfoProvider ImageFsInfoProvider, rootPath string) (Interface, error) {
return &cadvisorUnsupported{}, nil return &cadvisorUnsupported{}, nil
} }

View File

@ -30,7 +30,7 @@ type cadvisorClient struct {
var _ Interface = new(cadvisorClient) var _ Interface = new(cadvisorClient)
// New creates a cAdvisor and exports its API on the specified port if port > 0. // New creates a cAdvisor and exports its API on the specified port if port > 0.
func New(address string, port uint, runtime string, rootPath string) (Interface, error) { func New(address string, port uint, imageFsInfoProvider ImageFsInfoProvider, rootPath string) (Interface, error) {
return &cadvisorClient{}, nil return &cadvisorClient{}, nil
} }

View File

@ -33,7 +33,7 @@ type Interface interface {
VersionInfo() (*cadvisorapi.VersionInfo, error) VersionInfo() (*cadvisorapi.VersionInfo, error)
// Returns usage information about the filesystem holding Docker images. // Returns usage information about the filesystem holding container images.
ImagesFsInfo() (cadvisorapiv2.FsInfo, error) ImagesFsInfo() (cadvisorapiv2.FsInfo, error)
// Returns usage information about the root filesystem. // Returns usage information about the root filesystem.
@ -45,3 +45,9 @@ type Interface interface {
// HasDedicatedImageFs returns true iff a dedicated image filesystem exists for storing images. // HasDedicatedImageFs returns true iff a dedicated image filesystem exists for storing images.
HasDedicatedImageFs() (bool, error) HasDedicatedImageFs() (bool, error)
} }
// ImageFsInfoProvider informs cAdvisor how to find imagefs for container images.
type ImageFsInfoProvider interface {
// ImageFsInfoLabel returns the label cAdvisor should use to find the filesystem holding container images.
ImageFsInfoLabel() (string, error)
}

View File

@ -17,12 +17,44 @@ limitations under the License.
package cadvisor package cadvisor
import ( import (
"fmt"
cadvisorfs "github.com/google/cadvisor/fs"
cadvisorapi "github.com/google/cadvisor/info/v1" cadvisorapi "github.com/google/cadvisor/info/v1"
cadvisorapi2 "github.com/google/cadvisor/info/v2" cadvisorapi2 "github.com/google/cadvisor/info/v2"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
) )
// imageFsInfoProvider knows how to translate the configured runtime
// to its file system label for images.
type imageFsInfoProvider struct {
runtime string
runtimeEndpoint string
}
// ImageFsInfoLabel returns the image fs label for the configured runtime.
// For remote runtimes, it handles additional runtimes natively understood by cAdvisor.
func (i *imageFsInfoProvider) ImageFsInfoLabel() (string, error) {
switch i.runtime {
case "docker":
return cadvisorfs.LabelDockerImages, nil
case "rkt":
return cadvisorfs.LabelRktImages, nil
case "remote":
// TODO: pending rebase including https://github.com/google/cadvisor/pull/1741
if i.runtimeEndpoint == "/var/run/crio.sock" {
return "crio-images", nil
}
}
return "", fmt.Errorf("no imagefs label for configured runtime")
}
// NewImageFsInfoProvider returns a provider for the specified runtime configuration.
func NewImageFsInfoProvider(runtime, runtimeEndpoint string) ImageFsInfoProvider {
return &imageFsInfoProvider{runtime: runtime, runtimeEndpoint: runtimeEndpoint}
}
func CapacityFromMachineInfo(info *cadvisorapi.MachineInfo) v1.ResourceList { func CapacityFromMachineInfo(info *cadvisorapi.MachineInfo) v1.ResourceList {
c := v1.ResourceList{ c := v1.ResourceList{
v1.ResourceCPU: *resource.NewMilliQuantity( v1.ResourceCPU: *resource.NewMilliQuantity(

View File

@ -99,7 +99,7 @@ func containerRuntime() error {
} }
// Setup cadvisor to check the container environment // Setup cadvisor to check the container environment
c, err := cadvisor.New("", 0 /*don't start the http server*/, "docker", "/var/lib/kubelet") c, err := cadvisor.New("", 0 /*don't start the http server*/, cadvisor.NewImageFsInfoProvider("docker", ""), "/var/lib/kubelet")
if err != nil { if err != nil {
return printError("Container Runtime Check: %s Could not start cadvisor %v", failed, err) return printError("Container Runtime Check: %s Could not start cadvisor %v", failed, err)
} }