diff --git a/source/crd.go b/source/crd.go index c15227592..b32b93e6e 100644 --- a/source/crd.go +++ b/source/crd.go @@ -108,7 +108,7 @@ func NewCRDClientForAPIVersionKind(client kubernetes.Interface, kubeConfig, apiS } // NewCRDSource creates a new crdSource with the given config. -func NewCRDSource(crdClient rest.Interface, namespace, kind string, annotationFilter string, labelSelector labels.Selector, scheme *runtime.Scheme) (Source, error) { +func NewCRDSource(crdClient rest.Interface, namespace, kind string, annotationFilter string, labelSelector labels.Selector, scheme *runtime.Scheme, startInformer bool) (Source, error) { sourceCrd := crdSource{ crdResource: strings.ToLower(kind) + "s", namespace: namespace, @@ -117,43 +117,46 @@ func NewCRDSource(crdClient rest.Interface, namespace, kind string, annotationFi crdClient: crdClient, codec: runtime.NewParameterCodec(scheme), } - // external-dns already runs its sync-handler periodically (controlled by `--interval` flag) to ensure any - // missed or dropped events are handled. specify a resync period 0 to avoid unnecessary sync handler invocations. - informer := cache.NewSharedInformer( - &cache.ListWatch{ - ListFunc: func(lo metav1.ListOptions) (result runtime.Object, err error) { - return sourceCrd.List(context.TODO(), &lo) + if startInformer { + // external-dns already runs its sync-handler periodically (controlled by `--interval` flag) to ensure any + // missed or dropped events are handled. specify a resync period 0 to avoid unnecessary sync handler invocations. + informer := cache.NewSharedInformer( + &cache.ListWatch{ + ListFunc: func(lo metav1.ListOptions) (result runtime.Object, err error) { + return sourceCrd.List(context.TODO(), &lo) + }, + WatchFunc: func(lo metav1.ListOptions) (watch.Interface, error) { + return sourceCrd.watch(context.TODO(), &lo) + }, }, - WatchFunc: func(lo metav1.ListOptions) (watch.Interface, error) { - return sourceCrd.watch(context.TODO(), &lo) - }, - }, - &endpoint.DNSEndpoint{}, - 0) - sourceCrd.informer = &informer - go informer.Run(wait.NeverStop) + &endpoint.DNSEndpoint{}, + 0) + sourceCrd.informer = &informer + go informer.Run(wait.NeverStop) + } return &sourceCrd, nil } func (cs *crdSource) AddEventHandler(ctx context.Context, handler func()) { - log.Debug("Adding event handler for CRD") - - // Right now there is no way to remove event handler from informer, see: - // https://github.com/kubernetes/kubernetes/issues/79610 - informer := *cs.informer - informer.AddEventHandler( - cache.ResourceEventHandlerFuncs{ - AddFunc: func(obj interface{}) { - handler() + if cs.informer != nil { + log.Debug("Adding event handler for CRD") + // Right now there is no way to remove event handler from informer, see: + // https://github.com/kubernetes/kubernetes/issues/79610 + informer := *cs.informer + informer.AddEventHandler( + cache.ResourceEventHandlerFuncs{ + AddFunc: func(obj interface{}) { + handler() + }, + UpdateFunc: func(old interface{}, new interface{}) { + handler() + }, + DeleteFunc: func(obj interface{}) { + handler() + }, }, - UpdateFunc: func(old interface{}, new interface{}) { - handler() - }, - DeleteFunc: func(obj interface{}) { - handler() - }, - }, - ) + ) + } } // Endpoints returns endpoint objects. diff --git a/source/crd_test.go b/source/crd_test.go index a88fb4616..55b375f21 100644 --- a/source/crd_test.go +++ b/source/crd_test.go @@ -387,7 +387,7 @@ func testCRDSourceEndpoints(t *testing.T) { labelSelector, err := labels.Parse(ti.labelFilter) require.NoError(t, err) - cs, err := NewCRDSource(restClient, ti.namespace, ti.kind, ti.annotationFilter, labelSelector, scheme) + cs, err := NewCRDSource(restClient, ti.namespace, ti.kind, ti.annotationFilter, labelSelector, scheme, false) require.NoError(t, err) receivedEndpoints, err := cs.Endpoints(context.Background()) diff --git a/source/store.go b/source/store.go index 942400aef..3f15572d0 100644 --- a/source/store.go +++ b/source/store.go @@ -270,7 +270,7 @@ func BuildWithConfig(ctx context.Context, source string, p ClientGenerator, cfg if err != nil { return nil, err } - return NewCRDSource(crdClient, cfg.Namespace, cfg.CRDSourceKind, cfg.AnnotationFilter, cfg.LabelFilter, scheme) + return NewCRDSource(crdClient, cfg.Namespace, cfg.CRDSourceKind, cfg.AnnotationFilter, cfg.LabelFilter, scheme, true) case "skipper-routegroup": apiServerURL := cfg.APIServerURL tokenPath := ""