Skip to content
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

[agent] Adjust policy handling for IPv6 addresses #57

Merged
merged 1 commit into from
Aug 1, 2024
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
21 changes: 18 additions & 3 deletions internal/agent/nftables_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,23 @@ import (
"github.com/cloudandheat/ch-k8s-lbaas/internal/model"
)

var funcMap = template.FuncMap{
// A very simplistic check if a string looks like an IPv4 address
"isIPv4Address": func (ipString string) bool {
return strings.Count(ipString, ":") == 0 && strings.Count(ipString, ".") == 3
},
// A very simplistic check if a string looks like an IPv6 address
"isIPv6Address": func (ipString string) bool {
return strings.Count(ipString, ":") >= 2
},
// Replace colons with dash
"replaceColons": func (ipString string) string {
return strings.ReplaceAll(ipString, ":", "-")
},
}

var (
nftablesTemplate = template.Must(template.New("nftables.conf").Parse(`
nftablesTemplate = template.Must(template.New("nftables.conf").Funcs(funcMap).Parse(`
{{ $cfg := . }}

{{- if $cfg.PartialReload }}
Expand All @@ -57,14 +72,14 @@ delete chain {{ $cfg.FilterTableType }} {{ $cfg.FilterTableName }} {{ $chain }}
table {{ .FilterTableType }} {{ .FilterTableName }} {
chain {{ .FilterForwardChainName }} {
{{- range $dest := $cfg.PolicyAssignments }}
ct mark {{ $cfg.FWMarkBits | printf "0x%x" }} and {{ $cfg.FWMarkMask | printf "0x%x" }} ip daddr {{ $dest.Address }} goto {{ $cfg.PolicyPrefix }}POD-{{ $dest.Address }};
ct mark {{ $cfg.FWMarkBits | printf "0x%x" }} and {{ $cfg.FWMarkMask | printf "0x%x" }} {{if isIPv4Address $dest.Address }}ip{{else if isIPv6Address $dest.Address}}ip6{{end}} daddr {{ $dest.Address }} goto {{ $cfg.PolicyPrefix }}POD-{{replaceColons $dest.Address}};
{{- end }}
ct mark {{ $cfg.FWMarkBits | printf "0x%x" }} and {{ $cfg.FWMarkMask | printf "0x%x" }} accept;
}

# Using uppercase POD to prevent collisions with policy names like 'pod-x.x.x.x'
{{- range $pod := $cfg.PolicyAssignments }}
chain {{ $cfg.PolicyPrefix }}POD-{{ $pod.Address }} {
chain {{ $cfg.PolicyPrefix }}POD-{{replaceColons $pod.Address}} {
{{- range $pol := $pod.NetworkPolicies }}
jump {{ $cfg.PolicyPrefix }}{{ $pol }};
{{- end }}
Expand Down
12 changes: 12 additions & 0 deletions internal/agent/nftables_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ func TestNftablesStructuredConfigFromNonEmptyLBModel(t *testing.T) {
"block-range",
},
},
{
Address: "ff00::1",
NetworkPolicies: []string{
"allow-http",
},
},
{
Address: "ff00::2",
NetworkPolicies: []string{
"block-range",
},
},
},
}

Expand Down
Loading