Skip to content

Commit

Permalink
Add health check location
Browse files Browse the repository at this point in the history
Ideally, this feature should be a part of a bigger feature (the default
server) that solves #52.

For now, we add the "-health-status" parameter to the controller. If
it is present, the default server listening on port 80 with the health
check location "/nginx-health" gets added to the main nginx
configuration.

Closes #90
  • Loading branch information
Julian Strobl committed Dec 14, 2016
1 parent 1caf576 commit 4e5661c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
6 changes: 5 additions & 1 deletion nginx-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ var (
// Set during build
version string

healthStatus = flag.Bool("health-status", false,
`If present, the default server listening on port 80 with the health check
location "/nginx-health" gets added to the main nginx configuration.`)

proxyURL = flag.String("proxy", "",
`If specified, the controller assumes a kubctl proxy server is running on the
given url and creates a proxy client. Regenerated NGINX configuration files
Expand Down Expand Up @@ -52,7 +56,7 @@ func main() {
}
}

ngxc, _ := nginx.NewNginxController("/etc/nginx/", local)
ngxc, _ := nginx.NewNginxController("/etc/nginx/", local, *healthStatus)
ngxc.Start()
config := nginx.NewDefaultConfig()
cnf := nginx.NewConfigurator(ngxc, config)
Expand Down
13 changes: 13 additions & 0 deletions nginx-controller/nginx/nginx.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,18 @@ http {
{{if .SSLPreferServerCiphers}}ssl_prefer_server_ciphers on;{{end}}
{{if .SSLDHParam}}ssl_dhparam {{.SSLDHParam}};{{end}}

{{if .HealthStatus}}
server {
listen 80 default_server;
server_name _;

location /nginx-health {
access_log off;
default_type text/plain;
return 200 "healthy\n";
}
}
{{end}}

include /etc/nginx/conf.d/*.conf;
}
5 changes: 3 additions & 2 deletions nginx-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type NginxMainConfig struct {
ServerNamesHashBucketSize string
ServerNamesHashMaxSize string
LogFormat string
HealthStatus bool
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
SSLProtocols string
SSLPreferServerCiphers bool
Expand All @@ -98,7 +99,7 @@ func NewUpstreamWithDefaultServer(name string) Upstream {
}

// NewNginxController creates a NGINX controller
func NewNginxController(nginxConfPath string, local bool) (*NginxController, error) {
func NewNginxController(nginxConfPath string, local bool, healthStatus bool) (*NginxController, error) {
ngxc := NginxController{
nginxConfdPath: path.Join(nginxConfPath, "conf.d"),
nginxCertsPath: path.Join(nginxConfPath, "ssl"),
Expand All @@ -109,7 +110,7 @@ func NewNginxController(nginxConfPath string, local bool) (*NginxController, err
createDir(ngxc.nginxCertsPath)
}

cfg := &NginxMainConfig{ServerNamesHashMaxSize: NewDefaultConfig().MainServerNamesHashMaxSize}
cfg := &NginxMainConfig{ServerNamesHashMaxSize: NewDefaultConfig().MainServerNamesHashMaxSize, HealthStatus: healthStatus}
ngxc.UpdateMainConfigFile(cfg)

return &ngxc, nil
Expand Down
6 changes: 5 additions & 1 deletion nginx-plus-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ var (
// Set during build
version string

healthStatus = flag.Bool("health-status", false,
`If present, the default server listening on port 80 with the health check
location "/nginx-health" gets added to the main nginx configuration.`)

proxyURL = flag.String("proxy", "",
`If specified, the controller assumes a kubctl proxy server is running on the
given url and creates a proxy client. Regenerated NGINX configuration files
Expand Down Expand Up @@ -52,7 +56,7 @@ func main() {
}
}

ngxc, _ := nginx.NewNginxController("/etc/nginx/", local)
ngxc, _ := nginx.NewNginxController("/etc/nginx/", local, *healthStatus)
ngxc.Start()
config := nginx.NewDefaultConfig()
nginxAPI, err := nginx.NewNginxAPIController("http://127.0.0.1:8080/upstream_conf", "http://127.0.0.1:8080/status", local)
Expand Down
13 changes: 13 additions & 0 deletions nginx-plus-controller/nginx/nginx.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,18 @@ http {
'' close;
}

{{if .HealthStatus}}
server {
listen 80 default_server;
server_name _;

location /nginx-health {
access_log off;
default_type text/plain;
return 200 "healthy\n";
}
}
{{end}}

include /etc/nginx/conf.d/*.conf;
}
5 changes: 3 additions & 2 deletions nginx-plus-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ type NginxMainConfig struct {
ServerNamesHashBucketSize string
ServerNamesHashMaxSize string
LogFormat string
HealthStatus bool
}

// NewNginxController creates a NGINX controller
func NewNginxController(nginxConfPath string, local bool) (*NginxController, error) {
func NewNginxController(nginxConfPath string, local bool, healthStatus bool) (*NginxController, error) {
ngxc := NginxController{
nginxConfdPath: path.Join(nginxConfPath, "conf.d"),
nginxCertsPath: path.Join(nginxConfPath, "ssl"),
Expand All @@ -108,7 +109,7 @@ func NewNginxController(nginxConfPath string, local bool) (*NginxController, err
ngxc.writeStatusAndUpstreamConfAPIsConf()
}

cfg := &NginxMainConfig{ServerNamesHashMaxSize: NewDefaultConfig().MainServerNamesHashMaxSize}
cfg := &NginxMainConfig{ServerNamesHashMaxSize: NewDefaultConfig().MainServerNamesHashMaxSize, HealthStatus: healthStatus}
ngxc.UpdateMainConfigFile(cfg)

return &ngxc, nil
Expand Down

0 comments on commit 4e5661c

Please sign in to comment.