Skip to content

Commit

Permalink
automatically clean dummy interface and iptables rule when yurthub is…
Browse files Browse the repository at this point in the history
… stopped by k8s
  • Loading branch information
Congrool committed Oct 25, 2021
1 parent 754f4c2 commit 203ef53
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/yurthub/yurthub.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
"time"

"github.com/openyurtio/openyurt/cmd/yurthub/app"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/server"
)

func main() {
rand.Seed(time.Now().UnixNano())
cmd := app.NewCmdStartYurtHub(wait.NeverStop)
cmd := app.NewCmdStartYurtHub(server.SetupSignalHandler())
cmd.Flags().AddGoFlagSet(flag.CommandLine)
if err := cmd.Execute(); err != nil {
panic(err)
Expand Down
11 changes: 11 additions & 0 deletions pkg/yurthub/network/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,14 @@ func (im *IptablesManager) EnsureIptablesRules() error {
}
return nil
}

func (im *IptablesManager) CleanUpIptablesRules() {
for _, rule := range im.rules {
err := im.iptables.DeleteRule(rule.table, rule.chain, rule.args...)
if err != nil {
klog.Errorf("failed to delete iptables rule(%s -t %s %s %s), %v", rule.pos, rule.table, rule.chain, strings.Join(rule.args, " "), err)

}
klog.Infof("deleted the iptable rule(%s -t %s %s %s)", rule.pos, rule.table, rule.chain, strings.Join(rule.args, " "))
}
}
5 changes: 5 additions & 0 deletions pkg/yurthub/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func (m *NetworkManager) Run(stopCh <-chan struct{}) {
select {
case <-stopCh:
klog.Infof("exit network manager run goroutine normally")
m.iptablesManager.CleanUpIptablesRules()
err := m.ifController.DeleteDummyInterface(m.dummyIfName)
if err != nil {
klog.Errorf("failed to delete dummy interface %s, %v", m.dummyIfName, err)
}
return
case <-ticker.C:
if err := m.configureNetwork(); err != nil {
Expand Down

0 comments on commit 203ef53

Please sign in to comment.