Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for health status uri customisation #750

Merged
merged 3 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -40,9 +41,12 @@ var (
gitCommit string

healthStatus = flag.Bool("health-status", false,
`Add a location "/nginx-health" to the default server. The location responds with the 200 status code for any request.
`Add a location based on the value of health-status-uri to the default server. The location responds with the 200 status code for any request.
Useful for external health-checking of the Ingress controller`)

healthStatusURI = flag.String("health-status-uri", "/nginx-health",
`Sets the URI of health status location in the default server. Requires -health-status`)

proxyURL = flag.String("proxy", "",
`Use a proxy server to connect to Kubernetes API started by "kubectl proxy" command. For testing purposes only.
The Ingress controller does not start NGINX and does not write any generated NGINX configuration files to disk`)
Expand Down Expand Up @@ -137,6 +141,11 @@ func main() {
os.Exit(0)
}

healthStatusURIValidationError := validateLocation(*healthStatusURI)
if healthStatusURIValidationError != nil {
glog.Fatalf("Invalid value for health-status-uri: %v", healthStatusURIValidationError)
}

statusLockNameValidationError := validateResourceName(*leaderElectionLockName)
if statusLockNameValidationError != nil {
glog.Fatalf("Invalid value for leader-election-lock-name: %v", statusLockNameValidationError)
Expand Down Expand Up @@ -320,6 +329,7 @@ func main() {

staticCfgParams := &configs.StaticConfigParams{
HealthStatus: *healthStatus,
HealthStatusURI: *healthStatusURI,
NginxStatus: *nginxStatus,
NginxStatusAllowCIDRs: allowedCIDRs,
NginxStatusPort: *nginxStatusPort,
Expand Down Expand Up @@ -515,3 +525,19 @@ func getAndValidateSecret(kubeClient *kubernetes.Clientset, secretNsName string)
}
return secret, nil
}

const locationFmt = `/[^\s{};]*`
const locationErrMsg = "must start with / and must not include any whitespace character, `{`, `}` or `;`"

var locationRegexp = regexp.MustCompile("^" + locationFmt + "$")

func validateLocation(location string) error {
Dean-Coakley marked this conversation as resolved.
Show resolved Hide resolved
if location == "" || location == "/" {
return fmt.Errorf("invalid location format: '%v' is an invalid location", location)
}
if !locationRegexp.MatchString(location) {
msg := validation.RegexError(locationErrMsg, locationFmt, "/path", "/path/subpath-123")
return fmt.Errorf("invalid location format: %v", msg)
}
return nil
}
26 changes: 26 additions & 0 deletions cmd/nginx-ingress/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,29 @@ func TestValidateCIDRorIP(t *testing.T) {
}
}
}

func TestValidateLocation(t *testing.T) {
badLocations := []string{
"",
"/",
" /test",
"/bad;",
}
for _, badLocation := range badLocations {
err := validateLocation(badLocation)
if err == nil {
t.Errorf("validateLocation(%v) returned no error when it should have returned an error", badLocation)
}
}

goodLocations := []string{
"/test",
"/test/subtest",
}
for _, goodLocation := range goodLocations {
err := validateLocation(goodLocation)
if err != nil {
t.Errorf("validateLocation(%v) returned an error when it should have returned no error: %v", goodLocation, err)
}
}
}
1 change: 1 addition & 0 deletions deployments/helm-chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Parameter | Description | Default
`controller.watchNamespace` | Namespace to watch for Ingress resources. By default the Ingress controller watches all namespaces. | ""
`controller.enableCustomResources` | Enable the custom resources. | false
`controller.healthStatus` | Add a location "/nginx-health" to the default server. The location responds with the 200 status code for any request. Useful for external health-checking of the Ingress controller. | false
`controller.healthStatusURI` | Sets the URI of health status location in the default server. Requires `contoller.healthStatus`. | "/nginx-health"
`controller.nginxStatus.enable` | Enable the NGINX stub_status, or the NGINX Plus API. | true
`controller.nginxStatus.port` | Set the port where the NGINX stub_status or the NGINX Plus API is exposed. | 8080
`controller.nginxStatus.allowCidrs` | Whitelist IPv4 IP/CIDR blocks to allow access to NGINX stub_status or the NGINX Plus API. Separate multiple IP/CIDR by commas. | 127.0.0.1
Expand Down
1 change: 1 addition & 0 deletions deployments/helm-chart/templates/controller-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ spec:
- -watch-namespace={{ .Values.controller.watchNamespace }}
{{- end }}
- -health-status={{ .Values.controller.healthStatus }}
- -health-status-uri={{ .Values.controller.healthStatusURI }}
- -nginx-debug={{ .Values.controller.nginxDebug }}
- -v={{ .Values.controller.logLevel }}
- -nginx-status={{ .Values.controller.nginxStatus.enable }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ spec:
- -watch-namespace={{ .Values.controller.watchNamespace }}
{{- end }}
- -health-status={{ .Values.controller.healthStatus }}
- -health-status-uri={{ .Values.controller.healthStatusURI }}
- -nginx-debug={{ .Values.controller.nginxDebug }}
- -v={{ .Values.controller.logLevel }}
- -nginx-status={{ .Values.controller.nginxStatus.enable }}
Expand Down
5 changes: 4 additions & 1 deletion deployments/helm-chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ controller:
## Enable the custom resources.
enableCustomResources: false

## Add a location "/nginx-health" to the default server. The location responds with the 200 status code for any request.
## Add a location based on the value of health-status-uri to the default server. The location responds with the 200 status code for any request.
## Useful for external health-checking of the Ingress controller.
healthStatus: false

## Sets the URI of health status location in the default server. Requires contoller.healthStatus.
healthStatusURI: "/nginx-health"

nginxStatus:
## Enable the NGINX stub_status, or the NGINX Plus API.
enable: true
Expand Down
8 changes: 5 additions & 3 deletions docs/cli-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Usage of ./nginx-ingress:
the file "/etc/nginx/secrets/default" does not exist, the Ingress controller will fail to start
-wildcard-tls-secret string
A Secret with a TLS certificate and key for TLS termination of every Ingress host for which TLS termination is enabled but the Secret is not specified.
Format: <namespace>/<name>. If the argument is not set, for such Ingress hosts NGINX will break any attempt to establish a TLS connection.
Format: <namespace>/<name>. If the argument is not set, for such Ingress hosts NGINX will break any attempt to establish a TLS connection.
If the argument is set, but the Ingress controller is not able to fetch the Secret from Kubernetes API, the Ingress controller will fail to start.
-enable-custom-resources
Enable custom resources
Expand All @@ -21,8 +21,10 @@ Usage of ./nginx-ingress:
Specifies the name of the service with the type LoadBalancer through which the Ingress controller pods are exposed externally.
The external address of the service is used when reporting the status of Ingress resources. Requires -report-ingress-status.
-health-status
Add a location "/nginx-health" to the default server. The location responds with the 200 status code for any request.
Useful for external health-checking of the Ingress controller
Add a location based on the value of health-status-uri to the default server. The location responds with the 200 status code for any request.
Useful for external health-checking of the Ingress controller
-health-status-uri string
Sets the URI of health status location in the default server. Requires -health-status (default "/nginx-health")
-ingress-class string
A class of the Ingress controller. The Ingress controller only processes Ingress resources that belong to its class
- i.e. have the annotation "kubernetes.io/ingress.class" equal to the class. Additionally,
Expand Down
1 change: 1 addition & 0 deletions internal/configs/config_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type ConfigParams struct {
// StaticConfigParams holds immutable NGINX configuration parameters that affect the main NGINX config.
type StaticConfigParams struct {
HealthStatus bool
HealthStatusURI string
NginxStatus bool
NginxStatusAllowCIDRs []string
NginxStatusPort int
Expand Down
1 change: 1 addition & 0 deletions internal/configs/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool) *ConfigParams {
func GenerateNginxMainConfig(staticCfgParams *StaticConfigParams, config *ConfigParams) *version1.MainConfig {
nginxCfg := &version1.MainConfig{
HealthStatus: staticCfgParams.HealthStatus,
HealthStatusURI: staticCfgParams.HealthStatusURI,
NginxStatus: staticCfgParams.NginxStatus,
NginxStatusAllowCIDRs: staticCfgParams.NginxStatusAllowCIDRs,
NginxStatusPort: staticCfgParams.NginxStatusPort,
Expand Down
1 change: 1 addition & 0 deletions internal/configs/configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func createTestStaticConfigParams() *StaticConfigParams {
return &StaticConfigParams{
HealthStatus: true,
HealthStatusURI: "/nginx-health",
NginxStatus: true,
NginxStatusAllowCIDRs: []string{"127.0.0.1"},
NginxStatusPort: 8080,
Expand Down
1 change: 1 addition & 0 deletions internal/configs/version1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type MainConfig struct {
ErrorLogLevel string
StreamLogFormat string
HealthStatus bool
HealthStatusURI string
NginxStatus bool
NginxStatusAllowCIDRs []string
NginxStatusPort int
Expand Down
2 changes: 1 addition & 1 deletion internal/configs/version1/nginx-plus.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ http {
{{end}}

{{if .HealthStatus}}
location /nginx-health {
location {{.HealthStatusURI}} {
default_type text/plain;
return 200 "healthy\n";
}
Expand Down
2 changes: 1 addition & 1 deletion internal/configs/version1/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ http {
{{end}}

{{if .HealthStatus}}
location /nginx-health {
location {{.HealthStatusURI}} {
default_type text/plain;
return 200 "healthy\n";
}
Expand Down