Skip to content

Commit 9c98d29

Browse files
committed
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.
1 parent 0eaee48 commit 9c98d29

File tree

5 files changed

+26
-30
lines changed

5 files changed

+26
-30
lines changed

cmd/kube-proxy/app/server_linux.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,10 @@ func isIPTablesBased(mode proxyconfigapi.ProxyMode) bool {
109109
// is not v1.IPFamilyUnknown then it will also separately return the interface for just
110110
// that family.
111111
func getIPTables(primaryFamily v1.IPFamily) ([2]utiliptables.Interface, utiliptables.Interface) {
112-
execer := exec.New()
113-
114112
// Create iptables handlers for both families. Always ordered as IPv4, IPv6
115113
ipt := [2]utiliptables.Interface{
116-
utiliptables.New(execer, utiliptables.ProtocolIPv4),
117-
utiliptables.New(execer, utiliptables.ProtocolIPv6),
114+
utiliptables.New(utiliptables.ProtocolIPv4),
115+
utiliptables.New(utiliptables.ProtocolIPv6),
118116
}
119117

120118
var iptInterface utiliptables.Interface

pkg/kubelet/kubelet_network_linux.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"k8s.io/apimachinery/pkg/util/wait"
2626
"k8s.io/klog/v2"
2727
utiliptables "k8s.io/kubernetes/pkg/util/iptables"
28-
utilexec "k8s.io/utils/exec"
2928
)
3029

3130
const (
@@ -38,10 +37,9 @@ const (
3837
)
3938

4039
func (kl *Kubelet) initNetworkUtil() {
41-
exec := utilexec.New()
4240
iptClients := []utiliptables.Interface{
43-
utiliptables.New(exec, utiliptables.ProtocolIPv4),
44-
utiliptables.New(exec, utiliptables.ProtocolIPv6),
41+
utiliptables.New(utiliptables.ProtocolIPv4),
42+
utiliptables.New(utiliptables.ProtocolIPv6),
4543
}
4644

4745
for i := range iptClients {

pkg/util/iptables/iptables.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ func newInternal(exec utilexec.Interface, protocol Protocol, lockfilePath14x, lo
246246
}
247247

248248
// New returns a new Interface which will exec iptables.
249-
func New(exec utilexec.Interface, protocol Protocol) Interface {
250-
return newInternal(exec, protocol, "", "")
249+
func New(protocol Protocol) Interface {
250+
return newInternal(utilexec.New(), protocol, "", "")
251251
}
252252

253253
// EnsureChain is part of Interface.

pkg/util/iptables/iptables_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func testIPTablesVersionCmds(t *testing.T, protocol Protocol) {
6161
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
6262
},
6363
}
64-
_ = New(fexec, protocol)
64+
_ = newInternal(fexec, protocol, "", "")
6565

6666
// Check that proper iptables version command was used during runner instantiation
6767
if !sets.New(fcmd.CombinedOutputLog[0]...).HasAll(iptablesCmd, "--version") {
@@ -103,7 +103,7 @@ func testEnsureChain(t *testing.T, protocol Protocol) {
103103
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
104104
},
105105
}
106-
runner := New(fexec, protocol)
106+
runner := newInternal(fexec, protocol, "", "")
107107
// Success.
108108
exists, err := runner.EnsureChain(TableNAT, Chain("FOOBAR"))
109109
if err != nil {
@@ -160,7 +160,7 @@ func TestFlushChain(t *testing.T) {
160160
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
161161
},
162162
}
163-
runner := New(fexec, ProtocolIPv4)
163+
runner := newInternal(fexec, ProtocolIPv4, "", "")
164164
// Success.
165165
err := runner.FlushChain(TableNAT, Chain("FOOBAR"))
166166
if err != nil {
@@ -197,7 +197,7 @@ func TestDeleteChain(t *testing.T) {
197197
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
198198
},
199199
}
200-
runner := New(fexec, ProtocolIPv4)
200+
runner := newInternal(fexec, ProtocolIPv4, "", "")
201201
// Success.
202202
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
203203
if err != nil {
@@ -233,7 +233,7 @@ func TestEnsureRuleAlreadyExists(t *testing.T) {
233233
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
234234
},
235235
}
236-
runner := New(fexec, ProtocolIPv4)
236+
runner := newInternal(fexec, ProtocolIPv4, "", "")
237237
exists, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
238238
if err != nil {
239239
t.Errorf("expected success, got %v", err)
@@ -269,7 +269,7 @@ func TestEnsureRuleNew(t *testing.T) {
269269
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
270270
},
271271
}
272-
runner := New(fexec, ProtocolIPv4)
272+
runner := newInternal(fexec, ProtocolIPv4, "", "")
273273
exists, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
274274
if err != nil {
275275
t.Errorf("expected success, got %v", err)
@@ -302,7 +302,7 @@ func TestEnsureRuleErrorChecking(t *testing.T) {
302302
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
303303
},
304304
}
305-
runner := New(fexec, ProtocolIPv4)
305+
runner := newInternal(fexec, ProtocolIPv4, "", "")
306306
_, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
307307
if err == nil {
308308
t.Errorf("expected failure")
@@ -332,7 +332,7 @@ func TestEnsureRuleErrorCreating(t *testing.T) {
332332
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
333333
},
334334
}
335-
runner := New(fexec, ProtocolIPv4)
335+
runner := newInternal(fexec, ProtocolIPv4, "", "")
336336
_, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
337337
if err == nil {
338338
t.Errorf("expected failure")
@@ -359,7 +359,7 @@ func TestDeleteRuleDoesNotExist(t *testing.T) {
359359
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
360360
},
361361
}
362-
runner := New(fexec, ProtocolIPv4)
362+
runner := newInternal(fexec, ProtocolIPv4, "", "")
363363
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
364364
if err != nil {
365365
t.Errorf("expected success, got %v", err)
@@ -392,7 +392,7 @@ func TestDeleteRuleExists(t *testing.T) {
392392
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
393393
},
394394
}
395-
runner := New(fexec, ProtocolIPv4)
395+
runner := newInternal(fexec, ProtocolIPv4, "", "")
396396
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
397397
if err != nil {
398398
t.Errorf("expected success, got %v", err)
@@ -422,7 +422,7 @@ func TestDeleteRuleErrorChecking(t *testing.T) {
422422
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
423423
},
424424
}
425-
runner := New(fexec, ProtocolIPv4)
425+
runner := newInternal(fexec, ProtocolIPv4, "", "")
426426
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
427427
if err == nil {
428428
t.Errorf("expected failure")
@@ -452,7 +452,7 @@ func TestDeleteRuleErrorDeleting(t *testing.T) {
452452
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
453453
},
454454
}
455-
runner := New(fexec, ProtocolIPv4)
455+
runner := newInternal(fexec, ProtocolIPv4, "", "")
456456
err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
457457
if err == nil {
458458
t.Errorf("expected failure")
@@ -487,7 +487,7 @@ func TestGetIPTablesHasCheckCommand(t *testing.T) {
487487
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
488488
},
489489
}
490-
ipt := New(fexec, ProtocolIPv4)
490+
ipt := newInternal(fexec, ProtocolIPv4, "", "")
491491
runner := ipt.(*runner)
492492
if testCase.Expected != runner.hasCheck {
493493
t.Errorf("Expected result: %v, Got result: %v", testCase.Expected, runner.hasCheck)
@@ -648,7 +648,7 @@ func TestWaitFlagUnavailable(t *testing.T) {
648648
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
649649
},
650650
}
651-
runner := New(fexec, ProtocolIPv4)
651+
runner := newInternal(fexec, ProtocolIPv4, "", "")
652652
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
653653
if err != nil {
654654
t.Errorf("expected success, got %v", err)
@@ -679,7 +679,7 @@ func TestWaitFlagOld(t *testing.T) {
679679
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
680680
},
681681
}
682-
runner := New(fexec, ProtocolIPv4)
682+
runner := newInternal(fexec, ProtocolIPv4, "", "")
683683
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
684684
if err != nil {
685685
t.Errorf("expected success, got %v", err)
@@ -713,7 +713,7 @@ func TestWaitFlagNew(t *testing.T) {
713713
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
714714
},
715715
}
716-
runner := New(fexec, ProtocolIPv4)
716+
runner := newInternal(fexec, ProtocolIPv4, "", "")
717717
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
718718
if err != nil {
719719
t.Errorf("expected success, got %v", err)
@@ -744,7 +744,7 @@ func TestWaitIntervalFlagNew(t *testing.T) {
744744
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
745745
},
746746
}
747-
runner := New(fexec, ProtocolIPv4)
747+
runner := newInternal(fexec, ProtocolIPv4, "", "")
748748
err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
749749
if err != nil {
750750
t.Errorf("expected success, got %v", err)
@@ -789,7 +789,7 @@ COMMIT
789789
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
790790
},
791791
}
792-
runner := New(fexec, protocol)
792+
runner := newInternal(fexec, protocol, "", "")
793793
buffer := bytes.NewBuffer(nil)
794794

795795
// Success.
@@ -857,7 +857,7 @@ func testRestore(t *testing.T, protocol Protocol) {
857857
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
858858
},
859859
}
860-
runner := New(fexec, protocol)
860+
runner := newInternal(fexec, protocol, "", "")
861861

862862
// both flags true
863863
err := runner.Restore(TableNAT, []byte{}, FlushTables, RestoreCounters)

pkg/util/iptables/monitor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (mfc *monitorFakeCmd) Stop() {
191191

192192
func TestIPTablesMonitor(t *testing.T) {
193193
mfe := newMonitorFakeExec()
194-
ipt := New(mfe, ProtocolIPv4)
194+
ipt := newInternal(mfe, ProtocolIPv4, "", "")
195195

196196
var reloads uint32
197197
stopCh := make(chan struct{})

0 commit comments

Comments
 (0)