mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-27 15:39:39 +00:00
name runtime.Schemes so we can see which one fails
Kubernetes-commit: 340802b079dbf8d193f162d49663679bd7d24ef7
This commit is contained in:
parent
5242ede50e
commit
21fd7d2b8a
75
tools/cache/reflector.go
vendored
75
tools/cache/reflector.go
vendored
@ -24,9 +24,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
|
||||||
goruntime "runtime"
|
|
||||||
"runtime/debug"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -40,6 +37,7 @@ import (
|
|||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
"k8s.io/apimachinery/pkg/util/clock"
|
||||||
|
"k8s.io/apimachinery/pkg/util/naming"
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
@ -94,7 +92,7 @@ func NewNamespaceKeyedIndexerAndReflector(lw ListerWatcher, expectedType interfa
|
|||||||
// resyncPeriod, so that you can use reflectors to periodically process everything as
|
// resyncPeriod, so that you can use reflectors to periodically process everything as
|
||||||
// well as incrementally processing the things that change.
|
// well as incrementally processing the things that change.
|
||||||
func NewReflector(lw ListerWatcher, expectedType interface{}, store Store, resyncPeriod time.Duration) *Reflector {
|
func NewReflector(lw ListerWatcher, expectedType interface{}, store Store, resyncPeriod time.Duration) *Reflector {
|
||||||
return NewNamedReflector(getDefaultReflectorName(internalPackages...), lw, expectedType, store, resyncPeriod)
|
return NewNamedReflector(naming.GetNameFromCallsite(internalPackages...), lw, expectedType, store, resyncPeriod)
|
||||||
}
|
}
|
||||||
|
|
||||||
// reflectorDisambiguator is used to disambiguate started reflectors.
|
// reflectorDisambiguator is used to disambiguate started reflectors.
|
||||||
@ -125,74 +123,7 @@ func makeValidPrometheusMetricLabel(in string) string {
|
|||||||
|
|
||||||
// internalPackages are packages that ignored when creating a default reflector name. These packages are in the common
|
// internalPackages are packages that ignored when creating a default reflector name. These packages are in the common
|
||||||
// call chains to NewReflector, so they'd be low entropy names for reflectors
|
// call chains to NewReflector, so they'd be low entropy names for reflectors
|
||||||
var internalPackages = []string{"client-go/tools/cache/", "/runtime/asm_"}
|
var internalPackages = []string{"client-go/tools/cache/"}
|
||||||
|
|
||||||
// getDefaultReflectorName walks back through the call stack until we find a caller from outside of the ignoredPackages
|
|
||||||
// it returns back a shortpath/filename:line to aid in identification of this reflector when it starts logging
|
|
||||||
func getDefaultReflectorName(ignoredPackages ...string) string {
|
|
||||||
name := "????"
|
|
||||||
const maxStack = 10
|
|
||||||
for i := 1; i < maxStack; i++ {
|
|
||||||
_, file, line, ok := goruntime.Caller(i)
|
|
||||||
if !ok {
|
|
||||||
file, line, ok = extractStackCreator()
|
|
||||||
if !ok {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
i += maxStack
|
|
||||||
}
|
|
||||||
if hasPackage(file, ignoredPackages) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
file = trimPackagePrefix(file)
|
|
||||||
name = fmt.Sprintf("%s:%d", file, line)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return name
|
|
||||||
}
|
|
||||||
|
|
||||||
// hasPackage returns true if the file is in one of the ignored packages.
|
|
||||||
func hasPackage(file string, ignoredPackages []string) bool {
|
|
||||||
for _, ignoredPackage := range ignoredPackages {
|
|
||||||
if strings.Contains(file, ignoredPackage) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// trimPackagePrefix reduces duplicate values off the front of a package name.
|
|
||||||
func trimPackagePrefix(file string) string {
|
|
||||||
if l := strings.LastIndex(file, "k8s.io/client-go/pkg/"); l >= 0 {
|
|
||||||
return file[l+len("k8s.io/client-go/"):]
|
|
||||||
}
|
|
||||||
if l := strings.LastIndex(file, "/src/"); l >= 0 {
|
|
||||||
return file[l+5:]
|
|
||||||
}
|
|
||||||
if l := strings.LastIndex(file, "/pkg/"); l >= 0 {
|
|
||||||
return file[l+1:]
|
|
||||||
}
|
|
||||||
return file
|
|
||||||
}
|
|
||||||
|
|
||||||
var stackCreator = regexp.MustCompile(`(?m)^created by (.*)\n\s+(.*):(\d+) \+0x[[:xdigit:]]+$`)
|
|
||||||
|
|
||||||
// extractStackCreator retrieves the goroutine file and line that launched this stack. Returns false
|
|
||||||
// if the creator cannot be located.
|
|
||||||
// TODO: Go does not expose this via runtime https://github.com/golang/go/issues/11440
|
|
||||||
func extractStackCreator() (string, int, bool) {
|
|
||||||
stack := debug.Stack()
|
|
||||||
matches := stackCreator.FindStringSubmatch(string(stack))
|
|
||||||
if matches == nil || len(matches) != 4 {
|
|
||||||
return "", 0, false
|
|
||||||
}
|
|
||||||
line, err := strconv.Atoi(matches[3])
|
|
||||||
if err != nil {
|
|
||||||
return "", 0, false
|
|
||||||
}
|
|
||||||
return matches[2], line, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run starts a watch and handles watch events. Will restart the watch if it is closed.
|
// Run starts a watch and handles watch events. Will restart the watch if it is closed.
|
||||||
// Run will exit when stopCh is closed.
|
// Run will exit when stopCh is closed.
|
||||||
|
Loading…
Reference in New Issue
Block a user