aboutsummaryrefslogtreecommitdiff
path: root/plugin/kubernetes/object/endpoint.go
diff options
context:
space:
mode:
authorGravatar Chris O'Haver <cohaver@infoblox.com> 2021-05-10 12:57:23 -0400
committerGravatar GitHub <noreply@github.com> 2021-05-10 09:57:23 -0700
commit24547447d0457b8600c63602d1a64a60d018c82f (patch)
tree98d7130c586273b5d9f7e458b104cb3625bf5d6e /plugin/kubernetes/object/endpoint.go
parent73545522965e22bcd132a6ded4128d301fea5ca3 (diff)
downloadcoredns-24547447d0457b8600c63602d1a64a60d018c82f.tar.gz
coredns-24547447d0457b8600c63602d1a64a60d018c82f.tar.zst
coredns-24547447d0457b8600c63602d1a64a60d018c82f.zip
plugin/kubernetes: Support both v1 and v1beta1 EndpointSlices (#4570)
* support v1 and v1beta1 endpointslice Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * update comments Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
Diffstat (limited to 'plugin/kubernetes/object/endpoint.go')
-rw-r--r--plugin/kubernetes/object/endpoint.go48
1 files changed, 47 insertions, 1 deletions
diff --git a/plugin/kubernetes/object/endpoint.go b/plugin/kubernetes/object/endpoint.go
index fc25aa4fb..9a5347a9b 100644
--- a/plugin/kubernetes/object/endpoint.go
+++ b/plugin/kubernetes/object/endpoint.go
@@ -4,7 +4,8 @@ import (
"fmt"
api "k8s.io/api/core/v1"
- discovery "k8s.io/api/discovery/v1beta1"
+ discovery "k8s.io/api/discovery/v1"
+ discoveryV1beta1 "k8s.io/api/discovery/v1beta1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
@@ -151,6 +152,51 @@ func EndpointSliceToEndpoints(obj meta.Object) (meta.Object, error) {
return e, nil
}
+// 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 {
+ for _, a := range end.Addresses {
+ ea := EndpointAddress{IP: a}
+ if end.Hostname != nil {
+ ea.Hostname = *end.Hostname
+ }
+ if end.TargetRef != nil {
+ 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
+}
+
// CopyWithoutSubsets copies e, without the subsets.
func (e *Endpoints) CopyWithoutSubsets() *Endpoints {
e1 := &Endpoints{