From 2e700adc02d6f1e4d0819e943921df9b6763290e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=A5=96=E5=BB=BA?= Date: Tue, 14 May 2024 16:03:29 +0800 Subject: [PATCH] fix lsp not updated correctly when logical switch is changed (#4015) Signed-off-by: zhangzujian --- pkg/ovs/ovn-nb-logical_switch_port.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/ovs/ovn-nb-logical_switch_port.go b/pkg/ovs/ovn-nb-logical_switch_port.go index ce8197992c7..506e2555db2 100644 --- a/pkg/ovs/ovn-nb-logical_switch_port.go +++ b/pkg/ovs/ovn-nb-logical_switch_port.go @@ -88,30 +88,35 @@ func buildLogicalSwitchPort(lspName, lsName, ip, mac, podName, namespace string, } func (c *OVNNbClient) CreateLogicalSwitchPort(lsName, lspName, ip, mac, podName, namespace string, portSecurity bool, securityGroups, vips string, enableDHCP bool, dhcpOptions *DHCPOptionsUUIDs, vpc string) error { - exist, err := c.LogicalSwitchPortExists(lspName) + existingLsp, err := c.GetLogicalSwitchPort(lspName, true) if err != nil { klog.Error(err) return err } - // update if exists - if exist { - lsp := buildLogicalSwitchPort(lspName, lsName, ip, mac, podName, namespace, portSecurity, securityGroups, vips, enableDHCP, dhcpOptions, vpc) - if err := c.UpdateLogicalSwitchPort(lsp, &lsp.Addresses, &lsp.Dhcpv4Options, &lsp.Dhcpv6Options, &lsp.PortSecurity, &lsp.ExternalIDs); err != nil { + var ops []ovsdb.Operation + lsp := buildLogicalSwitchPort(lspName, lsName, ip, mac, podName, namespace, portSecurity, securityGroups, vips, enableDHCP, dhcpOptions, vpc) + if existingLsp != nil { + if lsp.ExternalIDs[logicalSwitchKey] == lsName { + if err := c.UpdateLogicalSwitchPort(lsp, &lsp.Addresses, &lsp.Dhcpv4Options, &lsp.Dhcpv6Options, &lsp.PortSecurity, &lsp.ExternalIDs); err != nil { + klog.Error(err) + return fmt.Errorf("failed to update logical switch port %s: %v", lspName, err) + } + return nil + } + if ops, err = c.LogicalSwitchUpdatePortOp(lsp.ExternalIDs[logicalSwitchKey], existingLsp.UUID, ovsdb.MutateOperationDelete); err != nil { klog.Error(err) - return fmt.Errorf("failed to update logical switch port %s: %v", lspName, err) + return err } - return nil } - lsp := buildLogicalSwitchPort(lspName, lsName, ip, mac, podName, namespace, portSecurity, securityGroups, vips, enableDHCP, dhcpOptions, vpc) - ops, err := c.CreateLogicalSwitchPortOp(lsp, lsName) + createLspOps, err := c.CreateLogicalSwitchPortOp(lsp, lsName) if err != nil { klog.Error(err) return fmt.Errorf("generate operations for creating logical switch port %s: %v", lspName, err) } - if err = c.Transact("lsp-add", ops); err != nil { + if err = c.Transact("lsp-add", append(ops, createLspOps...)); err != nil { return fmt.Errorf("create logical switch port %s: %v", lspName, err) }