Skip to content

Commit

Permalink
delete residual ovs internal ports
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Aug 29, 2021
1 parent 7d94413 commit cf32ab1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/daemon/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,33 @@ func (c *Controller) loopEncapIpCheck() {
}
}

var lastNoPodOvsPort map[string]bool

func (c *Controller) markAndCleanInternalPort() error {
klog.V(4).Infof("start to gc ovs internal ports")
residualPorts := ovs.GetResidualInternalPorts()
if len(residualPorts) == 0 {
return nil
}

noPodOvsPort := map[string]bool{}
for _, portName := range residualPorts {
if !lastNoPodOvsPort[portName] {
noPodOvsPort[portName] = true
} else {
klog.Infof("gc ovs internal port %s", portName)
// Remove ovs port
output, err := ovs.Exec(ovs.IfExists, "--with-iface", "del-port", "br-int", portName)
if err != nil {
return fmt.Errorf("failed to delete ovs port %v, %q", err, output)
}
}
}
lastNoPodOvsPort = noPodOvsPort

return nil
}

// Run starts controller
func (c *Controller) Run(stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
Expand All @@ -1051,6 +1078,11 @@ func (c *Controller) Run(stopCh <-chan struct{}) {
go wait.Until(c.runPodWorker, time.Second, stopCh)
go wait.Until(c.runGateway, 3*time.Second, stopCh)
go wait.Until(c.loopEncapIpCheck, 3*time.Second, stopCh)
go wait.Until(func() {
if err := c.markAndCleanInternalPort(); err != nil {
klog.Errorf("gc ovs port error: %v", err)
}
}, 5*time.Minute, stopCh)
<-stopCh
klog.Info("Shutting down workers")
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/ovs/ovs-vsctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,26 @@ func ValidatePortVendor(port string) (bool, error) {
output, err := ovsFind("Port", "name", "external_ids:vendor="+util.CniTypeName)
return util.ContainsString(output, port), err
}

func GetResidualInternalPorts() []string {
residualPorts := make([]string, 0)
interfaceList, err := ovsFind("interface", "name,external_ids", "type=internal")
if err != nil {
klog.Errorf("failed to list ovs internal interface %v", err)
return residualPorts
}

for _, intf := range interfaceList {
name := strings.Trim(strings.Split(intf, "\n")[0], "\"")
if !strings.Contains(name, "_c") {
continue
}

// iface-id field does not exist in external_ids for residual internal port
externaIds := strings.Split(intf, "\n")[1]
if !strings.Contains(externaIds, "iface-id") {
residualPorts = append(residualPorts, name)
}
}
return residualPorts
}

0 comments on commit cf32ab1

Please sign in to comment.