Skip to content

Commit

Permalink
fix: ovn snat and fip delete (#2584)
Browse files Browse the repository at this point in the history
* fix: ovn snat and fip delete
---------

Co-authored-by: yl4811 <yl4811@yealink.com>
  • Loading branch information
ShaPoHun and yl4811 committed Apr 3, 2023
1 parent 048e931 commit da323a5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
13 changes: 11 additions & 2 deletions pkg/controller/ovn_dnat.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ func (c *Controller) handleAddOvnDnatRule(key string) error {
return err
}

if err = c.patchOvnDnatStatus(key, vpcName, cachedEip.Status.V4Ip,
internalV4Ip, mac, false); err != nil {
klog.Errorf("failed to patch status for dnat %s, %v", key, err)
return err
}

if err = c.handleAddOvnEipFinalizer(cachedEip, util.OvnDnatUseEipFinalizer); err != nil {
klog.Errorf("failed to add finalizer for ovn eip, %v", err)
return err
Expand Down Expand Up @@ -309,7 +315,7 @@ func (c *Controller) handleDelOvnDnatRule(key string) error {
return err
}

if cachedDnat.Status.Ready {
if cachedDnat.Status.Vpc != "" && cachedDnat.Status.V4Eip != "" && cachedDnat.Status.ExternalPort != "" {
if err = c.DelDnatRule(cachedDnat.Status.Vpc, cachedDnat.Name,
cachedDnat.Status.V4Eip, cachedDnat.Status.ExternalPort); err != nil {
klog.Errorf("failed to delete dnat, %v", err)
Expand Down Expand Up @@ -511,7 +517,10 @@ func (c *Controller) patchOvnDnatStatus(key, vpcName, v4Eip, podIp, podMac strin
changed = true
}

if ready && v4Eip != "" && dnat.Status.V4Eip != v4Eip {
if (v4Eip != "" && dnat.Status.V4Eip != v4Eip) ||
(vpcName != "" && dnat.Status.Vpc != vpcName) ||
(podIp != "" && dnat.Status.V4Ip != podIp) ||
(podMac != "" && dnat.Status.MacAddress != podMac) {
dnat.Status.Vpc = vpcName
dnat.Status.V4Eip = v4Eip
dnat.Status.V4Ip = podIp
Expand Down
18 changes: 14 additions & 4 deletions pkg/controller/ovn_fip.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ func (c *Controller) handleAddOvnFip(key string) error {
err = fmt.Errorf("failed to create fip %s, eip '%s' is using by other fip %s", key, eipName, cachedEip.Annotations[util.VpcNatAnnotation])
return err
}
if err = c.patchOvnFipStatus(key, vpcName, cachedEip.Status.V4Ip,
internalV4Ip, mac, false); err != nil {
klog.Errorf("failed to patch status for fip %s, %v", key, err)
return err
}
if err = c.handleAddOvnEipFinalizer(cachedEip, util.OvnFipUseEipFinalizer); err != nil {
klog.Errorf("failed to add finalizer for ovn eip, %v", err)
return err
Expand Down Expand Up @@ -384,9 +389,11 @@ func (c *Controller) handleDelOvnFip(key string) error {
return err
}
// ovn delete fip
if err = c.ovnLegacyClient.DeleteFipRule(cachedFip.Status.Vpc, cachedFip.Status.V4Eip, cachedFip.Status.V4Ip); err != nil {
klog.Errorf("failed to create fip, %v", err)
return err
if cachedFip.Status.Vpc != "" && cachedFip.Status.V4Eip != "" && cachedFip.Status.V4Ip != "" {
if err = c.ovnLegacyClient.DeleteFipRule(cachedFip.Status.Vpc, cachedFip.Status.V4Eip, cachedFip.Status.V4Ip); err != nil {
klog.Errorf("failed to delete fip, %v", err)
return err
}
}
if err = c.handleDelOvnFipFinalizer(cachedFip); err != nil {
klog.Errorf("failed to handle remove finalizer from ovn fip, %v", err)
Expand Down Expand Up @@ -495,7 +502,10 @@ func (c *Controller) patchOvnFipStatus(key, vpcName, v4Eip, podIp, podMac string
fip.Status.Ready = ready
changed = true
}
if ready && v4Eip != "" && fip.Status.V4Eip != v4Eip {
if (v4Eip != "" && fip.Status.V4Eip != v4Eip) ||
(vpcName != "" && fip.Status.Vpc != vpcName) ||
(podIp != "" && fip.Status.V4Ip != podIp) ||
(podMac != "" && fip.Status.MacAddress != podMac) {
fip.Status.Vpc = vpcName
fip.Status.V4Eip = v4Eip
fip.Status.V4Ip = podIp
Expand Down
18 changes: 14 additions & 4 deletions pkg/controller/ovn_snat.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ func (c *Controller) handleAddOvnSnatRule(key string) error {
err = fmt.Errorf("failed to get v4 internal ip for snat %s", key)
return err
}

if err = c.patchOvnSnatStatus(key, vpcName, cachedEip.Spec.V4Ip, v4IpCidr, false); err != nil {
klog.Errorf("failed to update status for snat %s, %v", key, err)
return err
}

// create snat
if err = c.handleAddOvnSnatRuleFinalizer(cachedSnat); err != nil {
klog.Errorf("failed to add finalizer for ovn snat, %v", err)
Expand Down Expand Up @@ -275,9 +281,11 @@ func (c *Controller) handleUpdateOvnSnatRule(key string) error {
if !cachedSnat.DeletionTimestamp.IsZero() {
klog.V(3).Infof("ovn clean snat %s", key)
// ovn delete snat
if err = c.ovnLegacyClient.DeleteSnatRule(cachedSnat.Status.Vpc, cachedEip.Spec.V4Ip, cachedSnat.Status.V4IpCidr); err != nil {
klog.Errorf("failed to delte snat, %v", err)
return err
if cachedSnat.Status.Vpc != "" && cachedSnat.Status.V4Eip != "" && cachedSnat.Status.V4IpCidr != "" {
if err = c.ovnLegacyClient.DeleteSnatRule(cachedSnat.Status.Vpc, cachedSnat.Status.V4Eip, cachedSnat.Status.V4IpCidr); err != nil {
klog.Errorf("failed to delete snat, %v", err)
return err
}
}
// reset eip
c.resetOvnEipQueue.Add(cachedSnat.Spec.OvnEip)
Expand Down Expand Up @@ -420,7 +428,9 @@ func (c *Controller) patchOvnSnatStatus(key, vpc, v4Eip, v4IpCidr string, ready
snat.Status.Ready = ready
changed = true
}
if ready && v4Eip != "" && snat.Status.V4Eip != v4Eip {
if (v4Eip != "" && snat.Status.V4Eip != v4Eip) ||
(v4IpCidr != "" && snat.Status.V4IpCidr != v4IpCidr) ||
(vpc != "" && snat.Status.Vpc != vpc) {
snat.Status.V4Eip = v4Eip
snat.Status.V4IpCidr = v4IpCidr
snat.Status.Vpc = vpc
Expand Down

0 comments on commit da323a5

Please sign in to comment.