Skip to content

Commit 39c99c6

Browse files
qiutingjunoilbeater
authored andcommitted
fix update dnat rules not effect correctly (#2518)
(cherry picked from commit ae51a65)
1 parent 7eb7ed6 commit 39c99c6

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed

dist/images/install.sh

+8
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,14 @@ spec:
640640
type: string
641641
redo:
642642
type: string
643+
protocol:
644+
type: string
645+
internalIp:
646+
type: string
647+
internalPort:
648+
type: string
649+
externalPort:
650+
type: string
643651
conditions:
644652
type: array
645653
items:

pkg/apis/kubeovn/v1/types.go

+5
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,11 @@ type IptablesDnatRuleStatus struct {
708708
NatGwDp string `json:"natGwDp" patchStrategy:"merge"`
709709
Redo string `json:"redo" patchStrategy:"merge"`
710710

711+
Protocol string `json:"protocol" patchStrategy:"merge"`
712+
InternalIp string `json:"internalIp" patchStrategy:"merge"`
713+
InternalPort string `json:"internalPort" patchStrategy:"merge"`
714+
ExternalPort string `json:"externalPort" patchStrategy:"merge"`
715+
711716
// Conditions represents the latest state of the object
712717
// +optional
713718
// +patchMergeKey=type

pkg/controller/vpc_nat_gw_nat.go

+39-17
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ func (c *Controller) enqueueUpdateIptablesDnatRule(old, new interface{}) {
100100

101101
if oldDnat.Status.V4ip != newDnat.Status.V4ip ||
102102
oldDnat.Spec.EIP != newDnat.Spec.EIP ||
103-
oldDnat.Status.Redo != newDnat.Status.Redo {
103+
oldDnat.Status.Redo != newDnat.Status.Redo ||
104+
oldDnat.Spec.Protocol != newDnat.Spec.Protocol ||
105+
oldDnat.Spec.InternalIp != newDnat.Spec.InternalIp ||
106+
oldDnat.Spec.InternalPort != newDnat.Spec.InternalPort ||
107+
oldDnat.Spec.ExternalPort != newDnat.Spec.ExternalPort {
104108
klog.V(3).Infof("enqueue update dnat %s", key)
105109
c.updateIptablesDnatRuleQueue.Add(key)
106110
return
@@ -781,24 +785,26 @@ func (c *Controller) handleUpdateIptablesDnatRule(key string) error {
781785
if vpcNatEnabled != "true" {
782786
return fmt.Errorf("iptables nat gw not enable")
783787
}
788+
789+
if err = c.deleteDnatInPod(cachedDnat.Status.NatGwDp, cachedDnat.Status.Protocol,
790+
cachedDnat.Status.V4ip, cachedDnat.Status.InternalIp,
791+
cachedDnat.Status.ExternalPort, cachedDnat.Status.InternalPort); err != nil {
792+
klog.Errorf("failed to delete old dnat, %v", err)
793+
return err
794+
}
795+
if err = c.createDnatInPod(eip.Spec.NatGwDp, cachedDnat.Spec.Protocol,
796+
eip.Status.IP, cachedDnat.Spec.InternalIp,
797+
cachedDnat.Spec.ExternalPort, cachedDnat.Spec.InternalPort); err != nil {
798+
klog.Errorf("failed to create new dnat %s, %v", key, err)
799+
return err
800+
}
801+
if err = c.patchDnatStatus(key, eip.Status.IP, eip.Spec.V6ip, eip.Spec.NatGwDp, "", true); err != nil {
802+
klog.Errorf("failed to patch status for dnat %s , %v", key, err)
803+
return err
804+
}
805+
784806
if c.dnatChangeEip(cachedDnat, eip) {
785807
klog.V(3).Infof("dnat change ip, old ip '%s', new ip %s", cachedDnat.Status.V4ip, eip.Status.IP)
786-
if err = c.deleteDnatInPod(cachedDnat.Status.NatGwDp, cachedDnat.Spec.Protocol,
787-
cachedDnat.Status.V4ip, cachedDnat.Spec.InternalIp,
788-
cachedDnat.Spec.ExternalPort, cachedDnat.Spec.InternalPort); err != nil {
789-
klog.Errorf("failed to delete old dnat, %v", err)
790-
return err
791-
}
792-
if err = c.createDnatInPod(eip.Spec.NatGwDp, cachedDnat.Spec.Protocol,
793-
eip.Status.IP, cachedDnat.Spec.InternalIp,
794-
cachedDnat.Spec.ExternalPort, cachedDnat.Spec.InternalPort); err != nil {
795-
klog.Errorf("failed to create new dnat %s, %v", key, err)
796-
return err
797-
}
798-
if err = c.patchDnatStatus(key, eip.Status.IP, eip.Spec.V6ip, eip.Spec.NatGwDp, "", true); err != nil {
799-
klog.Errorf("failed to patch status for dnat %s , %v", key, err)
800-
return err
801-
}
802808
if err = c.patchEipNat(eipName, util.DnatUsingEip); err != nil {
803809
klog.Errorf("failed to patch dnat use eip %s, %v", key, err)
804810
return err
@@ -1381,6 +1387,22 @@ func (c *Controller) patchDnatStatus(key, v4ip, v6ip, natGwDp, redo string, read
13811387
dnat.Status.NatGwDp = natGwDp
13821388
changed = true
13831389
}
1390+
if ready && dnat.Status.Protocol != "" && dnat.Status.Protocol != dnat.Spec.Protocol {
1391+
dnat.Status.Protocol = dnat.Spec.Protocol
1392+
changed = true
1393+
}
1394+
if ready && dnat.Status.InternalIp != "" && dnat.Status.InternalIp != dnat.Spec.InternalIp {
1395+
dnat.Status.InternalIp = dnat.Spec.InternalIp
1396+
changed = true
1397+
}
1398+
if ready && dnat.Status.InternalPort != "" && dnat.Status.InternalPort != dnat.Spec.InternalPort {
1399+
dnat.Status.InternalPort = dnat.Spec.InternalPort
1400+
changed = true
1401+
}
1402+
if ready && dnat.Status.ExternalPort != "" && dnat.Status.ExternalPort != dnat.Spec.ExternalPort {
1403+
dnat.Status.ExternalPort = dnat.Spec.ExternalPort
1404+
changed = true
1405+
}
13841406

13851407
if changed {
13861408
bytes, err := dnat.Status.Bytes()

yamls/crd.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ spec:
424424
type: string
425425
redo:
426426
type: string
427+
protocol:
428+
type: string
429+
internalIp:
430+
type: string
431+
internalPort:
432+
type: string
433+
externalPort:
434+
type: string
427435
conditions:
428436
type: array
429437
items:

0 commit comments

Comments
 (0)