Skip to content

Commit b25ee84

Browse files
committed
Allow IPPool to have Prefix /0
1 parent a48a815 commit b25ee84

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

internal/poolutil/pool_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ import (
2828
)
2929

3030
var _ = Describe("PoolSpecToIPSet", func() {
31+
It("should allow a pool spec with prefix /0", func() {
32+
spec := &v1alpha2.InClusterIPPoolSpec{
33+
Gateway: "192.168.0.1",
34+
Prefix: 0,
35+
Addresses: []string{
36+
"192.168.0.3-192.168.0.10",
37+
},
38+
}
39+
ipSet, err := PoolSpecToIPSet(spec)
40+
Expect(err).ToNot(HaveOccurred())
41+
Expect(ipSet.Contains(mustParse("192.168.0.3"))).To(BeTrue())
42+
Expect(ipSet.Contains(mustParse("192.168.0.3"))).To(BeTrue())
43+
Expect(ipSet.Contains(mustParse("192.168.0.5"))).To(BeTrue())
44+
Expect(ipSet.Contains(mustParse("192.168.0.6"))).To(BeTrue())
45+
Expect(ipSet.Contains(mustParse("192.168.0.7"))).To(BeTrue())
46+
Expect(IPSetCount(ipSet)).To(Equal(8))
47+
})
3148
It("converts a pool spec to a set", func() {
3249
spec := &v1alpha2.InClusterIPPoolSpec{
3350
Gateway: "192.168.0.1",

internal/webhooks/inclusterippool.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,6 @@ func (webhook *InClusterIPPool) validate(_, newPool types.GenericInClusterPool)
185185
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "addresses"), newPool.PoolSpec().Addresses, "addresses is required"))
186186
}
187187

188-
if newPool.PoolSpec().Prefix == 0 {
189-
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "prefix"), newPool.PoolSpec().Prefix, "a valid prefix is required"))
190-
}
191-
192188
var hasIPv4Addr, hasIPv6Addr bool
193189
for _, address := range newPool.PoolSpec().Addresses {
194190
ipSet, err := poolutil.AddressToIPSet(address)

internal/webhooks/inclusterippool_test.go

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -418,17 +418,6 @@ func TestInvalidScenarios(t *testing.T) {
418418
},
419419
expectedError: "provided address is not a valid IP, range, nor CIDR",
420420
},
421-
{
422-
testcase: "omitting a prefix should not be allowed",
423-
spec: v1alpha2.InClusterIPPoolSpec{
424-
Addresses: []string{
425-
"10.0.0.25",
426-
"10.0.0.26",
427-
},
428-
Gateway: "10.0.0.1",
429-
},
430-
expectedError: "a valid prefix is required",
431-
},
432421
{
433422
testcase: "specifying an invalid prefix",
434423
spec: v1alpha2.InClusterIPPoolSpec{
@@ -648,6 +637,63 @@ func TestInvalidScenarios(t *testing.T) {
648637
}
649638
}
650639

640+
func TestIPPool_Prefix(t *testing.T) {
641+
g := NewWithT(t)
642+
643+
scheme := runtime.NewScheme()
644+
g.Expect(ipamv1.AddToScheme(scheme)).To(Succeed())
645+
646+
namespacedPool := &v1alpha2.InClusterIPPool{
647+
ObjectMeta: metav1.ObjectMeta{
648+
Name: "my-pool",
649+
},
650+
Spec: v1alpha2.InClusterIPPoolSpec{
651+
Addresses: []string{"10.0.0.10-10.0.0.20"},
652+
Prefix: 0,
653+
Gateway: "10.0.0.1",
654+
},
655+
}
656+
657+
globalPool := &v1alpha2.GlobalInClusterIPPool{
658+
ObjectMeta: metav1.ObjectMeta{
659+
Name: "my-pool",
660+
},
661+
Spec: v1alpha2.InClusterIPPoolSpec{
662+
Addresses: []string{"10.0.0.10-10.0.0.20"},
663+
Prefix: 0,
664+
Gateway: "10.0.0.1",
665+
},
666+
}
667+
emptyPrefixPool := &v1alpha2.InClusterIPPool{
668+
ObjectMeta: metav1.ObjectMeta{
669+
Name: "my-pool",
670+
},
671+
Spec: v1alpha2.InClusterIPPoolSpec{
672+
Addresses: []string{"10.0.0.10-10.0.0.20"},
673+
Gateway: "10.0.0.1",
674+
},
675+
}
676+
677+
ips := []client.Object{
678+
createIP("my-ip", "10.0.0.10", namespacedPool),
679+
createIP("my-ip-2", "10.0.0.10", globalPool),
680+
}
681+
682+
fakeClient := fake.NewClientBuilder().
683+
WithScheme(scheme).
684+
WithObjects(ips...).
685+
WithIndex(&ipamv1.IPAddress{}, index.IPAddressPoolRefCombinedField, index.IPAddressByCombinedPoolRef).
686+
Build()
687+
688+
webhook := InClusterIPPool{
689+
Client: fakeClient,
690+
}
691+
692+
g.Expect(webhook.validate(&v1alpha2.InClusterIPPool{}, namespacedPool)).Error().To(BeNil(), "should allow /0 prefix InClusterIPPool")
693+
g.Expect(webhook.validate(&v1alpha2.GlobalInClusterIPPool{}, globalPool)).Error().To(BeNil(), "should allow /0 prefix GlobalInClusterIPPool")
694+
g.Expect(webhook.validate(&v1alpha2.InClusterIPPool{}, emptyPrefixPool)).Error().To(BeNil(), "should allow empty prefix InClusterIPPool")
695+
}
696+
651697
func runInvalidScenarioTests(t *testing.T, tt invalidScenarioTest, pool types.GenericInClusterPool, webhook InClusterIPPool) {
652698
t.Helper()
653699
t.Run(tt.testcase, func(t *testing.T) {

0 commit comments

Comments
 (0)