Skip to content

Commit

Permalink
Correct Error Handling to avoid panics and add more logging to template
Browse files Browse the repository at this point in the history
When a type error is found we return an empty object of the expected
type(Example: return "" for string return type). Also makes adds logging
to all errors caused by type and corrects all related logging for consistency.
  • Loading branch information
Fernando Diaz committed Sep 22, 2017
1 parent 37bd14d commit 0c17825
Showing 1 changed file with 45 additions and 13 deletions.
58 changes: 45 additions & 13 deletions controllers/nginx/pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,14 @@ func formatIP(input string) string {
}

// buildResolvers returns the resolvers reading the /etc/resolv.conf file
func buildResolvers(a interface{}) string {
func buildResolvers(input interface{}) string {
// NGINX need IPV6 addresses to be surrounded by brackets
nss := a.([]net.IP)
nss, ok := input.([]net.IP)
if !ok {
glog.Errorf("expected a '[]net.IP' type but %T was returned", input)
return ""
}

if len(nss) == 0 {
return ""
}
Expand All @@ -196,6 +201,7 @@ func buildResolvers(a interface{}) string {
func buildLocation(input interface{}) string {
location, ok := input.(*ingress.Location)
if !ok {
glog.Errorf("expected an '*ingress.Location' type but %T was returned", input)
return slash
}

Expand All @@ -220,6 +226,7 @@ func buildLocation(input interface{}) string {
func buildAuthLocation(input interface{}) string {
location, ok := input.(*ingress.Location)
if !ok {
glog.Errorf("expected an '*ingress.Location' type but %T was returned", input)
return ""
}

Expand All @@ -237,6 +244,7 @@ func buildAuthResponseHeaders(input interface{}) []string {
location, ok := input.(*ingress.Location)
res := []string{}
if !ok {
glog.Errorf("expected an '*ingress.Location' type but %T was returned", input)
return res
}

Expand All @@ -256,7 +264,8 @@ func buildAuthResponseHeaders(input interface{}) []string {
func buildLogFormatUpstream(input interface{}) string {
cfg, ok := input.(config.Configuration)
if !ok {
glog.Errorf("error an ingress.buildLogFormatUpstream type but %T was returned", input)
glog.Errorf("expected a 'config.Configuration' type but %T was returned", input)
return ""
}

return cfg.BuildLogFormatUpstream()
Expand All @@ -267,9 +276,15 @@ func buildLogFormatUpstream(input interface{}) string {
// If the annotation ingress.kubernetes.io/add-base-url:"true" is specified it will
// add a base tag in the head of the response from the service
func buildProxyPass(host string, b interface{}, loc interface{}) string {
backends := b.([]*ingress.Backend)
backends, ok := b.([]*ingress.Backend)
if !ok {
glog.Errorf("expected an '[]*ingress.Backend' type but %T was returned", b)
return ""
}

location, ok := loc.(*ingress.Location)
if !ok {
glog.Errorf("expected a '*ingress.Location' type but %T was returned", loc)
return ""
}

Expand Down Expand Up @@ -345,6 +360,7 @@ func filterRateLimits(input interface{}) []ratelimit.RateLimit {

servers, ok := input.([]*ingress.Server)
if !ok {
glog.Errorf("expected a '[]ratelimit.RateLimit' type but %T was returned", input)
return ratelimits
}
for _, server := range servers {
Expand All @@ -368,6 +384,7 @@ func buildRateLimitZones(input interface{}) []string {

servers, ok := input.([]*ingress.Server)
if !ok {
glog.Errorf("expected a '[]*ingress.Server' type but %T was returned", input)
return zones.List()
}

Expand Down Expand Up @@ -417,6 +434,7 @@ func buildRateLimit(input interface{}) []string {

loc, ok := input.(*ingress.Location)
if !ok {
glog.Errorf("expected an '*ingress.Location' type but %T was returned", input)
return limits
}

Expand Down Expand Up @@ -456,7 +474,7 @@ func buildRateLimit(input interface{}) []string {
func isLocationAllowed(input interface{}) bool {
loc, ok := input.(*ingress.Location)
if !ok {
glog.Errorf("expected an ingress.Location type but %T was returned", input)
glog.Errorf("expected an '*ingress.Location' type but %T was returned", input)
return false
}

Expand All @@ -473,7 +491,11 @@ var (
// size of the string to be used as a variable in nginx to avoid
// issue with the size of the variable bucket size directive
func buildDenyVariable(a interface{}) string {
l := a.(string)
l, ok := a.(string)
if !ok {
glog.Errorf("expected a 'string' type but %T was returned", a)
return ""
}

if _, ok := denyPathSlugMap[l]; !ok {
denyPathSlugMap[l] = buildRandomUUID()
Expand All @@ -484,9 +506,16 @@ func buildDenyVariable(a interface{}) string {

// TODO: Needs Unit Tests
func buildUpstreamName(host string, b interface{}, loc interface{}) string {
backends := b.([]*ingress.Backend)

backends, ok := b.([]*ingress.Backend)
if !ok {
glog.Errorf("expected an '[]*ingress.Backend' type but %T was returned", b)
return ""
}

location, ok := loc.(*ingress.Location)
if !ok {
glog.Errorf("expected a '*ingress.Location' type but %T was returned", loc)
return ""
}

Expand Down Expand Up @@ -522,7 +551,8 @@ func isSticky(host string, loc *ingress.Location, stickyLocations map[string][]s
func buildNextUpstream(input interface{}) string {
nextUpstream, ok := input.(string)
if !ok {
glog.Errorf("expected an string type but %T was returned", input)
glog.Errorf("expected a 'string' type but %T was returned", input)
return ""
}

parts := strings.Split(nextUpstream, " ")
Expand All @@ -540,7 +570,8 @@ func buildNextUpstream(input interface{}) string {
func buildAuthSignURL(input interface{}) string {
s, ok := input.(string)
if !ok {
glog.Errorf("expected an string type but %T was returned", input)
glog.Errorf("expected an 'string' type but %T was returned", input)
return ""
}

u, _ := url.Parse(s)
Expand All @@ -561,7 +592,7 @@ func buildRandomUUID() string {
func isValidClientBodyBufferSize(input interface{}) bool {
s, ok := input.(string)
if !ok {
glog.Errorf("expected an string type but %T was returned", input)
glog.Errorf("expected an 'string' type but %T was returned", input)
return false
}

Expand Down Expand Up @@ -602,13 +633,13 @@ type ingressInformation struct {
func getIngressInformation(i, p interface{}) *ingressInformation {
ing, ok := i.(*extensions.Ingress)
if !ok {
glog.V(3).Infof("expected an Ingress type but %T was returned", i)
glog.Errorf("expected an '*extensions.Ingress' type but %T was returned", i)
return &ingressInformation{}
}

path, ok := p.(string)
if !ok {
glog.V(3).Infof("expected a string type but %T was returned", p)
glog.Errorf("expected a 'string' type but %T was returned", p)
return &ingressInformation{}
}

Expand Down Expand Up @@ -645,7 +676,8 @@ func getIngressInformation(i, p interface{}) *ingressInformation {
func buildForwardedFor(input interface{}) string {
s, ok := input.(string)
if !ok {
glog.Errorf("expected an string type but %T was returned", input)
glog.Errorf("expected a 'string' type but %T was returned", input)
return ""
}

ffh := strings.Replace(s, "-", "_", -1)
Expand Down

0 comments on commit 0c17825

Please sign in to comment.