| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | package object
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2020-06-15 10:15:41 -04:00
										 |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	api "k8s.io/api/core/v1"
 | 
					
						
							| 
									
										
										
										
											2021-05-10 12:57:23 -04:00
										 |  |  | 	discovery "k8s.io/api/discovery/v1"
 | 
					
						
							|  |  |  | 	discoveryV1beta1 "k8s.io/api/discovery/v1beta1"
 | 
					
						
							| 
									
										
										
										
											2020-12-01 15:29:05 -05:00
										 |  |  | 	meta "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	"k8s.io/apimachinery/pkg/runtime"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Endpoints is a stripped down api.Endpoints with only the items we need for CoreDNS.
 | 
					
						
							|  |  |  | type Endpoints struct {
 | 
					
						
							| 
									
										
										
										
											2019-06-01 08:39:52 +01:00
										 |  |  | 	// Don't add new fields to this struct without talking to the CoreDNS maintainers.
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	Version   string
 | 
					
						
							|  |  |  | 	Name      string
 | 
					
						
							|  |  |  | 	Namespace string
 | 
					
						
							|  |  |  | 	Index     string
 | 
					
						
							|  |  |  | 	IndexIP   []string
 | 
					
						
							|  |  |  | 	Subsets   []EndpointSubset
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*Empty
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // EndpointSubset is a group of addresses with a common set of ports. The
 | 
					
						
							|  |  |  | // expanded set of endpoints is the Cartesian product of Addresses x Ports.
 | 
					
						
							|  |  |  | type EndpointSubset struct {
 | 
					
						
							|  |  |  | 	Addresses []EndpointAddress
 | 
					
						
							|  |  |  | 	Ports     []EndpointPort
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // EndpointAddress is a tuple that describes single IP address.
 | 
					
						
							|  |  |  | type EndpointAddress struct {
 | 
					
						
							|  |  |  | 	IP            string
 | 
					
						
							|  |  |  | 	Hostname      string
 | 
					
						
							|  |  |  | 	NodeName      string
 | 
					
						
							|  |  |  | 	TargetRefName string
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // EndpointPort is a tuple that describes a single port.
 | 
					
						
							|  |  |  | type EndpointPort struct {
 | 
					
						
							|  |  |  | 	Port     int32
 | 
					
						
							|  |  |  | 	Name     string
 | 
					
						
							|  |  |  | 	Protocol string
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 15:10:45 +08:00
										 |  |  | // EndpointsKey returns a string using for the index.
 | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | func EndpointsKey(name, namespace string) string { return name + "." + namespace }
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-06 21:34:32 +08:00
										 |  |  | // ToEndpoints converts an *api.Endpoints to a *Endpoints.
 | 
					
						
							| 
									
										
										
										
											2020-12-01 15:29:05 -05:00
										 |  |  | func ToEndpoints(obj meta.Object) (meta.Object, error) {
 | 
					
						
							|  |  |  | 	end, ok := obj.(*api.Endpoints)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("unexpected object %v", obj)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	e := &Endpoints{
 | 
					
						
							|  |  |  | 		Version:   end.GetResourceVersion(),
 | 
					
						
							|  |  |  | 		Name:      end.GetName(),
 | 
					
						
							|  |  |  | 		Namespace: end.GetNamespace(),
 | 
					
						
							|  |  |  | 		Index:     EndpointsKey(end.GetName(), end.GetNamespace()),
 | 
					
						
							|  |  |  | 		Subsets:   make([]EndpointSubset, len(end.Subsets)),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for i, eps := range end.Subsets {
 | 
					
						
							|  |  |  | 		sub := EndpointSubset{
 | 
					
						
							|  |  |  | 			Addresses: make([]EndpointAddress, len(eps.Addresses)),
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if len(eps.Ports) == 0 {
 | 
					
						
							| 
									
										
										
										
											2019-08-21 16:08:55 -04:00
										 |  |  | 			// Add sentinel if there are no ports.
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 			sub.Ports = []EndpointPort{{Port: -1}}
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			sub.Ports = make([]EndpointPort, len(eps.Ports))
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for j, a := range eps.Addresses {
 | 
					
						
							|  |  |  | 			ea := EndpointAddress{IP: a.IP, Hostname: a.Hostname}
 | 
					
						
							|  |  |  | 			if a.NodeName != nil {
 | 
					
						
							|  |  |  | 				ea.NodeName = *a.NodeName
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			if a.TargetRef != nil {
 | 
					
						
							|  |  |  | 				ea.TargetRefName = a.TargetRef.Name
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			sub.Addresses[j] = ea
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for k, p := range eps.Ports {
 | 
					
						
							|  |  |  | 			ep := EndpointPort{Port: p.Port, Name: p.Name, Protocol: string(p.Protocol)}
 | 
					
						
							|  |  |  | 			sub.Ports[k] = ep
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		e.Subsets[i] = sub
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, eps := range end.Subsets {
 | 
					
						
							|  |  |  | 		for _, a := range eps.Addresses {
 | 
					
						
							|  |  |  | 			e.IndexIP = append(e.IndexIP, a.IP)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 15:29:05 -05:00
										 |  |  | 	*end = api.Endpoints{}
 | 
					
						
							| 
									
										
										
										
											2020-06-15 10:15:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 15:29:05 -05:00
										 |  |  | 	return e, nil
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 15:29:05 -05:00
										 |  |  | // EndpointSliceToEndpoints converts a *discovery.EndpointSlice to a *Endpoints.
 | 
					
						
							|  |  |  | func EndpointSliceToEndpoints(obj meta.Object) (meta.Object, error) {
 | 
					
						
							|  |  |  | 	ends, ok := obj.(*discovery.EndpointSlice)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("unexpected object %v", obj)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 	e := &Endpoints{
 | 
					
						
							|  |  |  | 		Version:   ends.GetResourceVersion(),
 | 
					
						
							|  |  |  | 		Name:      ends.GetName(),
 | 
					
						
							|  |  |  | 		Namespace: ends.GetNamespace(),
 | 
					
						
							|  |  |  | 		Index:     EndpointsKey(ends.Labels[discovery.LabelServiceName], ends.GetNamespace()),
 | 
					
						
							|  |  |  | 		Subsets:   make([]EndpointSubset, 1),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(ends.Ports) == 0 {
 | 
					
						
							|  |  |  | 		// Add sentinel if there are no ports.
 | 
					
						
							|  |  |  | 		e.Subsets[0].Ports = []EndpointPort{{Port: -1}}
 | 
					
						
							|  |  |  | 	} else {
 | 
					
						
							|  |  |  | 		e.Subsets[0].Ports = make([]EndpointPort, len(ends.Ports))
 | 
					
						
							|  |  |  | 		for k, p := range ends.Ports {
 | 
					
						
							|  |  |  | 			ep := EndpointPort{Port: *p.Port, Name: *p.Name, Protocol: string(*p.Protocol)}
 | 
					
						
							|  |  |  | 			e.Subsets[0].Ports[k] = ep
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, end := range ends.Endpoints {
 | 
					
						
							| 
									
										
										
										
											2021-05-17 16:10:30 -04:00
										 |  |  | 		if !endpointsliceReady(end.Conditions.Ready) {
 | 
					
						
							| 
									
										
										
										
											2021-04-27 12:29:32 -04:00
										 |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 		for _, a := range end.Addresses {
 | 
					
						
							|  |  |  | 			ea := EndpointAddress{IP: a}
 | 
					
						
							|  |  |  | 			if end.Hostname != nil {
 | 
					
						
							|  |  |  | 				ea.Hostname = *end.Hostname
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2021-10-06 11:59:37 -04:00
										 |  |  | 			// ignore pod names that are too long to be a valid label
 | 
					
						
							|  |  |  | 			if end.TargetRef != nil && len(end.TargetRef.Name) < 64 {
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 				ea.TargetRefName = end.TargetRef.Name
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2021-05-04 04:55:49 -04:00
										 |  |  | 			if end.NodeName != nil {
 | 
					
						
							|  |  |  | 				ea.NodeName = *end.NodeName
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 			e.Subsets[0].Addresses = append(e.Subsets[0].Addresses, ea)
 | 
					
						
							|  |  |  | 			e.IndexIP = append(e.IndexIP, a)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 15:29:05 -05:00
										 |  |  | 	*ends = discovery.EndpointSlice{}
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 15:29:05 -05:00
										 |  |  | 	return e, nil
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 12:57:23 -04:00
										 |  |  | // EndpointSliceV1beta1ToEndpoints converts a v1beta1 *discovery.EndpointSlice to a *Endpoints.
 | 
					
						
							|  |  |  | func EndpointSliceV1beta1ToEndpoints(obj meta.Object) (meta.Object, error) {
 | 
					
						
							|  |  |  | 	ends, ok := obj.(*discoveryV1beta1.EndpointSlice)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("unexpected object %v", obj)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	e := &Endpoints{
 | 
					
						
							|  |  |  | 		Version:   ends.GetResourceVersion(),
 | 
					
						
							|  |  |  | 		Name:      ends.GetName(),
 | 
					
						
							|  |  |  | 		Namespace: ends.GetNamespace(),
 | 
					
						
							|  |  |  | 		Index:     EndpointsKey(ends.Labels[discovery.LabelServiceName], ends.GetNamespace()),
 | 
					
						
							|  |  |  | 		Subsets:   make([]EndpointSubset, 1),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(ends.Ports) == 0 {
 | 
					
						
							|  |  |  | 		// Add sentinel if there are no ports.
 | 
					
						
							|  |  |  | 		e.Subsets[0].Ports = []EndpointPort{{Port: -1}}
 | 
					
						
							|  |  |  | 	} else {
 | 
					
						
							|  |  |  | 		e.Subsets[0].Ports = make([]EndpointPort, len(ends.Ports))
 | 
					
						
							|  |  |  | 		for k, p := range ends.Ports {
 | 
					
						
							|  |  |  | 			ep := EndpointPort{Port: *p.Port, Name: *p.Name, Protocol: string(*p.Protocol)}
 | 
					
						
							|  |  |  | 			e.Subsets[0].Ports[k] = ep
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, end := range ends.Endpoints {
 | 
					
						
							| 
									
										
										
										
											2021-05-17 16:10:30 -04:00
										 |  |  | 		if !endpointsliceReady(end.Conditions.Ready) {
 | 
					
						
							| 
									
										
										
										
											2021-05-10 13:29:56 -04:00
										 |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2021-05-10 12:57:23 -04:00
										 |  |  | 		for _, a := range end.Addresses {
 | 
					
						
							|  |  |  | 			ea := EndpointAddress{IP: a}
 | 
					
						
							|  |  |  | 			if end.Hostname != nil {
 | 
					
						
							|  |  |  | 				ea.Hostname = *end.Hostname
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2021-10-06 11:59:37 -04:00
										 |  |  | 			// ignore pod names that are too long to be a valid label
 | 
					
						
							|  |  |  | 			if end.TargetRef != nil && len(end.TargetRef.Name) < 64 {
 | 
					
						
							| 
									
										
										
										
											2021-05-10 12:57:23 -04:00
										 |  |  | 				ea.TargetRefName = end.TargetRef.Name
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			// EndpointSlice does not contain NodeName, leave blank
 | 
					
						
							|  |  |  | 			e.Subsets[0].Addresses = append(e.Subsets[0].Addresses, ea)
 | 
					
						
							|  |  |  | 			e.IndexIP = append(e.IndexIP, a)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*ends = discoveryV1beta1.EndpointSlice{}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return e, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 16:10:30 -04:00
										 |  |  | func endpointsliceReady(ready *bool) bool {
 | 
					
						
							|  |  |  | 	// Per API docs: a nil value indicates an unknown state. In most cases consumers
 | 
					
						
							|  |  |  | 	// should interpret this unknown state as ready.
 | 
					
						
							|  |  |  | 	if ready == nil {
 | 
					
						
							|  |  |  | 		return true
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return *ready
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | // CopyWithoutSubsets copies e, without the subsets.
 | 
					
						
							|  |  |  | func (e *Endpoints) CopyWithoutSubsets() *Endpoints {
 | 
					
						
							|  |  |  | 	e1 := &Endpoints{
 | 
					
						
							|  |  |  | 		Version:   e.Version,
 | 
					
						
							|  |  |  | 		Name:      e.Name,
 | 
					
						
							|  |  |  | 		Namespace: e.Namespace,
 | 
					
						
							|  |  |  | 		Index:     e.Index,
 | 
					
						
							|  |  |  | 		IndexIP:   make([]string, len(e.IndexIP)),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	copy(e1.IndexIP, e.IndexIP)
 | 
					
						
							|  |  |  | 	return e1
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var _ runtime.Object = &Endpoints{}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // DeepCopyObject implements the ObjectKind interface.
 | 
					
						
							|  |  |  | func (e *Endpoints) DeepCopyObject() runtime.Object {
 | 
					
						
							|  |  |  | 	e1 := &Endpoints{
 | 
					
						
							|  |  |  | 		Version:   e.Version,
 | 
					
						
							|  |  |  | 		Name:      e.Name,
 | 
					
						
							|  |  |  | 		Namespace: e.Namespace,
 | 
					
						
							|  |  |  | 		Index:     e.Index,
 | 
					
						
							|  |  |  | 		IndexIP:   make([]string, len(e.IndexIP)),
 | 
					
						
							|  |  |  | 		Subsets:   make([]EndpointSubset, len(e.Subsets)),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	copy(e1.IndexIP, e.IndexIP)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, eps := range e.Subsets {
 | 
					
						
							|  |  |  | 		sub := EndpointSubset{
 | 
					
						
							|  |  |  | 			Addresses: make([]EndpointAddress, len(eps.Addresses)),
 | 
					
						
							|  |  |  | 			Ports:     make([]EndpointPort, len(eps.Ports)),
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		for j, a := range eps.Addresses {
 | 
					
						
							|  |  |  | 			ea := EndpointAddress{IP: a.IP, Hostname: a.Hostname, NodeName: a.NodeName, TargetRefName: a.TargetRefName}
 | 
					
						
							|  |  |  | 			sub.Addresses[j] = ea
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		for k, p := range eps.Ports {
 | 
					
						
							|  |  |  | 			ep := EndpointPort{Port: p.Port, Name: p.Name, Protocol: p.Protocol}
 | 
					
						
							|  |  |  | 			sub.Ports[k] = ep
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		e1.Subsets[i] = sub
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return e1
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GetNamespace implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (e *Endpoints) GetNamespace() string { return e.Namespace }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SetNamespace implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (e *Endpoints) SetNamespace(namespace string) {}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GetName implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (e *Endpoints) GetName() string { return e.Name }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SetName implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (e *Endpoints) SetName(name string) {}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GetResourceVersion implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (e *Endpoints) GetResourceVersion() string { return e.Version }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SetResourceVersion implements the metav1.Object interface.
 | 
					
						
							|  |  |  | func (e *Endpoints) SetResourceVersion(version string) {}
 |