Skip to content

Commit c50d06d

Browse files
committed
Always evaluate Raw Factory last
Signed-off-by: Harshal Patil <[email protected]>
1 parent 14155ee commit c50d06d

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

container/factory.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ func NewContainerHandler(name string, watchType watcher.ContainerWatchSource, me
242242
defer factoriesLock.RUnlock()
243243

244244
// Create the ContainerHandler with the first factory that supports it.
245-
for _, factory := range factories[watchType] {
245+
// Note that since RawContainerHandler can support a wide range of paths,
246+
// it's evaluated last just to make sure if any other ContainerHandler
247+
// can support it.
248+
for _, factory := range GetReorderedFactoryList(watchType) {
246249
canHandle, canAccept, err := factory.CanHandleAndAccept(name)
247250
if err != nil {
248251
klog.V(4).Infof("Error trying to work out if we can handle %s: %v", name, err)
@@ -285,3 +288,26 @@ func DebugInfo() map[string][]string {
285288
}
286289
return out
287290
}
291+
292+
// GetReorderedFactoryList returns the list of ContainerHandlerFactory where the
293+
// RawContainerHandler is always the last element.
294+
func GetReorderedFactoryList(watchType watcher.ContainerWatchSource) []ContainerHandlerFactory {
295+
ContainerHandlerFactoryList := make([]ContainerHandlerFactory, 0, len(factories))
296+
297+
var rawFactory ContainerHandlerFactory
298+
for _, v := range factories[watchType] {
299+
if v != nil {
300+
if v.String() == "raw" {
301+
rawFactory = v
302+
continue
303+
}
304+
ContainerHandlerFactoryList = append(ContainerHandlerFactoryList, v)
305+
}
306+
}
307+
308+
if rawFactory != nil {
309+
ContainerHandlerFactoryList = append(ContainerHandlerFactoryList, rawFactory)
310+
}
311+
312+
return ContainerHandlerFactoryList
313+
}

container/factory_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,25 @@ func TestNewContainerHandler_Accept(t *testing.T) {
162162
t.Error("Expected NewContainerHandler to ignore the container.")
163163
}
164164
}
165+
166+
func TestRawContainerHandler_Last(t *testing.T) {
167+
chf1 := &mockContainerHandlerFactory{
168+
Name: "raw",
169+
}
170+
container.RegisterContainerHandlerFactory(chf1, []watcher.ContainerWatchSource{watcher.Raw})
171+
cfh2 := &mockContainerHandlerFactory{
172+
Name: "crio",
173+
}
174+
container.RegisterContainerHandlerFactory(cfh2, []watcher.ContainerWatchSource{watcher.Raw})
175+
176+
cfh3 := &mockContainerHandlerFactory{
177+
Name: "containerd",
178+
}
179+
container.RegisterContainerHandlerFactory(cfh3, []watcher.ContainerWatchSource{watcher.Raw})
180+
181+
list := container.GetReorderedFactoryList(watcher.Raw)
182+
183+
if list[len(list)-1].String() != "raw" {
184+
t.Error("Expected raw container handler to be last in the list.")
185+
}
186+
}

0 commit comments

Comments
 (0)