| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | package kubernetes | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2018-02-08 10:11:04 -06:00
										 |  |  | 	"sync/atomic" | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/kubernetes/object" | 
					
						
							| 
									
										
										
										
											2018-06-27 07:45:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-03 19:11:28 +08:00
										 |  |  | 	api "k8s.io/api/core/v1" | 
					
						
							| 
									
										
										
										
											2021-05-10 12:57:23 -04:00
										 |  |  | 	discovery "k8s.io/api/discovery/v1" | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	meta "k8s.io/apimachinery/pkg/apis/meta/v1" | 
					
						
							|  |  |  | 	"k8s.io/apimachinery/pkg/labels" | 
					
						
							|  |  |  | 	"k8s.io/apimachinery/pkg/runtime" | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 	"k8s.io/apimachinery/pkg/watch" | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	"k8s.io/client-go/kubernetes" | 
					
						
							|  |  |  | 	"k8s.io/client-go/tools/cache" | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-08 10:11:04 -06:00
										 |  |  | const ( | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	podIPIndex            = "PodIP" | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 	svcNameNamespaceIndex = "ServiceNameNamespace" | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	svcIPIndex            = "ServiceIP" | 
					
						
							| 
									
										
										
										
											2022-07-06 13:55:15 -04:00
										 |  |  | 	svcExtIPIndex         = "ServiceExternalIP" | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	epNameNamespaceIndex  = "EndpointNameNamespace" | 
					
						
							|  |  |  | 	epIPIndex             = "EndpointsIP" | 
					
						
							| 
									
										
										
										
											2018-02-08 10:11:04 -06:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | type dnsController interface { | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	ServiceList() []*object.Service | 
					
						
							|  |  |  | 	EndpointsList() []*object.Endpoints | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	SvcIndex(string) []*object.Service | 
					
						
							|  |  |  | 	SvcIndexReverse(string) []*object.Service | 
					
						
							| 
									
										
										
										
											2022-07-06 13:55:15 -04:00
										 |  |  | 	SvcExtIndexReverse(string) []*object.Service | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	PodIndex(string) []*object.Pod | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	EpIndex(string) []*object.Endpoints | 
					
						
							|  |  |  | 	EpIndexReverse(string) []*object.Endpoints | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 	GetNodeByName(context.Context, string) (*api.Node, error) | 
					
						
							| 
									
										
										
										
											2021-07-29 21:27:25 -06:00
										 |  |  | 	GetNamespaceByName(string) (*object.Namespace, error) | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	Run() | 
					
						
							| 
									
										
										
										
											2017-10-20 22:53:17 +01:00
										 |  |  | 	HasSynced() bool | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	Stop() error | 
					
						
							| 
									
										
										
										
											2018-02-08 10:11:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | 	// Modified returns the timestamp of the most recent changes to services.  If the passed bool is true, it should | 
					
						
							|  |  |  | 	// return the timestamp of the most recent changes to services with external facing IP addresses | 
					
						
							|  |  |  | 	Modified(bool) int64 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type dnsControl struct { | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | 	// modified tracks timestamp of the most recent changes | 
					
						
							| 
									
										
										
										
											2019-02-17 15:31:12 +07:00
										 |  |  | 	// It needs to be first because it is guaranteed to be 8-byte | 
					
						
							| 
									
										
										
										
											2018-02-08 10:11:04 -06:00
										 |  |  | 	// aligned ( we use sync.LoadAtomic with this ) | 
					
						
							|  |  |  | 	modified int64 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | 	// extModified tracks timestamp of the most recent changes to | 
					
						
							|  |  |  | 	// services with external facing IP addresses | 
					
						
							|  |  |  | 	extModified int64 | 
					
						
							| 
									
										
										
										
											2018-02-08 10:11:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	client kubernetes.Interface | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 	selector          labels.Selector | 
					
						
							| 
									
										
										
										
											2019-03-22 08:32:40 -06:00
										 |  |  | 	namespaceSelector labels.Selector | 
					
						
							| 
									
										
										
										
											2016-08-12 20:44:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	svcController cache.Controller | 
					
						
							|  |  |  | 	podController cache.Controller | 
					
						
							|  |  |  | 	epController  cache.Controller | 
					
						
							| 
									
										
										
										
											2018-02-15 14:21:54 -05:00
										 |  |  | 	nsController  cache.Controller | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	svcLister cache.Indexer | 
					
						
							|  |  |  | 	podLister cache.Indexer | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	epLister  cache.Indexer | 
					
						
							| 
									
										
										
										
											2018-09-22 15:12:02 +01:00
										 |  |  | 	nsLister  cache.Store | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// stopLock is used to enforce only a single call to Stop is active. | 
					
						
							|  |  |  | 	// Needed because we allow stopping through an http endpoint and | 
					
						
							|  |  |  | 	// allowing concurrent stoppers leads to stack traces. | 
					
						
							|  |  |  | 	stopLock sync.Mutex | 
					
						
							|  |  |  | 	shutdown bool | 
					
						
							|  |  |  | 	stopCh   chan struct{} | 
					
						
							| 
									
										
										
										
											2018-06-27 07:45:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	zones            []string | 
					
						
							|  |  |  | 	endpointNameMode bool | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | type dnsControlOpts struct { | 
					
						
							| 
									
										
										
										
											2018-02-16 11:05:52 -05:00
										 |  |  | 	initPodCache       bool | 
					
						
							|  |  |  | 	initEndpointsCache bool | 
					
						
							| 
									
										
										
										
											2018-05-23 14:57:59 +02:00
										 |  |  | 	ignoreEmptyService bool | 
					
						
							| 
									
										
										
										
											2019-03-22 08:32:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:52:18 +01:00
										 |  |  | 	// Label handling. | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 	labelSelector          *meta.LabelSelector | 
					
						
							|  |  |  | 	selector               labels.Selector | 
					
						
							| 
									
										
										
										
											2019-03-22 08:32:40 -06:00
										 |  |  | 	namespaceLabelSelector *meta.LabelSelector | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 	namespaceSelector      labels.Selector | 
					
						
							| 
									
										
										
										
											2018-06-27 07:45:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 15:29:05 -05:00
										 |  |  | 	zones            []string | 
					
						
							|  |  |  | 	endpointNameMode bool | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 15:18:08 -04:00
										 |  |  | // newdnsController creates a controller for CoreDNS. | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | func newdnsController(ctx context.Context, kubeClient kubernetes.Interface, opts dnsControlOpts) *dnsControl { | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	dns := dnsControl{ | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 		client:            kubeClient, | 
					
						
							|  |  |  | 		selector:          opts.selector, | 
					
						
							| 
									
										
										
										
											2019-03-22 08:32:40 -06:00
										 |  |  | 		namespaceSelector: opts.namespaceSelector, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 		stopCh:            make(chan struct{}), | 
					
						
							|  |  |  | 		zones:             opts.zones, | 
					
						
							|  |  |  | 		endpointNameMode:  opts.endpointNameMode, | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-15 14:21:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	dns.svcLister, dns.svcController = object.NewIndexerInformer( | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		&cache.ListWatch{ | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 			ListFunc:  serviceListFunc(ctx, dns.client, api.NamespaceAll, dns.selector), | 
					
						
							|  |  |  | 			WatchFunc: serviceWatchFunc(ctx, dns.client, api.NamespaceAll, dns.selector), | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-10-17 19:35:00 +01:00
										 |  |  | 		&api.Service{}, | 
					
						
							| 
									
										
										
										
											2019-03-26 11:26:19 -04:00
										 |  |  | 		cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete}, | 
					
						
							| 
									
										
										
										
											2022-07-06 13:55:15 -04:00
										 |  |  | 		cache.Indexers{svcNameNamespaceIndex: svcNameNamespaceIndexFunc, svcIPIndex: svcIPIndexFunc, svcExtIPIndex: svcExtIPIndexFunc}, | 
					
						
							| 
									
										
										
										
											2020-12-01 15:29:05 -05:00
										 |  |  | 		object.DefaultProcessor(object.ToService, nil), | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	) | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 	podLister, podController := object.NewIndexerInformer( | 
					
						
							|  |  |  | 		&cache.ListWatch{ | 
					
						
							|  |  |  | 			ListFunc:  podListFunc(ctx, dns.client, api.NamespaceAll, dns.selector), | 
					
						
							|  |  |  | 			WatchFunc: podWatchFunc(ctx, dns.client, api.NamespaceAll, dns.selector), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		&api.Pod{}, | 
					
						
							|  |  |  | 		cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete}, | 
					
						
							|  |  |  | 		cache.Indexers{podIPIndex: podIPIndexFunc}, | 
					
						
							|  |  |  | 		object.DefaultProcessor(object.ToPod, nil), | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	dns.podLister = podLister | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | 	if opts.initPodCache { | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 		dns.podController = podController | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-15 14:21:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 	epLister, epController := object.NewIndexerInformer( | 
					
						
							|  |  |  | 		&cache.ListWatch{ | 
					
						
							|  |  |  | 			ListFunc:  endpointSliceListFunc(ctx, dns.client, api.NamespaceAll, dns.selector), | 
					
						
							|  |  |  | 			WatchFunc: endpointSliceWatchFunc(ctx, dns.client, api.NamespaceAll, dns.selector), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		&discovery.EndpointSlice{}, | 
					
						
							|  |  |  | 		cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete}, | 
					
						
							|  |  |  | 		cache.Indexers{epNameNamespaceIndex: epNameNamespaceIndexFunc, epIPIndex: epIPIndexFunc}, | 
					
						
							|  |  |  | 		object.DefaultProcessor(object.EndpointSliceToEndpoints, dns.EndpointSliceLatencyRecorder()), | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	dns.epLister = epLister | 
					
						
							| 
									
										
										
										
											2018-02-16 11:05:52 -05:00
										 |  |  | 	if opts.initEndpointsCache { | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 		dns.epController = epController | 
					
						
							| 
									
										
										
										
											2018-02-16 11:05:52 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-29 21:27:25 -06:00
										 |  |  | 	dns.nsLister, dns.nsController = object.NewIndexerInformer( | 
					
						
							| 
									
										
										
										
											2018-02-15 14:21:54 -05:00
										 |  |  | 		&cache.ListWatch{ | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 			ListFunc:  namespaceListFunc(ctx, dns.client, dns.namespaceSelector), | 
					
						
							|  |  |  | 			WatchFunc: namespaceWatchFunc(ctx, dns.client, dns.namespaceSelector), | 
					
						
							| 
									
										
										
										
											2018-02-15 14:21:54 -05:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-03-22 08:32:40 -06:00
										 |  |  | 		&api.Namespace{}, | 
					
						
							| 
									
										
										
										
											2021-07-29 21:27:25 -06:00
										 |  |  | 		cache.ResourceEventHandlerFuncs{}, | 
					
						
							|  |  |  | 		cache.Indexers{}, | 
					
						
							|  |  |  | 		object.DefaultProcessor(object.ToNamespace, nil), | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2018-02-15 14:21:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	return &dns | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 15:29:05 -05:00
										 |  |  | 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())) | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-12-01 15:29:05 -05:00
										 |  |  | 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())) | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-06-15 10:15:41 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | func podIPIndexFunc(obj interface{}) ([]string, error) { | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	p, ok := obj.(*object.Pod) | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2018-09-22 16:54:06 +01:00
										 |  |  | 		return nil, errObj | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	return []string{p.PodIP}, nil | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | func svcIPIndexFunc(obj interface{}) ([]string, error) { | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	svc, ok := obj.(*object.Service) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2018-09-22 16:54:06 +01:00
										 |  |  | 		return nil, errObj | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-07-06 13:55:15 -04:00
										 |  |  | 	idx := make([]string, len(svc.ClusterIPs)) | 
					
						
							| 
									
										
										
										
											2020-12-21 05:30:24 -05:00
										 |  |  | 	copy(idx, svc.ClusterIPs) | 
					
						
							| 
									
										
										
										
											2022-07-06 13:55:15 -04:00
										 |  |  | 	return idx, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func svcExtIPIndexFunc(obj interface{}) ([]string, error) { | 
					
						
							|  |  |  | 	svc, ok := obj.(*object.Service) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		return nil, errObj | 
					
						
							| 
									
										
										
										
											2018-12-14 09:41:51 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-07-06 13:55:15 -04:00
										 |  |  | 	idx := make([]string, len(svc.ExternalIPs)) | 
					
						
							|  |  |  | 	copy(idx, svc.ExternalIPs) | 
					
						
							| 
									
										
										
										
											2020-12-21 05:30:24 -05:00
										 |  |  | 	return idx, nil | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | func svcNameNamespaceIndexFunc(obj interface{}) ([]string, error) { | 
					
						
							|  |  |  | 	s, ok := obj.(*object.Service) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		return nil, errObj | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return []string{s.Index}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func epNameNamespaceIndexFunc(obj interface{}) ([]string, error) { | 
					
						
							|  |  |  | 	s, ok := obj.(*object.Endpoints) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		return nil, errObj | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return []string{s.Index}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | func epIPIndexFunc(obj interface{}) ([]string, error) { | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	ep, ok := obj.(*object.Endpoints) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2018-09-22 16:54:06 +01:00
										 |  |  | 		return nil, errObj | 
					
						
							| 
									
										
										
										
											2017-11-10 15:38:45 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	return ep.IndexIP, nil | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | func serviceListFunc(ctx context.Context, c kubernetes.Interface, ns string, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) { | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	return func(opts meta.ListOptions) (runtime.Object, error) { | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		if s != nil { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:44:18 +03:00
										 |  |  | 			opts.LabelSelector = s.String() | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 		return c.CoreV1().Services(ns).List(ctx, opts) | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | func podListFunc(ctx context.Context, c kubernetes.Interface, ns string, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) { | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	return func(opts meta.ListOptions) (runtime.Object, error) { | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 		if s != nil { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:44:18 +03:00
										 |  |  | 			opts.LabelSelector = s.String() | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-05-29 08:06:46 +01:00
										 |  |  | 		if len(opts.FieldSelector) > 0 { | 
					
						
							|  |  |  | 			opts.FieldSelector = opts.FieldSelector + "," | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		opts.FieldSelector = opts.FieldSelector + "status.phase!=Succeeded,status.phase!=Failed,status.phase!=Unknown" | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 		return c.CoreV1().Pods(ns).List(ctx, opts) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func endpointSliceListFunc(ctx context.Context, c kubernetes.Interface, ns string, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) { | 
					
						
							|  |  |  | 	return func(opts meta.ListOptions) (runtime.Object, error) { | 
					
						
							|  |  |  | 		if s != nil { | 
					
						
							|  |  |  | 			opts.LabelSelector = s.String() | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-10 12:57:23 -04:00
										 |  |  | 		return c.DiscoveryV1().EndpointSlices(ns).List(ctx, opts) | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | func namespaceListFunc(ctx context.Context, c kubernetes.Interface, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) { | 
					
						
							| 
									
										
										
										
											2018-02-15 14:21:54 -05:00
										 |  |  | 	return func(opts meta.ListOptions) (runtime.Object, error) { | 
					
						
							|  |  |  | 		if s != nil { | 
					
						
							|  |  |  | 			opts.LabelSelector = s.String() | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 		return c.CoreV1().Namespaces().List(ctx, opts) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func serviceWatchFunc(ctx context.Context, c kubernetes.Interface, ns string, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) { | 
					
						
							|  |  |  | 	return func(options meta.ListOptions) (watch.Interface, error) { | 
					
						
							|  |  |  | 		if s != nil { | 
					
						
							|  |  |  | 			options.LabelSelector = s.String() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return c.CoreV1().Services(ns).Watch(ctx, options) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func podWatchFunc(ctx context.Context, c kubernetes.Interface, ns string, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) { | 
					
						
							|  |  |  | 	return func(options meta.ListOptions) (watch.Interface, error) { | 
					
						
							|  |  |  | 		if s != nil { | 
					
						
							|  |  |  | 			options.LabelSelector = s.String() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if len(options.FieldSelector) > 0 { | 
					
						
							|  |  |  | 			options.FieldSelector = options.FieldSelector + "," | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		options.FieldSelector = options.FieldSelector + "status.phase!=Succeeded,status.phase!=Failed,status.phase!=Unknown" | 
					
						
							|  |  |  | 		return c.CoreV1().Pods(ns).Watch(ctx, options) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 12:57:23 -04:00
										 |  |  | func endpointSliceWatchFunc(ctx context.Context, c kubernetes.Interface, ns string, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) { | 
					
						
							|  |  |  | 	return func(options meta.ListOptions) (watch.Interface, error) { | 
					
						
							|  |  |  | 		if s != nil { | 
					
						
							|  |  |  | 			options.LabelSelector = s.String() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return c.DiscoveryV1().EndpointSlices(ns).Watch(ctx, options) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | func namespaceWatchFunc(ctx context.Context, c kubernetes.Interface, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) { | 
					
						
							|  |  |  | 	return func(options meta.ListOptions) (watch.Interface, error) { | 
					
						
							|  |  |  | 		if s != nil { | 
					
						
							|  |  |  | 			options.LabelSelector = s.String() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return c.CoreV1().Namespaces().Watch(ctx, options) | 
					
						
							| 
									
										
										
										
											2018-02-15 14:21:54 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | // Stop stops the  controller. | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | func (dns *dnsControl) Stop() error { | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	dns.stopLock.Lock() | 
					
						
							|  |  |  | 	defer dns.stopLock.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Only try draining the workqueue if we haven't already. | 
					
						
							|  |  |  | 	if !dns.shutdown { | 
					
						
							|  |  |  | 		close(dns.stopCh) | 
					
						
							|  |  |  | 		dns.shutdown = true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return fmt.Errorf("shutdown already in progress") | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Run starts the controller. | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | func (dns *dnsControl) Run() { | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	go dns.svcController.Run(dns.stopCh) | 
					
						
							| 
									
										
										
										
											2018-02-16 11:05:52 -05:00
										 |  |  | 	if dns.epController != nil { | 
					
						
							| 
									
										
										
										
											2021-03-26 08:54:39 -04:00
										 |  |  | 		go func() { | 
					
						
							|  |  |  | 			dns.epController.Run(dns.stopCh) | 
					
						
							|  |  |  | 		}() | 
					
						
							| 
									
										
										
										
											2018-02-16 11:05:52 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 	if dns.podController != nil { | 
					
						
							|  |  |  | 		go dns.podController.Run(dns.stopCh) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-15 14:21:54 -05:00
										 |  |  | 	go dns.nsController.Run(dns.stopCh) | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	<-dns.stopCh | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-20 22:53:17 +01:00
										 |  |  | // HasSynced calls on all controllers. | 
					
						
							|  |  |  | func (dns *dnsControl) HasSynced() bool { | 
					
						
							|  |  |  | 	a := dns.svcController.HasSynced() | 
					
						
							| 
									
										
										
										
											2018-02-16 11:05:52 -05:00
										 |  |  | 	b := true | 
					
						
							|  |  |  | 	if dns.epController != nil { | 
					
						
							|  |  |  | 		b = dns.epController.HasSynced() | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-20 22:53:17 +01:00
										 |  |  | 	c := true | 
					
						
							|  |  |  | 	if dns.podController != nil { | 
					
						
							|  |  |  | 		c = dns.podController.HasSynced() | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-15 14:21:54 -05:00
										 |  |  | 	d := dns.nsController.HasSynced() | 
					
						
							|  |  |  | 	return a && b && c && d | 
					
						
							| 
									
										
										
										
											2017-10-20 22:53:17 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | func (dns *dnsControl) ServiceList() (svcs []*object.Service) { | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	os := dns.svcLister.List() | 
					
						
							|  |  |  | 	for _, o := range os { | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 		s, ok := o.(*object.Service) | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		if !ok { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		svcs = append(svcs, s) | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 	return svcs | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | func (dns *dnsControl) EndpointsList() (eps []*object.Endpoints) { | 
					
						
							|  |  |  | 	os := dns.epLister.List() | 
					
						
							|  |  |  | 	for _, o := range os { | 
					
						
							|  |  |  | 		ep, ok := o.(*object.Endpoints) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		eps = append(eps, ep) | 
					
						
							| 
									
										
										
										
											2017-09-21 13:06:35 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	return eps | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dns *dnsControl) PodIndex(ip string) (pods []*object.Pod) { | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	os, err := dns.podLister.ByIndex(podIPIndex, ip) | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	for _, o := range os { | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 		p, ok := o.(*object.Pod) | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		if !ok { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 		pods = append(pods, p) | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	return pods | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | func (dns *dnsControl) SvcIndex(idx string) (svcs []*object.Service) { | 
					
						
							|  |  |  | 	os, err := dns.svcLister.ByIndex(svcNameNamespaceIndex, idx) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	for _, o := range os { | 
					
						
							|  |  |  | 		s, ok := o.(*object.Service) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		svcs = append(svcs, s) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	return svcs | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | func (dns *dnsControl) SvcIndexReverse(ip string) (svcs []*object.Service) { | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	os, err := dns.svcLister.ByIndex(svcIPIndex, ip) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, o := range os { | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 		s, ok := o.(*object.Service) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 		if !ok { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 		svcs = append(svcs, s) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	return svcs | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-06 13:55:15 -04:00
										 |  |  | func (dns *dnsControl) SvcExtIndexReverse(ip string) (svcs []*object.Service) { | 
					
						
							|  |  |  | 	os, err := dns.svcLister.ByIndex(svcExtIPIndex, ip) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, o := range os { | 
					
						
							|  |  |  | 		s, ok := o.(*object.Service) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		svcs = append(svcs, s) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return svcs | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | func (dns *dnsControl) EpIndex(idx string) (ep []*object.Endpoints) { | 
					
						
							|  |  |  | 	os, err := dns.epLister.ByIndex(epNameNamespaceIndex, idx) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	for _, o := range os { | 
					
						
							|  |  |  | 		e, ok := o.(*object.Endpoints) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ep = append(ep, e) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	return ep | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | func (dns *dnsControl) EpIndexReverse(ip string) (ep []*object.Endpoints) { | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	os, err := dns.epLister.ByIndex(epIPIndex, ip) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, o := range os { | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 		e, ok := o.(*object.Endpoints) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 		if !ok { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 		ep = append(ep, e) | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	return ep | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 21:51:51 +00:00
										 |  |  | // GetNodeByName return the node by name. If nothing is found an error is | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | // returned. This query causes a round trip to the k8s API server, so use | 
					
						
							|  |  |  | // sparingly. Currently, this is only used for Federation. | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | func (dns *dnsControl) GetNodeByName(ctx context.Context, name string) (*api.Node, error) { | 
					
						
							|  |  |  | 	v1node, err := dns.client.CoreV1().Nodes().Get(ctx, name, meta.GetOptions{}) | 
					
						
							| 
									
										
										
										
											2018-09-22 16:54:06 +01:00
										 |  |  | 	return v1node, err | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-13 21:51:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | // GetNamespaceByName returns the namespace by name. If nothing is found an error is returned. | 
					
						
							| 
									
										
										
										
											2021-07-29 21:27:25 -06:00
										 |  |  | func (dns *dnsControl) GetNamespaceByName(name string) (*object.Namespace, error) { | 
					
						
							|  |  |  | 	o, exists, err := dns.nsLister.GetByKey(name) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !exists { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("namespace not found") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ns, ok := o.(*object.Namespace) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("found key but not namespace") | 
					
						
							| 
									
										
										
										
											2017-11-13 21:51:51 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-29 21:27:25 -06:00
										 |  |  | 	return ns, nil | 
					
						
							| 
									
										
										
										
											2017-11-13 21:51:51 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-08 10:11:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 03:29:41 +09:00
										 |  |  | func (dns *dnsControl) Add(obj interface{})               { dns.updateModified() } | 
					
						
							|  |  |  | func (dns *dnsControl) Delete(obj interface{})            { dns.updateModified() } | 
					
						
							| 
									
										
										
										
											2019-03-26 11:26:19 -04:00
										 |  |  | func (dns *dnsControl) Update(oldObj, newObj interface{}) { dns.detectChanges(oldObj, newObj) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // detectChanges detects changes in objects, and updates the modified timestamp | 
					
						
							|  |  |  | func (dns *dnsControl) detectChanges(oldObj, newObj interface{}) { | 
					
						
							|  |  |  | 	// If both objects have the same resource version, they are identical. | 
					
						
							|  |  |  | 	if newObj != nil && oldObj != nil && (oldObj.(meta.Object).GetResourceVersion() == newObj.(meta.Object).GetResourceVersion()) { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	obj := newObj | 
					
						
							|  |  |  | 	if obj == nil { | 
					
						
							|  |  |  | 		obj = oldObj | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	switch ob := obj.(type) { | 
					
						
							|  |  |  | 	case *object.Service: | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | 		imod, emod := serviceModified(oldObj, newObj) | 
					
						
							|  |  |  | 		if imod { | 
					
						
							|  |  |  | 			dns.updateModified() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if emod { | 
					
						
							| 
									
										
										
										
											2023-04-25 23:25:07 +08:00
										 |  |  | 			dns.updateExtModified() | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-26 11:26:19 -04:00
										 |  |  | 	case *object.Pod: | 
					
						
							| 
									
										
										
										
											2021-07-15 03:29:41 +09:00
										 |  |  | 		dns.updateModified() | 
					
						
							| 
									
										
										
										
											2020-06-15 10:15:41 -04:00
										 |  |  | 	case *object.Endpoints: | 
					
						
							|  |  |  | 		if !endpointsEquivalent(oldObj.(*object.Endpoints), newObj.(*object.Endpoints)) { | 
					
						
							| 
									
										
										
										
											2021-07-15 03:29:41 +09:00
										 |  |  | 			dns.updateModified() | 
					
						
							| 
									
										
										
										
											2020-06-15 10:15:41 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-26 11:26:19 -04:00
										 |  |  | 	default: | 
					
						
							|  |  |  | 		log.Warningf("Updates for %T not supported.", ob) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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) | 
					
						
							|  |  |  | func subsetsEquivalent(sa, sb object.EndpointSubset) bool { | 
					
						
							|  |  |  | 	if len(sa.Addresses) != len(sb.Addresses) { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(sa.Ports) != len(sb.Ports) { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// in Addresses and Ports, we should be able to rely on | 
					
						
							|  |  |  | 	// these being sorted and able to be compared | 
					
						
							|  |  |  | 	// they are supposed to be in a canonical format | 
					
						
							|  |  |  | 	for addr, aaddr := range sa.Addresses { | 
					
						
							|  |  |  | 		baddr := sb.Addresses[addr] | 
					
						
							|  |  |  | 		if aaddr.IP != baddr.IP { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if aaddr.Hostname != baddr.Hostname { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for port, aport := range sa.Ports { | 
					
						
							|  |  |  | 		bport := sb.Ports[port] | 
					
						
							|  |  |  | 		if aport.Name != bport.Name { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if aport.Port != bport.Port { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if aport.Protocol != bport.Protocol { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // endpointsEquivalent checks if the update to an endpoint is something | 
					
						
							|  |  |  | // that matters to us or if they are effectively equivalent. | 
					
						
							|  |  |  | func endpointsEquivalent(a, b *object.Endpoints) bool { | 
					
						
							| 
									
										
										
										
											2019-10-04 17:48:43 +02:00
										 |  |  | 	if a == nil || b == nil { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-26 11:26:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if len(a.Subsets) != len(b.Subsets) { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// we should be able to rely on | 
					
						
							|  |  |  | 	// these being sorted and able to be compared | 
					
						
							|  |  |  | 	// they are supposed to be in a canonical format | 
					
						
							|  |  |  | 	for i, sa := range a.Subsets { | 
					
						
							|  |  |  | 		sb := b.Subsets[i] | 
					
						
							|  |  |  | 		if !subsetsEquivalent(sa, sb) { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | // serviceModified checks the services passed for changes that result in changes | 
					
						
							|  |  |  | // to internal and or external records.  It returns two booleans, one for internal | 
					
						
							|  |  |  | // record changes, and a second for external record changes | 
					
						
							|  |  |  | func serviceModified(oldObj, newObj interface{}) (intSvc, extSvc bool) { | 
					
						
							|  |  |  | 	if oldObj != nil && newObj == nil { | 
					
						
							|  |  |  | 		// deleted service only modifies external zone records if it had external ips | 
					
						
							|  |  |  | 		return true, len(oldObj.(*object.Service).ExternalIPs) > 0 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if oldObj == nil && newObj != nil { | 
					
						
							|  |  |  | 		// added service only modifies external zone records if it has external ips | 
					
						
							|  |  |  | 		return true, len(newObj.(*object.Service).ExternalIPs) > 0 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	newSvc := newObj.(*object.Service) | 
					
						
							|  |  |  | 	oldSvc := oldObj.(*object.Service) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// External IPs are mutable, affecting external zone records | 
					
						
							|  |  |  | 	if len(oldSvc.ExternalIPs) != len(newSvc.ExternalIPs) { | 
					
						
							|  |  |  | 		extSvc = true | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		for i := range oldSvc.ExternalIPs { | 
					
						
							|  |  |  | 			if oldSvc.ExternalIPs[i] != newSvc.ExternalIPs[i] { | 
					
						
							|  |  |  | 				extSvc = true | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// ExternalName is mutable, affecting internal zone records | 
					
						
							|  |  |  | 	intSvc = oldSvc.ExternalName != newSvc.ExternalName | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if intSvc && extSvc { | 
					
						
							|  |  |  | 		return intSvc, extSvc | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// All Port fields are mutable, affecting both internal/external zone records | 
					
						
							|  |  |  | 	if len(oldSvc.Ports) != len(newSvc.Ports) { | 
					
						
							|  |  |  | 		return true, true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i := range oldSvc.Ports { | 
					
						
							|  |  |  | 		if oldSvc.Ports[i].Name != newSvc.Ports[i].Name { | 
					
						
							|  |  |  | 			return true, true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if oldSvc.Ports[i].Port != newSvc.Ports[i].Port { | 
					
						
							|  |  |  | 			return true, true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if oldSvc.Ports[i].Protocol != newSvc.Ports[i].Protocol { | 
					
						
							|  |  |  | 			return true, true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return intSvc, extSvc | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dns *dnsControl) Modified(external bool) int64 { | 
					
						
							|  |  |  | 	if external { | 
					
						
							|  |  |  | 		return atomic.LoadInt64(&dns.extModified) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return atomic.LoadInt64(&dns.modified) | 
					
						
							| 
									
										
										
										
											2018-02-08 10:11:04 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // updateModified set dns.modified to the current time. | 
					
						
							| 
									
										
										
										
											2021-07-15 03:29:41 +09:00
										 |  |  | func (dns *dnsControl) updateModified() { | 
					
						
							| 
									
										
										
										
											2018-02-08 10:11:04 -06:00
										 |  |  | 	unix := time.Now().Unix() | 
					
						
							|  |  |  | 	atomic.StoreInt64(&dns.modified, unix) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | // updateExtModified set dns.extModified to the current time. | 
					
						
							| 
									
										
										
										
											2023-04-25 23:25:07 +08:00
										 |  |  | func (dns *dnsControl) updateExtModified() { | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | 	unix := time.Now().Unix() | 
					
						
							|  |  |  | 	atomic.StoreInt64(&dns.extModified, unix) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-22 16:54:06 +01:00
										 |  |  | var errObj = errors.New("obj was not of the correct type") |