@@ -50,7 +50,6 @@ import (
50
50
"k8s.io/kubernetes/pkg/proxy/nftables"
51
51
proxyutil "k8s.io/kubernetes/pkg/proxy/util"
52
52
utiliptables "k8s.io/kubernetes/pkg/util/iptables"
53
- "k8s.io/utils/exec"
54
53
)
55
54
56
55
// timeoutForNodePodCIDR is the time to wait for allocators to assign a PodCIDR to the
@@ -105,37 +104,15 @@ func isIPTablesBased(mode proxyconfigapi.ProxyMode) bool {
105
104
return mode == proxyconfigapi .ProxyModeIPTables || mode == proxyconfigapi .ProxyModeIPVS
106
105
}
107
106
108
- // getIPTables returns an array of [IPv4, IPv6] utiliptables.Interfaces. If primaryFamily
109
- // is not v1.IPFamilyUnknown then it will also separately return the interface for just
110
- // that family.
111
- func getIPTables (primaryFamily v1.IPFamily ) ([2 ]utiliptables.Interface , utiliptables.Interface ) {
112
- execer := exec .New ()
113
-
114
- // Create iptables handlers for both families. Always ordered as IPv4, IPv6
115
- ipt := [2 ]utiliptables.Interface {
116
- utiliptables .New (execer , utiliptables .ProtocolIPv4 ),
117
- utiliptables .New (execer , utiliptables .ProtocolIPv6 ),
118
- }
119
-
120
- var iptInterface utiliptables.Interface
121
- if primaryFamily == v1 .IPv4Protocol {
122
- iptInterface = ipt [0 ]
123
- } else if primaryFamily == v1 .IPv6Protocol {
124
- iptInterface = ipt [1 ]
125
- }
126
-
127
- return ipt , iptInterface
128
- }
129
-
130
107
// platformCheckSupported is called immediately before creating the Proxier, to check
131
108
// what IP families are supported (and whether the configuration is usable at all).
132
109
func (s * ProxyServer ) platformCheckSupported (ctx context.Context ) (ipv4Supported , ipv6Supported , dualStackSupported bool , err error ) {
133
110
logger := klog .FromContext (ctx )
134
111
135
112
if isIPTablesBased (s .Config .Mode ) {
136
- ipt , _ := getIPTables ( v1 . IPFamilyUnknown )
137
- ipv4Supported = ipt [ 0 ]. Present ()
138
- ipv6Supported = ipt [ 1 ]. Present ()
113
+ ipts := utiliptables . NewDualStack ( )
114
+ ipv4Supported = ipts [ v1 . IPv4Protocol ] != nil
115
+ ipv6Supported = ipts [ v1 . IPv6Protocol ] != nil
139
116
140
117
if ! ipv4Supported && ! ipv6Supported {
141
118
err = fmt .Errorf ("iptables is not available on this host" )
@@ -166,14 +143,13 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
166
143
167
144
if config .Mode == proxyconfigapi .ProxyModeIPTables {
168
145
logger .Info ("Using iptables Proxier" )
146
+ ipts := utiliptables .NewDualStack ()
169
147
170
148
if dualStack {
171
- ipt , _ := getIPTables (s .PrimaryIPFamily )
172
-
173
149
// TODO this has side effects that should only happen when Run() is invoked.
174
150
proxier , err = iptables .NewDualStackProxier (
175
151
ctx ,
176
- ipt ,
152
+ ipts ,
177
153
utilsysctl .New (),
178
154
config .SyncPeriod .Duration ,
179
155
config .MinSyncPeriod .Duration ,
@@ -190,13 +166,12 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
190
166
)
191
167
} else {
192
168
// Create a single-stack proxier if and only if the node does not support dual-stack (i.e, no iptables support).
193
- _ , iptInterface := getIPTables (s .PrimaryIPFamily )
194
169
195
170
// TODO this has side effects that should only happen when Run() is invoked.
196
171
proxier , err = iptables .NewProxier (
197
172
ctx ,
198
173
s .PrimaryIPFamily ,
199
- iptInterface ,
174
+ ipts [ s . PrimaryIPFamily ] ,
200
175
utilsysctl .New (),
201
176
config .SyncPeriod .Duration ,
202
177
config .MinSyncPeriod .Duration ,
@@ -222,13 +197,13 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
222
197
if err := ipvs .CanUseIPVSProxier (ctx , ipvsInterface , ipsetInterface , config .IPVS .Scheduler ); err != nil {
223
198
return nil , fmt .Errorf ("can't use the IPVS proxier: %v" , err )
224
199
}
200
+ ipts := utiliptables .NewDualStack ()
225
201
226
202
logger .Info ("Using ipvs Proxier" )
227
203
if dualStack {
228
- ipt , _ := getIPTables (s .PrimaryIPFamily )
229
204
proxier , err = ipvs .NewDualStackProxier (
230
205
ctx ,
231
- ipt ,
206
+ ipts ,
232
207
ipvsInterface ,
233
208
ipsetInterface ,
234
209
utilsysctl .New (),
@@ -251,11 +226,10 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
251
226
initOnly ,
252
227
)
253
228
} else {
254
- _ , iptInterface := getIPTables (s .PrimaryIPFamily )
255
229
proxier , err = ipvs .NewProxier (
256
230
ctx ,
257
231
s .PrimaryIPFamily ,
258
- iptInterface ,
232
+ ipts [ s . PrimaryIPFamily ] ,
259
233
ipvsInterface ,
260
234
ipsetInterface ,
261
235
utilsysctl .New (),
@@ -509,7 +483,7 @@ func platformCleanup(ctx context.Context, mode proxyconfigapi.ProxyMode, cleanup
509
483
510
484
// Clean up iptables and ipvs rules if switching to nftables, or if cleanupAndExit
511
485
if ! isIPTablesBased (mode ) || cleanupAndExit {
512
- ipts , _ := getIPTables ( v1 . IPFamilyUnknown )
486
+ ipts := utiliptables . NewDualStack ( )
513
487
ipsetInterface := utilipset .New ()
514
488
ipvsInterface := utilipvs .New ()
515
489
0 commit comments