From 9121e784966d52bb93696d83d9ab394ac2efd77d Mon Sep 17 00:00:00 2001 From: Chris O'Haver Date: Tue, 1 Dec 2020 15:29:05 -0500 Subject: plugin/kubernetes: Fix dns programming duration metric (#4255) * get data reqd to record latency before calling toFuncs * refactor out unnecessary toFunc wrappers * remove latency metric unit tests per PR feedback Signed-off-by: Chris O'Haver --- plugin/kubernetes/controller.go | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'plugin/kubernetes/controller.go') diff --git a/plugin/kubernetes/controller.go b/plugin/kubernetes/controller.go index f9373eee9..2319cf203 100644 --- a/plugin/kubernetes/controller.go +++ b/plugin/kubernetes/controller.go @@ -92,9 +92,8 @@ type dnsControlOpts struct { namespaceLabelSelector *meta.LabelSelector namespaceSelector labels.Selector - zones []string - endpointNameMode bool - skipAPIObjectsCleanup bool + zones []string + endpointNameMode bool } // newDNSController creates a controller for CoreDNS. @@ -116,7 +115,7 @@ func newdnsController(ctx context.Context, kubeClient kubernetes.Interface, opts &api.Service{}, cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete}, cache.Indexers{svcNameNamespaceIndex: svcNameNamespaceIndexFunc, svcIPIndex: svcIPIndexFunc}, - object.DefaultProcessor(object.ToService(opts.skipAPIObjectsCleanup), nil), + object.DefaultProcessor(object.ToService, nil), ) if opts.initPodCache { @@ -128,7 +127,7 @@ func newdnsController(ctx context.Context, kubeClient kubernetes.Interface, opts &api.Pod{}, cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete}, cache.Indexers{podIPIndex: podIPIndexFunc}, - object.DefaultProcessor(object.ToPod(opts.skipAPIObjectsCleanup), nil), + object.DefaultProcessor(object.ToPod, nil), ) } @@ -136,28 +135,28 @@ func newdnsController(ctx context.Context, kubeClient kubernetes.Interface, opts var ( apiObj runtime.Object listWatch cache.ListWatch - to func(bool) object.ToFunc - latency object.RecordLatencyFunc + to object.ToFunc + latency *object.EndpointLatencyRecorder ) if opts.useEndpointSlices { apiObj = &discovery.EndpointSlice{} listWatch.ListFunc = endpointSliceListFunc(ctx, dns.client, api.NamespaceAll, dns.selector) listWatch.WatchFunc = endpointSliceWatchFunc(ctx, dns.client, api.NamespaceAll, dns.selector) to = object.EndpointSliceToEndpoints - latency = dns.recordEndpointSliceDNSProgrammingLatency + latency = dns.EndpointSliceLatencyRecorder() } else { apiObj = &api.Endpoints{} listWatch.ListFunc = endpointsListFunc(ctx, dns.client, api.NamespaceAll, dns.selector) listWatch.WatchFunc = endpointsWatchFunc(ctx, dns.client, api.NamespaceAll, dns.selector) to = object.ToEndpoints - latency = dns.recordEndpointDNSProgrammingLatency + latency = dns.EndpointsLatencyRecorder() } dns.epLister, dns.epController = object.NewIndexerInformer( &listWatch, apiObj, cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete}, cache.Indexers{epNameNamespaceIndex: epNameNamespaceIndexFunc, epIPIndex: epIPIndexFunc}, - object.DefaultProcessor(to(opts.skipAPIObjectsCleanup), latency), + object.DefaultProcessor(to, latency), ) } @@ -173,12 +172,19 @@ func newdnsController(ctx context.Context, kubeClient kubernetes.Interface, opts return &dns } -func (dns *dnsControl) recordEndpointDNSProgrammingLatency(obj meta.Object) { - recordDNSProgrammingLatency(dns.getServices(obj.(*api.Endpoints)), obj) +func (dns *dnsControl) EndpointsLatencyRecorder() *object.EndpointLatencyRecorder { + return &object.EndpointLatencyRecorder{ + ServiceFunc: func(o meta.Object) []*object.Service { + return dns.SvcIndex(object.ServiceKey(o.GetName(), o.GetNamespace())) + }, + } } - -func (dns *dnsControl) recordEndpointSliceDNSProgrammingLatency(obj meta.Object) { - recordDNSProgrammingLatency(dns.SvcIndex(object.ServiceKey(obj.GetLabels()[discovery.LabelServiceName], obj.GetNamespace())), obj) +func (dns *dnsControl) EndpointSliceLatencyRecorder() *object.EndpointLatencyRecorder { + return &object.EndpointLatencyRecorder{ + ServiceFunc: func(o meta.Object) []*object.Service { + return dns.SvcIndex(object.ServiceKey(o.GetLabels()[discovery.LabelServiceName], o.GetNamespace())) + }, + } } func podIPIndexFunc(obj interface{}) ([]string, error) { @@ -518,10 +524,6 @@ func (dns *dnsControl) detectChanges(oldObj, newObj interface{}) { } } -func (dns *dnsControl) getServices(endpoints *api.Endpoints) []*object.Service { - return dns.SvcIndex(object.ServiceKey(endpoints.GetName(), endpoints.GetNamespace())) -} - // subsetsEquivalent checks if two endpoint subsets are significantly equivalent // I.e. that they have the same ready addresses, host names, ports (including protocol // and service names for SRV) -- cgit v1.2.3