From 8e0e4b17fd08891a5bd3dd98a6d7c9855ef5cbe2 Mon Sep 17 00:00:00 2001 From: Qinghao Huang <8191686+wfnuser@users.noreply.github.com> Date: Sun, 18 Feb 2024 15:19:30 +0800 Subject: [PATCH] Fix: Resolve issue with skipped execution of sg annotations (#3700) The problem causing ineffective application of sg annotations is that, during virtual machine restart, the logical switch port is intentionally not deleted.(I guess). When sg annotations are added and the VM is restarted, the create logical switch port logic is skipped as it detects the existing lsp. Consequently, the annotation fails to attach to the lsp. Even when we sync lsp for sg, it has no effect. A simple fix is to update the existing lsp during lsp creation if it already exists. This approach ensures correct annotation attachment and addresses the skipped execution issue. Signed-off-by: wfnuser --- pkg/ovs/ovn-nb-logical_switch_port.go | 34 +++++++++++++++++---------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/pkg/ovs/ovn-nb-logical_switch_port.go b/pkg/ovs/ovn-nb-logical_switch_port.go index 0874117f719..01154ff1897 100644 --- a/pkg/ovs/ovn-nb-logical_switch_port.go +++ b/pkg/ovs/ovn-nb-logical_switch_port.go @@ -16,18 +16,7 @@ import ( "github.com/kubeovn/kube-ovn/pkg/util" ) -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) - if err != nil { - klog.Error(err) - return err - } - - // ignore - if exist { - return nil - } - +func buildLogicalSwitchPort(lspName, ip, mac, podName, namespace string, portSecurity bool, securityGroups, vips string, enableDHCP bool, dhcpOptions *DHCPOptionsUUIDs, vpc string) *ovnnb.LogicalSwitchPort { /* normal lsp creation */ lsp := &ovnnb.LogicalSwitchPort{ UUID: ovsclient.NamedUUID(), @@ -88,6 +77,27 @@ func (c *OVNNbClient) CreateLogicalSwitchPort(lsName, lspName, ip, mac, podName, } } + return lsp +} + +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) + if err != nil { + klog.Error(err) + return err + } + + // update if exists + if exist { + lsp := buildLogicalSwitchPort(lspName, ip, mac, podName, namespace, portSecurity, securityGroups, vips, enableDHCP, dhcpOptions, vpc) + if err := c.UpdateLogicalSwitchPort(lsp, &lsp.PortSecurity, &lsp.ExternalIDs); err != nil { + klog.Error(err) + return fmt.Errorf("failed to update logical switch port %s: %v", lspName, err) + } + return nil + } + + lsp := buildLogicalSwitchPort(lspName, ip, mac, podName, namespace, portSecurity, securityGroups, vips, enableDHCP, dhcpOptions, vpc) ops, err := c.CreateLogicalSwitchPortOp(lsp, lsName) if err != nil { klog.Error(err)