@@ -10,42 +10,31 @@ import (
10
10
"github.com/openservicemesh/osm/pkg/constants"
11
11
)
12
12
13
- func genIPTablesOutboundStaticRules (proxyMode configv1alpha2.LocalProxyMode ) []string {
14
- // iptablesOutboundStaticRules is the list of iptables rules related to outbound traffic interception and redirection
15
- iptablesOutboundStaticRules := []string {
16
- // Redirects outbound TCP traffic hitting OSM_PROXY_OUT_REDIRECT chain to Envoy's outbound listener port
17
- fmt .Sprintf ("-A OSM_PROXY_OUT_REDIRECT -p tcp -j REDIRECT --to-port %d" , constants .EnvoyOutboundListenerPort ),
18
-
19
- // Traffic to the Proxy Admin port flows to the Proxy -- not redirected
20
- fmt .Sprintf ("-A OSM_PROXY_OUT_REDIRECT -p tcp --dport %d -j ACCEPT" , constants .EnvoyAdminPort ),
21
- }
22
-
23
- if proxyMode == configv1alpha2 .LocalProxyModePodIP {
24
- // For envoy -> local service container proxying, send traffic to pod IP instead of localhost
25
- iptablesOutboundStaticRules = append (iptablesOutboundStaticRules , fmt .Sprintf ("-A OUTPUT -p tcp -o lo -d 127.0.0.1/32 -m owner --uid-owner %d -j DNAT --to-destination $POD_IP" , constants .EnvoyUID ))
26
- }
13
+ // iptablesOutboundStaticRules is the list of iptables rules related to outbound traffic interception and redirection
14
+ var iptablesOutboundStaticRules = []string {
15
+ // Redirects outbound TCP traffic hitting OSM_PROXY_OUT_REDIRECT chain to Envoy's outbound listener port
16
+ fmt .Sprintf ("-A OSM_PROXY_OUT_REDIRECT -p tcp -j REDIRECT --to-port %d" , constants .EnvoyOutboundListenerPort ),
27
17
28
- iptablesOutboundStaticRules = append (iptablesOutboundStaticRules , []string {
29
- // For all other outbound TCP traffic jump from OUTPUT chain to OSM_PROXY_OUTBOUND chain
30
- "-A OUTPUT -p tcp -j OSM_PROXY_OUTBOUND" ,
18
+ // Traffic to the Proxy Admin port flows to the Proxy -- not redirected
19
+ fmt .Sprintf ("-A OSM_PROXY_OUT_REDIRECT -p tcp --dport %d -j ACCEPT" , constants .EnvoyAdminPort ),
31
20
32
- // Outbound traffic from Envoy to the local app over the loopback interface should jump to the inbound proxy redirect chain.
33
- // So when an app directs traffic to itself via the k8s service, traffic flows as follows:
34
- // app -> local envoy's outbound listener -> iptables -> local envoy's inbound listener -> app
35
- fmt .Sprintf ("-A OSM_PROXY_OUTBOUND -o lo ! -d 127.0.0.1/32 -m owner --uid-owner %d -j OSM_PROXY_IN_REDIRECT" , constants .EnvoyUID ),
21
+ // For outbound TCP traffic jump from OUTPUT chain to OSM_PROXY_OUTBOUND chain
22
+ "-A OUTPUT -p tcp -j OSM_PROXY_OUTBOUND" ,
36
23
37
- // Outbound traffic from the app to itself over the loopback interface is not be redirected via the proxy.
38
- // E.g. when app sends traffic to itself via the pod IP.
39
- fmt .Sprintf ("-A OSM_PROXY_OUTBOUND -o lo -m owner ! --uid-owner %d -j RETURN" , constants .EnvoyUID ),
24
+ // Outbound traffic from Envoy to the local app over the loopback interface should jump to the inbound proxy redirect chain.
25
+ // So when an app directs traffic to itself via the k8s service, traffic flows as follows:
26
+ // app -> local envoy's outbound listener -> iptables -> local envoy's inbound listener -> app
27
+ fmt .Sprintf ("-A OSM_PROXY_OUTBOUND -o lo ! -d 127.0.0.1/32 -m owner --uid-owner %d -j OSM_PROXY_IN_REDIRECT" , constants .EnvoyUID ),
40
28
41
- // Don't redirect Envoy traffic back to itself, return it to the next chain for processing
42
- fmt .Sprintf ("-A OSM_PROXY_OUTBOUND -m owner --uid-owner %d -j RETURN" , constants .EnvoyUID ),
29
+ // Outbound traffic from the app to itself over the loopback interface is not be redirected via the proxy.
30
+ // E.g. when app sends traffic to itself via the pod IP.
31
+ fmt .Sprintf ("-A OSM_PROXY_OUTBOUND -o lo -m owner ! --uid-owner %d -j RETURN" , constants .EnvoyUID ),
43
32
44
- // Skip localhost traffic, doesn't need to be routed via the proxy
45
- "-A OSM_PROXY_OUTBOUND -d 127.0.0.1/32 -j RETURN" ,
46
- }... )
33
+ // Don't redirect Envoy traffic back to itself, return it to the next chain for processing
34
+ fmt .Sprintf ("-A OSM_PROXY_OUTBOUND -m owner --uid-owner %d -j RETURN" , constants .EnvoyUID ),
47
35
48
- return iptablesOutboundStaticRules
36
+ // Skip localhost traffic, doesn't need to be routed via the proxy
37
+ "-A OSM_PROXY_OUTBOUND -d 127.0.0.1/32 -j RETURN" ,
49
38
}
50
39
51
40
// iptablesInboundStaticRules is the list of iptables rules related to inbound traffic interception and redirection
@@ -105,11 +94,16 @@ func generateIptablesCommands(proxyMode configv1alpha2.LocalProxyMode, outboundI
105
94
cmds = append (cmds , rule )
106
95
}
107
96
108
- iptablesOutboundStaticRules := genIPTablesOutboundStaticRules (proxyMode )
109
-
110
97
// 3. Create outbound rules
111
98
cmds = append (cmds , iptablesOutboundStaticRules ... )
112
99
100
+ if proxyMode == configv1alpha2 .LocalProxyModePodIP {
101
+ // For envoy -> local service container proxying, send traffic to pod IP instead of localhost
102
+ // *Note: it is important to use the insert option '-I' instead of the append option '-A' to ensure the
103
+ // DNAT to the pod ip for envoy -> localhost traffic happens before the rule that redirects traffic to the proxy
104
+ iptablesOutboundStaticRules = append (cmds , fmt .Sprintf ("-I OUTPUT -p tcp -o lo -d 127.0.0.1/32 -m owner --uid-owner %d -j DNAT --to-destination $POD_IP" , constants .EnvoyUID ))
105
+ }
106
+
113
107
// Ignore outbound traffic in specified interfaces
114
108
for _ , iface := range networkInterfaceExclusionList {
115
109
cmds = append (cmds , fmt .Sprintf ("-A OSM_PROXY_OUTBOUND -o %s -j RETURN" , iface ))
0 commit comments