From 54f6729dc83f38c6bd58fb507be697794ddff1b9 Mon Sep 17 00:00:00 2001 From: Ross Guarino Date: Thu, 8 Jun 2017 20:11:00 -0700 Subject: [PATCH] feat(template): wrap IPv6 addresses in [] Add formatIP helper function which will wrap IPv6 addresses in [] and print IPv4 addresses as is. Closes #828 --- controllers/nginx/pkg/template/template.go | 15 +++++++++++++++ .../nginx/pkg/template/template_test.go | 19 +++++++++++++++++++ .../rootfs/etc/nginx/template/nginx.tmpl | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/controllers/nginx/pkg/template/template.go b/controllers/nginx/pkg/template/template.go index dea4ed310..0c0beeff7 100644 --- a/controllers/nginx/pkg/template/template.go +++ b/controllers/nginx/pkg/template/template.go @@ -142,9 +142,24 @@ var ( "hasSuffix": strings.HasSuffix, "toUpper": strings.ToUpper, "toLower": strings.ToLower, + "formatIP": formatIP, } ) +// fomatIP will wrap IPv6 addresses in [] and return IPv4 addresses +// without modification. If the input cannot be parsed as an IP address +// it is returned without modification. +func formatIP(input string) string { + ip := net.ParseIP(input) + if ip == nil { + return input + } + if v4 := ip.To4(); v4 != nil { + return input + } + return fmt.Sprintf("[%s]", input) +} + // buildResolvers returns the resolvers reading the /etc/resolv.conf file func buildResolvers(a interface{}) string { // NGINX need IPV6 addresses to be surrounded by brakets diff --git a/controllers/nginx/pkg/template/template_test.go b/controllers/nginx/pkg/template/template_test.go index 7dfe83ed4..1e1d2ef3d 100644 --- a/controllers/nginx/pkg/template/template_test.go +++ b/controllers/nginx/pkg/template/template_test.go @@ -88,6 +88,25 @@ var ( } ) +func TestFormatIP(t *testing.T) { + cases := map[string]struct { + Input, Output string + }{ + "ipv4-localhost": {"127.0.0.1", "127.0.0.1"}, + "ipv4-internet": {"8.8.8.8", "8.8.8.8"}, + "ipv6-localhost": {"::1", "[::1]"}, + "ipv6-internet": {"2001:4860:4860::8888", "[2001:4860:4860::8888]"}, + "invalid-ip": {"nonsense", "nonsense"}, + "empty-ip": {"", ""}, + } + for k, tc := range cases { + res := formatIP(tc.Input) + if res != tc.Output { + t.Errorf("%s: called formatIp('%s'); expected '%v' but returned '%v'", k, tc.Input, tc.Output, res) + } + } +} + func TestBuildLocation(t *testing.T) { for k, tc := range tmplFuncTestcases { loc := &ingress.Location{ diff --git a/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl b/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl index c49ab8d93..a2e670ad5 100644 --- a/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl +++ b/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl @@ -226,7 +226,7 @@ http { {{ $cfg.LoadBalanceAlgorithm }}; {{ end }} {{ end }} - {{ range $server := $upstream.Endpoints }}server {{ $server.Address }}:{{ $server.Port }} max_fails={{ $server.MaxFails }} fail_timeout={{ $server.FailTimeout }}; + {{ range $server := $upstream.Endpoints }}server {{ $server.Address | formatIP }}:{{ $server.Port }} max_fails={{ $server.MaxFails }} fail_timeout={{ $server.FailTimeout }}; {{ end }} } {{ end }}