Skip to content

Commit

Permalink
add felixConfiguration attr: natOutgoingExclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
wayne-cheng committed Nov 6, 2024
1 parent c914980 commit 829de87
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 3 deletions.
15 changes: 15 additions & 0 deletions api/pkg/apis/projectcalico/v3/felixconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ const (
WindowsManageFirewallRulesDisabled WindowsManageFirewallRulesMode = "Disabled"
)

// +kubebuilder:validation:Enum=IPPoolsOnly;IPPoolsAndHostIPs
type NATOutgoingExclusionsType string

const (
NATOutgoingExclusionsIPPoolsOnly NATOutgoingExclusionsType = "IPPoolsOnly"
NATOutgoingExclusionsIPPoolsAndHostIPs NATOutgoingExclusionsType = "IPPoolsAndHostIPs"
)

// FelixConfigurationSpec contains the values of the Felix configuration.
type FelixConfigurationSpec struct {
// UseInternalDataplaneDriver, if true, Felix will use its internal dataplane programming logic. If false, it
Expand Down Expand Up @@ -465,6 +473,13 @@ type FelixConfigurationSpec struct {
// (i.e. it uses the iptables MASQUERADE target).
NATOutgoingAddress string `json:"natOutgoingAddress,omitempty"`

// When a IP pool setting `natOutgoing` is true, packets sent from Calico networked containers in this IP pool to destinations will be masqueraded.
// Configure which type of destinations is excluded from being masqueraded.
// - IPPoolsOnly: destinations outside of this IP pool will be masqueraded.
// - IPPoolsAndHostIPs: destinations outside of this IP pool and all hosts will be masqueraded.
// [Default: IPPoolsOnly]
NATOutgoingExclusions *NATOutgoingExclusionsType `json:"natOutgoingExclusions,omitempty" validate:"omitempty,oneof=IPPoolsOnly IPPoolsAndHostIPs"`

// DeviceRouteSourceAddress IPv4 address to set as the source hint for routes programmed by Felix. When not set
// the source address for local traffic from host to workload will be determined by the kernel.
DeviceRouteSourceAddress string `json:"deviceRouteSourceAddress,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions api/pkg/apis/projectcalico/v3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions api/pkg/openapi/generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions felix/config/config_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,10 @@ type Config struct {
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"`
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"`

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

UsageReportingEnabled bool `config:"bool;true"`
UsageReportingInitialDelaySecs time.Duration `config:"seconds;300"`
Expand Down
1 change: 1 addition & 0 deletions felix/dataplane/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ func StartDataplaneDriver(configParams *config.Config,
NATPortRange: configParams.NATPortRange,
IptablesNATOutgoingInterfaceFilter: configParams.IptablesNATOutgoingInterfaceFilter,
NATOutgoingAddress: configParams.NATOutgoingAddress,
NATOutgoingExclusions: configParams.NATOutgoingExclusions,
BPFEnabled: configParams.BPFEnabled,
BPFForceTrackPacketsFromIfaces: replaceWildcards(configParams.NFTablesMode == "Enabled", configParams.BPFForceTrackPacketsFromIfaces),
ServiceLoopPrevention: configParams.ServiceLoopPrevention,
Expand Down
7 changes: 7 additions & 0 deletions felix/rules/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sort"
"strings"

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

check := apiv3.NATOutgoingExclusionsType(r.Config.NATOutgoingExclusions)
if check == apiv3.NATOutgoingExclusionsIPPoolsAndHostIPs {
allHostsIPsSetName := ipConf.NameForMainIPSet(IPSetIDAllHostNets)
match = match.NotDestIPSet(allHostsIPsSetName)
}

if protocol != "" {
match = match.Protocol(protocol)
}
Expand Down
18 changes: 18 additions & 0 deletions felix/rules/nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ var _ = Describe("NAT", func() {
},
}))
})
It("should render rules when active with all hosts NAT exclusion", func() {
localConfig := rrConfigNormal
rrConfigNormal.NATOutgoingExclusions = "IPPoolsAndHostIPs"
renderer = NewRenderer(localConfig)

Expect(renderer.NATOutgoingChain(true, 4)).To(Equal(&generictables.Chain{
Name: "cali-nat-outgoing",
Rules: []generictables.Rule{
{
Action: MasqAction{},
Match: Match().
SourceIPSet("cali40masq-ipam-pools").
NotDestIPSet("cali40all-ipam-pools").
NotDestIPSet("cali40all-hosts-net"),
},
},
}))
})
It("should render rules when active with an explicit SNAT address", func() {
snatAddress := "192.168.0.1"
localConfig := rrConfigNormal
Expand Down
1 change: 1 addition & 0 deletions felix/rules/rule_defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ type Config struct {
IptablesNATOutgoingInterfaceFilter string

NATOutgoingAddress net.IP
NATOutgoingExclusions string
BPFEnabled bool
BPFForceTrackPacketsFromIfaces []string
ServiceLoopPrevention string
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions manifests/calico-bpf.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions manifests/calico-policy-only.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions manifests/calico-typha.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions manifests/calico-vxlan.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions manifests/calico.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions manifests/canal.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions manifests/crds.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions manifests/flannel-migration/calico.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions manifests/ocp/crd.projectcalico.org_felixconfigurations.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions manifests/operator-crds.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions manifests/tigera-operator.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 829de87

Please sign in to comment.