Files
client-go/tools/cache/event_handler_name.go
Patrick Ohly e70bc766e0 client-go cache: wait for cache sync via channels, better logging
The main advantage is that waiting on channels creates a causal relationship
between goroutines which is visible to synctest. When a controller in a
synctest bubble does a WaitFor in a test's background goroutine for the
controller, the test can use synctest.Wait to wait for completion of cache
sync, without requiring any test specific "has controller synced" API. Without
this, the test had to poll or otherwise wait for the controller.

The polling in WaitForCacheSync moved the virtual clock forward by a random
amount, depending on how often it had to check in wait.Poll. Now tests can be
written such that all events during a test happen at a predictable time. This
will be demonstrated in a separate commit for the
pkg/controller/devicetainteviction unit test.

The benefit for normal production is immediate continuation when the last
informer is synced (not really a problem, but still...) and more important,
nicer logging thanks to the names associated with the thing that is being
waited for. The caller decides whether logging is enabled or disabled and
describes what is being waited for (typically informer caches, but maybe also
event handlers or even something else entirely as long as it implements the
DoneChecker interface).

Before:

    Waiting for caches to sync
    Caches are synced

After:

    Waiting for="cache and event handler sync"
    Done waiting for="cache and event handler sync" instance="SharedIndexInformer *v1.Pod"
    Done waiting for="cache and event handler sync" instance="SharedIndexInformer *v1.ResourceClaim"
    Done waiting for="cache and event handler sync" instance="SharedIndexInformer *v1.ResourceSlice"
    Done waiting for="cache and event handler sync" instance="SharedIndexInformer *v1.DeviceClass"
    Done waiting for="cache and event handler sync" instance="SharedIndexInformer *v1alpha3.DeviceTaintRule"
    Done waiting for="cache and event handler sync" instance="SharedIndexInformer *v1.ResourceClaim + event handler k8s.io/kubernetes/pkg/controller/devicetainteviction.(*Controller).Run"
    Done waiting for="cache and event handler sync" instance="SharedIndexInformer *v1.Pod + event handler k8s.io/kubernetes/pkg/controller/devicetainteviction.(*Controller).Run"
    Done waiting for="cache and event handler sync" instance="SharedIndexInformer *v1alpha3.DeviceTaintRule + event handler k8s.io/kubernetes/pkg/controller/devicetainteviction.(*Controller).Run"
    Done waiting for="cache and event handler sync" instance="SharedIndexInformer *v1.ResourceSlice + event handler k8s.io/kubernetes/pkg/controller/devicetainteviction.(*Controller).Run"

The "SharedIndexInformer *v1.Pod" is also how this appears in metrics.

Kubernetes-commit: fdcbb6cba9a04c028b158bf66d505df7431f63fe
2025-11-18 12:39:11 +01:00

120 lines
2.8 KiB
Go

/*
Copyright The Kubernetes Authors.
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 cache
import (
"fmt"
"reflect"
"runtime"
"strings"
)
func nameForHandler(handler ResourceEventHandler) (name string) {
defer func() {
// Last resort: let Sprintf handle it.
if name == "" {
name = fmt.Sprintf("%T", handler)
}
}()
if handler == nil {
return ""
}
switch handler := handler.(type) {
case *ResourceEventHandlerFuncs:
return nameForHandlerFuncs(*handler)
case ResourceEventHandlerFuncs:
return nameForHandlerFuncs(handler)
default:
// We can use the fully qualified name of whatever
// provides the interface. We don't care whether
// it contains fields or methods which provide
// the interface methods.
value := reflect.ValueOf(handler)
if value.Type().Kind() == reflect.Interface {
// Probably not needed, but let's play it safe.
value = value.Elem()
}
if value.Type().Kind() == reflect.Pointer {
value = value.Elem()
}
name := value.Type().PkgPath()
if name != "" {
name += "."
}
if typeName := value.Type().Name(); typeName != "" {
name += typeName
}
return name
}
}
func nameForHandlerFuncs(funcs ResourceEventHandlerFuncs) string {
return nameForFunctions(funcs.AddFunc, funcs.UpdateFunc, funcs.DeleteFunc)
}
func nameForFunctions(fs ...any) string {
// If all functions are defined in the same place, then we
// don't care about the actual function name in
// e.g. "main.FuncName" or "main.(*Foo).FuncName-fm", instead
// we use the common qualifier.
//
// But we don't know that yet, so we also collect all names.
var qualifier string
singleQualifier := true
var names []string
for _, f := range fs {
if f == nil {
continue
}
name := nameForFunction(f)
if name == "" {
continue
}
names = append(names, name)
newQualifier := name
index := strings.LastIndexByte(newQualifier, '.')
if index > 0 {
newQualifier = newQualifier[:index]
}
switch qualifier {
case "":
qualifier = newQualifier
case newQualifier:
// So far, so good...
default:
// Nope, different.
singleQualifier = false
}
}
if singleQualifier {
return qualifier
}
return strings.Join(names, "+")
}
func nameForFunction(f any) string {
fn := runtime.FuncForPC(reflect.ValueOf(f).Pointer())
if fn == nil {
return ""
}
return fn.Name()
}