|
| 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 controllerring_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "maps" |
| 21 | + "time" |
| 22 | + |
| 23 | + . "github.com/onsi/ginkgo/v2" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + gomegatypes "github.com/onsi/gomega/types" |
| 26 | + admissionregistrationv1 "k8s.io/api/admissionregistration/v1" |
| 27 | + coordinationv1 "k8s.io/api/coordination/v1" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/utils/ptr" |
| 30 | + . "sigs.k8s.io/controller-runtime/pkg/envtest/komega" |
| 31 | + |
| 32 | + shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1" |
| 33 | + "github.com/timebertt/kubernetes-controller-sharding/pkg/utils/test" |
| 34 | + . "github.com/timebertt/kubernetes-controller-sharding/pkg/utils/test/matchers" |
| 35 | +) |
| 36 | + |
| 37 | +var _ = Describe("ControllerRing controller", func() { |
| 38 | + var ( |
| 39 | + controllerRing *shardingv1alpha1.ControllerRing |
| 40 | + ) |
| 41 | + |
| 42 | + BeforeEach(func(ctx SpecContext) { |
| 43 | + controllerRing = &shardingv1alpha1.ControllerRing{ |
| 44 | + ObjectMeta: metav1.ObjectMeta{ |
| 45 | + GenerateName: testRunID + "-", |
| 46 | + Labels: maps.Clone(testRunLabels), |
| 47 | + }, |
| 48 | + Spec: shardingv1alpha1.ControllerRingSpec{ |
| 49 | + Resources: []shardingv1alpha1.RingResource{ |
| 50 | + { |
| 51 | + GroupResource: metav1.GroupResource{Group: "apps", Resource: "deployments"}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + NamespaceSelector: nil, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + Expect(testClient.Create(ctx, controllerRing)).To(Succeed()) |
| 59 | + log.Info("Created ControllerRing for test", "controllerRingName", controllerRing.Name) |
| 60 | + |
| 61 | + DeferCleanup(func(ctx SpecContext) { |
| 62 | + Expect(testClient.Delete(ctx, controllerRing)).To(Or(Succeed(), BeNotFoundError())) |
| 63 | + }, NodeTimeout(time.Minute)) |
| 64 | + }, NodeTimeout(time.Minute), OncePerOrdered) |
| 65 | + |
| 66 | + It("should observe the generation", func(ctx SpecContext) { |
| 67 | + Eventually(ctx, Object(controllerRing)).Should( |
| 68 | + HaveField("Status.ObservedGeneration", Equal(controllerRing.Generation)), |
| 69 | + ) |
| 70 | + }, SpecTimeout(time.Minute)) |
| 71 | + |
| 72 | + It("should report readiness", func(ctx SpecContext) { |
| 73 | + Eventually(ctx, Object(controllerRing)).Should( |
| 74 | + HaveField("Status.Conditions", ConsistOf( |
| 75 | + MatchCondition( |
| 76 | + OfType(shardingv1alpha1.ControllerRingReady), |
| 77 | + WithStatus(metav1.ConditionTrue), |
| 78 | + ), |
| 79 | + )), |
| 80 | + ) |
| 81 | + }, SpecTimeout(time.Minute)) |
| 82 | + |
| 83 | + It("should apply the sharder webhook", func(ctx SpecContext) { |
| 84 | + webhookConfig := &admissionregistrationv1.MutatingWebhookConfiguration{ |
| 85 | + ObjectMeta: metav1.ObjectMeta{Name: "controllerring-" + controllerRing.Name}, |
| 86 | + } |
| 87 | + Eventually(ctx, Object(webhookConfig)).Should(And( |
| 88 | + HaveField("ObjectMeta.OwnerReferences", ConsistOf(And( |
| 89 | + HaveField("Kind", Equal("ControllerRing")), |
| 90 | + HaveField("Name", Equal(controllerRing.Name)), |
| 91 | + ))), |
| 92 | + HaveField("Webhooks", ConsistOf(And( |
| 93 | + HaveField("ClientConfig.Service.Path", HaveValue(Equal("/webhooks/sharder/controllerring/"+controllerRing.Name))), |
| 94 | + HaveField("Rules", ConsistOf(And( |
| 95 | + HaveField("APIGroups", ConsistOf("apps")), |
| 96 | + HaveField("Resources", ConsistOf("deployments")), |
| 97 | + ))), |
| 98 | + ))), |
| 99 | + )) |
| 100 | + }, SpecTimeout(time.Minute)) |
| 101 | + |
| 102 | + Describe("should reflect the shard leases in the status", Ordered, func() { |
| 103 | + var lease *coordinationv1.Lease |
| 104 | + |
| 105 | + It("Create available shard lease", func(ctx SpecContext) { |
| 106 | + lease = newLease(controllerRing.Name) |
| 107 | + Expect(testClient.Create(ctx, lease)).To(Succeed()) |
| 108 | + |
| 109 | + Eventually(ctx, Object(controllerRing)).Should(haveStatusShards(1, 1)) |
| 110 | + }, SpecTimeout(time.Minute)) |
| 111 | + |
| 112 | + It("Create orphaned shard lease", func(ctx SpecContext) { |
| 113 | + lease = newLease(controllerRing.Name) |
| 114 | + lease.Spec.HolderIdentity = nil |
| 115 | + Expect(testClient.Create(ctx, lease)).To(Succeed()) |
| 116 | + |
| 117 | + Eventually(ctx, Object(controllerRing)).Should(haveStatusShards(1, 2)) |
| 118 | + }, SpecTimeout(time.Minute)) |
| 119 | + |
| 120 | + It("Make lease healthy", func(ctx SpecContext) { |
| 121 | + Eventually(ctx, Update(lease, func() { |
| 122 | + lease.Spec.HolderIdentity = ptr.To(lease.Name) |
| 123 | + })).Should(Succeed()) |
| 124 | + |
| 125 | + Eventually(ctx, Object(controllerRing)).Should(haveStatusShards(2, 2)) |
| 126 | + }, SpecTimeout(time.Minute)) |
| 127 | + |
| 128 | + It("Make lease unhealthy", func(ctx SpecContext) { |
| 129 | + Eventually(ctx, Update(lease, func() { |
| 130 | + lease.Spec.HolderIdentity = nil |
| 131 | + })).Should(Succeed()) |
| 132 | + |
| 133 | + Eventually(ctx, Object(controllerRing)).Should(haveStatusShards(1, 2)) |
| 134 | + }, SpecTimeout(time.Minute)) |
| 135 | + |
| 136 | + It("Delete unhealthy lease", func(ctx SpecContext) { |
| 137 | + Expect(testClient.Delete(ctx, lease)).To(Succeed()) |
| 138 | + |
| 139 | + Eventually(ctx, Object(controllerRing)).Should(haveStatusShards(1, 1)) |
| 140 | + }, SpecTimeout(time.Minute)) |
| 141 | + }) |
| 142 | +}) |
| 143 | + |
| 144 | +func newLease(controllerRingName string) *coordinationv1.Lease { |
| 145 | + name := testRunID + "-" + test.RandomSuffix() |
| 146 | + |
| 147 | + lease := &coordinationv1.Lease{ |
| 148 | + ObjectMeta: metav1.ObjectMeta{ |
| 149 | + Name: name, |
| 150 | + Namespace: testRunID, |
| 151 | + Labels: maps.Clone(testRunLabels), |
| 152 | + }, |
| 153 | + Spec: coordinationv1.LeaseSpec{ |
| 154 | + HolderIdentity: ptr.To(name), |
| 155 | + LeaseDurationSeconds: ptr.To[int32](10), |
| 156 | + AcquireTime: ptr.To(metav1.NewMicroTime(clock.Now().Add(-5 * time.Minute))), |
| 157 | + RenewTime: ptr.To(metav1.NewMicroTime(clock.Now().Add(-2 * time.Second))), |
| 158 | + }, |
| 159 | + } |
| 160 | + metav1.SetMetaDataLabel(&lease.ObjectMeta, shardingv1alpha1.LabelControllerRing, controllerRingName) |
| 161 | + |
| 162 | + return lease |
| 163 | +} |
| 164 | + |
| 165 | +func haveStatusShards(availableShards, shards int32) gomegatypes.GomegaMatcher { |
| 166 | + return And( |
| 167 | + HaveField("Status.AvailableShards", Equal(availableShards)), |
| 168 | + HaveField("Status.Shards", Equal(shards)), |
| 169 | + ) |
| 170 | +} |
0 commit comments