Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 2de59eb

Browse files
committed
Reduce leaky abstraction by not passing configurator to generateIPTables
Signed-off-by: Keith Mattix II <[email protected]>
1 parent e4c67f2 commit 2de59eb

File tree

3 files changed

+7
-22
lines changed

3 files changed

+7
-22
lines changed

pkg/injector/init_container.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
func getInitContainerSpec(containerName string, cfg configurator.Configurator, outboundIPRangeExclusionList []string,
1111
outboundIPRangeInclusionList []string, outboundPortExclusionList []int,
1212
inboundPortExclusionList []int, enablePrivilegedInitContainer bool, pullPolicy corev1.PullPolicy, networkInterfaceExclusionList []string) corev1.Container {
13-
iptablesInitCommand := generateIptablesCommands(cfg, outboundIPRangeExclusionList, outboundIPRangeInclusionList, outboundPortExclusionList, inboundPortExclusionList, networkInterfaceExclusionList)
13+
proxyMode := cfg.GetMeshConfig().Spec.Sidecar.LocalProxyMode
14+
iptablesInitCommand := generateIptablesCommands(proxyMode, outboundIPRangeExclusionList, outboundIPRangeInclusionList, outboundPortExclusionList, inboundPortExclusionList, networkInterfaceExclusionList)
1415

1516
return corev1.Container{
1617
Name: containerName,

pkg/injector/iptables.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import (
77

88
configv1alpha2 "github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"
99

10-
"github.com/openservicemesh/osm/pkg/configurator"
1110
"github.com/openservicemesh/osm/pkg/constants"
1211
)
1312

14-
func genIPTablesOutboundStaticRules(cfg configurator.Configurator) []string {
13+
func genIPTablesOutboundStaticRules(proxyMode configv1alpha2.LocalProxyMode) []string {
1514
// iptablesOutboundStaticRules is the list of iptables rules related to outbound traffic interception and redirection
1615
iptablesOutboundStaticRules := []string{
1716
// Redirects outbound TCP traffic hitting OSM_PROXY_OUT_REDIRECT chain to Envoy's outbound listener port
@@ -21,9 +20,7 @@ func genIPTablesOutboundStaticRules(cfg configurator.Configurator) []string {
2120
fmt.Sprintf("-A OSM_PROXY_OUT_REDIRECT -p tcp --dport %d -j ACCEPT", constants.EnvoyAdminPort),
2221
}
2322

24-
localProxyMode := cfg.GetMeshConfig().Spec.Sidecar.LocalProxyMode
25-
26-
if localProxyMode == configv1alpha2.LocalProxyModePodIP {
23+
if proxyMode == configv1alpha2.LocalProxyModePodIP {
2724
// For envoy -> local service container proxying, send traffic to pod IP instead of localhost
2825
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))
2926
}
@@ -76,7 +73,7 @@ var iptablesInboundStaticRules = []string{
7673
}
7774

7875
// generateIptablesCommands generates a list of iptables commands to set up sidecar interception and redirection
79-
func generateIptablesCommands(cfg configurator.Configurator, outboundIPRangeExclusionList []string, outboundIPRangeInclusionList []string, outboundPortExclusionList []int, inboundPortExclusionList []int, networkInterfaceExclusionList []string) string {
76+
func generateIptablesCommands(proxyMode configv1alpha2.LocalProxyMode, outboundIPRangeExclusionList []string, outboundIPRangeInclusionList []string, outboundPortExclusionList []int, inboundPortExclusionList []int, networkInterfaceExclusionList []string) string {
8077
var rules strings.Builder
8178

8279
fmt.Fprintln(&rules, `# OSM sidecar interception rules
@@ -108,7 +105,7 @@ func generateIptablesCommands(cfg configurator.Configurator, outboundIPRangeExcl
108105
cmds = append(cmds, rule)
109106
}
110107

111-
iptablesOutboundStaticRules := genIPTablesOutboundStaticRules(cfg)
108+
iptablesOutboundStaticRules := genIPTablesOutboundStaticRules(proxyMode)
112109

113110
// 3. Create outbound rules
114111
cmds = append(cmds, iptablesOutboundStaticRules...)

pkg/injector/iptables_test.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ package injector
33
import (
44
"testing"
55

6-
"github.com/golang/mock/gomock"
7-
. "github.com/onsi/ginkgo"
86
"github.com/stretchr/testify/assert"
97

108
configv1alpha2 "github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"
11-
12-
"github.com/openservicemesh/osm/pkg/configurator"
139
)
1410

1511
func TestGenerateIptablesCommands(t *testing.T) {
@@ -132,17 +128,8 @@ EOF
132128
for _, tc := range testCases {
133129
t.Run(tc.name, func(t *testing.T) {
134130
a := assert.New(t)
135-
mockCtrl := gomock.NewController(GinkgoT())
136-
mockConfigurator := configurator.NewMockConfigurator(mockCtrl)
137-
mockConfigurator.EXPECT().GetMeshConfig().Return(configv1alpha2.MeshConfig{
138-
Spec: configv1alpha2.MeshConfigSpec{
139-
Sidecar: configv1alpha2.SidecarSpec{
140-
LocalProxyMode: tc.proxyMode,
141-
},
142-
},
143-
}).Times(1)
144131

145-
actual := generateIptablesCommands(mockConfigurator, tc.outboundIPRangeExclusions, tc.outboundIPRangeInclusions, tc.outboundPortExclusions, tc.inboundPortExclusions, tc.networkInterfaceExclusions)
132+
actual := generateIptablesCommands(tc.proxyMode, tc.outboundIPRangeExclusions, tc.outboundIPRangeInclusions, tc.outboundPortExclusions, tc.inboundPortExclusions, tc.networkInterfaceExclusions)
146133
a.Equal(tc.expected, actual)
147134
})
148135
}

0 commit comments

Comments
 (0)