Skip to content

Commit eded5ec

Browse files
dkoshkinjimmidyson
authored andcommitted
fix: use Pods CIDR from the Cluster spec
1 parent e6fb22f commit eded5ec

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

pkg/handlers/cni/calico/handler.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,15 @@ func generateProviderCNICRS(
283283
},
284284
)
285285

286-
podSubnet, podSubnetSpecified := cluster.GetAnnotations()[cni.PodSubnetAnnotationKey]
286+
podSubnet, err := cni.PodCIDR(cluster)
287+
if err != nil {
288+
return nil, err
289+
}
287290

288291
var b bytes.Buffer
289292

290293
for _, o := range parsed {
291-
if podSubnetSpecified &&
292-
podSubnet != "" &&
294+
if podSubnet != "" &&
293295
o.GetObjectKind().GroupVersionKind().GroupKind() == calicoInstallationGK {
294296
obj := o.(*unstructured.Unstructured).Object
295297

pkg/handlers/cni/cluster.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cni
5+
6+
import (
7+
"errors"
8+
9+
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
10+
)
11+
12+
var ErrMultiplePodsCIDRBlocks = errors.New("cluster has more than 1 Pods network CIDR blocks")
13+
14+
// PodCIDR will return the Pods network CIDR.
15+
// If not set returns an empty string.
16+
// If more than 1 CIDRBlocks is defined will return an error.
17+
func PodCIDR(cluster *capiv1.Cluster) (string, error) {
18+
var subnets []string
19+
if cluster.Spec.ClusterNetwork != nil &&
20+
cluster.Spec.ClusterNetwork.Pods != nil {
21+
subnets = cluster.Spec.ClusterNetwork.Pods.CIDRBlocks
22+
}
23+
switch {
24+
case len(subnets) == 1:
25+
return cluster.Spec.ClusterNetwork.Pods.CIDRBlocks[0], nil
26+
case len(subnets) > 1:
27+
return "", ErrMultiplePodsCIDRBlocks
28+
default:
29+
return "", nil
30+
}
31+
}

pkg/handlers/cni/cluster_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cni
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"sigs.k8s.io/cluster-api/api/v1beta1"
11+
)
12+
13+
func Test_PodCIDR(t *testing.T) {
14+
t.Parallel()
15+
16+
tests := []struct {
17+
name string
18+
cluster *v1beta1.Cluster
19+
wantCIDR string
20+
wantErr error
21+
}{
22+
{
23+
name: "no Pods CIDR set",
24+
cluster: &v1beta1.Cluster{
25+
Spec: v1beta1.ClusterSpec{},
26+
},
27+
},
28+
{
29+
name: "no Pods CIDR set, but Services CIDR is set",
30+
cluster: &v1beta1.Cluster{
31+
Spec: v1beta1.ClusterSpec{
32+
ClusterNetwork: &v1beta1.ClusterNetwork{
33+
Services: &v1beta1.NetworkRanges{
34+
CIDRBlocks: []string{"192.168.0.1/16"},
35+
},
36+
},
37+
},
38+
},
39+
},
40+
{
41+
name: "Pods CIDR set",
42+
cluster: &v1beta1.Cluster{
43+
Spec: v1beta1.ClusterSpec{
44+
ClusterNetwork: &v1beta1.ClusterNetwork{
45+
Pods: &v1beta1.NetworkRanges{
46+
CIDRBlocks: []string{"192.168.0.1/16"},
47+
},
48+
},
49+
},
50+
},
51+
wantCIDR: "192.168.0.1/16",
52+
},
53+
{
54+
name: "error: multiple Pods CIDRs set",
55+
cluster: &v1beta1.Cluster{
56+
Spec: v1beta1.ClusterSpec{
57+
ClusterNetwork: &v1beta1.ClusterNetwork{
58+
Pods: &v1beta1.NetworkRanges{
59+
CIDRBlocks: []string{"192.168.0.1/16", "10.0.0.1/16"},
60+
},
61+
},
62+
},
63+
},
64+
wantErr: ErrMultiplePodsCIDRBlocks,
65+
},
66+
}
67+
for idx := range tests {
68+
tt := tests[idx]
69+
t.Run(tt.name, func(t *testing.T) {
70+
t.Parallel()
71+
cidr, err := PodCIDR(tt.cluster)
72+
assert.ErrorIs(t, err, tt.wantErr)
73+
assert.Equal(t, tt.wantCIDR, cidr)
74+
})
75+
}
76+
}

pkg/handlers/cni/constants.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ import "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers"
77

88
const (
99
CNIProviderLabelKey = handlers.MetadataDomain + "/cni"
10-
11-
PodSubnetAnnotationKey = handlers.MetadataDomain + "/pod-subnet"
1210
)

0 commit comments

Comments
 (0)