mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 04:06:03 +00:00
Use metadata informers instead of dynamic informers in controller manager
All controllers in controller-manager that deal with objects generically work with those objects without needing the full object. Update the GC and quota controller to use PartialObjectMetadata input objects which is faster and more efficient.
This commit is contained in:
parent
98d87a4f03
commit
d631f9b7e9
@ -121,10 +121,10 @@ go_library(
|
||||
"//staging/src/k8s.io/client-go/discovery/cached:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/discovery/cached/memory:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/dynamic:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/dynamic/dynamicinformer:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/informers:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/metadata:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/metadata/metadatainformer:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/rest:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/restmapper:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/scale:go_default_library",
|
||||
|
@ -31,7 +31,7 @@ import (
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
@ -43,10 +43,10 @@ import (
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
"k8s.io/apiserver/pkg/util/term"
|
||||
cacheddiscovery "k8s.io/client-go/discovery/cached"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/client-go/dynamic/dynamicinformer"
|
||||
"k8s.io/client-go/informers"
|
||||
clientset "k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/metadata"
|
||||
"k8s.io/client-go/metadata/metadatainformer"
|
||||
restclient "k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/restmapper"
|
||||
"k8s.io/client-go/tools/leaderelection"
|
||||
@ -239,7 +239,7 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
|
||||
}
|
||||
|
||||
controllerContext.InformerFactory.Start(controllerContext.Stop)
|
||||
controllerContext.GenericInformerFactory.Start(controllerContext.Stop)
|
||||
controllerContext.ObjectOrMetadataInformerFactory.Start(controllerContext.Stop)
|
||||
close(controllerContext.InformersStarted)
|
||||
|
||||
select {}
|
||||
@ -295,9 +295,11 @@ type ControllerContext struct {
|
||||
// InformerFactory gives access to informers for the controller.
|
||||
InformerFactory informers.SharedInformerFactory
|
||||
|
||||
// GenericInformerFactory gives access to informers for typed resources
|
||||
// and dynamic resources.
|
||||
GenericInformerFactory controller.InformerFactory
|
||||
// ObjectOrMetadataInformerFactory gives access to informers for typed resources
|
||||
// and dynamic resources by their metadata. All generic controllers currently use
|
||||
// object metadata - if a future controller needs access to the full object this
|
||||
// would become GenericInformerFactory and take a dynamic client.
|
||||
ObjectOrMetadataInformerFactory controller.InformerFactory
|
||||
|
||||
// ComponentConfig provides access to init options for a given controller
|
||||
ComponentConfig kubectrlmgrconfig.KubeControllerManagerConfiguration
|
||||
@ -448,8 +450,8 @@ func CreateControllerContext(s *config.CompletedConfig, rootClientBuilder, clien
|
||||
versionedClient := rootClientBuilder.ClientOrDie("shared-informers")
|
||||
sharedInformers := informers.NewSharedInformerFactory(versionedClient, ResyncPeriod(s)())
|
||||
|
||||
dynamicClient := dynamic.NewForConfigOrDie(rootClientBuilder.ConfigOrDie("dynamic-informers"))
|
||||
dynamicInformers := dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, ResyncPeriod(s)())
|
||||
metadataClient := metadata.NewForConfigOrDie(rootClientBuilder.ConfigOrDie("metadata-informers"))
|
||||
metadataInformers := metadatainformer.NewSharedInformerFactory(metadataClient, ResyncPeriod(s)())
|
||||
|
||||
// If apiserver is not running we should wait for some time and fail only then. This is particularly
|
||||
// important when we start apiserver and controller manager at the same time.
|
||||
@ -477,17 +479,17 @@ func CreateControllerContext(s *config.CompletedConfig, rootClientBuilder, clien
|
||||
}
|
||||
|
||||
ctx := ControllerContext{
|
||||
ClientBuilder: clientBuilder,
|
||||
InformerFactory: sharedInformers,
|
||||
GenericInformerFactory: controller.NewInformerFactory(sharedInformers, dynamicInformers),
|
||||
ComponentConfig: s.ComponentConfig,
|
||||
RESTMapper: restMapper,
|
||||
AvailableResources: availableResources,
|
||||
Cloud: cloud,
|
||||
LoopMode: loopMode,
|
||||
Stop: stop,
|
||||
InformersStarted: make(chan struct{}),
|
||||
ResyncPeriod: ResyncPeriod(s),
|
||||
ClientBuilder: clientBuilder,
|
||||
InformerFactory: sharedInformers,
|
||||
ObjectOrMetadataInformerFactory: controller.NewInformerFactory(sharedInformers, metadataInformers),
|
||||
ComponentConfig: s.ComponentConfig,
|
||||
RESTMapper: restMapper,
|
||||
AvailableResources: availableResources,
|
||||
Cloud: cloud,
|
||||
LoopMode: loopMode,
|
||||
Stop: stop,
|
||||
InformersStarted: make(chan struct{}),
|
||||
ResyncPeriod: ResyncPeriod(s),
|
||||
}
|
||||
return ctx, nil
|
||||
}
|
||||
|
@ -33,7 +33,6 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
cacheddiscovery "k8s.io/client-go/discovery/cached/memory"
|
||||
"k8s.io/client-go/dynamic"
|
||||
clientset "k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/metadata"
|
||||
restclient "k8s.io/client-go/rest"
|
||||
@ -334,7 +333,7 @@ func startResourceQuotaController(ctx ControllerContext) (http.Handler, bool, er
|
||||
QuotaClient: resourceQuotaControllerClient.CoreV1(),
|
||||
ResourceQuotaInformer: ctx.InformerFactory.Core().V1().ResourceQuotas(),
|
||||
ResyncPeriod: controller.StaticResyncPeriodFunc(ctx.ComponentConfig.ResourceQuotaController.ResourceQuotaSyncPeriod.Duration),
|
||||
InformerFactory: ctx.GenericInformerFactory,
|
||||
InformerFactory: ctx.ObjectOrMetadataInformerFactory,
|
||||
ReplenishmentResyncPeriod: ctx.ResyncPeriod,
|
||||
DiscoveryFunc: discoveryFunc,
|
||||
IgnoredResourcesFunc: quotaConfiguration.IgnoredResources,
|
||||
@ -439,7 +438,7 @@ func startGarbageCollectorController(ctx ControllerContext) (http.Handler, bool,
|
||||
ctx.RESTMapper,
|
||||
deletableResources,
|
||||
ignoredResources,
|
||||
ctx.GenericInformerFactory,
|
||||
ctx.ObjectOrMetadataInformerFactory,
|
||||
ctx.InformersStarted,
|
||||
)
|
||||
if err != nil {
|
||||
|
@ -123,10 +123,10 @@ func TestController_DiscoveryError(t *testing.T) {
|
||||
testClientBuilder := TestClientBuilder{clientset: testClientset}
|
||||
testInformerFactory := informers.NewSharedInformerFactoryWithOptions(testClientset, time.Duration(1))
|
||||
ctx := ControllerContext{
|
||||
ClientBuilder: testClientBuilder,
|
||||
InformerFactory: testInformerFactory,
|
||||
GenericInformerFactory: testInformerFactory,
|
||||
InformersStarted: make(chan struct{}),
|
||||
ClientBuilder: testClientBuilder,
|
||||
InformerFactory: testInformerFactory,
|
||||
ObjectOrMetadataInformerFactory: testInformerFactory,
|
||||
InformersStarted: make(chan struct{}),
|
||||
}
|
||||
for funcName, controllerInit := range controllerInitFuncMap {
|
||||
_, _, err := controllerInit(ctx)
|
||||
|
@ -80,11 +80,11 @@ go_library(
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/authentication/serviceaccount:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/dynamic/dynamicinformer:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/informers:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/typed/authentication/v1:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/metadata/metadatainformer:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/rest:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
|
||||
|
@ -18,8 +18,8 @@ package controller
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/dynamic/dynamicinformer"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/metadata/metadatainformer"
|
||||
)
|
||||
|
||||
// InformerFactory creates informers for each group version resource.
|
||||
@ -29,28 +29,28 @@ type InformerFactory interface {
|
||||
}
|
||||
|
||||
type informerFactory struct {
|
||||
typedInformerFactory informers.SharedInformerFactory
|
||||
dynamicInformerFactory dynamicinformer.DynamicSharedInformerFactory
|
||||
typedInformerFactory informers.SharedInformerFactory
|
||||
metadataInformerFactory metadatainformer.SharedInformerFactory
|
||||
}
|
||||
|
||||
func (i *informerFactory) ForResource(resource schema.GroupVersionResource) (informers.GenericInformer, error) {
|
||||
informer, err := i.typedInformerFactory.ForResource(resource)
|
||||
if err != nil {
|
||||
return i.dynamicInformerFactory.ForResource(resource), nil
|
||||
return i.metadataInformerFactory.ForResource(resource), nil
|
||||
}
|
||||
return informer, nil
|
||||
}
|
||||
|
||||
func (i *informerFactory) Start(stopCh <-chan struct{}) {
|
||||
i.typedInformerFactory.Start(stopCh)
|
||||
i.dynamicInformerFactory.Start(stopCh)
|
||||
i.metadataInformerFactory.Start(stopCh)
|
||||
}
|
||||
|
||||
// NewInformerFactory creates a new InformerFactory which works with both typed
|
||||
// resources and dynamic resources
|
||||
func NewInformerFactory(typedInformerFactory informers.SharedInformerFactory, dynamicInformerFactory dynamicinformer.DynamicSharedInformerFactory) InformerFactory {
|
||||
// resources and metadata-only resources
|
||||
func NewInformerFactory(typedInformerFactory informers.SharedInformerFactory, metadataInformerFactory metadatainformer.SharedInformerFactory) InformerFactory {
|
||||
return &informerFactory{
|
||||
typedInformerFactory: typedInformerFactory,
|
||||
dynamicInformerFactory: dynamicInformerFactory,
|
||||
typedInformerFactory: typedInformerFactory,
|
||||
metadataInformerFactory: metadataInformerFactory,
|
||||
}
|
||||
}
|
||||
|
@ -29,10 +29,10 @@ go_test(
|
||||
"//staging/src/k8s.io/apiserver/pkg/storage/names:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/discovery/cached/memory:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/dynamic:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/dynamic/dynamicinformer:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/informers:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/metadata:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/metadata/metadatainformer:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/restmapper:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
|
||||
"//test/integration:go_default_library",
|
||||
|
@ -38,10 +38,10 @@ import (
|
||||
"k8s.io/apiserver/pkg/storage/names"
|
||||
cacheddiscovery "k8s.io/client-go/discovery/cached/memory"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/client-go/dynamic/dynamicinformer"
|
||||
"k8s.io/client-go/informers"
|
||||
clientset "k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/metadata"
|
||||
"k8s.io/client-go/metadata/metadatainformer"
|
||||
"k8s.io/client-go/restmapper"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
|
||||
@ -242,7 +242,7 @@ func setupWithServer(t *testing.T, result *kubeapiservertesting.TestServer, work
|
||||
t.Fatalf("failed to create dynamicClient: %v", err)
|
||||
}
|
||||
sharedInformers := informers.NewSharedInformerFactory(clientSet, 0)
|
||||
dynamicInformers := dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, 0)
|
||||
metadataInformers := metadatainformer.NewSharedInformerFactory(metadataClient, 0)
|
||||
alwaysStarted := make(chan struct{})
|
||||
close(alwaysStarted)
|
||||
gc, err := garbagecollector.NewGarbageCollector(
|
||||
@ -250,7 +250,7 @@ func setupWithServer(t *testing.T, result *kubeapiservertesting.TestServer, work
|
||||
restMapper,
|
||||
deletableResources,
|
||||
garbagecollector.DefaultIgnoredResources(),
|
||||
controller.NewInformerFactory(sharedInformers, dynamicInformers),
|
||||
controller.NewInformerFactory(sharedInformers, metadataInformers),
|
||||
alwaysStarted,
|
||||
)
|
||||
if err != nil {
|
||||
|
2
vendor/modules.txt
vendored
2
vendor/modules.txt
vendored
@ -1315,8 +1315,6 @@ k8s.io/client-go/discovery/cached/disk
|
||||
k8s.io/client-go/discovery/cached/memory
|
||||
k8s.io/client-go/discovery/fake
|
||||
k8s.io/client-go/dynamic
|
||||
k8s.io/client-go/dynamic/dynamicinformer
|
||||
k8s.io/client-go/dynamic/dynamiclister
|
||||
k8s.io/client-go/dynamic/fake
|
||||
k8s.io/client-go/informers
|
||||
k8s.io/client-go/informers/admissionregistration
|
||||
|
Loading…
Reference in New Issue
Block a user