-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Antonio Murdaca <[email protected]>
- Loading branch information
Showing
6 changed files
with
638 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2017 Google Inc. All Rights Reserved. | ||
// | ||
// 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. | ||
|
||
// Handler for CRI-O containers. | ||
package crio | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type Info struct { | ||
StorageDriver string `json:"storage_driver"` | ||
StorageRoot string `json:"storage_root"` | ||
} | ||
|
||
type ContainerInfo struct { | ||
Pid int `json:"pid"` | ||
Image string `json:"image"` | ||
CreatedTime int64 `json:"created_time"` | ||
Labels map[string]string `json:"labels"` | ||
Annotations map[string]string `json:"annotations"` | ||
LogPath string `json:"log_path"` | ||
Root string `json:"root"` | ||
} | ||
|
||
type crioClient interface { | ||
Info() (Info, error) | ||
ContainerInfo(string) (*ContainerInfo, error) | ||
} | ||
|
||
type crioClientImpl struct { | ||
client *http.Client | ||
} | ||
|
||
// Client ... | ||
func Client() (crioClient, error) { | ||
c := &http.Client{} | ||
return &crioClientImpl{ | ||
client: c, | ||
}, nil | ||
} | ||
|
||
// Info ... | ||
func (c *crioClientImpl) Info() (Info, error) { | ||
info := Info{} | ||
// TODO(runcom): use crio.sock, PR coming in CRI-O | ||
// and also, get it from pkg/kubelet/cadvisor/cadvisor_linux.go | ||
resp, err := c.client.Get("http://localhost:7373/info") | ||
if err != nil { | ||
return info, err | ||
} | ||
defer resp.Body.Close() | ||
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { | ||
return info, err | ||
} | ||
return info, nil | ||
} | ||
|
||
// ContainerInfo ... | ||
func (c *crioClientImpl) ContainerInfo(id string) (*ContainerInfo, error) { | ||
// TODO(runcom): use crio.sock, PR coming in CRI-O | ||
// and also, get it from pkg/kubelet/cadvisor/cadvisor_linux.go | ||
resp, err := c.client.Get("http://localhost:7373/containers/" + id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
cInfo := ContainerInfo{} | ||
if err := json.NewDecoder(resp.Body).Decode(&cInfo); err != nil { | ||
return nil, err | ||
} | ||
return &cInfo, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
// Copyright 2017 Google Inc. All Rights Reserved. | ||
// | ||
// 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 crio | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"path" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/google/cadvisor/container" | ||
"github.com/google/cadvisor/container/libcontainer" | ||
"github.com/google/cadvisor/fs" | ||
info "github.com/google/cadvisor/info/v1" | ||
"github.com/google/cadvisor/manager/watcher" | ||
|
||
"github.com/golang/glog" | ||
) | ||
|
||
var ArgCrioEndpoint = flag.String("crio", "unix:///var/run/crio.sock", "crio endpoint") | ||
|
||
// The namespace under which crio aliases are unique. | ||
const CrioNamespace = "crio" | ||
|
||
// Regexp that identifies docker cgroups, containers started with | ||
// --cgroup-parent have another prefix than 'docker' | ||
var crioCgroupRegexp = regexp.MustCompile(`([a-z0-9]{64})`) | ||
|
||
type storageDriver string | ||
|
||
const ( | ||
// TODO add full set of supported drivers in future.. | ||
overlayStorageDriver storageDriver = "overlay" | ||
overlay2StorageDriver storageDriver = "overlay2" | ||
) | ||
|
||
type crioFactory struct { | ||
machineInfoFactory info.MachineInfoFactory | ||
|
||
storageDriver storageDriver | ||
storageDir string | ||
|
||
// Information about the mounted cgroup subsystems. | ||
cgroupSubsystems libcontainer.CgroupSubsystems | ||
|
||
// Information about mounted filesystems. | ||
fsInfo fs.FsInfo | ||
|
||
ignoreMetrics container.MetricSet | ||
|
||
client crioClient | ||
} | ||
|
||
func (self *crioFactory) String() string { | ||
return CrioNamespace | ||
} | ||
|
||
func (self *crioFactory) NewContainerHandler(name string, inHostNamespace bool) (handler container.ContainerHandler, err error) { | ||
client, err := Client() | ||
if err != nil { | ||
return | ||
} | ||
// TODO are there any env vars we need to white list, if so, do it here... | ||
metadataEnvs := []string{} | ||
handler, err = newCrioContainerHandler( | ||
client, | ||
name, | ||
self.machineInfoFactory, | ||
self.fsInfo, | ||
self.storageDriver, | ||
self.storageDir, | ||
&self.cgroupSubsystems, | ||
inHostNamespace, | ||
metadataEnvs, | ||
self.ignoreMetrics, | ||
) | ||
return | ||
} | ||
|
||
// Returns the CRIO ID from the full container name. | ||
func ContainerNameToCrioId(name string) string { | ||
id := path.Base(name) | ||
|
||
if matches := crioCgroupRegexp.FindStringSubmatch(id); matches != nil { | ||
return matches[1] | ||
} | ||
|
||
return id | ||
} | ||
|
||
// isContainerName returns true if the cgroup with associated name | ||
// corresponds to a crio container. | ||
func isContainerName(name string) bool { | ||
// always ignore .mount cgroup even if associated with crio and delegate to systemd | ||
if strings.HasSuffix(name, ".mount") { | ||
return false | ||
} | ||
return crioCgroupRegexp.MatchString(path.Base(name)) | ||
} | ||
|
||
// crio handles all containers under /crio | ||
func (self *crioFactory) CanHandleAndAccept(name string) (bool, bool, error) { | ||
glog.Infof("CRIO CAN HANDLE AND ACCEPT: %v", name) | ||
if strings.HasPrefix(path.Base(name), "crio-conmon") { | ||
glog.Info("SKIPPING CRIO-CONMON") | ||
} | ||
if !strings.HasPrefix(path.Base(name), CrioNamespace) { | ||
return false, false, nil | ||
} | ||
// if the container is not associated with docker, we can't handle it or accept it. | ||
if !isContainerName(name) { | ||
return false, false, nil | ||
} | ||
glog.Infof("CRIO HANDLE AND ACCEPT: %v", name) | ||
// TODO should we call equivalent of a crio info to be sure its really ours | ||
// and to know if the container is running... | ||
return true, true, nil | ||
} | ||
|
||
func (self *crioFactory) DebugInfo() map[string][]string { | ||
return map[string][]string{} | ||
} | ||
|
||
var ( | ||
version_regexp_string = `(\d+)\.(\d+)\.(\d+)` | ||
version_re = regexp.MustCompile(version_regexp_string) | ||
apiversion_regexp_string = `(\d+)\.(\d+)` | ||
apiversion_re = regexp.MustCompile(apiversion_regexp_string) | ||
) | ||
|
||
// Register root container before running this function! | ||
func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, ignoreMetrics container.MetricSet) error { | ||
client, err := Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
info, err := client.Info() | ||
if err != nil { | ||
return err | ||
} | ||
// TODO determine crio version so we can work differently w/ future versions if needed | ||
|
||
cgroupSubsystems, err := libcontainer.GetCgroupSubsystems() | ||
if err != nil { | ||
return fmt.Errorf("failed to get cgroup subsystems: %v", err) | ||
} | ||
|
||
glog.Infof("Registering CRI-O factory") | ||
f := &crioFactory{ | ||
client: client, | ||
cgroupSubsystems: cgroupSubsystems, | ||
fsInfo: fsInfo, | ||
machineInfoFactory: factory, | ||
storageDriver: storageDriver(info.StorageDriver), | ||
storageDir: info.StorageRoot, | ||
ignoreMetrics: ignoreMetrics, | ||
} | ||
|
||
container.RegisterContainerHandlerFactory(f, []watcher.ContainerWatchSource{watcher.Raw}) | ||
return nil | ||
} |
Oops, something went wrong.