Skip to content

Commit d705776

Browse files
committed
add felixConfiguration attr: natOutgoingExclusions
1 parent aa3fb74 commit d705776

21 files changed

+224
-3
lines changed

api/pkg/apis/projectcalico/v3/felixconfig.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ const (
114114
WindowsManageFirewallRulesDisabled WindowsManageFirewallRulesMode = "Disabled"
115115
)
116116

117+
// +kubebuilder:validation:Enum=IPPoolsOnly;IPPoolsAndHostIPs
118+
type NATOutgoingExclusionsType string
119+
120+
const (
121+
NATOutgoingExclusionsIPPoolsOnly NATOutgoingExclusionsType = "IPPoolsOnly"
122+
NATOutgoingExclusionsIPPoolsAndHostIPs NATOutgoingExclusionsType = "IPPoolsAndHostIPs"
123+
)
124+
117125
// FelixConfigurationSpec contains the values of the Felix configuration.
118126
type FelixConfigurationSpec struct {
119127
// UseInternalDataplaneDriver, if true, Felix will use its internal dataplane programming logic. If false, it
@@ -474,6 +482,13 @@ type FelixConfigurationSpec struct {
474482
// (i.e. it uses the iptables MASQUERADE target).
475483
NATOutgoingAddress string `json:"natOutgoingAddress,omitempty"`
476484

485+
// When a IP pool setting `natOutgoing` is true, packets sent from Calico networked containers in this IP pool to destinations will be masqueraded.
486+
// Configure which type of destinations is excluded from being masqueraded.
487+
// - IPPoolsOnly: destinations outside of this IP pool will be masqueraded.
488+
// - IPPoolsAndHostIPs: destinations outside of this IP pool and all hosts will be masqueraded.
489+
// [Default: IPPoolsOnly]
490+
NATOutgoingExclusions *NATOutgoingExclusionsType `json:"natOutgoingExclusions,omitempty" validate:"omitempty,oneof=IPPoolsOnly IPPoolsAndHostIPs"`
491+
477492
// DeviceRouteSourceAddress IPv4 address to set as the source hint for routes programmed by Felix. When not set
478493
// the source address for local traffic from host to workload will be determined by the kernel.
479494
DeviceRouteSourceAddress string `json:"deviceRouteSourceAddress,omitempty"`

api/pkg/apis/projectcalico/v3/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/pkg/openapi/generated.openapi.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

felix/config/config_params.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,10 @@ type Config struct {
394394
FailsafeInboundHostPorts []ProtoPort `config:"port-list;tcp:22,udp:68,tcp:179,tcp:2379,tcp:2380,tcp:5473,tcp:6443,tcp:6666,tcp:6667;die-on-fail"`
395395
FailsafeOutboundHostPorts []ProtoPort `config:"port-list;udp:53,udp:67,tcp:179,tcp:2379,tcp:2380,tcp:5473,tcp:6443,tcp:6666,tcp:6667;die-on-fail"`
396396

397-
KubeNodePortRanges []numorstring.Port `config:"portrange-list;30000:32767"`
398-
NATPortRange numorstring.Port `config:"portrange;"`
399-
NATOutgoingAddress net.IP `config:"ipv4;"`
397+
KubeNodePortRanges []numorstring.Port `config:"portrange-list;30000:32767"`
398+
NATPortRange numorstring.Port `config:"portrange;"`
399+
NATOutgoingAddress net.IP `config:"ipv4;"`
400+
NATOutgoingExclusions string `config:"oneof(IPPoolsOnly,IPPoolsAndHostIPs);IPPoolsOnly"`
400401

401402
UsageReportingEnabled bool `config:"bool;true"`
402403
UsageReportingInitialDelaySecs time.Duration `config:"seconds;300"`

felix/dataplane/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ func StartDataplaneDriver(
278278
NATPortRange: configParams.NATPortRange,
279279
IptablesNATOutgoingInterfaceFilter: configParams.IptablesNATOutgoingInterfaceFilter,
280280
NATOutgoingAddress: configParams.NATOutgoingAddress,
281+
NATOutgoingExclusions: configParams.NATOutgoingExclusions,
281282
BPFEnabled: configParams.BPFEnabled,
282283
BPFForceTrackPacketsFromIfaces: replaceWildcards(configParams.NFTablesMode == "Enabled", configParams.BPFForceTrackPacketsFromIfaces),
283284
ServiceLoopPrevention: configParams.ServiceLoopPrevention,

felix/docs/config-params.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

felix/docs/config-params.md

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

felix/rules/nat.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"sort"
2020
"strings"
2121

22+
apiv3 "github.com/projectcalico/api/pkg/apis/projectcalico/v3"
2223
tcdefs "github.com/projectcalico/calico/felix/bpf/tc/defs"
2324
. "github.com/projectcalico/calico/felix/generictables"
2425
)
@@ -58,6 +59,12 @@ func (r *DefaultRuleRenderer) makeNATOutgoingRuleIPTables(ipVersion uint8, proto
5859
SourceIPSet(masqIPsSetName).
5960
NotDestIPSet(allIPsSetName)
6061

62+
check := apiv3.NATOutgoingExclusionsType(r.Config.NATOutgoingExclusions)
63+
if check == apiv3.NATOutgoingExclusionsIPPoolsAndHostIPs {
64+
allHostsIPsSetName := ipConf.NameForMainIPSet(IPSetIDAllHostNets)
65+
match = match.NotDestIPSet(allHostsIPsSetName)
66+
}
67+
6168
if protocol != "" {
6269
match = match.Protocol(protocol)
6370
}

felix/rules/nat_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ var _ = Describe("NAT", func() {
5959
},
6060
}))
6161
})
62+
It("should render rules when active with all hosts NAT exclusion", func() {
63+
localConfig := rrConfigNormal
64+
rrConfigNormal.NATOutgoingExclusions = "IPPoolsAndHostIPs"
65+
renderer = NewRenderer(localConfig)
66+
67+
Expect(renderer.NATOutgoingChain(true, 4)).To(Equal(&generictables.Chain{
68+
Name: "cali-nat-outgoing",
69+
Rules: []generictables.Rule{
70+
{
71+
Action: MasqAction{},
72+
Match: Match().
73+
SourceIPSet("cali40masq-ipam-pools").
74+
NotDestIPSet("cali40all-ipam-pools").
75+
NotDestIPSet("cali40all-hosts-net"),
76+
},
77+
},
78+
}))
79+
})
6280
It("should render rules when active with an explicit SNAT address", func() {
6381
snatAddress := "192.168.0.1"
6482
localConfig := rrConfigNormal

felix/rules/rule_defs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ type Config struct {
354354
IptablesNATOutgoingInterfaceFilter string
355355

356356
NATOutgoingAddress net.IP
357+
NATOutgoingExclusions string
357358
BPFEnabled bool
358359
BPFForceTrackPacketsFromIfaces []string
359360
ServiceLoopPrevention string

libcalico-go/config/crd/crd.projectcalico.org_felixconfigurations.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/calico-bpf.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/calico-policy-only.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/calico-typha.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/calico-vxlan.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/calico.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/canal.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/crds.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/flannel-migration/calico.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)