Skip to content

Pod in nat-outgoing should not be SNATed when it accesses local cluster #8961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -122,6 +122,14 @@ const (
FlowLogsPolicyEvaluationModeContinuous FlowLogsPolicyEvaluationModeType = "Continuous"
)

// +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 @@ -482,6 +490,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 @@ -412,9 +412,10 @@ type Config struct {
FlowLogsLocalReporter string `config:"oneof(Enabled,Disabled);Disabled"`
FlowLogsPolicyEvaluationMode string `config:"oneof(None,Continuous);Continuous"`

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 @@ -288,6 +288,7 @@ func StartDataplaneDriver(
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
29 changes: 29 additions & 0 deletions felix/docs/config-params.json

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

16 changes: 16 additions & 0 deletions felix/docs/config-params.md

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

8 changes: 8 additions & 0 deletions felix/rules/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ 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 +60,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 @@ -60,6 +60,24 @@ var _ = Describe("NAT", func() {
},
}))
})
It("should render rules when active with all hosts NAT exclusion", func() {
localConfig := rrConfigNormal
localConfig.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 @@ -434,6 +434,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.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const (
)

const (
numBaseFelixConfigs = 161
numBaseFelixConfigs = 162
)

var _ = Describe("Test the generic configuration update processor and the concrete implementations", func() {
Expand Down
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.

Loading