Skip to content

Commit d079bac

Browse files
committed
feat(model): support locations
watch router.deis.io/proxyLocations and router.deis.io/proxyDomain and update routing rules according to settings passed there.
1 parent ad8a231 commit d079bac

3 files changed

Lines changed: 89 additions & 34 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ _Note that Kubernetes annotation maps are all of Go type `map[string]string`. A
288288
| <a name="app-nginx-proxy-buffers-number"></a>routable application | service | [router.deis.io/nginx.proxyBuffers.number](#app-nginx-proxy-buffers-number) | `"8"` | `number` argument to the nginx `proxy_buffers` directive. This can be used to override the same option set globally on the router. |
289289
| <a name="app-nginx-proxy-buffers-size"></a>routable application | service | [router.deis.io/nginx.proxyBuffers.size](#app-nginx-proxy-buffers-size) | `"4k"` | `size` argument to the nginx `proxy_buffers` directive expressed in bytes (no suffix), kilobytes (suffixes `k` and `K`), or megabytes (suffixes `m` and `M`). This can be used to override the same option set globally on the router. |
290290
| <a name="app-nginx-proxy-buffers-busy-size"></a>routable application | service | [router.deis.io/nginx.proxyBuffers.busySize](#app-nginx-proxy-buffers-busy-size) | `"8k"` | nginx `proxy_busy_buffers_size` expressed in bytes (no suffix), kilobytes (suffixes `k` and `K`), or megabytes (suffixes `m` and `M`). This can be used to override the same option set globally on the router. |
291+
|<a name="app-proxy-locations"></a>routable application | service | [router.deis.io/proxyLocations](#app-proxy-locations) | N/A | A list of locations of this servide to plug-in into another service determined by `router.deis.io/proxyDomain` |
292+
|<a name="app-proxy-domain"></a>routable application | service | [router.deis.io/proxyDomain](#app-proxy-domain) | N/A | A reference to another service to plug-in `router.deis.io/proxyLocations` to |
291293

292294
#### Annotations by example
293295

model/model.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package model
33
import (
44
"bytes"
55
"encoding/gob"
6+
goerrors "errors"
67
"fmt"
78
"log"
89
"strings"
@@ -150,6 +151,14 @@ type AppConfig struct {
150151
Maintenance bool `key:"maintenance" constraint:"(?i)^(true|false)$"`
151152
SSLConfig *SSLConfig `key:"ssl"`
152153
Nginx *NginxAppConfig `key:"nginx"`
154+
ProxyLocations []string `key:"proxyLocations"`
155+
ProxyDomain string `key:"proxyDomain"`
156+
Locations []*Location
157+
}
158+
159+
type Location struct {
160+
App *AppConfig
161+
Path string
153162
}
154163

155164
func newAppConfig(routerConfig *RouterConfig) (*AppConfig, error) {
@@ -391,6 +400,11 @@ func build(kubeClient *kubernetes.Clientset, routerDeployment *v1beta1ext.Deploy
391400
routerConfig.AppConfigs = append(routerConfig.AppConfigs, appConfig)
392401
}
393402
}
403+
err = linkLocations(routerConfig.AppConfigs)
404+
if err != nil {
405+
return nil, err
406+
}
407+
addRootLocations(routerConfig.AppConfigs)
394408
if builderService != nil {
395409
builderConfig, err := buildBuilderConfig(builderService)
396410
if err != nil {
@@ -403,6 +417,41 @@ func build(kubeClient *kubernetes.Clientset, routerDeployment *v1beta1ext.Deploy
403417
return routerConfig, nil
404418
}
405419

420+
func appByDomain(appConfigs []*AppConfig, domain string) *AppConfig {
421+
for _, app := range appConfigs {
422+
for _, appDomain := range app.Domains {
423+
if domain == appDomain {
424+
return app
425+
}
426+
}
427+
}
428+
return nil
429+
}
430+
431+
func linkLocations(appConfigs []*AppConfig) error {
432+
for _, app := range appConfigs {
433+
if app.ProxyDomain != "" && len(app.ProxyLocations) > 0 {
434+
targetApp := appByDomain(appConfigs, app.ProxyDomain)
435+
if targetApp == nil {
436+
return goerrors.New(fmt.Sprintf("Can't find ProxyDomain '%s' in any application", app.ProxyDomain))
437+
}
438+
439+
for _, loc := range app.ProxyLocations {
440+
location := &Location{App: app, Path: loc}
441+
targetApp.Locations = append(targetApp.Locations, location)
442+
}
443+
}
444+
}
445+
return nil
446+
}
447+
448+
func addRootLocations(appConfigs []*AppConfig) {
449+
for _, app := range appConfigs {
450+
rootLocation := &Location{App: app, Path: "/"}
451+
app.Locations = append(app.Locations, rootLocation)
452+
}
453+
}
454+
406455
func buildRouterConfig(routerDeployment *v1beta1.Deployment, platformCertSecret *v1.Secret, dhParamSecret *v1.Secret) (*RouterConfig, error) {
407456
routerConfig, err := newRouterConfig()
408457
if err != nil {
@@ -449,6 +498,7 @@ func buildAppConfig(kubeClient *kubernetes.Clientset, service v1.Service, router
449498
if err != nil {
450499
return nil, err
451500
}
501+
452502
// If no domains are found, we don't have the information we need to build routes
453503
// to this application. Abort.
454504
if len(appConfig.Domains) == 0 {

nginx/config.go

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -270,41 +270,44 @@ http {
270270
271271
vhost_traffic_status_filter_by_set_key {{ $appConfig.Name }} application::*;
272272
273-
location / {
274-
{{ if $routerConfig.RequestIDs }}
275-
add_header X-Request-Id $request_id always;
276-
add_header X-Correlation-Id $correlation_id always;
277-
{{end}}
278-
279-
{{ if $appConfig.Maintenance }}return 503;{{ else if $appConfig.Available }}
280-
proxy_buffering {{ if $appConfig.Nginx.ProxyBuffersConfig.Enabled }}on{{ else }}off{{ end }};
281-
proxy_buffer_size {{ $appConfig.Nginx.ProxyBuffersConfig.Size }};
282-
proxy_buffers {{ $appConfig.Nginx.ProxyBuffersConfig.Number }} {{ $appConfig.Nginx.ProxyBuffersConfig.Size }};
283-
proxy_busy_buffers_size {{ $appConfig.Nginx.ProxyBuffersConfig.BusySize }};
284-
proxy_set_header Host $host;
285-
proxy_set_header X-Forwarded-For $remote_addr;
286-
proxy_set_header X-Forwarded-Proto $access_scheme;
287-
proxy_set_header X-Forwarded-Port $forwarded_port;
288-
proxy_redirect off;
289-
proxy_connect_timeout {{ $appConfig.ConnectTimeout }};
290-
proxy_send_timeout {{ $appConfig.TCPTimeout }};
291-
proxy_read_timeout {{ $appConfig.TCPTimeout }};
292-
proxy_http_version 1.1;
293-
proxy_set_header Upgrade $http_upgrade;
294-
proxy_set_header Connection $connection_upgrade;
295-
{{ if $routerConfig.RequestIDs }}
296-
proxy_set_header X-Request-Id $request_id;
297-
proxy_set_header X-Correlation-Id $correlation_id;
298-
{{ end }}
299-
300-
{{ if or $enforceSecure $appConfig.SSLConfig.Enforce }}if ($access_scheme !~* "^https|wss$") {
301-
return 301 $uri_scheme://$host$request_uri;
302-
}{{ end }}
303-
304-
{{ if $hstsConfig.Enabled }}add_header Strict-Transport-Security $sts always;{{ end }}
273+
{{range $location := $appConfig.Locations}}
274+
location {{ $location.Path }} {
275+
{{ if $routerConfig.RequestIDs }}
276+
add_header X-Request-Id $request_id always;
277+
add_header X-Correlation-Id $correlation_id always;
278+
{{end}}
279+
280+
{{ if $location.App.Maintenance }}return 503;{{ else if $location.App.Available }}
281+
proxy_buffering {{ if $location.App.Nginx.ProxyBuffersConfig.Enabled }}on{{ else }}off{{ end }};
282+
proxy_buffer_size {{ $location.App.Nginx.ProxyBuffersConfig.Size }};
283+
proxy_buffers {{ $location.App.Nginx.ProxyBuffersConfig.Number }} {{ $location.App.Nginx.ProxyBuffersConfig.Size }};
284+
proxy_busy_buffers_size {{ $location.App.Nginx.ProxyBuffersConfig.BusySize }};
285+
proxy_set_header Host $host;
286+
proxy_set_header X-Forwarded-For $remote_addr;
287+
proxy_set_header X-Forwarded-Proto $access_scheme;
288+
proxy_set_header X-Forwarded-Port $forwarded_port;
289+
proxy_redirect off;
290+
proxy_connect_timeout {{ $location.App.ConnectTimeout }};
291+
proxy_send_timeout {{ $location.App.TCPTimeout }};
292+
proxy_read_timeout {{ $location.App.TCPTimeout }};
293+
proxy_http_version 1.1;
294+
proxy_set_header Upgrade $http_upgrade;
295+
proxy_set_header Connection $connection_upgrade;
296+
{{ if $routerConfig.RequestIDs }}
297+
proxy_set_header X-Request-Id $request_id;
298+
proxy_set_header X-Correlation-Id $correlation_id;
299+
{{ end }}
300+
301+
{{ if or $enforceSecure $location.App.SSLConfig.Enforce }}if ($access_scheme !~* "^https|wss$") {
302+
return 301 $uri_scheme://$host$request_uri;
303+
}{{ end }}
304+
305+
{{ if $hstsConfig.Enabled }}add_header Strict-Transport-Security $sts always;{{ end }}
306+
307+
proxy_pass http://{{$location.App.ServiceIP}}:80;{{ else }}return 503;{{ end }}
308+
}
309+
{{end}}
305310
306-
proxy_pass http://{{$appConfig.ServiceIP}}:80;{{ else }}return 503;{{ end }}
307-
}
308311
{{ if $appConfig.Maintenance }}error_page 503 @maintenance;
309312
location @maintenance {
310313
root /;

0 commit comments

Comments
 (0)