Merge pull request #65904 from deads2k/api-02-trackscheme

Automatic merge from submit-queue (batch tested with PRs 65946, 65904, 65913, 65906, 65920). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

track schemes by name for error reporting

Getting an error message about a type not being in the scheme is hard to fix if you don't know which scheme is failing.  This adds a name to the scheme which can be set during creation or can be set based on the calling stack.  If you use the old constructor a name is generated for you based on the stack.  Something like "k8s.io/client-go/dynamic/scheme.go:28" for instance.

Also moves a typer to its point of use.  This was debt from previous refactors which I noticed going through.

@kubernetes/sig-api-machinery-misc
@sttts

```release-note
NONE
```

Kubernetes-commit: 8e2fdb32bc84103b15310a221a375470bf567bdc
This commit is contained in:
Kubernetes Publisher 2018-07-07 16:25:08 -07:00
commit a452c6c436
3 changed files with 88 additions and 234 deletions

166
Godeps/Godeps.json generated
View File

@ -268,327 +268,331 @@
},
{
"ImportPath": "k8s.io/api/admissionregistration/v1alpha1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/admissionregistration/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/apps/v1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/apps/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/apps/v1beta2",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/authentication/v1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/authentication/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/authorization/v1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/authorization/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/autoscaling/v1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/autoscaling/v2beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/batch/v1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/batch/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/batch/v2alpha1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/certificates/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/coordination/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/core/v1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/events/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/extensions/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/imagepolicy/v1alpha1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/networking/v1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/policy/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/rbac/v1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/rbac/v1alpha1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/rbac/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/scheduling/v1alpha1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/scheduling/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/settings/v1alpha1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/storage/v1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/storage/v1alpha1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/api/storage/v1beta1",
"Rev": "5f0d8f067e3bc465077d0333e891450326a577a3"
"Rev": "8be2a0b24ed0dac9cfc1ac2d987ea16cfcdbecb6"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/equality",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/errors",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/meta",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/resource",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/testing",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/testing/fuzzer",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/testing/roundtrip",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/fuzzer",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/internalversion",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1beta1",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/conversion",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/conversion/queryparams",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/fields",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/labels",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/schema",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/json",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/protobuf",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/recognizer",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/streaming",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/versioning",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/selection",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/types",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/cache",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/clock",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/diff",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/errors",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/framer",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/httpstream",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/httpstream/spdy",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/intstr",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/json",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/mergepatch",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/naming",
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/net",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/remotecommand",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/runtime",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/sets",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/strategicpatch",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/validation",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/validation/field",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/wait",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/yaml",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/version",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/watch",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/json",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/netutil",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/reflect",
"Rev": "9c0d9e23a515298bbe709bccf24fe790ecdd6f72"
"Rev": "bce280dade67588308614434551c0b865433e44d"
},
{
"ImportPath": "k8s.io/kube-openapi/pkg/util/proto",

View File

@ -1,81 +0,0 @@
/*
Copyright 2016 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 discovery
import (
"reflect"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// UnstructuredObjectTyper provides a runtime.ObjectTyper implementation for
// runtime.Unstructured object based on discovery information.
type UnstructuredObjectTyper struct {
typers []runtime.ObjectTyper
}
// NewUnstructuredObjectTyper returns a runtime.ObjectTyper for
// unstructured objects based on discovery information. It accepts a list of fallback typers
// for handling objects that are not runtime.Unstructured. It does not delegate the Recognizes
// check, only ObjectKinds.
// TODO this only works for the apiextensions server and doesn't recognize any types. Move to point of use.
func NewUnstructuredObjectTyper(typers ...runtime.ObjectTyper) *UnstructuredObjectTyper {
dot := &UnstructuredObjectTyper{
typers: typers,
}
return dot
}
// ObjectKinds returns a slice of one element with the group,version,kind of the
// provided object, or an error if the object is not runtime.Unstructured or
// has no group,version,kind information. unversionedType will always be false
// because runtime.Unstructured object should always have group,version,kind
// information set.
func (d *UnstructuredObjectTyper) ObjectKinds(obj runtime.Object) (gvks []schema.GroupVersionKind, unversionedType bool, err error) {
if _, ok := obj.(runtime.Unstructured); ok {
gvk := obj.GetObjectKind().GroupVersionKind()
if len(gvk.Kind) == 0 {
return nil, false, runtime.NewMissingKindErr("object has no kind field ")
}
if len(gvk.Version) == 0 {
return nil, false, runtime.NewMissingVersionErr("object has no apiVersion field")
}
return []schema.GroupVersionKind{gvk}, false, nil
}
var lastErr error
for _, typer := range d.typers {
gvks, unversioned, err := typer.ObjectKinds(obj)
if err != nil {
lastErr = err
continue
}
return gvks, unversioned, nil
}
if lastErr == nil {
lastErr = runtime.NewNotRegisteredErrForType(reflect.TypeOf(obj))
}
return nil, false, lastErr
}
// Recognizes returns true if the provided group,version,kind was in the
// discovery information.
func (d *UnstructuredObjectTyper) Recognizes(gvk schema.GroupVersionKind) bool {
return false
}
var _ runtime.ObjectTyper = &UnstructuredObjectTyper{}

View File

@ -24,9 +24,6 @@ import (
"net"
"net/url"
"reflect"
"regexp"
goruntime "runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
@ -40,6 +37,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/naming"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"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
// well as incrementally processing the things that change.
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.
@ -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
// call chains to NewReflector, so they'd be low entropy names for reflectors
var internalPackages = []string{"client-go/tools/cache/", "/runtime/asm_"}
// 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
}
var internalPackages = []string{"client-go/tools/cache/"}
// Run starts a watch and handles watch events. Will restart the watch if it is closed.
// Run will exit when stopCh is closed.