44 "context"
55
66 corev1 "k8s.io/api/core/v1"
7+ discoveryv1 "k8s.io/api/discovery/v1"
78 "k8s.io/apimachinery/pkg/api/errors"
89 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910 "k8s.io/apimachinery/pkg/runtime"
@@ -25,7 +26,7 @@ const (
2526)
2627
2728// PodReconciler reconciles Pods labelled with envoy-router/enabled=true.
28- // For each such pod it maintains a selector-less Service, an Endpoints object ,
29+ // For each such pod it maintains a selector-less Service, an EndpointSlice ,
2930// and an HTTPRoute that maps /<pod-name> to the pod's IP.
3031type PodReconciler struct {
3132 client.Client
@@ -53,15 +54,17 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
5354 if err := r .cleanup (ctx , pod ); err != nil {
5455 return ctrl.Result {}, err
5556 }
57+ patch := client .MergeFrom (pod .DeepCopy ())
5658 controllerutil .RemoveFinalizer (pod , finalizerName )
57- return ctrl.Result {}, r .Update (ctx , pod )
59+ return ctrl.Result {}, r .Patch (ctx , pod , patch )
5860 }
5961 return ctrl.Result {}, nil
6062 }
6163
6264 if ! controllerutil .ContainsFinalizer (pod , finalizerName ) {
65+ patch := client .MergeFrom (pod .DeepCopy ())
6366 controllerutil .AddFinalizer (pod , finalizerName )
64- if err := r .Update (ctx , pod ); err != nil {
67+ if err := r .Patch (ctx , pod , patch ); err != nil {
6568 return ctrl.Result {}, err
6669 }
6770 }
@@ -74,7 +77,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
7477 if err := r .ensureService (ctx , pod ); err != nil {
7578 return ctrl.Result {}, err
7679 }
77- if err := r .ensureEndpoints (ctx , pod ); err != nil {
80+ if err := r .ensureEndpointSlice (ctx , pod ); err != nil {
7881 return ctrl.Result {}, err
7982 }
8083 if err := r .ensureHTTPRoute (ctx , pod ); err != nil {
@@ -94,9 +97,9 @@ func (r *PodReconciler) cleanup(ctx context.Context, pod *corev1.Pod) error {
9497 }
9598 }
9699
97- ep := & corev1. Endpoints {}
98- if err := r .Get (ctx , nn , ep ); err == nil {
99- if err := r .Delete (ctx , ep ); err != nil && ! errors .IsNotFound (err ) {
100+ eps := & discoveryv1. EndpointSlice {}
101+ if err := r .Get (ctx , nn , eps ); err == nil {
102+ if err := r .Delete (ctx , eps ); err != nil && ! errors .IsNotFound (err ) {
100103 return err
101104 }
102105 }
@@ -142,30 +145,43 @@ func (r *PodReconciler) ensureService(ctx context.Context, pod *corev1.Pod) erro
142145 return r .Update (ctx , existing )
143146}
144147
145- func (r * PodReconciler ) ensureEndpoints (ctx context.Context , pod * corev1.Pod ) error {
146- desired := & corev1.Endpoints {
148+ func (r * PodReconciler ) ensureEndpointSlice (ctx context.Context , pod * corev1.Pod ) error {
149+ ready := true
150+ port := r .PodPort
151+ protocol := corev1 .ProtocolTCP
152+ addrType := discoveryv1 .AddressTypeIPv4
153+
154+ desired := & discoveryv1.EndpointSlice {
147155 ObjectMeta : metav1.ObjectMeta {
148156 Name : pod .Name ,
149157 Namespace : pod .Namespace ,
150- Labels : map [string ]string {managedByLabel : managedByValue },
158+ Labels : map [string ]string {
159+ managedByLabel : managedByValue ,
160+ discoveryv1 .LabelServiceName : pod .Name ,
161+ },
151162 },
152- Subsets : []corev1.EndpointSubset {
163+ AddressType : addrType ,
164+ Endpoints : []discoveryv1.Endpoint {
153165 {
154- Addresses : []corev1. EndpointAddress {{ IP : pod .Status .PodIP } },
155- Ports : []corev1. EndpointPort {{ Port : r . PodPort , Protocol : corev1 . ProtocolTCP } },
166+ Addresses : [] string { pod .Status .PodIP },
167+ Conditions : discoveryv1. EndpointConditions { Ready : & ready },
156168 },
157169 },
170+ Ports : []discoveryv1.EndpointPort {
171+ {Port : & port , Protocol : & protocol },
172+ },
158173 }
159174
160- existing := & corev1. Endpoints {}
175+ existing := & discoveryv1. EndpointSlice {}
161176 err := r .Get (ctx , types.NamespacedName {Name : pod .Name , Namespace : pod .Namespace }, existing )
162177 if errors .IsNotFound (err ) {
163178 return r .Create (ctx , desired )
164179 }
165180 if err != nil {
166181 return err
167182 }
168- existing .Subsets = desired .Subsets
183+ existing .Endpoints = desired .Endpoints
184+ existing .Ports = desired .Ports
169185 return r .Update (ctx , existing )
170186}
171187
0 commit comments