Refactor ingress nginx variables

This commit is contained in:
Manuel Alejandro de Brito Fontes 2020-12-11 12:27:15 -03:00
parent 6d90b5e10a
commit 9c0a39636d
4 changed files with 15 additions and 4 deletions

View file

@ -51,6 +51,8 @@ func updateServerLocations(locations []*ingress.Location) []*ingress.Location {
continue continue
} }
location.IngressPath = location.Path
// only Prefix locations could require an additional location block // only Prefix locations could require an additional location block
if *location.PathType != pathTypePrefix { if *location.PathType != pathTypePrefix {
newLocations = append(newLocations, location) newLocations = append(newLocations, location)

View file

@ -809,6 +809,7 @@ func isValidByteSize(input interface{}, isOffset bool) bool {
type ingressInformation struct { type ingressInformation struct {
Namespace string Namespace string
Path string
Rule string Rule string
Service string Service string
ServicePort string ServicePort string
@ -848,7 +849,7 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
return &ingressInformation{} return &ingressInformation{}
} }
path, ok := p.(string) ingressPath, ok := p.(string)
if !ok { if !ok {
klog.Errorf("expected a 'string' type but %T was returned", p) klog.Errorf("expected a 'string' type but %T was returned", p)
return &ingressInformation{} return &ingressInformation{}
@ -862,6 +863,12 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
Namespace: ing.GetNamespace(), Namespace: ing.GetNamespace(),
Rule: ing.GetName(), Rule: ing.GetName(),
Annotations: ing.Annotations, Annotations: ing.Annotations,
Path: ingressPath,
}
if ingressPath == "" {
ingressPath = "/"
info.Path = "/"
} }
if ing.Spec.Backend != nil { if ing.Spec.Backend != nil {
@ -890,7 +897,7 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
} }
for _, rPath := range rule.HTTP.Paths { for _, rPath := range rule.HTTP.Paths {
if path != rPath.Path { if ingressPath != rPath.Path {
continue continue
} }

View file

@ -232,6 +232,8 @@ type Location struct {
IsDefBackend bool `json:"isDefBackend"` IsDefBackend bool `json:"isDefBackend"`
// Ingress returns the ingress from which this location was generated // Ingress returns the ingress from which this location was generated
Ingress *Ingress `json:"ingress"` Ingress *Ingress `json:"ingress"`
// IngressPath original path defined in the ingress rule
IngressPath string `json:"ingressPath"`
// Backend describes the name of the backend to use. // Backend describes the name of the backend to use.
Backend string `json:"backend"` Backend string `json:"backend"`
// Service describes the referenced services from the ingress // Service describes the referenced services from the ingress

View file

@ -1079,12 +1079,12 @@ stream {
{{ end }} {{ end }}
location {{ $path }} { location {{ $path }} {
{{ $ing := (getIngressInformation $location.Ingress $server.Hostname $location.Path) }} {{ $ing := (getIngressInformation $location.Ingress $server.Hostname $location.IngressPath) }}
set $namespace {{ $ing.Namespace | quote}}; set $namespace {{ $ing.Namespace | quote}};
set $ingress_name {{ $ing.Rule | quote }}; set $ingress_name {{ $ing.Rule | quote }};
set $service_name {{ $ing.Service | quote }}; set $service_name {{ $ing.Service | quote }};
set $service_port {{ $ing.ServicePort | quote }}; set $service_port {{ $ing.ServicePort | quote }};
set $location_path {{ $location.Path | escapeLiteralDollar | quote }}; set $location_path {{ $ing.Path | escapeLiteralDollar | quote }};
{{ buildOpentracingForLocation $all.Cfg.EnableOpentracing $location }} {{ buildOpentracingForLocation $all.Cfg.EnableOpentracing $location }}