From e12138f4dc720f547bbd585dd19241e63cdb88ee Mon Sep 17 00:00:00 2001 From: Fernando Diaz Date: Thu, 17 Aug 2017 12:05:01 -0500 Subject: [PATCH] Remove any aliases that conflict with a hostname Removes the alias association if an existing server with the same hostname as the alias exists. This is done to disallow any duplicate server creation when the alias annotation is provided. --- controllers/nginx/pkg/template/template.go | 2 +- controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl | 9 +++++---- core/pkg/ingress/annotations/alias/main.go | 7 +++---- core/pkg/ingress/controller/controller.go | 10 ++++++++++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/controllers/nginx/pkg/template/template.go b/controllers/nginx/pkg/template/template.go index 605434e03..d4dc1379b 100644 --- a/controllers/nginx/pkg/template/template.go +++ b/controllers/nginx/pkg/template/template.go @@ -490,4 +490,4 @@ func buildNextUpstream(input interface{}) string { } return strings.Join(nextUpstreamCodes, " ") -} +} \ No newline at end of file diff --git a/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl b/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl index 4b0eada94..13d750819 100644 --- a/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl +++ b/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl @@ -1,4 +1,5 @@ {{ $all := . }} +{{ $servers := .Servers }} {{ $cfg := .Cfg }} {{ $IsIPV6Enabled := .IsIPV6Enabled }} {{ $healthzURI := .HealthzURI }} @@ -269,7 +270,7 @@ http { {{ end }} {{/* build the maps that will be use to validate the Whitelist */}} - {{ range $index, $server := .Servers }} + {{ range $index, $server := $servers }} {{ range $location := $server.Locations }} {{ $path := buildLocation $location }} @@ -288,18 +289,18 @@ http { {{/* build all the required rate limit zones. Each annotation requires a dedicated zone */}} {{/* 1MB -> 16 thousand 64-byte states or about 8 thousand 128-byte states */}} - {{ range $zone := (buildRateLimitZones $cfg.LimitConnZoneVariable .Servers) }} + {{ range $zone := (buildRateLimitZones $cfg.LimitConnZoneVariable $servers) }} {{ $zone }} {{ end }} {{ $backlogSize := .BacklogSize }} - {{ range $index, $server := .Servers }} + {{ range $index, $server := $servers }} server { server_name {{ $server.Hostname }}; {{ template "SERVER" serverConfig $all $server }} {{ template "CUSTOM_ERRORS" $all }} } - {{if $server.Alias }} + {{ if $server.Alias }} server { server_name {{ $server.Alias }}; {{ template "SERVER" serverConfig $all $server }} diff --git a/core/pkg/ingress/annotations/alias/main.go b/core/pkg/ingress/annotations/alias/main.go index 1e56d15f3..ddfbab208 100644 --- a/core/pkg/ingress/annotations/alias/main.go +++ b/core/pkg/ingress/annotations/alias/main.go @@ -1,5 +1,5 @@ /* -Copyright 2016 The Kubernetes Authors. +Copyright 2017 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -29,14 +29,13 @@ const ( type alias struct { } -// NewParser creates a new CORS annotation parser +// NewParser creates a new Alias annotation parser func NewParser() parser.IngressAnnotation { return alias{} } // Parse parses the annotations contained in the ingress rule -// used to indicate if the location/s contains a fragment of -// configuration to be included inside the paths of the rules +// used to add an alias to the provided hosts func (a alias) Parse(ing *extensions.Ingress) (interface{}, error) { return parser.GetStringAnnotation(annotation, ing) } \ No newline at end of file diff --git a/core/pkg/ingress/controller/controller.go b/core/pkg/ingress/controller/controller.go index 033e2bb81..bce28cee6 100644 --- a/core/pkg/ingress/controller/controller.go +++ b/core/pkg/ingress/controller/controller.go @@ -605,6 +605,16 @@ func (ic *GenericController) getBackendServers() ([]*ingress.Backend, []*ingress upstreams := ic.createUpstreams(ings) servers := ic.createServers(ings, upstreams) + // If a server has a hostname equivalent to a pre-existing alias, then we remove the alias + for _, server := range servers { + for j, alias := range servers { + if server.Hostname == alias.Alias { + glog.Warningf("There is a conflict with hostname '%v' and alias of `%v`.", server.Hostname, alias.Hostname) + servers[j].Alias = "" + } + } + } + for _, ingIf := range ings { ing := ingIf.(*extensions.Ingress)