2015-03-10 05:39:00 +00:00
|
|
|
// +build cgo,linux
|
|
|
|
|
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-03-10 05:39:00 +00:00
|
|
|
|
|
|
|
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 cadvisor
|
|
|
|
|
|
|
|
import (
|
2015-12-16 00:12:30 +00:00
|
|
|
"flag"
|
2015-03-10 05:39:00 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-07-05 06:08:47 +00:00
|
|
|
"os"
|
2017-08-15 20:40:21 +00:00
|
|
|
"path"
|
2015-03-10 05:39:00 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2015-06-12 22:59:13 +00:00
|
|
|
"github.com/google/cadvisor/cache/memory"
|
2016-11-30 07:27:27 +00:00
|
|
|
cadvisormetrics "github.com/google/cadvisor/container"
|
2015-04-09 22:38:36 +00:00
|
|
|
"github.com/google/cadvisor/events"
|
2015-10-16 03:00:28 +00:00
|
|
|
cadvisorapi "github.com/google/cadvisor/info/v1"
|
|
|
|
cadvisorapiv2 "github.com/google/cadvisor/info/v2"
|
2015-03-10 05:39:00 +00:00
|
|
|
"github.com/google/cadvisor/manager"
|
2016-08-20 22:04:34 +00:00
|
|
|
"github.com/google/cadvisor/metrics"
|
2015-03-10 05:39:00 +00:00
|
|
|
"github.com/google/cadvisor/utils/sysfs"
|
2016-08-20 22:04:34 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/types"
|
2015-03-10 05:39:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type cadvisorClient struct {
|
2017-08-31 18:17:34 +00:00
|
|
|
imageFsInfoProvider ImageFsInfoProvider
|
|
|
|
rootPath string
|
2015-03-10 05:39:00 +00:00
|
|
|
manager.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Interface = new(cadvisorClient)
|
|
|
|
|
|
|
|
// TODO(vmarmol): Make configurable.
|
2015-04-24 21:44:13 +00:00
|
|
|
// The amount of time for which to keep stats in memory.
|
|
|
|
const statsCacheDuration = 2 * time.Minute
|
2015-07-30 14:44:45 +00:00
|
|
|
const maxHousekeepingInterval = 15 * time.Second
|
2015-12-16 00:12:30 +00:00
|
|
|
const defaultHousekeepingInterval = 10 * time.Second
|
2015-07-30 14:44:45 +00:00
|
|
|
const allowDynamicHousekeeping = true
|
2015-03-10 05:39:00 +00:00
|
|
|
|
2015-12-16 00:12:30 +00:00
|
|
|
func init() {
|
2016-04-26 00:32:16 +00:00
|
|
|
// Override cAdvisor flag defaults.
|
|
|
|
flagOverrides := map[string]string{
|
|
|
|
// Override the default cAdvisor housekeeping interval.
|
|
|
|
"housekeeping_interval": defaultHousekeepingInterval.String(),
|
|
|
|
// Disable event storage by default.
|
|
|
|
"event_storage_event_limit": "default=0",
|
|
|
|
"event_storage_age_limit": "default=0",
|
|
|
|
}
|
|
|
|
for name, defaultValue := range flagOverrides {
|
|
|
|
if f := flag.Lookup(name); f != nil {
|
|
|
|
f.DefValue = defaultValue
|
|
|
|
f.Value.Set(defaultValue)
|
|
|
|
} else {
|
|
|
|
glog.Errorf("Expected cAdvisor flag %q not found", name)
|
|
|
|
}
|
2015-12-16 00:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-20 22:04:34 +00:00
|
|
|
func containerLabels(c *cadvisorapi.ContainerInfo) map[string]string {
|
2017-08-28 19:27:38 +00:00
|
|
|
// Prometheus requires that all metrics in the same family have the same labels,
|
|
|
|
// so we arrange to supply blank strings for missing labels
|
|
|
|
var name, image, podName, namespace, containerName string
|
2016-08-20 22:04:34 +00:00
|
|
|
if len(c.Aliases) > 0 {
|
2017-08-28 19:27:38 +00:00
|
|
|
name = c.Aliases[0]
|
2016-08-20 22:04:34 +00:00
|
|
|
}
|
2017-08-28 19:27:38 +00:00
|
|
|
image = c.Spec.Image
|
2016-08-20 22:04:34 +00:00
|
|
|
if v, ok := c.Spec.Labels[types.KubernetesPodNameLabel]; ok {
|
2017-08-28 19:27:38 +00:00
|
|
|
podName = v
|
2016-08-20 22:04:34 +00:00
|
|
|
}
|
|
|
|
if v, ok := c.Spec.Labels[types.KubernetesPodNamespaceLabel]; ok {
|
2017-08-28 19:27:38 +00:00
|
|
|
namespace = v
|
2016-08-20 22:04:34 +00:00
|
|
|
}
|
|
|
|
if v, ok := c.Spec.Labels[types.KubernetesContainerNameLabel]; ok {
|
2017-08-28 19:27:38 +00:00
|
|
|
containerName = v
|
|
|
|
}
|
|
|
|
set := map[string]string{
|
|
|
|
metrics.LabelID: c.Name,
|
|
|
|
metrics.LabelName: name,
|
|
|
|
metrics.LabelImage: image,
|
|
|
|
"pod_name": podName,
|
|
|
|
"namespace": namespace,
|
|
|
|
"container_name": containerName,
|
2016-08-20 22:04:34 +00:00
|
|
|
}
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a cAdvisor and exports its API on the specified port if port > 0.
|
2018-07-02 12:40:54 +00:00
|
|
|
func New(imageFsInfoProvider ImageFsInfoProvider, rootPath string, usingLegacyStats bool) (Interface, error) {
|
2017-04-04 18:48:09 +00:00
|
|
|
sysFs := sysfs.NewRealSysFs()
|
2015-03-10 05:39:00 +00:00
|
|
|
|
2018-02-20 23:31:46 +00:00
|
|
|
ignoreMetrics := cadvisormetrics.MetricSet{
|
2018-06-06 00:12:56 +00:00
|
|
|
cadvisormetrics.NetworkTcpUsageMetrics: struct{}{},
|
|
|
|
cadvisormetrics.NetworkUdpUsageMetrics: struct{}{},
|
|
|
|
cadvisormetrics.PerCpuUsageMetrics: struct{}{},
|
|
|
|
cadvisormetrics.ProcessSchedulerMetrics: struct{}{},
|
2018-02-20 23:31:46 +00:00
|
|
|
}
|
2017-11-14 19:43:08 +00:00
|
|
|
if !usingLegacyStats {
|
|
|
|
ignoreMetrics[cadvisormetrics.DiskUsageMetrics] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2015-03-10 05:39:00 +00:00
|
|
|
// Create and start the cAdvisor container manager.
|
2017-11-14 19:43:08 +00:00
|
|
|
m, err := manager.New(memory.New(statsCacheDuration, nil), sysFs, maxHousekeepingInterval, allowDynamicHousekeeping, ignoreMetrics, http.DefaultClient)
|
2015-03-10 05:39:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:08:47 +00:00
|
|
|
if _, err := os.Stat(rootPath); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2017-08-15 20:40:21 +00:00
|
|
|
if err := os.MkdirAll(path.Clean(rootPath), 0750); err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating root directory %q: %v", rootPath, err)
|
|
|
|
}
|
2017-07-05 06:08:47 +00:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("failed to Stat %q: %v", rootPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 05:39:00 +00:00
|
|
|
cadvisorClient := &cadvisorClient{
|
2017-08-31 18:17:34 +00:00
|
|
|
imageFsInfoProvider: imageFsInfoProvider,
|
|
|
|
rootPath: rootPath,
|
|
|
|
Manager: m,
|
2015-03-10 05:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cadvisorClient, nil
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:24:24 +00:00
|
|
|
func (cc *cadvisorClient) Start() error {
|
|
|
|
return cc.Manager.Start()
|
|
|
|
}
|
|
|
|
|
2015-10-16 03:00:28 +00:00
|
|
|
func (cc *cadvisorClient) ContainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
|
2015-04-20 03:26:07 +00:00
|
|
|
return cc.GetContainerInfo(name, req)
|
2015-03-10 05:39:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 20:14:06 +00:00
|
|
|
func (cc *cadvisorClient) ContainerInfoV2(name string, options cadvisorapiv2.RequestOptions) (map[string]cadvisorapiv2.ContainerInfo, error) {
|
|
|
|
return cc.GetContainerInfoV2(name, options)
|
|
|
|
}
|
|
|
|
|
2015-10-16 03:00:28 +00:00
|
|
|
func (cc *cadvisorClient) VersionInfo() (*cadvisorapi.VersionInfo, error) {
|
2015-04-20 03:26:07 +00:00
|
|
|
return cc.GetVersionInfo()
|
2015-03-31 07:22:37 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 03:00:28 +00:00
|
|
|
func (cc *cadvisorClient) SubcontainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (map[string]*cadvisorapi.ContainerInfo, error) {
|
2015-04-23 16:23:57 +00:00
|
|
|
infos, err := cc.SubcontainersInfo(name, req)
|
2016-05-20 02:29:31 +00:00
|
|
|
if err != nil && len(infos) == 0 {
|
2015-04-23 16:23:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-16 03:00:28 +00:00
|
|
|
result := make(map[string]*cadvisorapi.ContainerInfo, len(infos))
|
2015-04-23 16:23:57 +00:00
|
|
|
for _, info := range infos {
|
|
|
|
result[info.Name] = info
|
|
|
|
}
|
2016-05-20 02:29:31 +00:00
|
|
|
return result, err
|
2015-04-23 16:23:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 03:00:28 +00:00
|
|
|
func (cc *cadvisorClient) MachineInfo() (*cadvisorapi.MachineInfo, error) {
|
2015-04-20 03:26:07 +00:00
|
|
|
return cc.GetMachineInfo()
|
2015-03-10 05:39:00 +00:00
|
|
|
}
|
2015-03-16 01:40:34 +00:00
|
|
|
|
2016-05-18 05:05:55 +00:00
|
|
|
func (cc *cadvisorClient) ImagesFsInfo() (cadvisorapiv2.FsInfo, error) {
|
2017-08-31 18:17:34 +00:00
|
|
|
label, err := cc.imageFsInfoProvider.ImageFsInfoLabel()
|
|
|
|
if err != nil {
|
|
|
|
return cadvisorapiv2.FsInfo{}, err
|
2016-05-18 05:05:55 +00:00
|
|
|
}
|
|
|
|
return cc.getFsInfo(label)
|
2015-05-12 08:24:08 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 03:00:28 +00:00
|
|
|
func (cc *cadvisorClient) RootFsInfo() (cadvisorapiv2.FsInfo, error) {
|
2016-10-22 23:06:47 +00:00
|
|
|
return cc.GetDirFsInfo(cc.rootPath)
|
2015-05-12 08:24:08 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 03:00:28 +00:00
|
|
|
func (cc *cadvisorClient) getFsInfo(label string) (cadvisorapiv2.FsInfo, error) {
|
2015-05-12 08:24:08 +00:00
|
|
|
res, err := cc.GetFsInfo(label)
|
2015-03-16 01:40:34 +00:00
|
|
|
if err != nil {
|
2015-10-16 03:00:28 +00:00
|
|
|
return cadvisorapiv2.FsInfo{}, err
|
2015-03-16 01:40:34 +00:00
|
|
|
}
|
|
|
|
if len(res) == 0 {
|
2015-10-16 03:00:28 +00:00
|
|
|
return cadvisorapiv2.FsInfo{}, fmt.Errorf("failed to find information for the filesystem labeled %q", label)
|
2015-03-16 01:40:34 +00:00
|
|
|
}
|
2015-05-12 08:24:08 +00:00
|
|
|
// TODO(vmarmol): Handle this better when a label has more than one image filesystem.
|
2015-03-16 01:40:34 +00:00
|
|
|
if len(res) > 1 {
|
2015-05-12 08:24:08 +00:00
|
|
|
glog.Warningf("More than one filesystem labeled %q: %#v. Only using the first one", label, res)
|
2015-03-16 01:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res[0], nil
|
|
|
|
}
|
2015-04-09 22:38:36 +00:00
|
|
|
|
2015-04-09 22:38:36 +00:00
|
|
|
func (cc *cadvisorClient) WatchEvents(request *events.Request) (*events.EventChannel, error) {
|
|
|
|
return cc.WatchForEvents(request)
|
2015-04-09 22:38:36 +00:00
|
|
|
}
|