Skip to content

Commit b446a0e

Browse files
fix: overlay should check NNC label (#1707)
* fix: overlay should check NNC label * Handle more Overlay CNI edge cases - GetSubnet will retry on errors. - Subnet CIDR can be read from address prefix or address prefixes. - Extra unit tests. --------- Co-authored-by: Sean Jeffrey <seanjeffrey@microsoft.com>
1 parent e98e2c3 commit b446a0e

File tree

3 files changed

+96
-13
lines changed

3 files changed

+96
-13
lines changed

pkg/azure/client.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,15 @@ func (az *azClient) ApplyRouteTable(subnetID string, routeTableID string) error
296296
return nil
297297
}
298298

299-
func (az *azClient) GetSubnet(subnetID string) (n.Subnet, error) {
300-
_, subnetResourceGroup, subnetVnetName, subnetName := ParseSubResourceID(subnetID)
301-
subnet, err := az.subnetsClient.Get(az.ctx, string(subnetResourceGroup), string(subnetVnetName), string(subnetName), "")
302-
return subnet, err
299+
func (az *azClient) GetSubnet(subnetID string) (subnet n.Subnet, err error) {
300+
_ = utils.Retry(retryCount, retryPause,
301+
func() (utils.Retriable, error) {
302+
_, subnetResourceGroup, subnetVnetName, subnetName := ParseSubResourceID(subnetID)
303+
subnet, err = az.subnetsClient.Get(az.ctx, string(subnetResourceGroup), string(subnetVnetName), string(subnetName), "")
304+
return utils.Retriable(true), err
305+
})
306+
307+
return
303308
}
304309

305310
// DeployGatewayWithVnet creates Application Gateway within the specifid VNet. Implements AzClient interface.

pkg/cni/overlay.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
overlayextensionconfig_v1alpha1 "github.com/Azure/azure-container-networking/crd/overlayextensionconfig/api/v1alpha1"
1010
"github.com/pkg/errors"
1111
apierrors "k8s.io/apimachinery/pkg/api/errors"
12-
meta "k8s.io/apimachinery/pkg/api/meta"
12+
"k8s.io/apimachinery/pkg/api/meta"
1313
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414
"k8s.io/klog/v2"
1515
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -22,6 +22,9 @@ const (
2222
)
2323

2424
const (
25+
// PodNetworkTypeLabel is the name of the label on NNCs to tell what mode the network is in.
26+
PodNetworkTypeLabel = "kubernetes.azure.com/podnetwork-type"
27+
2528
// OverlayExtensionConfigName is the name of the overlay extension config resource
2629
OverlayExtensionConfigName = "agic-overlay-extension-config"
2730

@@ -52,7 +55,16 @@ func (r *Reconciler) reconcileOverlayCniIfNeeded(ctx context.Context, subnetID s
5255
return errors.Wrap(err, "failed to get subnet")
5356
}
5457

55-
subnetCIDR := *subnet.AddressPrefix
58+
var subnetCIDR string
59+
if subnet.AddressPrefix != nil {
60+
subnetCIDR = *subnet.AddressPrefix
61+
} else if subnet.AddressPrefixes != nil && len(*subnet.AddressPrefixes) > 0 {
62+
subnetCIDR = (*subnet.AddressPrefixes)[0]
63+
} else {
64+
return errors.New("subnet does not have an address prefix(es)")
65+
}
66+
67+
klog.Infof("Using subnet prefix %q", subnetCIDR)
5668
err = r.reconcileOverlayExtensionConfig(ctx, subnetCIDR)
5769
if err != nil {
5870
return errors.Wrap(err, "failed to reconcile overlay resources")
@@ -72,7 +84,13 @@ func (r *Reconciler) isClusterOverlayCNI(ctx context.Context) (bool, error) {
7284
return false, errors.Wrap(err, "failed to list node network configs")
7385
}
7486

75-
return len(nodeNetworkConfigs.Items) > 0, nil
87+
// if any NNCs are overlay then this cluster is using CNI Overlay
88+
for _, nnc := range nodeNetworkConfigs.Items {
89+
if val, ok := nnc.Labels[PodNetworkTypeLabel]; ok && val == "overlay" {
90+
return true, nil
91+
}
92+
}
93+
return false, nil
7694
}
7795

7896
func (r *Reconciler) reconcileOverlayExtensionConfig(ctx context.Context, subnetCIDR string) error {

pkg/cni/overlay_test.go

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,36 @@ var _ = Describe("Overlay CNI", func() {
9292
}, appGw, agicPod, namespace, false)
9393
})
9494

95+
Context("NodeNetworkConfigList does not have the overlay label", func() {
96+
It("returns nil error", func() {
97+
// Create NodeNetworkConfigList so that cluster is not considered as overlay CNI
98+
config := &nodenetworkconfig_v1alpha.NodeNetworkConfig{
99+
ObjectMeta: metav1.ObjectMeta{
100+
Name: "test-node-network-config",
101+
Namespace: namespace,
102+
Labels: map[string]string{
103+
cni.PodNetworkTypeLabel: "not overlay",
104+
},
105+
},
106+
Spec: nodenetworkconfig_v1alpha.NodeNetworkConfigSpec{},
107+
}
108+
Expect(k8sClient.Create(ctx, config)).To(BeNil())
109+
Expect(reconciler.Reconcile(ctx)).To(BeNil())
110+
})
111+
})
112+
95113
Context("Handle Overlay CNI cluster", func() {
96114
BeforeEach(func() {
97115
// Create NodeNetworkConfig so that cluster is considered as overlay CNI
98116
config := &nodenetworkconfig_v1alpha.NodeNetworkConfig{
99-
ObjectMeta: metav1.ObjectMeta{Name: "test-node-network-config", Namespace: namespace},
100-
Spec: nodenetworkconfig_v1alpha.NodeNetworkConfigSpec{},
117+
ObjectMeta: metav1.ObjectMeta{
118+
Name: "test-node-network-config",
119+
Namespace: namespace,
120+
Labels: map[string]string{
121+
cni.PodNetworkTypeLabel: "overlay",
122+
},
123+
},
124+
Spec: nodenetworkconfig_v1alpha.NodeNetworkConfigSpec{},
101125
}
102126
Expect(k8sClient.Create(ctx, config)).To(BeNil())
103127

@@ -149,6 +173,38 @@ var _ = Describe("Overlay CNI", func() {
149173
Expect(err.Error()).To(ContainSubstring("failed to get subnet"))
150174
})
151175

176+
It("should return error if subnet does not have an address prefix or address prefixes", func() {
177+
azClient.GetSubnetFunc = func(subnetID string) (n.Subnet, error) {
178+
return n.Subnet{
179+
SubnetPropertiesFormat: &n.SubnetPropertiesFormat{
180+
AddressPrefix: nil,
181+
AddressPrefixes: &[]string{},
182+
}}, nil
183+
}
184+
185+
err := reconciler.Reconcile(ctx)
186+
Expect(err).To(Not(BeNil()))
187+
Expect(err.Error()).To(ContainSubstring("subnet does not have an address prefix(es)"))
188+
})
189+
190+
It("should not return error if subnet has an entry in address prefixes", func() {
191+
azClient.GetSubnetFunc = func(subnetID string) (n.Subnet, error) {
192+
return n.Subnet{
193+
SubnetPropertiesFormat: &n.SubnetPropertiesFormat{
194+
AddressPrefix: nil,
195+
AddressPrefixes: &[]string{"CIDR1"},
196+
}}, nil
197+
}
198+
199+
err := reconciler.Reconcile(ctx)
200+
Expect(err).To(Not(HaveOccurred()))
201+
202+
var config overlayextensionconfig_v1alpha1.OverlayExtensionConfig
203+
Expect(k8sClient.Get(ctx, ctrl_client.ObjectKey{Name: cni.OverlayExtensionConfigName, Namespace: namespace}, &config)).To(BeNil())
204+
Expect(config.Labels).To(HaveLen(1))
205+
Expect(config.Spec.ExtensionIPRange).To(Equal("CIDR1"))
206+
})
207+
152208
It("should delete and recreate OEC if subnet CIDR changes", func() {
153209
// Create OverlayExtensionConfig
154210
Expect(k8sClient.Create(ctx, &overlayextensionconfig_v1alpha1.OverlayExtensionConfig{
@@ -197,8 +253,10 @@ var _ = Describe("Overlay CNI", func() {
197253

198254
// Create NodeNetworkConfig so that cluster is considered as overlay CNI
199255
Expect(k8sClient.Create(ctx, &nodenetworkconfig_v1alpha.NodeNetworkConfig{
200-
ObjectMeta: metav1.ObjectMeta{Name: "test-node-network-config", Namespace: namespace},
201-
Spec: nodenetworkconfig_v1alpha.NodeNetworkConfigSpec{},
256+
ObjectMeta: metav1.ObjectMeta{Name: "test-node-network-config", Namespace: namespace, Labels: map[string]string{
257+
cni.PodNetworkTypeLabel: "overlay",
258+
}},
259+
Spec: nodenetworkconfig_v1alpha.NodeNetworkConfigSpec{},
202260
})).To(BeNil())
203261

204262
Expect(reconciler.Reconcile(ctx)).To(BeNil())
@@ -216,8 +274,10 @@ var _ = Describe("Overlay CNI", func() {
216274

217275
// Create NodeNetworkConfig so that cluster is considered as overlay CNI
218276
Expect(k8sClient.Create(ctx, &nodenetworkconfig_v1alpha.NodeNetworkConfig{
219-
ObjectMeta: metav1.ObjectMeta{Name: "test-node-network-config", Namespace: namespace},
220-
Spec: nodenetworkconfig_v1alpha.NodeNetworkConfigSpec{},
277+
ObjectMeta: metav1.ObjectMeta{Name: "test-node-network-config", Namespace: namespace, Labels: map[string]string{
278+
cni.PodNetworkTypeLabel: "overlay",
279+
}},
280+
Spec: nodenetworkconfig_v1alpha.NodeNetworkConfigSpec{},
221281
})).To(BeNil())
222282

223283
go func() {

0 commit comments

Comments
 (0)