|
| 1 | +// Copyright 2017 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 crio |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "path" |
| 20 | + "regexp" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/google/cadvisor/container" |
| 24 | + "github.com/google/cadvisor/container/libcontainer" |
| 25 | + "github.com/google/cadvisor/fs" |
| 26 | + info "github.com/google/cadvisor/info/v1" |
| 27 | + "github.com/google/cadvisor/manager/watcher" |
| 28 | + |
| 29 | + "github.com/golang/glog" |
| 30 | +) |
| 31 | + |
| 32 | +// The namespace under which crio aliases are unique. |
| 33 | +const CrioNamespace = "crio" |
| 34 | + |
| 35 | +// Regexp that identifies CRI-O cgroups |
| 36 | +var crioCgroupRegexp = regexp.MustCompile(`([a-z0-9]{64})`) |
| 37 | + |
| 38 | +type storageDriver string |
| 39 | + |
| 40 | +const ( |
| 41 | + // TODO add full set of supported drivers in future.. |
| 42 | + overlayStorageDriver storageDriver = "overlay" |
| 43 | + overlay2StorageDriver storageDriver = "overlay2" |
| 44 | +) |
| 45 | + |
| 46 | +type crioFactory struct { |
| 47 | + machineInfoFactory info.MachineInfoFactory |
| 48 | + |
| 49 | + storageDriver storageDriver |
| 50 | + storageDir string |
| 51 | + |
| 52 | + // Information about the mounted cgroup subsystems. |
| 53 | + cgroupSubsystems libcontainer.CgroupSubsystems |
| 54 | + |
| 55 | + // Information about mounted filesystems. |
| 56 | + fsInfo fs.FsInfo |
| 57 | + |
| 58 | + ignoreMetrics container.MetricSet |
| 59 | + |
| 60 | + client crioClient |
| 61 | +} |
| 62 | + |
| 63 | +func (self *crioFactory) String() string { |
| 64 | + return CrioNamespace |
| 65 | +} |
| 66 | + |
| 67 | +func (self *crioFactory) NewContainerHandler(name string, inHostNamespace bool) (handler container.ContainerHandler, err error) { |
| 68 | + client, err := Client() |
| 69 | + if err != nil { |
| 70 | + return |
| 71 | + } |
| 72 | + // TODO are there any env vars we need to white list, if so, do it here... |
| 73 | + metadataEnvs := []string{} |
| 74 | + handler, err = newCrioContainerHandler( |
| 75 | + client, |
| 76 | + name, |
| 77 | + self.machineInfoFactory, |
| 78 | + self.fsInfo, |
| 79 | + self.storageDriver, |
| 80 | + self.storageDir, |
| 81 | + &self.cgroupSubsystems, |
| 82 | + inHostNamespace, |
| 83 | + metadataEnvs, |
| 84 | + self.ignoreMetrics, |
| 85 | + ) |
| 86 | + return |
| 87 | +} |
| 88 | + |
| 89 | +// Returns the CRIO ID from the full container name. |
| 90 | +func ContainerNameToCrioId(name string) string { |
| 91 | + id := path.Base(name) |
| 92 | + |
| 93 | + if matches := crioCgroupRegexp.FindStringSubmatch(id); matches != nil { |
| 94 | + return matches[1] |
| 95 | + } |
| 96 | + |
| 97 | + return id |
| 98 | +} |
| 99 | + |
| 100 | +// isContainerName returns true if the cgroup with associated name |
| 101 | +// corresponds to a crio container. |
| 102 | +func isContainerName(name string) bool { |
| 103 | + // always ignore .mount cgroup even if associated with crio and delegate to systemd |
| 104 | + if strings.HasSuffix(name, ".mount") { |
| 105 | + return false |
| 106 | + } |
| 107 | + return crioCgroupRegexp.MatchString(path.Base(name)) |
| 108 | +} |
| 109 | + |
| 110 | +// crio handles all containers under /crio |
| 111 | +func (self *crioFactory) CanHandleAndAccept(name string) (bool, bool, error) { |
| 112 | + if strings.HasPrefix(path.Base(name), "crio-conmon") { |
| 113 | + // TODO(runcom): should we include crio-conmon cgroups? |
| 114 | + return false, false, nil |
| 115 | + } |
| 116 | + if !strings.HasPrefix(path.Base(name), CrioNamespace) { |
| 117 | + return false, false, nil |
| 118 | + } |
| 119 | + // if the container is not associated with CRI-O, we can't handle it or accept it. |
| 120 | + if !isContainerName(name) { |
| 121 | + return false, false, nil |
| 122 | + } |
| 123 | + return true, true, nil |
| 124 | +} |
| 125 | + |
| 126 | +func (self *crioFactory) DebugInfo() map[string][]string { |
| 127 | + return map[string][]string{} |
| 128 | +} |
| 129 | + |
| 130 | +var ( |
| 131 | + // TODO(runcom): handle versioning in CRI-O |
| 132 | + version_regexp_string = `(\d+)\.(\d+)\.(\d+)` |
| 133 | + version_re = regexp.MustCompile(version_regexp_string) |
| 134 | + apiversion_regexp_string = `(\d+)\.(\d+)` |
| 135 | + apiversion_re = regexp.MustCompile(apiversion_regexp_string) |
| 136 | +) |
| 137 | + |
| 138 | +// Register root container before running this function! |
| 139 | +func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, ignoreMetrics container.MetricSet) error { |
| 140 | + client, err := Client() |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + info, err := client.Info() |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + // TODO determine crio version so we can work differently w/ future versions if needed |
| 151 | + |
| 152 | + cgroupSubsystems, err := libcontainer.GetCgroupSubsystems() |
| 153 | + if err != nil { |
| 154 | + return fmt.Errorf("failed to get cgroup subsystems: %v", err) |
| 155 | + } |
| 156 | + |
| 157 | + glog.Infof("Registering CRI-O factory") |
| 158 | + f := &crioFactory{ |
| 159 | + client: client, |
| 160 | + cgroupSubsystems: cgroupSubsystems, |
| 161 | + fsInfo: fsInfo, |
| 162 | + machineInfoFactory: factory, |
| 163 | + storageDriver: storageDriver(info.StorageDriver), |
| 164 | + storageDir: info.StorageRoot, |
| 165 | + ignoreMetrics: ignoreMetrics, |
| 166 | + } |
| 167 | + |
| 168 | + container.RegisterContainerHandlerFactory(f, []watcher.ContainerWatchSource{watcher.Raw}) |
| 169 | + return nil |
| 170 | +} |
0 commit comments