Skip to content

Commit e2743be

Browse files
committed
fix errors/warns
1 parent b3074c4 commit e2743be

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

charts/envoy-router/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ apiVersion: v2
22
name: envoy-router
33
description: Operator that dynamically manages HTTPRoutes for labelled pods via Envoy Gateway
44
type: application
5-
version: 0.1.0
6-
appVersion: "0.1.0"
5+
version: 0.1.1
6+
appVersion: "0.1.1"
77
icon: data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMDAgMTAwIj4KICA8IS0tIEJhY2tncm91bmQgLS0+CiAgPGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iNTAiIGZpbGw9IiMxYjFmM2EiLz4KICA8IS0tIEluY29taW5nIHJvdXRlcyAtLT4KICA8bGluZSB4MT0iOCIgeTE9IjI4IiB4Mj0iMzkiIHkyPSI0NCIgc3Ryb2tlPSIjMDBiNGEwIiBzdHJva2Utd2lkdGg9IjIuNSIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIi8+CiAgPGxpbmUgeDE9IjgiIHkxPSI1MCIgeDI9IjM3IiB5Mj0iNTAiIHN0cm9rZT0iIzAwYjRhMCIgc3Ryb2tlLXdpZHRoPSIyLjUiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIvPgogIDxsaW5lIHgxPSI4IiB5MT0iNzIiIHgyPSIzOSIgeTI9IjU2IiBzdHJva2U9IiMwMGI0YTAiIHN0cm9rZS13aWR0aD0iMi41IiBzdHJva2UtbGluZWNhcD0icm91bmQiLz4KICA8IS0tIEFycm93aGVhZHMgLS0+CiAgPHBvbHlnb24gcG9pbnRzPSI0MCw0NCAzMSwzNyAzMyw0NyIgZmlsbD0iIzAwYjRhMCIvPgogIDxwb2x5Z29uIHBvaW50cz0iMzgsNTAgMjksNDYgMjksNTQiIGZpbGw9IiMwMGI0YTAiLz4KICA8cG9seWdvbiBwb2ludHM9IjQwLDU2IDMxLDUzIDMzLDYzIiBmaWxsPSIjMDBiNGEwIi8+CiAgPCEtLSBDZW50cmFsIHJvdXRlciBub2RlIC0tPgogIDxjaXJjbGUgY3g9IjUwIiBjeT0iNTAiIHI9IjExIiBmaWxsPSIjMDBiNGEwIi8+CiAgPGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iNyIgZmlsbD0iIzFiMWYzYSIvPgogIDxjaXJjbGUgY3g9IjUwIiBjeT0iNTAiIHI9IjMiIGZpbGw9IiMwMGI0YTAiLz4KICA8IS0tIE91dGdvaW5nIGFycm93IC0tPgogIDxsaW5lIHgxPSI2MSIgeTE9IjUwIiB4Mj0iODMiIHkyPSI1MCIgc3Ryb2tlPSIjZmZmZmZmIiBzdHJva2Utd2lkdGg9IjMiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIvPgogIDxwb2x5Z29uIHBvaW50cz0iODgsNTAgNzksNDUgNzksNTUiIGZpbGw9IiNmZmZmZmYiLz4KPC9zdmc+Cg==

charts/envoy-router/templates/role.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ rules:
1111
resources: ["pods"]
1212
verbs: ["get", "list", "watch", "update", "patch"]
1313
- apiGroups: [""]
14-
resources: ["services", "endpoints"]
14+
resources: ["services"]
15+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
16+
- apiGroups: ["discovery.k8s.io"]
17+
resources: ["endpointslices"]
1518
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
1619
- apiGroups: ["gateway.networking.k8s.io"]
1720
resources: ["httproutes"]

charts/envoy-router/tests/rbac_test.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,22 @@ tests:
5555
resources: ["httproutes"]
5656
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
5757

58-
- it: Role grants full CRUD on services and endpoints
58+
- it: Role grants full CRUD on services
5959
template: templates/role.yaml
6060
asserts:
6161
- contains:
6262
path: rules
6363
content:
6464
apiGroups: [""]
65-
resources: ["services", "endpoints"]
65+
resources: ["services"]
66+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
67+
68+
- it: Role grants full CRUD on endpointslices
69+
template: templates/role.yaml
70+
asserts:
71+
- contains:
72+
path: rules
73+
content:
74+
apiGroups: ["discovery.k8s.io"]
75+
resources: ["endpointslices"]
6676
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]

internal/controller/pod_controller.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
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.
3031
type 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

Comments
 (0)