Make ConfigureIPTables idempotent and poll it

- Instead of EnsureBridge
- Adds ipset.ExistEntry method
- Refactor ipset method names Exist <-> EntryExists
- Turn on iptable-refresh by default (10s)
- Adds a test that checks iptables are refreshed, check EXPOSE recovers
This commit is contained in:
Simon Howe committed 2020-07-15 11:35:41 +02:00
1 parent 1ff09fd4d1
commit 94e75e45af
8 files changed
+228 -74

No files matched your search

+1 -17
View File
@@ -196,7 +196,7 @@ func createBaseRules(ipt *iptables.IPTables, ips ipset.Interface) error {
}
// delete `weave-local-pods` ipset which is no longer used by weave-npc
weaveLocalPodExist, err := ipsetExist(ips, npc.LocalIpset)
weaveLocalPodExist, err := ips.Exists(npc.LocalIpset)
if err != nil {
common.Log.Errorf("Failed to look if ipset '%s' exists", npc.LocalIpset)
} else if weaveLocalPodExist {
@@ -209,22 +209,6 @@ func createBaseRules(ipt *iptables.IPTables, ips ipset.Interface) error {
return nil
}
// Dummy way to check whether a given ipset exists.
// TODO(brb) Use "ipset -exist create <..>" for our purpose instead (for some reasons
// creating an ipset with -exist fails).
func ipsetExist(ips ipset.Interface, name ipset.Name) (bool, error) {
sets, err := ips.List(string(name))
if err != nil {
return false, err
}
for _, s := range sets {
if s == name {
return true, nil
}
}
return false, nil
}
func root(cmd *cobra.Command, args []string) {
var npController cache.Controller
+28
View File
@@ -168,6 +168,7 @@ func main() {
procPath string
discoveryEndpoint string
token string
iptablesRefresh time.Duration
advertiseAddress string
pluginConfig plugin.Config
defaultDockerHost = getenvOrDefault("DOCKER_HOST", "unix:///var/run/docker.sock")
@@ -216,6 +217,7 @@ func main() {
mflag.StringVar(&discoveryEndpoint, []string{"-peer-discovery-url"}, "https://cloud.weave.works/api/net", "url for peer discovery")
mflag.StringVar(&token, []string{"-token"}, "", "token for peer discovery")
mflag.StringVar(&advertiseAddress, []string{"-advertise-address"}, "", "address to advertise for peer discovery")
mflag.DurationVar(&iptablesRefresh, []string{"-iptables-refresh-interval"}, 10*time.Second, "Interval to reapply iptables. 0 to only apply on launch.")
mflag.BoolVar(&pluginConfig.Enable, []string{"-plugin"}, false, "enable Docker plugin (v1)")
mflag.BoolVar(&pluginConfig.EnableV2, []string{"-plugin-v2"}, false, "enable Docker plugin (v2)")
@@ -313,8 +315,11 @@ func main() {
}
}
ips := ipset.New(common.LogLogger(), 0)
err = weavenet.ResetIPTables(&bridgeConfig, ips)
checkFatal(err)
bridgeType, err := weavenet.EnsureBridge(procPath, &bridgeConfig, Log, ips)
checkFatal(err)
Log.Println("Bridge type is", bridgeType)
config.Password = determinePassword(password)
@@ -512,6 +517,29 @@ func main() {
go exposeForAWSVPC(allocator, defaultSubnet, bridgeConfig.WeaveBridgeName, waitReady.Add())
}
if iptablesRefresh > 0 {
Log.Printf("Entering iptable refresh loop (interval %v)", iptablesRefresh)
ticker := time.NewTicker(iptablesRefresh).C
go func() {
for {
select {
case <-ticker:
Log.Debug("Re-configuring iptables")
err := weavenet.ConfigureIPTables(&bridgeConfig, ips)
if err != nil {
Log.Errorf("Error configuring iptables: %s", err)
}
Log.Debug("Beginning re-exposure...")
weavenet.Reexpose(&bridgeConfig, Log)
Log.Debug("... re-exposure complete.")
}
}
}()
} else {
Log.Printf("Not refreshing iptables (interval: %v)", iptablesRefresh)
}
signals.SignalHandlerLoop(common.Log, router)
}