Skip to content

Commit 30ef611

Browse files
authored
Merge pull request kubernetes#128886 from npinaeva/kube-proxy-debug-log
kube-proxy,nftables: add debug logging for failed transactions.
2 parents 46f0b3f + 90e64a5 commit 30ef611

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pkg/proxy/nftables/proxier.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ import (
2828
"crypto/sha256"
2929
"encoding/base32"
3030
"fmt"
31+
"golang.org/x/time/rate"
3132
"net"
3233
"os"
34+
"os/exec"
3335
"reflect"
3436
"strconv"
3537
"strings"
@@ -193,7 +195,8 @@ type Proxier struct {
193195
// which proxier is operating on, can be directly consumed by knftables.
194196
serviceCIDRs string
195197

196-
logger klog.Logger
198+
logger klog.Logger
199+
logRateLimiter *rate.Limiter
197200

198201
clusterIPs *nftElementStorage
199202
serviceIPs *nftElementStorage
@@ -266,6 +269,7 @@ func NewProxier(ctx context.Context,
266269
networkInterfacer: proxyutil.RealNetwork{},
267270
staleChains: make(map[string]time.Time),
268271
logger: logger,
272+
logRateLimiter: rate.NewLimiter(rate.Every(24*time.Hour), 1),
269273
clusterIPs: newNFTElementStorage("set", clusterIPsSet),
270274
serviceIPs: newNFTElementStorage("map", serviceIPsMap),
271275
firewallIPs: newNFTElementStorage("map", firewallIPsMap),
@@ -1136,6 +1140,21 @@ func (s *nftElementStorage) cleanupLeftoverKeys(tx *knftables.Transaction) {
11361140
s.resetLeftoverKeys()
11371141
}
11381142

1143+
// logFailure logs the transaction and the full table with a rate limit.
1144+
func (proxier *Proxier) logFailure(tx *knftables.Transaction) {
1145+
if klogV4 := klog.V(4); klogV4.Enabled() && proxier.logRateLimiter.Allow() {
1146+
klogV4.InfoS("Failed transaction", "transaction", tx.String())
1147+
// knftables doesn't supporting listing the full table yet, this is a workaround.
1148+
cmd := exec.Command("nft", "list", "table", kubeProxyTable)
1149+
out, err := cmd.Output()
1150+
if err != nil {
1151+
klogV4.InfoS("Listing full table failed", "error", err)
1152+
} else {
1153+
klogV4.InfoS("Listing full table", "result", string(out))
1154+
}
1155+
}
1156+
}
1157+
11391158
// This is where all of the nftables calls happen.
11401159
// This assumes proxier.mu is NOT held
11411160
func (proxier *Proxier) syncProxyRules() {
@@ -1209,6 +1228,10 @@ func (proxier *Proxier) syncProxyRules() {
12091228
// (with a later timestamp) at the end of the sync.
12101229
proxier.logger.Error(err, "Unable to delete stale chains; will retry later")
12111230
metrics.NFTablesCleanupFailuresTotal.WithLabelValues(string(proxier.ipFamily)).Inc()
1231+
tryPartialSync = false
1232+
1233+
// Log failed transaction and list full kube-proxy table.
1234+
proxier.logFailure(tx)
12121235
}
12131236
}
12141237
}
@@ -1808,6 +1831,8 @@ func (proxier *Proxier) syncProxyRules() {
18081831
// staleChains is now incorrect since we didn't actually flush the
18091832
// chains in it. We can recompute it next time.
18101833
clear(proxier.staleChains)
1834+
// Log failed transaction and list full kube-proxy table.
1835+
proxier.logFailure(tx)
18111836
return
18121837
}
18131838
success = true

pkg/proxy/nftables/proxier_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package nftables
2121

2222
import (
2323
"fmt"
24+
"golang.org/x/time/rate"
2425
"net"
2526
"reflect"
2627
"testing"
@@ -132,6 +133,7 @@ func NewFakeProxier(ipFamily v1.IPFamily) (*knftables.Fake, *Proxier) {
132133
networkInterfacer: networkInterfacer,
133134
staleChains: make(map[string]time.Time),
134135
serviceCIDRs: serviceCIDRs,
136+
logRateLimiter: rate.NewLimiter(rate.Every(24*time.Hour), 1),
135137
clusterIPs: newNFTElementStorage("set", clusterIPsSet),
136138
serviceIPs: newNFTElementStorage("map", serviceIPsMap),
137139
firewallIPs: newNFTElementStorage("map", firewallIPsMap),

0 commit comments

Comments
 (0)