Skip to content

Commit

Permalink
Removed IngressNginxConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico Schieder committed Dec 3, 2016
1 parent 11a157a commit f210ac0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 63 deletions.
86 changes: 34 additions & 52 deletions nginx-controller/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func (cnf *Configurator) AddOrUpdateIngress(name string, ingEx *IngressEx) {
defer cnf.lock.Unlock()

pems := cnf.updateCertificates(ingEx)
for _, nginxCfg := range cnf.generateNginxCfg(ingEx, pems) {
cnf.nginx.AddOrUpdateConfig(nginxCfg.Server.Name, nginxCfg)
for _, server := range cnf.generateNginxCfg(ingEx, pems) {
cnf.nginx.AddOrUpdateConfig(server.Name, server)
}
if err := cnf.nginx.Reload(); err != nil {
glog.Errorf("Error when adding or updating ingress %q: %q", name, err)
Expand Down Expand Up @@ -81,21 +81,13 @@ func (cnf *Configurator) updateCertificates(ingEx *IngressEx) map[string]string
return pems
}

func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]string) []IngressNginxConfig {
func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]string) []Server {
ingCfg := cnf.createConfig(ingEx)

upstreams := make(map[string]Upstream)

wsServices := getWebsocketServices(ingEx)
rewrites := getRewrites(ingEx)
sslServices := getSSLServices(ingEx)

if ingEx.Ingress.Spec.Backend != nil {
name := getNameForUpstream(ingEx.Ingress, emptyHost, ingEx.Ingress.Spec.Backend.ServiceName)
upstream := cnf.createUpstream(ingEx, name, ingEx.Ingress.Spec.Backend, ingEx.Ingress.Namespace)
upstreams[name] = upstream
}

var servers []Server

for _, rule := range ingEx.Ingress.Spec.Rules {
Expand All @@ -109,27 +101,8 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
glog.Warningf("Host field of ingress rule in %v/%v is empty", ingEx.Ingress.Namespace, ingEx.Ingress.Name)
}

server := Server{
Name: serverName,
HTTP2: ingCfg.HTTP2,
ProxyProtocol: ingCfg.ProxyProtocol,
HSTS: ingCfg.HSTS,
HSTSMaxAge: ingCfg.HSTSMaxAge,
HSTSIncludeSubdomains: ingCfg.HSTSIncludeSubdomains,
RealIPHeader: ingCfg.RealIPHeader,
SetRealIPFrom: ingCfg.SetRealIPFrom,
RealIPRecursive: ingCfg.RealIPRecursive,
ProxyHideHeaders: ingCfg.ProxyHideHeaders,
ProxyPassHeaders: ingCfg.ProxyPassHeaders,
}

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

var locations []Location
var upstreams map[string]Upstream
rootLocation := false

for _, path := range rule.HTTP.Paths {
Expand All @@ -154,13 +127,41 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
locations = append(locations, loc)
}

server.Locations = locations
server := Server{
Name: serverName,
Locations: locations,
HTTP2: ingCfg.HTTP2,
ProxyProtocol: ingCfg.ProxyProtocol,
HSTS: ingCfg.HSTS,
HSTSMaxAge: ingCfg.HSTSMaxAge,
HSTSIncludeSubdomains: ingCfg.HSTSIncludeSubdomains,
RealIPHeader: ingCfg.RealIPHeader,
SetRealIPFrom: ingCfg.SetRealIPFrom,
RealIPRecursive: ingCfg.RealIPRecursive,
ProxyHideHeaders: ingCfg.ProxyHideHeaders,
ProxyPassHeaders: ingCfg.ProxyPassHeaders,
}

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

// server.Locations = locations
// server.Upstreams = upstreamMapToSlice(upstreams)
servers = append(servers, server)
}

if len(ingEx.Ingress.Spec.Rules) == 0 && ingEx.Ingress.Spec.Backend != nil {
upsName := getNameForUpstream(ingEx.Ingress, emptyHost, ingEx.Ingress.Spec.Backend.ServiceName)
upstream := cnf.createUpstream(ingEx, upsName, ingEx.Ingress.Spec.Backend, ingEx.Ingress.Namespace)
location := createLocation(pathOrDefault("/"), upstream, &ingCfg, wsServices[ingEx.Ingress.Spec.Backend.ServiceName], rewrites[ingEx.Ingress.Spec.Backend.ServiceName], sslServices[ingEx.Ingress.Spec.Backend.ServiceName])

server := Server{
Name: emptyHost,
Upstreams: []Upstream{upstream},
Locations: []Location{location},
HTTP2: ingCfg.HTTP2,
ProxyProtocol: ingCfg.ProxyProtocol,
HSTS: ingCfg.HSTS,
Expand All @@ -179,29 +180,10 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
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)
}

result := []IngressNginxConfig{}
for _, server := range servers {
upstreams := []Upstream{}
for _, location := range server.Locations {
upstreams = append(upstreams, location.Upstream)
}
result = append(result, IngressNginxConfig{
Upstreams: upstreams,
Server: server,
})
}
return result
return servers
}

func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
Expand Down
4 changes: 1 addition & 3 deletions nginx-controller/nginx/configurator_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package nginx

import (
"testing"
)
import "testing"

func TestPathOrDefaultReturnDefault(t *testing.T) {
path := ""
Expand Down
11 changes: 3 additions & 8 deletions nginx-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ type NginxController struct {
local bool
}

// IngressNginxConfig describes an NGINX configuration
type IngressNginxConfig struct {
Upstreams []Upstream
Server Server
}

// Upstream describes an NGINX upstream
type Upstream struct {
Name string
Expand All @@ -41,6 +35,7 @@ type UpstreamServer struct {
// Server describes an NGINX server
type Server struct {
Name string
Upstreams []Upstream
Locations []Location
SSL bool
SSLCertificate string
Expand Down Expand Up @@ -130,7 +125,7 @@ func (nginx *NginxController) DeleteConfig(name string) {

// AddOrUpdateConfig creates or updates a file with
// the specified configuration
func (nginx *NginxController) AddOrUpdateConfig(name string, config IngressNginxConfig) {
func (nginx *NginxController) AddOrUpdateConfig(name string, config Server) {
glog.V(3).Infof("Updating NGINX configuration")
filename := nginx.getIngressNginxConfigFileName(name)
nginx.templateIt(config, filename)
Expand Down Expand Up @@ -189,7 +184,7 @@ func (nginx *NginxController) getIngressNginxConfigFileName(name string) string
return path.Join(nginx.nginxConfdPath, name+".conf")
}

func (nginx *NginxController) templateIt(config IngressNginxConfig, filename string) {
func (nginx *NginxController) templateIt(config Server, filename string) {
tmpl, err := template.New("ingress.tmpl").ParseFiles("ingress.tmpl")
if err != nil {
glog.Fatal("Failed to parse template file")
Expand Down

0 comments on commit f210ac0

Please sign in to comment.