From ea4cd5ee414a719d49b4d7985a542ed2b31422bd Mon Sep 17 00:00:00 2001 From: bobz965 Date: Thu, 30 Nov 2023 14:40:10 +0800 Subject: [PATCH] fix: redo in updates Signed-off-by: bobz965 --- pkg/controller/vpc_nat_gateway.go | 1 + pkg/controller/vpc_nat_gw_eip.go | 24 +++++----- pkg/controller/vpc_nat_gw_nat.go | 73 ++++++++++++++++++------------- 3 files changed, 55 insertions(+), 43 deletions(-) diff --git a/pkg/controller/vpc_nat_gateway.go b/pkg/controller/vpc_nat_gateway.go index ec91505cffd7..1229c57fc35e 100644 --- a/pkg/controller/vpc_nat_gateway.go +++ b/pkg/controller/vpc_nat_gateway.go @@ -1112,6 +1112,7 @@ func (c *Controller) initVpcNatGw() error { // the nat gw maybe deleted err := fmt.Errorf("failed to get nat gw %s pod: %v", gw.Name, err) klog.Error(err) + continue } if vpcGwName, isVpcNatGw := pod.Annotations[util.VpcNatGatewayAnnotation]; isVpcNatGw { if _, hasInit := pod.Annotations[util.VpcNatGatewayInitAnnotation]; hasInit { diff --git a/pkg/controller/vpc_nat_gw_eip.go b/pkg/controller/vpc_nat_gw_eip.go index e32b69759994..f8ba41d78d32 100644 --- a/pkg/controller/vpc_nat_gw_eip.go +++ b/pkg/controller/vpc_nat_gw_eip.go @@ -215,18 +215,6 @@ func (c *Controller) handleAddIptablesEip(key string) error { klog.Error(err) return err } - gwPod, err := c.getNatGwPod(cachedEip.Spec.NatGwDp) - if err != nil { - klog.Error(err) - return err - } - // compare gw pod started time with eip redo time. if redo time before gw pod started. redo again - eipRedo, _ := time.ParseInLocation("2006-01-02T15:04:05", cachedEip.Status.Redo, time.Local) - if cachedEip.Status.Ready && cachedEip.Status.IP != "" && gwPod.Status.ContainerStatuses[0].State.Running.StartedAt.Before(&metav1.Time{Time: eipRedo}) { - // already ok - klog.V(3).Infof("eip %s already ok", key) - return nil - } var v4ip, v6ip, mac, eipV4Cidr, v4Gw string externalNetwork := util.GetExternalNetwork(cachedEip.Spec.ExternalSubnet) externalProvider := fmt.Sprintf("%s.%s", externalNetwork, attachmentNs) @@ -374,6 +362,18 @@ func (c *Controller) handleUpdateIptablesEip(key string) error { cachedEip.Status.Redo != "" && cachedEip.Status.IP != "" && cachedEip.DeletionTimestamp.IsZero() { + gwPod, err := c.getNatGwPod(cachedEip.Spec.NatGwDp) + if err != nil { + klog.Error(err) + return err + } + // compare gw pod started time with eip redo time. if redo time before gw pod started. redo again + eipRedo, _ := time.ParseInLocation("2006-01-02T15:04:05", cachedEip.Status.Redo, time.Local) + if cachedEip.Status.Ready && cachedEip.Status.IP != "" && gwPod.Status.ContainerStatuses[0].State.Running.StartedAt.Before(&metav1.Time{Time: eipRedo}) { + // already ok + klog.V(3).Infof("eip %s already ok", key) + return nil + } eipV4Cidr, err := c.getEipV4Cidr(cachedEip.Status.IP, externalNetwork) if err != nil { klog.Errorf("failed to get eip or v4Cidr, %v", err) diff --git a/pkg/controller/vpc_nat_gw_nat.go b/pkg/controller/vpc_nat_gw_nat.go index e5e6d9940335..0c92567768f2 100644 --- a/pkg/controller/vpc_nat_gw_nat.go +++ b/pkg/controller/vpc_nat_gw_nat.go @@ -493,10 +493,10 @@ func (c *Controller) handleAddIptablesFip(key string) error { klog.Error(err) return err } - /*if fip.Status.Ready && fip.Status.V4ip != "" { + if fip.Status.Ready && fip.Status.V4ip != "" { // already ok return nil - }*/ + } klog.V(3).Infof("handle add fip %s", key) // get eip eipName := fip.Spec.EIP @@ -515,19 +515,6 @@ func (c *Controller) handleAddIptablesFip(key string) error { return err } - gwPod, err := c.getNatGwPod(eip.Spec.NatGwDp) - if err != nil { - klog.Error(err) - return err - } - // compare gw pod started time with fip redo time. if redo time before gw pod started. redo again - fipRedo, _ := time.ParseInLocation("2006-01-02T15:04:05", fip.Status.Redo, time.Local) - if fip.Status.Ready && fip.Status.V4ip != "" && gwPod.Status.ContainerStatuses[0].State.Running.StartedAt.Before(&metav1.Time{Time: fipRedo}) { - // already ok - klog.V(3).Infof("fip %s already ok", key) - return nil - } - // create fip nat if err = c.createFipInPod(eip.Spec.NatGwDp, eip.Status.IP, fip.Spec.InternalIP); err != nil { klog.Errorf("failed to create fip, %v", err) @@ -656,6 +643,18 @@ func (c *Controller) handleUpdateIptablesFip(key string) error { cachedFip.Status.V4ip != "" && cachedFip.DeletionTimestamp.IsZero() { klog.V(3).Infof("reapply fip '%s' in pod ", key) + gwPod, err := c.getNatGwPod(eip.Spec.NatGwDp) + if err != nil { + klog.Error(err) + return err + } + // compare gw pod started time with fip redo time. if fip redo time before gw pod started. should redo again + fipRedo, _ := time.ParseInLocation("2006-01-02T15:04:05", cachedFip.Status.Redo, time.Local) + if cachedFip.Status.Ready && cachedFip.Status.V4ip != "" && gwPod.Status.ContainerStatuses[0].State.Running.StartedAt.Before(&metav1.Time{Time: fipRedo}) { + // already ok + klog.V(3).Infof("fip %s already ok", key) + return nil + } if err = c.createFipInPod(eip.Spec.NatGwDp, cachedFip.Status.V4ip, cachedFip.Spec.InternalIP); err != nil { klog.Errorf("failed to create fip, %v", err) return err @@ -694,10 +693,10 @@ func (c *Controller) handleAddIptablesDnatRule(key string) error { klog.Error(err) return err } - /*if dnat.Status.Ready && dnat.Status.V4ip != "" { + if dnat.Status.Ready && dnat.Status.V4ip != "" { // already ok return nil - }*/ + } klog.V(3).Infof("handle add iptables dnat %s", key) eipName := dnat.Spec.EIP if eipName == "" { @@ -839,6 +838,18 @@ func (c *Controller) handleUpdateIptablesDnatRule(key string) error { cachedDnat.Status.V4ip != "" && cachedDnat.DeletionTimestamp.IsZero() { klog.V(3).Infof("reapply dnat in pod for %s", key) + gwPod, err := c.getNatGwPod(eip.Spec.NatGwDp) + if err != nil { + klog.Error(err) + return err + } + // compare gw pod started time with dnat redo time. if redo time before gw pod started. redo again + dnatRedo, _ := time.ParseInLocation("2006-01-02T15:04:05", cachedDnat.Status.Redo, time.Local) + if cachedDnat.Status.Ready && cachedDnat.Status.V4ip != "" && gwPod.Status.ContainerStatuses[0].State.Running.StartedAt.Before(&metav1.Time{Time: dnatRedo}) { + // already ok + klog.V(3).Infof("dnat %s already ok", key) + return nil + } if err = c.createDnatInPod(eip.Spec.NatGwDp, cachedDnat.Spec.Protocol, cachedDnat.Status.V4ip, cachedDnat.Spec.InternalIP, cachedDnat.Spec.ExternalPort, cachedDnat.Spec.InternalPort); err != nil { @@ -879,10 +890,10 @@ func (c *Controller) handleAddIptablesSnatRule(key string) error { klog.Error(err) return err } - /*if snat.Status.Ready && snat.Status.V4ip != "" { + if snat.Status.Ready && snat.Status.V4ip != "" { // already ok return nil - }*/ + } klog.V(3).Infof("handle add iptables snat %s", key) eipName := snat.Spec.EIP if eipName == "" { @@ -894,18 +905,6 @@ func (c *Controller) handleAddIptablesSnatRule(key string) error { klog.Errorf("failed to get eip, %v", err) return err } - gwPod, err := c.getNatGwPod(eip.Spec.NatGwDp) - if err != nil { - klog.Error(err) - return err - } - // compare gw pod started time with snat redo time. if redo time before gw pod started. redo again - snatRedo, _ := time.ParseInLocation("2006-01-02T15:04:05", snat.Status.Redo, time.Local) - if snat.Status.Ready && snat.Status.V4ip != "" && gwPod.Status.ContainerStatuses[0].State.Running.StartedAt.Before(&metav1.Time{Time: snatRedo}) { - // already ok - klog.V(3).Infof("snat %s already ok", key) - return nil - } // create snat v4Cidr, _ := util.SplitStringIP(snat.Spec.InternalCIDR) if v4Cidr == "" { @@ -1023,6 +1022,18 @@ func (c *Controller) handleUpdateIptablesSnatRule(key string) error { cachedSnat.Status.Redo != "" && cachedSnat.Status.V4ip != "" && cachedSnat.DeletionTimestamp.IsZero() { + gwPod, err := c.getNatGwPod(eip.Spec.NatGwDp) + if err != nil { + klog.Error(err) + return err + } + // compare gw pod started time with snat redo time. if redo time before gw pod started. redo again + snatRedo, _ := time.ParseInLocation("2006-01-02T15:04:05", cachedSnat.Status.Redo, time.Local) + if cachedSnat.Status.Ready && cachedSnat.Status.V4ip != "" && gwPod.Status.ContainerStatuses[0].State.Running.StartedAt.Before(&metav1.Time{Time: snatRedo}) { + // already ok + klog.V(3).Infof("snat %s already ok", key) + return nil + } if err = c.createSnatInPod(cachedSnat.Status.NatGwDp, cachedSnat.Status.V4ip, v4CidrSpec); err != nil { klog.Errorf("failed to create new snat, %v", err) return err