Skip to content

Commit

Permalink
fix: redo in updates
Browse files Browse the repository at this point in the history
Signed-off-by: bobz965 <[email protected]>
  • Loading branch information
bobz965 committed Nov 30, 2023
1 parent b54535b commit ea4cd5e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 43 deletions.
1 change: 1 addition & 0 deletions pkg/controller/vpc_nat_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 12 additions & 12 deletions pkg/controller/vpc_nat_gw_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
73 changes: 42 additions & 31 deletions pkg/controller/vpc_nat_gw_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -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 == "" {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ea4cd5e

Please sign in to comment.