mirror of
https://github.com/kubernetes/sample-controller.git
synced 2026-08-03 00:00:02 +08:00
All code using the result of the generated client-go Informer() (a
cache.SharedIndexInformer) constantly has to do type casts from "any" to the
actual type of the objects managed by the informer.
This is:
- annoying at best
- often done inconsistently (should failed type assertions be logged and if so,
how?)
- a source of bugs (not handling FinalStateUnknown, not handling missing
object, type in event handler not matching the type in the informer)
In contrast, the Lister() result *is* typed. It converts without a type check
in e.g. ResourceIndexer[T].List:
err = cache.ListAllByNamespace(l.indexer, l.namespace, selector, func(m interface{}) {
ret = append(ret, m.(T))
})
This change here does the same wrapping for SharedIndexInformer, Indexer,
index functions and event handler support code. OnDelete is passed
a DeletedObject struct to cover the different scenarios that can occur
when reporting deletion (stale or even nil object!). Extracting key or
name from DeletedObject is guaranteed to never fail because the type-safe
API is restricted to types where the necessary meta data is guaranteed
to be available. This removes another class of tedious error checking.
While not strictly needed, aliases are generated for the generic types because
they are often shorter, easier to type (in particular with auto-complete) and
may help with avoiding an import of k8s.io/client-go/tools/cache.
This is a Go API break because all generated interfaces
change.
Kubernetes-commit: b83b3b39db5eee75af14cdb8d30cfc6899792ec2
46 lines
1.5 KiB
Go
46 lines
1.5 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.
|
|
*/
|
|
|
|
// Code generated by informer-gen. DO NOT EDIT.
|
|
|
|
package v1alpha1
|
|
|
|
import (
|
|
internalinterfaces "k8s.io/sample-controller/pkg/generated/informers/externalversions/internalinterfaces"
|
|
)
|
|
|
|
// Interface provides access to all the informers in this group version.
|
|
type Interface interface {
|
|
// Foos returns a FooInformer.
|
|
Foos() TypedFooInformer
|
|
}
|
|
|
|
type version struct {
|
|
factory internalinterfaces.SharedInformerFactory
|
|
namespace string
|
|
tweakListOptions internalinterfaces.TweakListOptionsFunc
|
|
}
|
|
|
|
// New returns a new Interface.
|
|
func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface {
|
|
return &version{factory: f, namespace: namespace, tweakListOptions: tweakListOptions}
|
|
}
|
|
|
|
// Foos returns a TypedFooInformer.
|
|
func (v *version) Foos() TypedFooInformer {
|
|
return &fooInformer{factory: v.factory, namespace: v.namespace, tweakListOptions: v.tweakListOptions}
|
|
}
|