github.com/google/cadvisor@v0.49.1/container/raw/factory.go (about) 1 // Copyright 2014 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package raw 16 17 import ( 18 "flag" 19 "fmt" 20 "strings" 21 22 "github.com/google/cadvisor/container" 23 "github.com/google/cadvisor/container/common" 24 "github.com/google/cadvisor/container/libcontainer" 25 "github.com/google/cadvisor/fs" 26 info "github.com/google/cadvisor/info/v1" 27 watch "github.com/google/cadvisor/watcher" 28 29 "k8s.io/klog/v2" 30 ) 31 32 var ( 33 DockerOnly = flag.Bool("docker_only", false, "Only report docker containers in addition to root stats") 34 disableRootCgroupStats = flag.Bool("disable_root_cgroup_stats", false, "Disable collecting root Cgroup stats") 35 ) 36 37 type rawFactory struct { 38 // Factory for machine information. 39 machineInfoFactory info.MachineInfoFactory 40 41 // Information about the cgroup subsystems. 42 cgroupSubsystems map[string]string 43 44 // Information about mounted filesystems. 45 fsInfo fs.FsInfo 46 47 // Watcher for inotify events. 48 watcher *common.InotifyWatcher 49 50 // List of metrics to be included. 51 includedMetrics map[container.MetricKind]struct{} 52 53 // List of raw container cgroup path prefix whitelist. 54 rawPrefixWhiteList []string 55 } 56 57 func (f *rawFactory) String() string { 58 return "raw" 59 } 60 61 func (f *rawFactory) NewContainerHandler(name string, metadataEnvAllowList []string, inHostNamespace bool) (container.ContainerHandler, error) { 62 rootFs := "/" 63 if !inHostNamespace { 64 rootFs = "/rootfs" 65 } 66 return newRawContainerHandler(name, f.cgroupSubsystems, f.machineInfoFactory, f.fsInfo, f.watcher, rootFs, f.includedMetrics) 67 } 68 69 // The raw factory can handle any container. If --docker_only is set to true, non-docker containers are ignored except for "/" and those whitelisted by raw_cgroup_prefix_whitelist flag. 70 func (f *rawFactory) CanHandleAndAccept(name string) (bool, bool, error) { 71 if name == "/" { 72 return true, true, nil 73 } 74 if *DockerOnly && f.rawPrefixWhiteList[0] == "" { 75 return true, false, nil 76 } 77 for _, prefix := range f.rawPrefixWhiteList { 78 if strings.HasPrefix(name, prefix) { 79 return true, true, nil 80 } 81 } 82 return true, false, nil 83 } 84 85 func (f *rawFactory) DebugInfo() map[string][]string { 86 return common.DebugInfo(f.watcher.GetWatches()) 87 } 88 89 func Register(machineInfoFactory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics map[container.MetricKind]struct{}, rawPrefixWhiteList []string) error { 90 cgroupSubsystems, err := libcontainer.GetCgroupSubsystems(includedMetrics) 91 if err != nil { 92 return fmt.Errorf("failed to get cgroup subsystems: %v", err) 93 } 94 if len(cgroupSubsystems) == 0 { 95 return fmt.Errorf("failed to find supported cgroup mounts for the raw factory") 96 } 97 98 watcher, err := common.NewInotifyWatcher() 99 if err != nil { 100 return err 101 } 102 103 klog.V(1).Infof("Registering Raw factory") 104 factory := &rawFactory{ 105 machineInfoFactory: machineInfoFactory, 106 fsInfo: fsInfo, 107 cgroupSubsystems: cgroupSubsystems, 108 watcher: watcher, 109 includedMetrics: includedMetrics, 110 rawPrefixWhiteList: rawPrefixWhiteList, 111 } 112 container.RegisterContainerHandlerFactory(factory, []watch.ContainerWatchSource{watch.Raw}) 113 return nil 114 }