Merge pull request #2437 from JordanP/rewrite_log

Add annotation to enable rewrite logs in a location
This commit is contained in:
k8s-ci-robot 2018-04-27 10:27:19 -07:00 committed by GitHub
commit 0813b38314
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 157 additions and 18 deletions

File diff suppressed because one or more lines are too long

View file

@ -23,36 +23,46 @@ import (
"k8s.io/ingress-nginx/internal/ingress/resolver"
)
type cors struct {
type log struct {
r resolver.Resolver
}
// Config contains the configuration to be used in the Ingress
type Config struct {
Access bool `json:"accessLog"`
Access bool `json:"accessLog"`
Rewrite bool `json:"rewriteLog"`
}
// Equal tests for equality between two Config types
func (bd1 *Config) Equal(bd2 *Config) bool {
if bd1.Access == bd2.Access {
return true
if bd1.Access != bd2.Access {
return false
}
return false
if bd1.Rewrite != bd2.Rewrite {
return false
}
return true
}
// NewParser creates a new access log annotation parser
// NewParser creates a new log annotations parser
func NewParser(r resolver.Resolver) parser.IngressAnnotation {
return cors{r}
return log{r}
}
// Parse parses the annotations contained in the ingress
// rule used to indicate if the location/s should enable logs
func (c cors) Parse(ing *extensions.Ingress) (interface{}, error) {
func (l log) Parse(ing *extensions.Ingress) (interface{}, error) {
accessEnabled, err := parser.GetBoolAnnotation("enable-access-log", ing)
if err != nil {
accessEnabled = true
}
return &Config{accessEnabled}, nil
rewriteEnabled, err := parser.GetBoolAnnotation("enable-rewrite-log", ing)
if err != nil {
rewriteEnabled = false
}
return &Config{Access: accessEnabled, Rewrite: rewriteEnabled}, nil
}

View file

@ -62,7 +62,7 @@ func buildIngress() *extensions.Ingress {
}
}
func TestIngressLogConfig(t *testing.T) {
func TestIngressAccessLogConfig(t *testing.T) {
ing := buildIngress()
data := map[string]string{}
@ -79,3 +79,21 @@ func TestIngressLogConfig(t *testing.T) {
t.Errorf("expected access be disabled but is enabled")
}
}
func TestIngressRewriteLogConfig(t *testing.T) {
ing := buildIngress()
data := map[string]string{}
data[parser.GetAnnotationWithPrefix("enable-rewrite-log")] = "true"
ing.SetAnnotations(data)
log, _ := NewParser(&resolver.Mock{}).Parse(ing)
nginxLogs, ok := log.(*Config)
if !ok {
t.Errorf("expected a Config type")
}
if !nginxLogs.Rewrite {
t.Errorf("expected rewrite log to be enabled but it is disabled")
}
}

View file

@ -264,7 +264,7 @@ type Location struct {
// +optional
XForwardedPrefix bool `json:"xForwardedPrefix,omitempty"`
// Logs allows to enable or disable the nginx logs
// By default this is enabled
// By default access logs are enabled and rewrite logs are disabled
Logs log.Config `json:"logs,omitempty"`
// GRPC indicates if the kubernetes service exposes a gRPC interface
// By default this is false