Skip to content

Commit 163312b

Browse files
committed
Switch from deprecated corev1.Endpoints to discoveryv1.EndpointSlice.
1 parent 18b4a44 commit 163312b

File tree

9 files changed

+100
-112
lines changed

9 files changed

+100
-112
lines changed

api/v1beta1/rabbitmqcluster_status_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/rabbitmq/cluster-operator/v2/internal/status"
77
appsv1 "k8s.io/api/apps/v1"
88
corev1 "k8s.io/api/core/v1"
9+
discoveryv1 "k8s.io/api/discovery/v1"
910
"k8s.io/apimachinery/pkg/api/resource"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
"k8s.io/apimachinery/pkg/runtime"
@@ -40,19 +41,17 @@ var _ = Describe("RabbitmqClusterStatus", func() {
4041
Conditions: nil,
4142
}
4243

43-
endPoints := &corev1.Endpoints{
44-
Subsets: []corev1.EndpointSubset{
44+
endPointSlice := &discoveryv1.EndpointSlice{
45+
Endpoints: []discoveryv1.Endpoint{
4546
{
46-
Addresses: []corev1.EndpointAddress{
47-
{
48-
IP: "127.0.0.1",
49-
},
47+
Addresses: []string{
48+
"127.0.0.1",
5049
},
5150
},
5251
},
5352
}
5453

55-
rabbitmqClusterStatus.SetConditions([]runtime.Object{sts, endPoints})
54+
rabbitmqClusterStatus.SetConditions([]runtime.Object{sts, endPointSlice})
5655

5756
Expect(rabbitmqClusterStatus.Conditions).To(HaveLen(4))
5857
Expect(rabbitmqClusterStatus.Conditions[0].Type).To(Equal(status.AllReplicasReady))

api/v1beta1/rabbitmqcluster_types_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/rabbitmq/cluster-operator/v2/internal/status"
1515
appsv1 "k8s.io/api/apps/v1"
1616
corev1 "k8s.io/api/core/v1"
17+
discoveryv1 "k8s.io/api/discovery/v1"
1718
k8sresource "k8s.io/apimachinery/pkg/api/resource"
1819
"k8s.io/apimachinery/pkg/runtime"
1920
"k8s.io/utils/ptr"
@@ -484,19 +485,17 @@ var _ = Describe("RabbitmqCluster", func() {
484485
Conditions: nil,
485486
}
486487

487-
endPoints := &corev1.Endpoints{
488-
Subsets: []corev1.EndpointSubset{
488+
endPointSlice := &discoveryv1.EndpointSlice{
489+
Endpoints: []discoveryv1.Endpoint{
489490
{
490-
Addresses: []corev1.EndpointAddress{
491-
{
492-
IP: "127.0.0.1",
493-
},
491+
Addresses: []string{
492+
"127.0.0.1",
494493
},
495494
},
496495
},
497496
}
498497

499-
rabbitmqClusterStatus.SetConditions([]runtime.Object{statefulset, endPoints})
498+
rabbitmqClusterStatus.SetConditions([]runtime.Object{statefulset, endPointSlice})
500499

501500
Expect(rabbitmqClusterStatus.Conditions).To(HaveLen(4))
502501
Expect(rabbitmqClusterStatus.Conditions[0].Type).To(Equal(status.AllReplicasReady))

config/rbac/role.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ rules:
6868
- list
6969
- update
7070
- watch
71+
- apiGroups:
72+
- discovery.k8s.io
73+
resources:
74+
- endpointslices
75+
verbs:
76+
- get
77+
- list
7178
- apiGroups:
7279
- rabbitmq.com
7380
resources:

controllers/rabbitmqcluster_controller.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/client-go/rest"
3131
"k8s.io/client-go/tools/record"
3232

33+
"k8s.io/apimachinery/pkg/labels"
3334
"k8s.io/apimachinery/pkg/types"
3435

3536
clientretry "k8s.io/client-go/util/retry"
@@ -43,6 +44,7 @@ import (
4344
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/v2/api/v1beta1"
4445
appsv1 "k8s.io/api/apps/v1"
4546
corev1 "k8s.io/api/core/v1"
47+
discoveryv1 "k8s.io/api/discovery/v1"
4648
rbacv1 "k8s.io/api/rbac/v1"
4749
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4850
)
@@ -77,7 +79,6 @@ type RabbitmqClusterReconciler struct {
7779
// +kubebuilder:rbac:groups="",resources=pods/exec,verbs=create
7880
// +kubebuilder:rbac:groups="",resources=pods,verbs=update;get;list;watch
7981
// +kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update
80-
// +kubebuilder:rbac:groups="",resources=endpoints,verbs=get;watch;list
8182
// +kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get;list;watch;create;update;delete
8283
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update
8384
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update
@@ -88,6 +89,8 @@ type RabbitmqClusterReconciler struct {
8889
// +kubebuilder:rbac:groups="",resources=serviceaccounts,verbs=get;list;watch;create;update
8990
// +kubebuilder:rbac:groups="",resources=persistentvolumeclaims,verbs=get;list;watch;create;update
9091
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=roles,verbs=get;list;watch;create;update
92+
// +kubebuilder:rbac:groups="discovery.k8s.io",resources=endpointslices,verbs=get;list
93+
// +kubebuilder:rbac:groups="",resources=endpoints,verbs=get;watch;list
9194
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=rolebindings,verbs=get;list;watch;create;update
9295

9396
func (r *RabbitmqClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
@@ -342,7 +345,8 @@ func (r *RabbitmqClusterReconciler) updateStatusConditions(ctx context.Context,
342345

343346
func (r *RabbitmqClusterReconciler) getChildResources(ctx context.Context, rmq *rabbitmqv1beta1.RabbitmqCluster) ([]runtime.Object, error) {
344347
sts := &appsv1.StatefulSet{}
345-
endPoints := &corev1.Endpoints{}
348+
endpointSliceList := &discoveryv1.EndpointSliceList{}
349+
endpointSlice := &discoveryv1.EndpointSlice{}
346350

347351
if err := r.Get(ctx,
348352
types.NamespacedName{Name: rmq.ChildResourceName("server"), Namespace: rmq.Namespace},
@@ -352,15 +356,25 @@ func (r *RabbitmqClusterReconciler) getChildResources(ctx context.Context, rmq *
352356
sts = nil
353357
}
354358

355-
if err := r.Get(ctx,
356-
types.NamespacedName{Name: rmq.ChildResourceName(resource.ServiceSuffix), Namespace: rmq.Namespace},
357-
endPoints); err != nil && !k8serrors.IsNotFound(err) {
359+
selector, err := labels.Parse(fmt.Sprintf("%s=%s", discoveryv1.LabelServiceName, rmq.Name))
360+
if err != nil {
358361
return nil, err
359-
} else if k8serrors.IsNotFound(err) {
360-
endPoints = nil
361362
}
362363

363-
return []runtime.Object{sts, endPoints}, nil
364+
listOptions := client.ListOptions{
365+
LabelSelector: selector,
366+
Namespace: rmq.Namespace,
367+
}
368+
369+
if err := r.List(ctx, endpointSliceList, &listOptions); err != nil {
370+
return nil, err
371+
} else if len(endpointSliceList.Items) == 0 {
372+
endpointSlice = nil
373+
} else {
374+
endpointSlice = &endpointSliceList.Items[0]
375+
}
376+
377+
return []runtime.Object{sts, endpointSlice}, nil
364378
}
365379

366380
func (r *RabbitmqClusterReconciler) setReconcileSuccess(ctx context.Context, rabbitmqCluster *rabbitmqv1beta1.RabbitmqCluster, condition corev1.ConditionStatus, reason, msg string) {

internal/status/all_replicas_ready_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
rabbitmqstatus "github.com/rabbitmq/cluster-operator/v2/internal/status"
1818
appsv1 "k8s.io/api/apps/v1"
1919
corev1 "k8s.io/api/core/v1"
20+
discoveryv1 "k8s.io/api/discovery/v1"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
"k8s.io/apimachinery/pkg/runtime"
2223
)
@@ -44,7 +45,7 @@ var _ = Describe("AllReplicasReady", func() {
4445
})
4546

4647
It("returns the expected condition", func() {
47-
condition := rabbitmqstatus.AllReplicasReadyCondition([]runtime.Object{&corev1.Endpoints{}, sts}, oldCondition)
48+
condition := rabbitmqstatus.AllReplicasReadyCondition([]runtime.Object{&discoveryv1.EndpointSlice{}, sts}, oldCondition)
4849

4950
By("having status true and reason message", func() {
5051
Expect(condition.Status).To(Equal(corev1.ConditionTrue))
@@ -112,7 +113,7 @@ var _ = Describe("AllReplicasReady", func() {
112113
})
113114

114115
It("updates the transition time", func() {
115-
condition := rabbitmqstatus.AllReplicasReadyCondition([]runtime.Object{&corev1.Endpoints{}, sts}, oldCondition)
116+
condition := rabbitmqstatus.AllReplicasReadyCondition([]runtime.Object{&discoveryv1.EndpointSlice{}, sts}, oldCondition)
116117
Expect(condition.LastTransitionTime).ToNot(Equal(emptyTime))
117118
})
118119
})

internal/status/cluster_available.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ import (
1313
"time"
1414

1515
corev1 "k8s.io/api/core/v1"
16+
discoveryv1 "k8s.io/api/discovery/v1"
1617
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1718
"k8s.io/apimachinery/pkg/runtime"
1819
)
1920

2021
type ClusterAvailableConditionManager struct {
21-
condition RabbitmqClusterCondition
22-
endpoints *corev1.Endpoints
22+
condition RabbitmqClusterCondition
23+
endpointslice *discoveryv1.EndpointSlice
2324
}
2425

2526
func ClusterAvailableCondition(resources []runtime.Object,
@@ -32,16 +33,16 @@ func ClusterAvailableCondition(resources []runtime.Object,
3233

3334
for _, res := range resources {
3435
switch resource := res.(type) {
35-
case *corev1.Endpoints:
36+
case *discoveryv1.EndpointSlice:
3637
if resource == nil {
3738
condition.Status = corev1.ConditionUnknown
3839
condition.Reason = "CouldNotRetrieveEndpoints"
3940
condition.Message = "Could not verify available service endpoints"
4041
goto assignLastTransitionTime
4142
}
4243

43-
for _, subset := range resource.Subsets {
44-
if len(subset.Addresses) > 0 {
44+
for _, endpoint := range resource.Endpoints {
45+
if len(endpoint.Addresses) > 0 {
4546
condition.Status = corev1.ConditionTrue
4647
condition.Reason = "AtLeastOneEndpointAvailable"
4748
goto assignLastTransitionTime

0 commit comments

Comments
 (0)