mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 10:19:50 +00:00
Remove exec arg from utiliptables.New
It was there so you could mock the results via a FakeExec, but these days any unit tests outside of pkg/util/iptables that want to mock iptables results use a FakeIPTables instead of a real utiliptables.Interface with a FakeExec.
This commit is contained in:
parent
0eaee48ecb
commit
9c98d29795
@ -109,12 +109,10 @@ func isIPTablesBased(mode proxyconfigapi.ProxyMode) bool {
|
|||||||
// is not v1.IPFamilyUnknown then it will also separately return the interface for just
|
// is not v1.IPFamilyUnknown then it will also separately return the interface for just
|
||||||
// that family.
|
// that family.
|
||||||
func getIPTables(primaryFamily v1.IPFamily) ([2]utiliptables.Interface, utiliptables.Interface) {
|
func getIPTables(primaryFamily v1.IPFamily) ([2]utiliptables.Interface, utiliptables.Interface) {
|
||||||
execer := exec.New()
|
|
||||||
|
|
||||||
// Create iptables handlers for both families. Always ordered as IPv4, IPv6
|
// Create iptables handlers for both families. Always ordered as IPv4, IPv6
|
||||||
ipt := [2]utiliptables.Interface{
|
ipt := [2]utiliptables.Interface{
|
||||||
utiliptables.New(execer, utiliptables.ProtocolIPv4),
|
utiliptables.New(utiliptables.ProtocolIPv4),
|
||||||
utiliptables.New(execer, utiliptables.ProtocolIPv6),
|
utiliptables.New(utiliptables.ProtocolIPv6),
|
||||||
}
|
}
|
||||||
|
|
||||||
var iptInterface utiliptables.Interface
|
var iptInterface utiliptables.Interface
|
||||||
|
@ -25,7 +25,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
utiliptables "k8s.io/kubernetes/pkg/util/iptables"
|
utiliptables "k8s.io/kubernetes/pkg/util/iptables"
|
||||||
utilexec "k8s.io/utils/exec"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -38,10 +37,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (kl *Kubelet) initNetworkUtil() {
|
func (kl *Kubelet) initNetworkUtil() {
|
||||||
exec := utilexec.New()
|
|
||||||
iptClients := []utiliptables.Interface{
|
iptClients := []utiliptables.Interface{
|
||||||
utiliptables.New(exec, utiliptables.ProtocolIPv4),
|
utiliptables.New(utiliptables.ProtocolIPv4),
|
||||||
utiliptables.New(exec, utiliptables.ProtocolIPv6),
|
utiliptables.New(utiliptables.ProtocolIPv6),
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range iptClients {
|
for i := range iptClients {
|
||||||
|
@ -246,8 +246,8 @@ func newInternal(exec utilexec.Interface, protocol Protocol, lockfilePath14x, lo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new Interface which will exec iptables.
|
// New returns a new Interface which will exec iptables.
|
||||||
func New(exec utilexec.Interface, protocol Protocol) Interface {
|
func New(protocol Protocol) Interface {
|
||||||
return newInternal(exec, protocol, "", "")
|
return newInternal(utilexec.New(), protocol, "", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnsureChain is part of Interface.
|
// EnsureChain is part of Interface.
|
||||||
|
@ -61,7 +61,7 @@ func testIPTablesVersionCmds(t *testing.T, protocol Protocol) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_ = New(fexec, protocol)
|
_ = newInternal(fexec, protocol, "", "")
|
||||||
|
|
||||||
// Check that proper iptables version command was used during runner instantiation
|
// Check that proper iptables version command was used during runner instantiation
|
||||||
if !sets.New(fcmd.CombinedOutputLog[0]...).HasAll(iptablesCmd, "--version") {
|
if !sets.New(fcmd.CombinedOutputLog[0]...).HasAll(iptablesCmd, "--version") {
|
||||||
@ -103,7 +103,7 @@ func testEnsureChain(t *testing.T, protocol Protocol) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, protocol)
|
runner := newInternal(fexec, protocol, "", "")
|
||||||
// Success.
|
// Success.
|
||||||
exists, err := runner.EnsureChain(TableNAT, Chain("FOOBAR"))
|
exists, err := runner.EnsureChain(TableNAT, Chain("FOOBAR"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -160,7 +160,7 @@ func TestFlushChain(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
// Success.
|
// Success.
|
||||||
err := runner.FlushChain(TableNAT, Chain("FOOBAR"))
|
err := runner.FlushChain(TableNAT, Chain("FOOBAR"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -197,7 +197,7 @@ func TestDeleteChain(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
// Success.
|
// Success.
|
||||||
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
|
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -233,7 +233,7 @@ func TestEnsureRuleAlreadyExists(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
exists, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
|
exists, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected success, got %v", err)
|
t.Errorf("expected success, got %v", err)
|
||||||
@ -269,7 +269,7 @@ func TestEnsureRuleNew(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
exists, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
|
exists, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected success, got %v", err)
|
t.Errorf("expected success, got %v", err)
|
||||||
@ -302,7 +302,7 @@ func TestEnsureRuleErrorChecking(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
_, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
|
_, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected failure")
|
t.Errorf("expected failure")
|
||||||
@ -332,7 +332,7 @@ func TestEnsureRuleErrorCreating(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
_, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
|
_, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected failure")
|
t.Errorf("expected failure")
|
||||||
@ -359,7 +359,7 @@ func TestDeleteRuleDoesNotExist(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
|
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected success, got %v", err)
|
t.Errorf("expected success, got %v", err)
|
||||||
@ -392,7 +392,7 @@ func TestDeleteRuleExists(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
|
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected success, got %v", err)
|
t.Errorf("expected success, got %v", err)
|
||||||
@ -422,7 +422,7 @@ func TestDeleteRuleErrorChecking(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
|
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected failure")
|
t.Errorf("expected failure")
|
||||||
@ -452,7 +452,7 @@ func TestDeleteRuleErrorDeleting(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
|
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected failure")
|
t.Errorf("expected failure")
|
||||||
@ -487,7 +487,7 @@ func TestGetIPTablesHasCheckCommand(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
ipt := New(fexec, ProtocolIPv4)
|
ipt := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
runner := ipt.(*runner)
|
runner := ipt.(*runner)
|
||||||
if testCase.Expected != runner.hasCheck {
|
if testCase.Expected != runner.hasCheck {
|
||||||
t.Errorf("Expected result: %v, Got result: %v", testCase.Expected, runner.hasCheck)
|
t.Errorf("Expected result: %v, Got result: %v", testCase.Expected, runner.hasCheck)
|
||||||
@ -648,7 +648,7 @@ func TestWaitFlagUnavailable(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
|
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected success, got %v", err)
|
t.Errorf("expected success, got %v", err)
|
||||||
@ -679,7 +679,7 @@ func TestWaitFlagOld(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
|
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected success, got %v", err)
|
t.Errorf("expected success, got %v", err)
|
||||||
@ -713,7 +713,7 @@ func TestWaitFlagNew(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
|
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected success, got %v", err)
|
t.Errorf("expected success, got %v", err)
|
||||||
@ -744,7 +744,7 @@ func TestWaitIntervalFlagNew(t *testing.T) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, ProtocolIPv4)
|
runner := newInternal(fexec, ProtocolIPv4, "", "")
|
||||||
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
|
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected success, got %v", err)
|
t.Errorf("expected success, got %v", err)
|
||||||
@ -789,7 +789,7 @@ COMMIT
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, protocol)
|
runner := newInternal(fexec, protocol, "", "")
|
||||||
buffer := bytes.NewBuffer(nil)
|
buffer := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
// Success.
|
// Success.
|
||||||
@ -857,7 +857,7 @@ func testRestore(t *testing.T, protocol Protocol) {
|
|||||||
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runner := New(fexec, protocol)
|
runner := newInternal(fexec, protocol, "", "")
|
||||||
|
|
||||||
// both flags true
|
// both flags true
|
||||||
err := runner.Restore(TableNAT, []byte{}, FlushTables, RestoreCounters)
|
err := runner.Restore(TableNAT, []byte{}, FlushTables, RestoreCounters)
|
||||||
|
@ -191,7 +191,7 @@ func (mfc *monitorFakeCmd) Stop() {
|
|||||||
|
|
||||||
func TestIPTablesMonitor(t *testing.T) {
|
func TestIPTablesMonitor(t *testing.T) {
|
||||||
mfe := newMonitorFakeExec()
|
mfe := newMonitorFakeExec()
|
||||||
ipt := New(mfe, ProtocolIPv4)
|
ipt := newInternal(mfe, ProtocolIPv4, "", "")
|
||||||
|
|
||||||
var reloads uint32
|
var reloads uint32
|
||||||
stopCh := make(chan struct{})
|
stopCh := make(chan struct{})
|
||||||
|
Loading…
Reference in New Issue
Block a user