|
4 | 4 | package metal |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "net" |
| 8 | + |
7 | 9 | metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1" |
8 | 10 | . "github.com/onsi/ginkgo/v2" |
9 | 11 | . "github.com/onsi/gomega" |
@@ -116,4 +118,105 @@ var _ = Describe("NodeReconciler", func() { |
116 | 118 | Eventually(Object(serverClaim)).Should(HaveField("Labels", HaveKeyWithValue(metalv1alpha1.ServerMaintenanceApprovalKey, TrueStr))) |
117 | 119 | }) |
118 | 120 |
|
| 121 | + Context("PodCIDR assignment", func() { |
| 122 | + BeforeEach(func() { |
| 123 | + PodPrefixSize = 24 |
| 124 | + }) |
| 125 | + |
| 126 | + AfterEach(func() { |
| 127 | + PodPrefixSize = 0 |
| 128 | + }) |
| 129 | + |
| 130 | + It("should assign PodCIDR to a node with NodeInternalIP", func(ctx SpecContext) { |
| 131 | + By("Setting the NodeInternalIP address on the node") |
| 132 | + Eventually(UpdateStatus(node, func() { |
| 133 | + node.Status.Addresses = append(node.Status.Addresses, corev1.NodeAddress{ |
| 134 | + Type: corev1.NodeInternalIP, |
| 135 | + Address: "10.0.5.42", |
| 136 | + }) |
| 137 | + })).Should(Succeed()) |
| 138 | + |
| 139 | + By("Verifying PodCIDR is assigned to the node") |
| 140 | + Eventually(Object(node)).Should(HaveField("Spec.PodCIDR", "10.0.5.0/24")) |
| 141 | + Eventually(Object(node)).Should(HaveField("Spec.PodCIDRs", ContainElement("10.0.5.0/24"))) |
| 142 | + }) |
| 143 | + |
| 144 | + It("should not overwrite existing PodCIDR", func(ctx SpecContext) { |
| 145 | + By("Setting the PodCIDR on the node") |
| 146 | + originalNode := node.DeepCopy() |
| 147 | + node.Spec.PodCIDR = "192.168.0.0/24" |
| 148 | + Expect(k8sClient.Patch(ctx, node, client.MergeFrom(originalNode))).To(Succeed()) |
| 149 | + |
| 150 | + By("Setting the NodeInternalIP address on the node") |
| 151 | + Eventually(UpdateStatus(node, func() { |
| 152 | + node.Status.Addresses = append(node.Status.Addresses, corev1.NodeAddress{ |
| 153 | + Type: corev1.NodeInternalIP, |
| 154 | + Address: "10.0.5.42", |
| 155 | + }) |
| 156 | + })).Should(Succeed()) |
| 157 | + |
| 158 | + By("Verifying PodCIDR was not overwritten") |
| 159 | + Consistently(Object(node)).Should(HaveField("Spec.PodCIDR", "192.168.0.0/24")) |
| 160 | + }) |
| 161 | + |
| 162 | + It("should not assign PodCIDR if node has no NodeInternalIP", func(ctx SpecContext) { |
| 163 | + By("Triggering reconciliation by patching the claim") |
| 164 | + originalServerClaim := serverClaim.DeepCopy() |
| 165 | + serverClaim.Labels = map[string]string{ |
| 166 | + metalv1alpha1.ServerMaintenanceNeededLabelKey: TrueStr, |
| 167 | + } |
| 168 | + Expect(k8sClient.Patch(ctx, serverClaim, client.MergeFrom(originalServerClaim))).To(Succeed()) |
| 169 | + |
| 170 | + By("Verifying PodCIDR remains empty") |
| 171 | + Consistently(Object(node)).Should(HaveField("Spec.PodCIDR", "")) |
| 172 | + }) |
| 173 | + |
| 174 | + It("should not assign PodCIDR if PodPrefixSize is disabled", func(ctx SpecContext) { |
| 175 | + By("Disabling PodCIDR assignment") |
| 176 | + PodPrefixSize = 0 |
| 177 | + |
| 178 | + By("Setting the NodeInternalIP address on the node") |
| 179 | + Eventually(UpdateStatus(node, func() { |
| 180 | + node.Status.Addresses = append(node.Status.Addresses, corev1.NodeAddress{ |
| 181 | + Type: corev1.NodeInternalIP, |
| 182 | + Address: "10.0.5.42", |
| 183 | + }) |
| 184 | + })).Should(Succeed()) |
| 185 | + |
| 186 | + By("Triggering reconciliation by patching the claim") |
| 187 | + originalServerClaim := serverClaim.DeepCopy() |
| 188 | + serverClaim.Labels = map[string]string{ |
| 189 | + metalv1alpha1.ServerMaintenanceNeededLabelKey: TrueStr, |
| 190 | + } |
| 191 | + Expect(k8sClient.Patch(ctx, serverClaim, client.MergeFrom(originalServerClaim))).To(Succeed()) |
| 192 | + |
| 193 | + By("Verifying PodCIDR remains empty despite having NodeInternalIP") |
| 194 | + Consistently(Object(node)).Should(HaveField("Spec.PodCIDR", "")) |
| 195 | + }) |
| 196 | + }) |
| 197 | +}) |
| 198 | + |
| 199 | +var _ = Describe("zeroHostBits", func() { |
| 200 | + DescribeTable("should correctly mask IPv4 addresses", |
| 201 | + func(ip string, maskSize int, expected string) { |
| 202 | + result := zeroHostBits(net.ParseIP(ip), maskSize) |
| 203 | + Expect(result.String()).To(Equal(expected)) |
| 204 | + }, |
| 205 | + Entry("mask /24", "10.0.5.42", 24, "10.0.5.0"), |
| 206 | + Entry("mask /16", "10.0.5.42", 16, "10.0.0.0"), |
| 207 | + Entry("mask /8", "10.20.30.40", 8, "10.0.0.0"), |
| 208 | + Entry("mask /32", "10.0.5.42", 32, "10.0.5.42"), |
| 209 | + Entry("mask /0", "10.0.5.42", 0, "0.0.0.0"), |
| 210 | + ) |
| 211 | + |
| 212 | + DescribeTable("should correctly mask IPv6 addresses", |
| 213 | + func(ip string, maskSize int, expected string) { |
| 214 | + result := zeroHostBits(net.ParseIP(ip), maskSize) |
| 215 | + Expect(result.String()).To(Equal(expected)) |
| 216 | + }, |
| 217 | + Entry("mask /64", "2001:db8::1", 64, "2001:db8::"), |
| 218 | + Entry("mask /48", "2001:db8:1234:5678::1", 48, "2001:db8:1234::"), |
| 219 | + Entry("mask /128", "2001:db8::1", 128, "2001:db8::1"), |
| 220 | + Entry("mask /0", "2001:db8::1", 0, "::"), |
| 221 | + ) |
119 | 222 | }) |
0 commit comments