From d7e6cbb00358a1fb05c13757df2ce937a2452761 Mon Sep 17 00:00:00 2001 From: l1b0k Date: Sat, 21 Sep 2024 22:47:12 +0800 Subject: [PATCH] fix max ip count Signed-off-by: l1b0k --- pkg/controller/node/node.go | 5 +- pkg/controller/node/node_controller_test.go | 78 +++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/pkg/controller/node/node.go b/pkg/controller/node/node.go index 4424ae5b..067dee16 100644 --- a/pkg/controller/node/node.go +++ b/pkg/controller/node/node.go @@ -236,7 +236,10 @@ func (r *ReconcileNode) k8sAnno(ctx context.Context, k8sNode *corev1.Node, node lo.ForEach(node.Spec.Flavor, func(item networkv1beta1.Flavor, index int) { if item.NetworkInterfaceType == networkv1beta1.ENITypeSecondary && item.NetworkInterfaceTrafficMode == networkv1beta1.NetworkInterfaceTrafficModeStandard { - secondaryIP = item.Count * node.Spec.NodeCap.IPv4PerAdapter + secondaryIP += item.Count * node.Spec.NodeCap.IPv4PerAdapter + } + if item.NetworkInterfaceType == networkv1beta1.ENITypeTrunk { + secondaryIP += item.Count * node.Spec.NodeCap.IPv4PerAdapter } }) diff --git a/pkg/controller/node/node_controller_test.go b/pkg/controller/node/node_controller_test.go index b9113315..a473d11b 100644 --- a/pkg/controller/node/node_controller_test.go +++ b/pkg/controller/node/node_controller_test.go @@ -194,6 +194,84 @@ var _ = Describe("Node Controller", func() { Expect(k8sNode.Annotations["k8s.aliyun.com/trunk-on"]).To(Equal("eni-1")) Expect(k8sNode.Annotations["k8s.aliyun.com/max-available-ip"]).To(Equal("20")) }) + + It("should successfully create cr", func() { + resource := &networkv1beta1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: resourceName, + }, + Spec: networkv1beta1.NodeSpec{ + NodeMetadata: networkv1beta1.NodeMetadata{}, + NodeCap: networkv1beta1.NodeCap{}, + ENISpec: &networkv1beta1.ENISpec{ + Tag: nil, + TagFilter: nil, + VSwitchOptions: nil, + SecurityGroupIDs: nil, + ResourceGroupID: "", + EnableIPv4: true, + EnableIPv6: false, + EnableERDMA: false, + EnableTrunk: true, + VSwitchSelectPolicy: "", + }, + Pool: nil, + Flavor: []networkv1beta1.Flavor{ + { + NetworkInterfaceType: networkv1beta1.ENITypeSecondary, + NetworkInterfaceTrafficMode: networkv1beta1.NetworkInterfaceTrafficModeStandard, + Count: 2, + }, + { + NetworkInterfaceType: networkv1beta1.ENITypeTrunk, + NetworkInterfaceTrafficMode: networkv1beta1.NetworkInterfaceTrafficModeStandard, + Count: 1, + }, + }, + }, + } + + By("create cr") + err := k8sClient.Create(ctx, resource) + Expect(err).NotTo(HaveOccurred()) + + update := resource.DeepCopy() + _, err = controllerutil.CreateOrPatch(ctx, k8sClient, update, func() error { + update.Status = networkv1beta1.NodeStatus{ + NextSyncOpenAPITime: metav1.Time{}, + LastSyncOpenAPITime: metav1.Time{}, + NetworkInterfaces: map[string]*networkv1beta1.NetworkInterface{ + "eni-1": { + ID: "eni-1", + NetworkInterfaceType: networkv1beta1.ENITypeTrunk, + NetworkInterfaceTrafficMode: networkv1beta1.NetworkInterfaceTrafficModeStandard, + SecurityGroupIDs: []string{"ff"}, + Status: "InUse", + }, + }, + } + return nil + }) + Expect(err).NotTo(HaveOccurred()) + + By("Reconciling the created resource") + controllerReconciler := &ReconcileNode{ + client: k8sClient, + scheme: k8sClient.Scheme(), + aliyun: aliyun, + } + + _, err = controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: typeNamespacedName, + }) + Expect(err).NotTo(HaveOccurred()) + + k8sNode := &corev1.Node{} + err = k8sClient.Get(ctx, typeNamespacedName, k8sNode) + Expect(err).NotTo(HaveOccurred()) + Expect(k8sNode.Annotations["k8s.aliyun.com/trunk-on"]).To(Equal("eni-1")) + Expect(k8sNode.Annotations["k8s.aliyun.com/max-available-ip"]).To(Equal("30")) + }) }) })