Skip to content

Commit 6b95cec

Browse files
authored
fix pg set port fail when lsp is already deleted (#2658)
* fix pg set port fail when lsp is already deleted
1 parent 5ad2baf commit 6b95cec

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

pkg/controller/node.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -920,17 +920,23 @@ func (c *Controller) fetchPodsOnNode(nodeName string, pods []*v1.Pod) ([]string,
920920
return ports, nil
921921
}
922922

923-
func (c *Controller) checkPodsChangedOnNode(pgName string, nameIdMap map[string]string, pgPorts, ports []string) (bool, error) {
923+
func (c *Controller) checkPodsChangedOnNode(pgName string, nameIdMap map[string]string, pgPorts, ports []string) (bool, []string, error) {
924+
925+
toAddPorts := make([]string, 0)
924926
for _, port := range ports {
925927
if portId, ok := nameIdMap[port]; ok {
926928
if !util.IsStringIn(portId, pgPorts) {
927-
klog.Infof("pod on node changed, new added port %v should add to node port group %v", port, pgName)
928-
return true, nil
929+
toAddPorts = append(toAddPorts, port)
929930
}
930931
}
931932
}
932933

933-
return false, nil
934+
if len(toAddPorts) != 0 {
935+
klog.Infof("pod on node changed, new added port %v should add to node port group %v", toAddPorts, pgName)
936+
return true, toAddPorts, nil
937+
}
938+
939+
return false, toAddPorts, nil
934940
}
935941

936942
func (c *Controller) CheckNodePortGroup() {
@@ -974,14 +980,14 @@ func (c *Controller) checkAndUpdateNodePortGroup() error {
974980
// The port-group should already created when add node
975981
pgName := strings.Replace(node.Annotations[util.PortNameAnnotation], "-", ".", -1)
976982
nodeIP := node.Annotations[util.IpAddressAnnotation]
977-
983+
var toAddPorts []string
978984
ports, err := c.fetchPodsOnNode(node.Name, pods)
979985
if err != nil {
980986
klog.Errorf("failed to fetch pods for node %v, %v", node.Name, err)
981987
return err
982988
}
983989

984-
changed, err := c.checkPodsChangedOnNode(pgName, nameIdMap, namePortsMap[pgName], ports)
990+
changed, toAddPorts, err := c.checkPodsChangedOnNode(pgName, nameIdMap, namePortsMap[pgName], ports)
985991
if err != nil {
986992
klog.Errorf("failed to check pod status for node %v, %v", node.Name, err)
987993
continue
@@ -998,10 +1004,15 @@ func (c *Controller) checkAndUpdateNodePortGroup() error {
9981004
}
9991005
lastNpExists[node.Name] = networkPolicyExists
10001006

1001-
err = c.ovnLegacyClient.SetPortsToPortGroup(pgName, ports)
1002-
if err != nil {
1003-
klog.Errorf("failed to set port group for node %v, %v", node.Name, err)
1004-
return err
1007+
if len(toAddPorts) != 0 {
1008+
c.ovnPgKeyMutex.Lock(pgName)
1009+
for _, toAddPort := range toAddPorts {
1010+
if err = c.ovnClient.PortGroupAddPort(pgName, toAddPort); err != nil {
1011+
c.ovnPgKeyMutex.Unlock(pgName)
1012+
return err
1013+
}
1014+
}
1015+
c.ovnPgKeyMutex.Unlock(pgName)
10051016
}
10061017

10071018
if networkPolicyExists {

0 commit comments

Comments
 (0)