Skip to content

Commit edc6086

Browse files
committed
Avoid passing extra ipv6 flag around
1 parent 94489ef commit edc6086

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

cni-plugin/main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,16 @@ func cmdAdd(args *skel.CmdArgs) error {
299299
options.IPTablesMode = cmd.IPTablesModeLegacy
300300
}
301301

302-
if err := buildAndConfigure(logEntry, &options, false); err != nil {
302+
// always trigger the IPv4 rules
303+
optIPv4 := options
304+
optIPv4.IPv6 = false
305+
if err := buildAndConfigure(logEntry, &optIPv4); err != nil {
303306
return err
304307
}
308+
309+
// trigger the IPv6 rules
305310
if options.IPv6 {
306-
if err := buildAndConfigure(logEntry, &options, true); err != nil {
311+
if err := buildAndConfigure(logEntry, &options); err != nil {
307312
return err
308313
}
309314
}
@@ -357,8 +362,8 @@ func getAPIServerPorts(ctx context.Context, api *kubernetes.Clientset) ([]string
357362
return ports, nil
358363
}
359364

360-
func buildAndConfigure(logEntry *logrus.Entry, options *cmd.RootOptions, ipv6 bool) error {
361-
firewallConfiguration, err := cmd.BuildFirewallConfiguration(options, ipv6)
365+
func buildAndConfigure(logEntry *logrus.Entry, options *cmd.RootOptions) error {
366+
firewallConfiguration, err := cmd.BuildFirewallConfiguration(options)
362367
if err != nil {
363368
logEntry.Errorf("linkerd-cni: could not create a Firewall Configuration from the options: %v", options)
364369
return err
@@ -367,7 +372,7 @@ func buildAndConfigure(logEntry *logrus.Entry, options *cmd.RootOptions, ipv6 bo
367372
err = iptables.ConfigureFirewall(*firewallConfiguration)
368373
// We couldn't find a robust way of checking IPv6 support besides trying to just call ip6tables-save.
369374
// If IPv4 rules worked but not IPv6, let's not fail the container (the actual problem will get logged).
370-
if !ipv6 && err != nil {
375+
if !options.IPv6 && err != nil {
371376
logEntry.Errorf("linkerd-cni: could not configure firewall: %s", err)
372377
return err
373378
}

proxy-init/cmd/root.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
const (
1818
// IPTablesModeLegacy signals the usage of the iptables-legacy commands
1919
IPTablesModeLegacy = "legacy"
20-
// ipTablesModeNFT signals the usage of the iptables-nft commands
21-
ipTablesModeNFT = "nft"
20+
// IPTablesModeNFT signals the usage of the iptables-nft commands
21+
IPTablesModeNFT = "nft"
2222

2323
cmdLegacy = "iptables-legacy"
2424
cmdLegacySave = "iptables-legacy-save"
@@ -102,7 +102,10 @@ func NewRootCmd() *cobra.Command {
102102
return err
103103
}
104104

105-
config, err := BuildFirewallConfiguration(options, false)
105+
// always trigger the IPv4 rules
106+
optIPv4 := *options
107+
optIPv4.IPv6 = false
108+
config, err := BuildFirewallConfiguration(&optIPv4)
106109
if err != nil {
107110
return err
108111
}
@@ -115,7 +118,8 @@ func NewRootCmd() *cobra.Command {
115118
return nil
116119
}
117120

118-
config, err = BuildFirewallConfiguration(options, true)
121+
// trigger the IPv6 rules
122+
config, err = BuildFirewallConfiguration(options)
119123
if err != nil {
120124
return err
121125
}
@@ -157,12 +161,12 @@ func NewRootCmd() *cobra.Command {
157161
}
158162

159163
// BuildFirewallConfiguration returns an iptables FirewallConfiguration suitable to use to configure iptables.
160-
func BuildFirewallConfiguration(options *RootOptions, ipv6 bool) (*iptables.FirewallConfiguration, error) {
164+
func BuildFirewallConfiguration(options *RootOptions) (*iptables.FirewallConfiguration, error) {
161165
if options.FirewallBinPath != "" || options.FirewallSaveBinPath != "" {
162166
return nil, errors.New("--firewal-bin-path and firewall-save-bin-path are no longer supported; please use --iptables-mode instead")
163167
}
164168

165-
if options.IPTablesMode != IPTablesModeLegacy && options.IPTablesMode != ipTablesModeNFT {
169+
if options.IPTablesMode != IPTablesModeLegacy && options.IPTablesMode != IPTablesModeNFT {
166170
return nil, errors.New("--iptables-mode valid values are only \"legacy\" and \"nft\"")
167171
}
168172

@@ -174,7 +178,7 @@ func BuildFirewallConfiguration(options *RootOptions, ipv6 bool) (*iptables.Fire
174178
return nil, fmt.Errorf("--outgoing-proxy-port must be a valid TCP port number")
175179
}
176180

177-
cmd, cmdSave := getCommands(options.IPTablesMode, ipv6)
181+
cmd, cmdSave := getCommands(options)
178182

179183
sanitizedSubnets := []string{}
180184
for _, subnet := range options.SubnetsToIgnore {
@@ -220,15 +224,15 @@ func getFormatter(format string) log.Formatter {
220224
}
221225
}
222226

223-
func getCommands(mode string, ipv6 bool) (string, string) {
224-
if mode == IPTablesModeLegacy {
225-
if ipv6 {
227+
func getCommands(options *RootOptions) (string, string) {
228+
if options.IPTablesMode == IPTablesModeLegacy {
229+
if options.IPv6 {
226230
return cmdLegacyIPv6, cmdLegacyIPv6Save
227231
}
228232
return cmdLegacy, cmdLegacySave
229233
}
230234

231-
if ipv6 {
235+
if options.IPv6 {
232236
return cmdNFTIPv6, cmdNFTIPv6Save
233237
}
234238

proxy-init/cmd/root_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ func TestBuildFirewallConfiguration(t *testing.T) {
3131
options.IncomingProxyPort = expectedIncomingProxyPort
3232
options.OutgoingProxyPort = expectedOutgoingProxyPort
3333
options.ProxyUserID = expectedProxyUserID
34+
options.IPv6 = false
3435

35-
config, err := BuildFirewallConfiguration(options, false)
36+
config, err := BuildFirewallConfiguration(options)
3637
if err != nil {
3738
t.Fatalf("Unexpected error: %s", err)
3839
}
@@ -87,7 +88,7 @@ func TestBuildFirewallConfiguration(t *testing.T) {
8788
errorMessage: "0.0.0.0 is not a valid CIDR address",
8889
},
8990
} {
90-
_, err := BuildFirewallConfiguration(tt.options, false)
91+
_, err := BuildFirewallConfiguration(tt.options)
9192
if err == nil {
9293
t.Fatalf("Expected error for config [%v], got nil", tt.options)
9394
}
@@ -112,7 +113,7 @@ func TestBuildFirewallConfiguration(t *testing.T) {
112113
errorMessage: "",
113114
},
114115
} {
115-
_, err := BuildFirewallConfiguration(tt.options, false)
116+
_, err := BuildFirewallConfiguration(tt.options)
116117
if err != nil {
117118
t.Fatalf("Got error error for config [%v]", tt.options)
118119
}

0 commit comments

Comments
 (0)