|
| 1 | +/* |
| 2 | +Copyright 2025 Tim Ebert. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package handler_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + coordinationv1 "k8s.io/api/coordination/v1" |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 32 | + |
| 33 | + shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1" |
| 34 | + . "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/handler" |
| 35 | +) |
| 36 | + |
| 37 | +var _ = Describe("ControllerRing", func() { |
| 38 | + var ( |
| 39 | + ctx context.Context |
| 40 | + |
| 41 | + fakeClient client.Client |
| 42 | + ) |
| 43 | + |
| 44 | + BeforeEach(func() { |
| 45 | + ctx = context.Background() |
| 46 | + |
| 47 | + fakeClient = fakeclient.NewFakeClient() |
| 48 | + }) |
| 49 | + |
| 50 | + Describe("#MapControllerRingToLeases", func() { |
| 51 | + var ( |
| 52 | + mapFunc handler.MapFunc |
| 53 | + |
| 54 | + obj *shardingv1alpha1.ControllerRing |
| 55 | + ) |
| 56 | + |
| 57 | + BeforeEach(func() { |
| 58 | + mapFunc = MapControllerRingToLeases(fakeClient) |
| 59 | + |
| 60 | + obj = &shardingv1alpha1.ControllerRing{ |
| 61 | + ObjectMeta: metav1.ObjectMeta{ |
| 62 | + Name: "foo", |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + lease := &coordinationv1.Lease{ |
| 67 | + ObjectMeta: metav1.ObjectMeta{ |
| 68 | + Name: "foo-1", |
| 69 | + Namespace: "foo-system", |
| 70 | + Labels: map[string]string{ |
| 71 | + "alpha.sharding.timebertt.dev/controllerring": "foo", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + Expect(fakeClient.Create(ctx, lease.DeepCopy())).To(Succeed()) |
| 76 | + |
| 77 | + lease.Name = "foo-2" |
| 78 | + Expect(fakeClient.Create(ctx, lease.DeepCopy())).To(Succeed()) |
| 79 | + |
| 80 | + lease.Name = "foo-3" |
| 81 | + lease.Labels["alpha.sharding.timebertt.dev/controllerring"] = "bar" |
| 82 | + Expect(fakeClient.Create(ctx, lease.DeepCopy())).To(Succeed()) |
| 83 | + |
| 84 | + lease.Name = "foo-4" |
| 85 | + lease.Labels = nil |
| 86 | + Expect(fakeClient.Create(ctx, lease.DeepCopy())).To(Succeed()) |
| 87 | + }) |
| 88 | + |
| 89 | + It("should ignore other object kinds", func() { |
| 90 | + Expect(mapFunc(ctx, &corev1.Pod{})).To(BeEmpty()) |
| 91 | + }) |
| 92 | + |
| 93 | + It("should return requests for all matching leases", func() { |
| 94 | + Expect(mapFunc(ctx, obj)).To(ConsistOf( |
| 95 | + reconcile.Request{NamespacedName: types.NamespacedName{Namespace: "foo-system", Name: "foo-1"}}, |
| 96 | + reconcile.Request{NamespacedName: types.NamespacedName{Namespace: "foo-system", Name: "foo-2"}}, |
| 97 | + )) |
| 98 | + }) |
| 99 | + }) |
| 100 | +}) |
0 commit comments