|
| 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 ring_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "strconv" |
| 21 | + "time" |
| 22 | + |
| 23 | + . "github.com/onsi/ginkgo/v2" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + coordinationv1 "k8s.io/api/coordination/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/util/sets" |
| 28 | + "k8s.io/utils/ptr" |
| 29 | + |
| 30 | + shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1" |
| 31 | + "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/consistenthash" |
| 32 | + . "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/ring" |
| 33 | +) |
| 34 | + |
| 35 | +var _ = Describe("FromLeases", func() { |
| 36 | + var ( |
| 37 | + now time.Time |
| 38 | + controllerRing *shardingv1alpha1.ControllerRing |
| 39 | + leaseList *coordinationv1.LeaseList |
| 40 | + ) |
| 41 | + |
| 42 | + BeforeEach(func() { |
| 43 | + now = time.Now() |
| 44 | + controllerRing = &shardingv1alpha1.ControllerRing{ObjectMeta: metav1.ObjectMeta{Name: "foo"}} |
| 45 | + |
| 46 | + leaseList = &coordinationv1.LeaseList{} |
| 47 | + |
| 48 | + leaseTemplate := &coordinationv1.Lease{ |
| 49 | + ObjectMeta: metav1.ObjectMeta{ |
| 50 | + Name: "foo-0", |
| 51 | + }, |
| 52 | + Spec: coordinationv1.LeaseSpec{ |
| 53 | + HolderIdentity: ptr.To("foo-0"), |
| 54 | + LeaseDurationSeconds: ptr.To[int32](10), |
| 55 | + AcquireTime: ptr.To(metav1.NewMicroTime(now.Add(-5 * time.Minute))), |
| 56 | + RenewTime: ptr.To(metav1.NewMicroTime(now.Add(-2 * time.Second))), |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + lease := leaseTemplate.DeepCopy() |
| 61 | + leaseList.Items = append(leaseList.Items, *lease) |
| 62 | + |
| 63 | + lease = leaseTemplate.DeepCopy() |
| 64 | + lease.Name = "foo-1" |
| 65 | + lease.Spec.HolderIdentity = ptr.To("foo-1") |
| 66 | + leaseList.Items = append(leaseList.Items, *lease) |
| 67 | + |
| 68 | + lease = leaseTemplate.DeepCopy() |
| 69 | + lease.Name = "foo-2" |
| 70 | + lease.Spec.HolderIdentity = nil |
| 71 | + leaseList.Items = append(leaseList.Items, *lease) |
| 72 | + |
| 73 | + lease = leaseTemplate.DeepCopy() |
| 74 | + lease.Name = "foo-3" |
| 75 | + lease.Spec.RenewTime = ptr.To(metav1.NewMicroTime(now.Add(-time.Minute))) |
| 76 | + leaseList.Items = append(leaseList.Items, *lease) |
| 77 | + }) |
| 78 | + |
| 79 | + It("should create a ring from the available shards", func() { |
| 80 | + ring, shards := FromLeases(controllerRing, leaseList, now) |
| 81 | + Expect(ring).NotTo(BeNil()) |
| 82 | + Expect(shards).To(HaveLen(4)) |
| 83 | + Expect(shards.AvailableShards().IDs()).To(ConsistOf("foo-0", "foo-1")) |
| 84 | + Expect(probeRingNodes(ring)).To(ConsistOf("foo-0", "foo-1")) |
| 85 | + }) |
| 86 | +}) |
| 87 | + |
| 88 | +func probeRingNodes(ring *consistenthash.Ring) []string { |
| 89 | + nodes := sets.New[string]() |
| 90 | + |
| 91 | + for i := 0; i < 1000; i++ { |
| 92 | + nodes.Insert(ring.Hash(strconv.Itoa(i))) |
| 93 | + } |
| 94 | + |
| 95 | + return nodes.UnsortedList() |
| 96 | +} |
0 commit comments