refactor(k8s): baseName and ObjectMeta.
This commit is contained in:
parent
742dde72bb
commit
a4a8345a33
2 changed files with 28 additions and 30 deletions
|
@ -168,6 +168,7 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
|
||||||
Backends: map[string]*types.Backend{},
|
Backends: map[string]*types.Backend{},
|
||||||
Frontends: map[string]*types.Frontend{},
|
Frontends: map[string]*types.Frontend{},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, i := range ingresses {
|
for _, i := range ingresses {
|
||||||
ingressClass := i.Annotations[annotationKubernetesIngressClass]
|
ingressClass := i.Annotations[annotationKubernetesIngressClass]
|
||||||
|
|
||||||
|
@ -189,8 +190,9 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pa := range r.HTTP.Paths {
|
for _, pa := range r.HTTP.Paths {
|
||||||
if _, exists := templateObjects.Backends[r.Host+pa.Path]; !exists {
|
baseName := r.Host + pa.Path
|
||||||
templateObjects.Backends[r.Host+pa.Path] = &types.Backend{
|
if _, exists := templateObjects.Backends[baseName]; !exists {
|
||||||
|
templateObjects.Backends[baseName] = &types.Backend{
|
||||||
Servers: make(map[string]types.Server),
|
Servers: make(map[string]types.Server),
|
||||||
LoadBalancer: &types.LoadBalancer{
|
LoadBalancer: &types.LoadBalancer{
|
||||||
Method: "wrr",
|
Method: "wrr",
|
||||||
|
@ -199,31 +201,27 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
if realm := i.Annotations[annotationKubernetesAuthRealm]; realm != "" && realm != traefikDefaultRealm {
|
if realm := i.Annotations[annotationKubernetesAuthRealm]; realm != "" && realm != traefikDefaultRealm {
|
||||||
log.Errorf("Value for annotation %q on ingress %s/%s invalid: no realm customization supported", annotationKubernetesAuthRealm, i.ObjectMeta.Namespace, i.ObjectMeta.Name)
|
log.Errorf("Value for annotation %q on ingress %s/%s invalid: no realm customization supported", annotationKubernetesAuthRealm, i.Namespace, i.Name)
|
||||||
delete(templateObjects.Backends, r.Host+pa.Path)
|
delete(templateObjects.Backends, baseName)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, exists := templateObjects.Frontends[r.Host+pa.Path]; !exists {
|
if _, exists := templateObjects.Frontends[baseName]; !exists {
|
||||||
basicAuthCreds, err := handleBasicAuthConfig(i, k8sClient)
|
basicAuthCreds, err := handleBasicAuthConfig(i, k8sClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to retrieve basic auth configuration for ingress %s/%s: %s", i.ObjectMeta.Namespace, i.ObjectMeta.Name, err)
|
log.Errorf("Failed to retrieve basic auth configuration for ingress %s/%s: %s", i.Namespace, i.Name, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
passHostHeader := label.GetBoolValue(i.Annotations, label.TraefikFrontendPassHostHeader, !p.DisablePassHostHeaders)
|
passHostHeader := label.GetBoolValue(i.Annotations, label.TraefikFrontendPassHostHeader, !p.DisablePassHostHeaders)
|
||||||
passTLSCert := label.GetBoolValue(i.Annotations, label.TraefikFrontendPassTLSCert, p.EnablePassTLSCert)
|
passTLSCert := label.GetBoolValue(i.Annotations, label.TraefikFrontendPassTLSCert, p.EnablePassTLSCert)
|
||||||
|
|
||||||
priority := label.GetIntValue(i.Annotations, label.TraefikFrontendPriority, 0)
|
priority := label.GetIntValue(i.Annotations, label.TraefikFrontendPriority, 0)
|
||||||
|
|
||||||
entryPoints := label.GetSliceStringValue(i.Annotations, label.TraefikFrontendEntryPoints)
|
entryPoints := label.GetSliceStringValue(i.Annotations, label.TraefikFrontendEntryPoints)
|
||||||
|
|
||||||
whitelistSourceRange := label.GetSliceStringValue(i.Annotations, annotationKubernetesWhitelistSourceRange)
|
whitelistSourceRange := label.GetSliceStringValue(i.Annotations, annotationKubernetesWhitelistSourceRange)
|
||||||
|
|
||||||
errorPages := label.ParseErrorPages(i.Annotations, label.Prefix+label.BaseFrontendErrorPage, label.RegexpFrontendErrorPage)
|
errorPages := label.ParseErrorPages(i.Annotations, label.Prefix+label.BaseFrontendErrorPage, label.RegexpFrontendErrorPage)
|
||||||
|
|
||||||
templateObjects.Frontends[r.Host+pa.Path] = &types.Frontend{
|
templateObjects.Frontends[baseName] = &types.Frontend{
|
||||||
Backend: r.Host + pa.Path,
|
Backend: baseName,
|
||||||
PassHostHeader: passHostHeader,
|
PassHostHeader: passHostHeader,
|
||||||
PassTLSCert: passTLSCert,
|
PassTLSCert: passTLSCert,
|
||||||
Routes: make(map[string]types.Route),
|
Routes: make(map[string]types.Route),
|
||||||
|
@ -239,42 +237,42 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(r.Host) > 0 {
|
if len(r.Host) > 0 {
|
||||||
if _, exists := templateObjects.Frontends[r.Host+pa.Path].Routes[r.Host]; !exists {
|
if _, exists := templateObjects.Frontends[baseName].Routes[r.Host]; !exists {
|
||||||
templateObjects.Frontends[r.Host+pa.Path].Routes[r.Host] = types.Route{
|
templateObjects.Frontends[baseName].Routes[r.Host] = types.Route{
|
||||||
Rule: getRuleForHost(r.Host),
|
Rule: getRuleForHost(r.Host),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if rule := getRuleForPath(pa, i); rule != "" {
|
if rule := getRuleForPath(pa, i); rule != "" {
|
||||||
templateObjects.Frontends[r.Host+pa.Path].Routes[pa.Path] = types.Route{
|
templateObjects.Frontends[baseName].Routes[pa.Path] = types.Route{
|
||||||
Rule: rule,
|
Rule: rule,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
service, exists, err := k8sClient.GetService(i.ObjectMeta.Namespace, pa.Backend.ServiceName)
|
service, exists, err := k8sClient.GetService(i.Namespace, pa.Backend.ServiceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error while retrieving service information from k8s API %s/%s: %v", i.ObjectMeta.Namespace, pa.Backend.ServiceName, err)
|
log.Errorf("Error while retrieving service information from k8s API %s/%s: %v", i.Namespace, pa.Backend.ServiceName, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
log.Errorf("Service not found for %s/%s", i.ObjectMeta.Namespace, pa.Backend.ServiceName)
|
log.Errorf("Service not found for %s/%s", i.Namespace, pa.Backend.ServiceName)
|
||||||
delete(templateObjects.Frontends, r.Host+pa.Path)
|
delete(templateObjects.Frontends, baseName)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if expression := service.Annotations[label.TraefikBackendCircuitBreaker]; expression != "" {
|
if expression := service.Annotations[label.TraefikBackendCircuitBreaker]; expression != "" {
|
||||||
templateObjects.Backends[r.Host+pa.Path].CircuitBreaker = &types.CircuitBreaker{
|
templateObjects.Backends[baseName].CircuitBreaker = &types.CircuitBreaker{
|
||||||
Expression: expression,
|
Expression: expression,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templateObjects.Backends[r.Host+pa.Path].LoadBalancer = getLoadBalancer(service)
|
templateObjects.Backends[baseName].LoadBalancer = getLoadBalancer(service)
|
||||||
templateObjects.Backends[r.Host+pa.Path].Buffering = getBuffering(service)
|
templateObjects.Backends[baseName].Buffering = getBuffering(service)
|
||||||
|
|
||||||
if maxConn := getMaxConn(service); maxConn != nil {
|
if maxConn := getMaxConn(service); maxConn != nil {
|
||||||
templateObjects.Backends[r.Host+pa.Path].MaxConn = maxConn
|
templateObjects.Backends[baseName].MaxConn = maxConn
|
||||||
}
|
}
|
||||||
|
|
||||||
protocol := label.DefaultProtocol
|
protocol := label.DefaultProtocol
|
||||||
|
@ -288,24 +286,24 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
|
||||||
url := protocol + "://" + service.Spec.ExternalName
|
url := protocol + "://" + service.Spec.ExternalName
|
||||||
name := url
|
name := url
|
||||||
|
|
||||||
templateObjects.Backends[r.Host+pa.Path].Servers[name] = types.Server{
|
templateObjects.Backends[baseName].Servers[name] = types.Server{
|
||||||
URL: url,
|
URL: url,
|
||||||
Weight: 1,
|
Weight: 1,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
endpoints, exists, err := k8sClient.GetEndpoints(service.ObjectMeta.Namespace, service.ObjectMeta.Name)
|
endpoints, exists, err := k8sClient.GetEndpoints(service.Namespace, service.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error retrieving endpoints %s/%s: %v", service.ObjectMeta.Namespace, service.ObjectMeta.Name, err)
|
log.Errorf("Error retrieving endpoints %s/%s: %v", service.Namespace, service.Name, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
log.Warnf("Endpoints not found for %s/%s", service.ObjectMeta.Namespace, service.ObjectMeta.Name)
|
log.Warnf("Endpoints not found for %s/%s", service.Namespace, service.Name)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(endpoints.Subsets) == 0 {
|
if len(endpoints.Subsets) == 0 {
|
||||||
log.Warnf("Endpoints not available for %s/%s", service.ObjectMeta.Namespace, service.ObjectMeta.Name)
|
log.Warnf("Endpoints not available for %s/%s", service.Namespace, service.Name)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -316,7 +314,7 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
|
||||||
if address.TargetRef != nil && address.TargetRef.Name != "" {
|
if address.TargetRef != nil && address.TargetRef.Name != "" {
|
||||||
name = address.TargetRef.Name
|
name = address.TargetRef.Name
|
||||||
}
|
}
|
||||||
templateObjects.Backends[r.Host+pa.Path].Servers[name] = types.Server{
|
templateObjects.Backends[baseName].Servers[name] = types.Server{
|
||||||
URL: url,
|
URL: url,
|
||||||
Weight: 1,
|
Weight: 1,
|
||||||
}
|
}
|
||||||
|
|
|
@ -202,7 +202,7 @@ func TestRuleType(t *testing.T) {
|
||||||
)))
|
)))
|
||||||
|
|
||||||
if test.ingressRuleType != "" {
|
if test.ingressRuleType != "" {
|
||||||
ingress.ObjectMeta.Annotations = map[string]string{
|
ingress.Annotations = map[string]string{
|
||||||
label.TraefikFrontendRuleType: test.ingressRuleType,
|
label.TraefikFrontendRuleType: test.ingressRuleType,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue