Skip to content

Commit

Permalink
fix update dnat rules not effect correctly (#2518)
Browse files Browse the repository at this point in the history
(cherry picked from commit ae51a65)
  • Loading branch information
qiutingjun authored and oilbeater committed Mar 25, 2023
1 parent 7eb7ed6 commit 39c99c6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
8 changes: 8 additions & 0 deletions dist/images/install.sh
Expand Up @@ -640,6 +640,14 @@ spec:
type: string
redo:
type: string
protocol:
type: string
internalIp:
type: string
internalPort:
type: string
externalPort:
type: string
conditions:
type: array
items:
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/kubeovn/v1/types.go
Expand Up @@ -708,6 +708,11 @@ type IptablesDnatRuleStatus struct {
NatGwDp string `json:"natGwDp" patchStrategy:"merge"`
Redo string `json:"redo" patchStrategy:"merge"`

Protocol string `json:"protocol" patchStrategy:"merge"`
InternalIp string `json:"internalIp" patchStrategy:"merge"`
InternalPort string `json:"internalPort" patchStrategy:"merge"`
ExternalPort string `json:"externalPort" patchStrategy:"merge"`

// Conditions represents the latest state of the object
// +optional
// +patchMergeKey=type
Expand Down
56 changes: 39 additions & 17 deletions pkg/controller/vpc_nat_gw_nat.go
Expand Up @@ -100,7 +100,11 @@ func (c *Controller) enqueueUpdateIptablesDnatRule(old, new interface{}) {

if oldDnat.Status.V4ip != newDnat.Status.V4ip ||
oldDnat.Spec.EIP != newDnat.Spec.EIP ||
oldDnat.Status.Redo != newDnat.Status.Redo {
oldDnat.Status.Redo != newDnat.Status.Redo ||
oldDnat.Spec.Protocol != newDnat.Spec.Protocol ||
oldDnat.Spec.InternalIp != newDnat.Spec.InternalIp ||
oldDnat.Spec.InternalPort != newDnat.Spec.InternalPort ||
oldDnat.Spec.ExternalPort != newDnat.Spec.ExternalPort {
klog.V(3).Infof("enqueue update dnat %s", key)
c.updateIptablesDnatRuleQueue.Add(key)
return
Expand Down Expand Up @@ -781,24 +785,26 @@ func (c *Controller) handleUpdateIptablesDnatRule(key string) error {
if vpcNatEnabled != "true" {
return fmt.Errorf("iptables nat gw not enable")
}

if err = c.deleteDnatInPod(cachedDnat.Status.NatGwDp, cachedDnat.Status.Protocol,
cachedDnat.Status.V4ip, cachedDnat.Status.InternalIp,
cachedDnat.Status.ExternalPort, cachedDnat.Status.InternalPort); err != nil {
klog.Errorf("failed to delete old dnat, %v", err)
return err
}
if err = c.createDnatInPod(eip.Spec.NatGwDp, cachedDnat.Spec.Protocol,
eip.Status.IP, cachedDnat.Spec.InternalIp,
cachedDnat.Spec.ExternalPort, cachedDnat.Spec.InternalPort); err != nil {
klog.Errorf("failed to create new dnat %s, %v", key, err)
return err
}
if err = c.patchDnatStatus(key, eip.Status.IP, eip.Spec.V6ip, eip.Spec.NatGwDp, "", true); err != nil {
klog.Errorf("failed to patch status for dnat %s , %v", key, err)
return err
}

if c.dnatChangeEip(cachedDnat, eip) {
klog.V(3).Infof("dnat change ip, old ip '%s', new ip %s", cachedDnat.Status.V4ip, eip.Status.IP)
if err = c.deleteDnatInPod(cachedDnat.Status.NatGwDp, cachedDnat.Spec.Protocol,
cachedDnat.Status.V4ip, cachedDnat.Spec.InternalIp,
cachedDnat.Spec.ExternalPort, cachedDnat.Spec.InternalPort); err != nil {
klog.Errorf("failed to delete old dnat, %v", err)
return err
}
if err = c.createDnatInPod(eip.Spec.NatGwDp, cachedDnat.Spec.Protocol,
eip.Status.IP, cachedDnat.Spec.InternalIp,
cachedDnat.Spec.ExternalPort, cachedDnat.Spec.InternalPort); err != nil {
klog.Errorf("failed to create new dnat %s, %v", key, err)
return err
}
if err = c.patchDnatStatus(key, eip.Status.IP, eip.Spec.V6ip, eip.Spec.NatGwDp, "", true); err != nil {
klog.Errorf("failed to patch status for dnat %s , %v", key, err)
return err
}
if err = c.patchEipNat(eipName, util.DnatUsingEip); err != nil {
klog.Errorf("failed to patch dnat use eip %s, %v", key, err)
return err
Expand Down Expand Up @@ -1381,6 +1387,22 @@ func (c *Controller) patchDnatStatus(key, v4ip, v6ip, natGwDp, redo string, read
dnat.Status.NatGwDp = natGwDp
changed = true
}
if ready && dnat.Status.Protocol != "" && dnat.Status.Protocol != dnat.Spec.Protocol {
dnat.Status.Protocol = dnat.Spec.Protocol
changed = true
}
if ready && dnat.Status.InternalIp != "" && dnat.Status.InternalIp != dnat.Spec.InternalIp {
dnat.Status.InternalIp = dnat.Spec.InternalIp
changed = true
}
if ready && dnat.Status.InternalPort != "" && dnat.Status.InternalPort != dnat.Spec.InternalPort {
dnat.Status.InternalPort = dnat.Spec.InternalPort
changed = true
}
if ready && dnat.Status.ExternalPort != "" && dnat.Status.ExternalPort != dnat.Spec.ExternalPort {
dnat.Status.ExternalPort = dnat.Spec.ExternalPort
changed = true
}

if changed {
bytes, err := dnat.Status.Bytes()
Expand Down
8 changes: 8 additions & 0 deletions yamls/crd.yaml
Expand Up @@ -424,6 +424,14 @@ spec:
type: string
redo:
type: string
protocol:
type: string
internalIp:
type: string
internalPort:
type: string
externalPort:
type: string
conditions:
type: array
items:
Expand Down

0 comments on commit 39c99c6

Please sign in to comment.