Skip to content

Commit

Permalink
Merge pull request #14 from swghosh/bz-1978528
Browse files Browse the repository at this point in the history
UPSTREAM: google/cadvisor: google#2957, google#2999; crio filter out systemd events, always evaluate raw factory last
  • Loading branch information
sjenning authored Nov 11, 2021
2 parents ace0aee + 558447c commit f81b61d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions container/crio/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import (
// The namespace under which crio aliases are unique.
const CrioNamespace = "crio"

// The namespace systemd runs components under.
const SystemdNamespace = "system-systemd"

// Regexp that identifies CRI-O cgroups
var crioCgroupRegexp = regexp.MustCompile(`([a-z0-9]{64})`)

Expand Down Expand Up @@ -116,6 +119,9 @@ func (f *crioFactory) CanHandleAndAccept(name string) (bool, bool, error) {
if !strings.HasPrefix(path.Base(name), CrioNamespace) {
return false, false, nil
}
if strings.HasPrefix(path.Base(name), SystemdNamespace) {
return true, false, nil
}
// if the container is not associated with CRI-O, we can't handle it or accept it.
if !isContainerName(name) {
return false, false, nil
Expand Down
28 changes: 27 additions & 1 deletion container/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ func NewContainerHandler(name string, watchType watcher.ContainerWatchSource, in
defer factoriesLock.RUnlock()

// Create the ContainerHandler with the first factory that supports it.
for _, factory := range factories[watchType] {
// Note that since RawContainerHandler can support a wide range of paths,
// it's evaluated last just to make sure if any other ContainerHandler
// can support it.
for _, factory := range GetReorderedFactoryList(watchType) {
canHandle, canAccept, err := factory.CanHandleAndAccept(name)
if err != nil {
klog.V(4).Infof("Error trying to work out if we can handle %s: %v", name, err)
Expand Down Expand Up @@ -246,3 +249,26 @@ func DebugInfo() map[string][]string {
}
return out
}

// GetReorderedFactoryList returns the list of ContainerHandlerFactory where the
// RawContainerHandler is always the last element.
func GetReorderedFactoryList(watchType watcher.ContainerWatchSource) []ContainerHandlerFactory {
ContainerHandlerFactoryList := make([]ContainerHandlerFactory, 0, len(factories))

var rawFactory ContainerHandlerFactory
for _, v := range factories[watchType] {
if v != nil {
if v.String() == "raw" {
rawFactory = v
continue
}
ContainerHandlerFactoryList = append(ContainerHandlerFactoryList, v)
}
}

if rawFactory != nil {
ContainerHandlerFactoryList = append(ContainerHandlerFactoryList, rawFactory)
}

return ContainerHandlerFactoryList
}
22 changes: 22 additions & 0 deletions container/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,25 @@ func TestNewContainerHandler_Accept(t *testing.T) {
t.Error("Expected NewContainerHandler to ignore the container.")
}
}

func TestRawContainerHandler_Last(t *testing.T) {
chf1 := &mockContainerHandlerFactory{
Name: "raw",
}
container.RegisterContainerHandlerFactory(chf1, []watcher.ContainerWatchSource{watcher.Raw})
cfh2 := &mockContainerHandlerFactory{
Name: "crio",
}
container.RegisterContainerHandlerFactory(cfh2, []watcher.ContainerWatchSource{watcher.Raw})

cfh3 := &mockContainerHandlerFactory{
Name: "containerd",
}
container.RegisterContainerHandlerFactory(cfh3, []watcher.ContainerWatchSource{watcher.Raw})

list := container.GetReorderedFactoryList(watcher.Raw)

if list[len(list)-1].String() != "raw" {
t.Error("Expected raw container handler to be last in the list.")
}
}

0 comments on commit f81b61d

Please sign in to comment.