Skip to content

Commit 766e594

Browse files
authored
Merge pull request kubernetes#129329 from olderTaoist/full-sync-one-hour
kube-proxy full sync iptables one hour even though nothing changes
2 parents 4281163 + 561c1d2 commit 766e594

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

pkg/proxy/iptables/proxier.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ type Proxier struct {
159159
// updating iptables with some partial data after kube-proxy restart.
160160
endpointSlicesSynced bool
161161
servicesSynced bool
162+
lastFullSync time.Time
162163
needFullSync bool
163164
initialized int32
164165
syncRunner *async.BoundedFrequencyRunner // governs calls to syncProxyRules
@@ -325,7 +326,7 @@ func NewProxier(ctx context.Context,
325326
// We pass syncPeriod to ipt.Monitor, which will call us only if it needs to.
326327
// We need to pass *some* maxInterval to NewBoundedFrequencyRunner anyway though.
327328
// time.Hour is arbitrary.
328-
proxier.syncRunner = async.NewBoundedFrequencyRunner("sync-runner", proxier.syncProxyRules, minSyncPeriod, time.Hour, burstSyncs)
329+
proxier.syncRunner = async.NewBoundedFrequencyRunner("sync-runner", proxier.syncProxyRules, minSyncPeriod, proxyutil.FullSyncPeriod, burstSyncs)
329330

330331
go ipt.Monitor(kubeProxyCanaryChain, []utiliptables.Table{utiliptables.TableMangle, utiliptables.TableNAT, utiliptables.TableFilter},
331332
proxier.forceSyncProxyRules, syncPeriod, wait.NeverStop)
@@ -541,6 +542,7 @@ func (proxier *Proxier) SyncLoop() {
541542
// synthesize "last change queued" time as the informers are syncing.
542543
metrics.SyncProxyRulesLastQueuedTimestamp.WithLabelValues(string(proxier.ipFamily)).SetToCurrentTime()
543544
proxier.syncRunner.Loop(wait.NeverStop)
545+
544546
}
545547

546548
func (proxier *Proxier) setInitialized(value bool) {
@@ -806,15 +808,14 @@ func (proxier *Proxier) syncProxyRules() {
806808
return
807809
}
808810

809-
// The value of proxier.needFullSync may change before the defer funcs run, so
810-
// we need to keep track of whether it was set at the *start* of the sync.
811-
tryPartialSync := !proxier.needFullSync
812-
813811
// Keep track of how long syncs take.
814812
start := time.Now()
813+
814+
doFullSync := proxier.needFullSync || (time.Since(proxier.lastFullSync) > proxyutil.FullSyncPeriod)
815+
815816
defer func() {
816817
metrics.SyncProxyRulesLatency.WithLabelValues(string(proxier.ipFamily)).Observe(metrics.SinceInSeconds(start))
817-
if tryPartialSync {
818+
if !doFullSync {
818819
metrics.SyncPartialProxyRulesLatency.WithLabelValues(string(proxier.ipFamily)).Observe(metrics.SinceInSeconds(start))
819820
} else {
820821
metrics.SyncFullProxyRulesLatency.WithLabelValues(string(proxier.ipFamily)).Observe(metrics.SinceInSeconds(start))
@@ -825,24 +826,26 @@ func (proxier *Proxier) syncProxyRules() {
825826
serviceUpdateResult := proxier.svcPortMap.Update(proxier.serviceChanges)
826827
endpointUpdateResult := proxier.endpointsMap.Update(proxier.endpointsChanges)
827828

828-
proxier.logger.V(2).Info("Syncing iptables rules")
829+
proxier.logger.V(2).Info("Syncing iptables rules", "fullSync", doFullSync)
829830

830831
success := false
831832
defer func() {
832833
if !success {
833834
proxier.logger.Info("Sync failed", "retryingTime", proxier.syncPeriod)
834835
proxier.syncRunner.RetryAfter(proxier.syncPeriod)
835-
if tryPartialSync {
836+
if !doFullSync {
836837
metrics.IPTablesPartialRestoreFailuresTotal.WithLabelValues(string(proxier.ipFamily)).Inc()
837838
}
838839
// proxier.serviceChanges and proxier.endpointChanges have already
839840
// been flushed, so we've lost the state needed to be able to do
840841
// a partial sync.
841842
proxier.needFullSync = true
843+
} else if doFullSync {
844+
proxier.lastFullSync = time.Now()
842845
}
843846
}()
844847

845-
if !tryPartialSync {
848+
if doFullSync {
846849
// Ensure that our jump rules (eg from PREROUTING to KUBE-SERVICES) exist.
847850
// We can't do this as part of the iptables-restore because we don't want
848851
// to specify/replace *all* of the rules in PREROUTING, etc.
@@ -1235,7 +1238,7 @@ func (proxier *Proxier) syncProxyRules() {
12351238
// then we can omit them from the restore input. However, we have to still
12361239
// figure out how many chains we _would_ have written, to make the metrics
12371240
// come out right, so we just compute them and throw them away.
1238-
if tryPartialSync && !serviceUpdateResult.UpdatedServices.Has(svcName.NamespacedName) && !endpointUpdateResult.UpdatedServices.Has(svcName.NamespacedName) {
1241+
if !doFullSync && !serviceUpdateResult.UpdatedServices.Has(svcName.NamespacedName) && !endpointUpdateResult.UpdatedServices.Has(svcName.NamespacedName) {
12391242
natChains = skippedNatChains
12401243
natRules = skippedNatRules
12411244
}

pkg/proxy/nftables/proxier.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ type Proxier struct {
163163
// updating nftables with some partial data after kube-proxy restart.
164164
endpointSlicesSynced bool
165165
servicesSynced bool
166+
lastFullSync time.Time
166167
needFullSync bool
167168
initialized int32
168169
syncRunner *async.BoundedFrequencyRunner // governs calls to syncProxyRules
@@ -281,7 +282,7 @@ func NewProxier(ctx context.Context,
281282
burstSyncs := 2
282283
logger.V(2).Info("NFTables sync params", "minSyncPeriod", minSyncPeriod, "syncPeriod", syncPeriod, "burstSyncs", burstSyncs)
283284
// We need to pass *some* maxInterval to NewBoundedFrequencyRunner. time.Hour is arbitrary.
284-
proxier.syncRunner = async.NewBoundedFrequencyRunner("sync-runner", proxier.syncProxyRules, minSyncPeriod, time.Hour, burstSyncs)
285+
proxier.syncRunner = async.NewBoundedFrequencyRunner("sync-runner", proxier.syncProxyRules, minSyncPeriod, proxyutil.FullSyncPeriod, burstSyncs)
285286

286287
return proxier, nil
287288
}
@@ -1167,19 +1168,18 @@ func (proxier *Proxier) syncProxyRules() {
11671168
return
11681169
}
11691170

1171+
// Keep track of how long syncs take.
1172+
start := time.Now()
1173+
11701174
//
11711175
// Below this point we will not return until we try to write the nftables rules.
11721176
//
11731177

1174-
// The value of proxier.needFullSync may change before the defer funcs run, so
1175-
// we need to keep track of whether it was set at the *start* of the sync.
1176-
tryPartialSync := !proxier.needFullSync
1178+
doFullSync := proxier.needFullSync || (time.Since(proxier.lastFullSync) > proxyutil.FullSyncPeriod)
11771179

1178-
// Keep track of how long syncs take.
1179-
start := time.Now()
11801180
defer func() {
11811181
metrics.SyncProxyRulesLatency.WithLabelValues(string(proxier.ipFamily)).Observe(metrics.SinceInSeconds(start))
1182-
if tryPartialSync {
1182+
if !doFullSync {
11831183
metrics.SyncPartialProxyRulesLatency.WithLabelValues(string(proxier.ipFamily)).Observe(metrics.SinceInSeconds(start))
11841184
} else {
11851185
metrics.SyncFullProxyRulesLatency.WithLabelValues(string(proxier.ipFamily)).Observe(metrics.SinceInSeconds(start))
@@ -1190,7 +1190,7 @@ func (proxier *Proxier) syncProxyRules() {
11901190
serviceUpdateResult := proxier.svcPortMap.Update(proxier.serviceChanges)
11911191
endpointUpdateResult := proxier.endpointsMap.Update(proxier.endpointsChanges)
11921192

1193-
proxier.logger.V(2).Info("Syncing nftables rules")
1193+
proxier.logger.V(2).Info("Syncing nftables rules", "fullSync", doFullSync)
11941194

11951195
success := false
11961196
defer func() {
@@ -1201,6 +1201,8 @@ func (proxier *Proxier) syncProxyRules() {
12011201
// been flushed, so we've lost the state needed to be able to do
12021202
// a partial sync.
12031203
proxier.needFullSync = true
1204+
} else if doFullSync {
1205+
proxier.lastFullSync = time.Now()
12041206
}
12051207
}()
12061208

@@ -1228,7 +1230,7 @@ func (proxier *Proxier) syncProxyRules() {
12281230
// (with a later timestamp) at the end of the sync.
12291231
proxier.logger.Error(err, "Unable to delete stale chains; will retry later")
12301232
metrics.NFTablesCleanupFailuresTotal.WithLabelValues(string(proxier.ipFamily)).Inc()
1231-
tryPartialSync = false
1233+
doFullSync = true
12321234

12331235
// Log failed transaction and list full kube-proxy table.
12341236
proxier.logFailure(tx)
@@ -1238,7 +1240,7 @@ func (proxier *Proxier) syncProxyRules() {
12381240

12391241
// Now start the actual syncing transaction
12401242
tx := proxier.nftables.NewTransaction()
1241-
if !tryPartialSync {
1243+
if doFullSync {
12421244
proxier.setupNFTables(tx)
12431245
}
12441246

@@ -1286,7 +1288,7 @@ func (proxier *Proxier) syncProxyRules() {
12861288

12871289
// skipServiceUpdate is used for all service-related chains and their elements.
12881290
// If no changes were done to the service or its endpoints, these objects may be skipped.
1289-
skipServiceUpdate := tryPartialSync &&
1291+
skipServiceUpdate := !doFullSync &&
12901292
!serviceUpdateResult.UpdatedServices.Has(svcName.NamespacedName) &&
12911293
!endpointUpdateResult.UpdatedServices.Has(svcName.NamespacedName)
12921294

pkg/proxy/util/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"net"
2222
"strings"
23+
"time"
2324

2425
v1 "k8s.io/api/core/v1"
2526
"k8s.io/apimachinery/pkg/util/sets"
@@ -37,6 +38,9 @@ const (
3738

3839
// IPv6ZeroCIDR is the CIDR block for the whole IPv6 address space
3940
IPv6ZeroCIDR = "::/0"
41+
42+
// FullSyncPeriod is iptables and nftables proxier full sync period
43+
FullSyncPeriod = 1 * time.Hour
4044
)
4145

4246
// IsZeroCIDR checks whether the input CIDR string is either

0 commit comments

Comments
 (0)