Skip to content

Commit

Permalink
Merge pull request #169 from nginxinc/empty-hostnames
Browse files Browse the repository at this point in the history
Ignore Ingress resources with empty hostnames
  • Loading branch information
pleshakov authored Aug 19, 2017
2 parents d8b7e48 + 355af50 commit 373a6b2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 47 deletions.
12 changes: 12 additions & 0 deletions nginx-controller/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,11 +789,17 @@ func (lbc *LoadBalancerController) createIngress(ing *extensions.Ingress) (*ngin
}
}

validRules := 0

for _, rule := range ing.Spec.Rules {
if rule.IngressRuleValue.HTTP == nil {
continue
}

if rule.Host == "" {
return nil, fmt.Errorf("Ingress rule contains empty host")
}

for _, path := range rule.HTTP.Paths {
endps, err := lbc.getEndpointsForIngressBackend(&path.Backend, ing.Namespace)
if err != nil {
Expand All @@ -803,6 +809,12 @@ func (lbc *LoadBalancerController) createIngress(ing *extensions.Ingress) (*ngin
ingEx.Endpoints[path.Backend.ServiceName+path.Backend.ServicePort.String()] = endps
}
}

validRules++
}

if validRules == 0 {
return nil, fmt.Errorf("Ingress contains no valid rules")
}

return ingEx, nil
Expand Down
43 changes: 0 additions & 43 deletions nginx-controller/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
serverName := rule.Host

statuzZone := rule.Host
if rule.Host == emptyHost {
statuzZone = ingEx.Ingress.Namespace + "-" + ingEx.Ingress.Name
glog.Warningf("Host field of ingress rule in %v/%v is empty", ingEx.Ingress.Namespace, ingEx.Ingress.Name)
}

server := Server{
Name: serverName,
Expand Down Expand Up @@ -155,45 +151,6 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
servers = append(servers, server)
}

if len(ingEx.Ingress.Spec.Rules) == 0 && ingEx.Ingress.Spec.Backend != nil {
serverName := emptyHost
statuzZone := ingEx.Ingress.Namespace + "-" + ingEx.Ingress.Name
glog.Warningf("Host field of ingress rule in %v/%v is empty", ingEx.Ingress.Namespace, ingEx.Ingress.Name)
server := Server{
Name: serverName,
ServerTokens: ingCfg.ServerTokens,
HTTP2: ingCfg.HTTP2,
RedirectToHTTPS: ingCfg.RedirectToHTTPS,
ProxyProtocol: ingCfg.ProxyProtocol,
HSTS: ingCfg.HSTS,
HSTSMaxAge: ingCfg.HSTSMaxAge,
HSTSIncludeSubdomains: ingCfg.HSTSIncludeSubdomains,
StatusZone: statuzZone,
RealIPHeader: ingCfg.RealIPHeader,
SetRealIPFrom: ingCfg.SetRealIPFrom,
RealIPRecursive: ingCfg.RealIPRecursive,
ProxyHideHeaders: ingCfg.ProxyHideHeaders,
ProxyPassHeaders: ingCfg.ProxyPassHeaders,
ServerSnippets: ingCfg.ServerSnippets,
}

if pemFile, ok := pems[emptyHost]; ok {
server.SSL = true
server.SSLCertificate = pemFile
server.SSLCertificateKey = pemFile
}

var locations []Location

upsName := getNameForUpstream(ingEx.Ingress, emptyHost, ingEx.Ingress.Spec.Backend.ServiceName)

loc := createLocation(pathOrDefault("/"), upstreams[upsName], &ingCfg, wsServices[ingEx.Ingress.Spec.Backend.ServiceName], rewrites[ingEx.Ingress.Spec.Backend.ServiceName], sslServices[ingEx.Ingress.Spec.Backend.ServiceName])
locations = append(locations, loc)

server.Locations = locations
servers = append(servers, server)
}

return IngressNginxConfig{Upstreams: upstreamMapToSlice(upstreams), Servers: servers}
}

Expand Down
2 changes: 0 additions & 2 deletions nginx-controller/nginx/templates/nginx-plus.ingress.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ server {

server_tokens "{{$server.ServerTokens}}";

{{if $server.Name}}
server_name {{$server.Name}};
{{end}}

status_zone {{$server.StatusZone}};

Expand Down
3 changes: 1 addition & 2 deletions nginx-controller/nginx/templates/nginx.ingress.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ server {

server_tokens {{$server.ServerTokens}};

{{if $server.Name}}
server_name {{$server.Name}};
{{end}}

{{range $proxyHideHeader := $server.ProxyHideHeaders}}
proxy_hide_header {{$proxyHideHeader}};{{end}}
{{range $proxyPassHeader := $server.ProxyPassHeaders}}
Expand Down

0 comments on commit 373a6b2

Please sign in to comment.