mirror of
https://github.com/kubernetes-sigs/external-dns.git
synced 2025-08-06 01:26:59 +02:00
Refactor getTTLFromAnnotations() to not return error (#3939)
* Refactor getTTLFromAnnotations() to not return error * Improve log messages
This commit is contained in:
parent
091ae320e1
commit
17e9637f11
@ -173,10 +173,7 @@ func (sc *ambassadorHostSource) endpointsFromHost(ctx context.Context, host *amb
|
||||
resource := fmt.Sprintf("host/%s/%s", host.Namespace, host.Name)
|
||||
|
||||
annotations := host.Annotations
|
||||
ttl, err := getTTLFromAnnotations(annotations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ttl := getTTLFromAnnotations(annotations, resource)
|
||||
|
||||
if host.Spec != nil {
|
||||
hostname := host.Spec.Hostname
|
||||
|
@ -186,10 +186,9 @@ func (sc *httpProxySource) endpointsFromTemplate(httpProxy *projectcontour.HTTPP
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ttl, err := getTTLFromAnnotations(httpProxy.Annotations)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
resource := fmt.Sprintf("HTTPProxy/%s/%s", httpProxy.Namespace, httpProxy.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(httpProxy.Annotations, resource)
|
||||
|
||||
targets := getTargetsFromTargetAnnotation(httpProxy.Annotations)
|
||||
if len(targets) == 0 {
|
||||
@ -205,8 +204,6 @@ func (sc *httpProxySource) endpointsFromTemplate(httpProxy *projectcontour.HTTPP
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(httpProxy.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("HTTPProxy/%s/%s", httpProxy.Namespace, httpProxy.Name)
|
||||
|
||||
var endpoints []*endpoint.Endpoint
|
||||
for _, hostname := range hostnames {
|
||||
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
|
||||
@ -252,10 +249,9 @@ func (sc *httpProxySource) endpointsFromHTTPProxy(httpProxy *projectcontour.HTTP
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
ttl, err := getTTLFromAnnotations(httpProxy.Annotations)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
resource := fmt.Sprintf("HTTPProxy/%s/%s", httpProxy.Namespace, httpProxy.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(httpProxy.Annotations, resource)
|
||||
|
||||
targets := getTargetsFromTargetAnnotation(httpProxy.Annotations)
|
||||
|
||||
@ -272,8 +268,6 @@ func (sc *httpProxySource) endpointsFromHTTPProxy(httpProxy *projectcontour.HTTP
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(httpProxy.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("HTTPProxy/%s/%s", httpProxy.Namespace, httpProxy.Name)
|
||||
|
||||
var endpoints []*endpoint.Endpoint
|
||||
|
||||
if virtualHost := httpProxy.Spec.VirtualHost; virtualHost != nil {
|
||||
|
@ -147,10 +147,9 @@ func (vs *f5VirtualServerSource) endpointsFromVirtualServers(virtualServers []*f
|
||||
var endpoints []*endpoint.Endpoint
|
||||
|
||||
for _, virtualServer := range virtualServers {
|
||||
ttl, err := getTTLFromAnnotations(virtualServer.Annotations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resource := fmt.Sprintf("f5-virtualserver/%s/%s", virtualServer.Namespace, virtualServer.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(virtualServer.Annotations, resource)
|
||||
|
||||
targets := getTargetsFromTargetAnnotation(virtualServer.Annotations)
|
||||
if len(targets) == 0 && virtualServer.Spec.VirtualServerAddress != "" {
|
||||
@ -160,7 +159,6 @@ func (vs *f5VirtualServerSource) endpointsFromVirtualServers(virtualServers []*f
|
||||
targets = append(targets, virtualServer.Status.VSAddress)
|
||||
}
|
||||
|
||||
resource := fmt.Sprintf("f5-virtualserver/%s/%s", virtualServer.Namespace, virtualServer.Name)
|
||||
endpoints = append(endpoints, endpointsForHostname(virtualServer.Spec.Host, targets, ttl, nil, "", resource)...)
|
||||
}
|
||||
|
||||
|
@ -230,10 +230,7 @@ func (src *gatewayRouteSource) Endpoints(ctx context.Context) ([]*endpoint.Endpo
|
||||
// Create endpoints from hostnames and targets.
|
||||
resource := fmt.Sprintf("%s/%s/%s", kind, meta.Namespace, meta.Name)
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annots)
|
||||
ttl, err := getTTLFromAnnotations(annots)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
ttl := getTTLFromAnnotations(annots, resource)
|
||||
for host, targets := range hostTargets {
|
||||
endpoints = append(endpoints, endpointsForHostname(host, targets, ttl, providerSpecific, setIdentifier, resource)...)
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ package source
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
@ -155,16 +156,16 @@ func (gs *glooSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, erro
|
||||
|
||||
func (gs *glooSource) generateEndpointsFromProxy(ctx context.Context, proxy *proxy, targets endpoint.Targets) ([]*endpoint.Endpoint, error) {
|
||||
endpoints := []*endpoint.Endpoint{}
|
||||
|
||||
resource := fmt.Sprintf("proxy/%s/%s", proxy.Metadata.Namespace, proxy.Metadata.Name)
|
||||
|
||||
for _, listener := range proxy.Spec.Listeners {
|
||||
for _, virtualHost := range listener.HTTPListener.VirtualHosts {
|
||||
annotations, err := gs.annotationsFromProxySource(ctx, virtualHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ttl, err := getTTLFromAnnotations(annotations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ttl := getTTLFromAnnotations(annotations, resource)
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annotations)
|
||||
for _, domain := range virtualHost.Domains {
|
||||
endpoints = append(endpoints, endpointsForHostname(strings.TrimSuffix(domain, "."), targets, ttl, providerSpecific, setIdentifier, "")...)
|
||||
|
@ -188,10 +188,9 @@ func (sc *ingressSource) endpointsFromTemplate(ing *networkv1.Ingress) ([]*endpo
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ttl, err := getTTLFromAnnotations(ing.Annotations)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(ing.Annotations, resource)
|
||||
|
||||
targets := getTargetsFromTargetAnnotation(ing.Annotations)
|
||||
if len(targets) == 0 {
|
||||
@ -200,8 +199,6 @@ func (sc *ingressSource) endpointsFromTemplate(ing *networkv1.Ingress) ([]*endpo
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ing.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)
|
||||
|
||||
var endpoints []*endpoint.Endpoint
|
||||
for _, hostname := range hostnames {
|
||||
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
|
||||
@ -289,10 +286,9 @@ func (sc *ingressSource) setDualstackLabel(ingress *networkv1.Ingress, endpoints
|
||||
|
||||
// endpointsFromIngress extracts the endpoints from ingress object
|
||||
func endpointsFromIngress(ing *networkv1.Ingress, ignoreHostnameAnnotation bool, ignoreIngressTLSSpec bool, ignoreIngressRulesSpec bool) []*endpoint.Endpoint {
|
||||
ttl, err := getTTLFromAnnotations(ing.Annotations)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(ing.Annotations, resource)
|
||||
|
||||
targets := getTargetsFromTargetAnnotation(ing.Annotations)
|
||||
|
||||
@ -302,8 +298,6 @@ func endpointsFromIngress(ing *networkv1.Ingress, ignoreHostnameAnnotation bool,
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ing.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)
|
||||
|
||||
// Gather endpoints defined on hosts sections of the ingress
|
||||
var definedHostsEndpoints []*endpoint.Endpoint
|
||||
// Skip endpoints if we do not want entries from Rules section
|
||||
|
@ -304,15 +304,14 @@ func (sc *gatewaySource) targetsFromGateway(ctx context.Context, gateway *networ
|
||||
// endpointsFromGatewayConfig extracts the endpoints from an Istio Gateway Config object
|
||||
func (sc *gatewaySource) endpointsFromGateway(ctx context.Context, hostnames []string, gateway *networkingv1alpha3.Gateway) ([]*endpoint.Endpoint, error) {
|
||||
var endpoints []*endpoint.Endpoint
|
||||
var err error
|
||||
|
||||
resource := fmt.Sprintf("gateway/%s/%s", gateway.Namespace, gateway.Name)
|
||||
|
||||
annotations := gateway.Annotations
|
||||
ttl, err := getTTLFromAnnotations(annotations)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
ttl := getTTLFromAnnotations(annotations, resource)
|
||||
|
||||
targets := getTargetsFromTargetAnnotation(annotations)
|
||||
|
||||
if len(targets) == 0 {
|
||||
targets, err = sc.targetsFromGateway(ctx, gateway)
|
||||
if err != nil {
|
||||
@ -322,8 +321,6 @@ func (sc *gatewaySource) endpointsFromGateway(ctx context.Context, hostnames []s
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annotations)
|
||||
|
||||
resource := fmt.Sprintf("gateway/%s/%s", gateway.Namespace, gateway.Name)
|
||||
|
||||
for _, host := range hostnames {
|
||||
endpoints = append(endpoints, endpointsForHostname(host, targets, ttl, providerSpecific, setIdentifier, resource)...)
|
||||
}
|
||||
|
@ -222,15 +222,12 @@ func (sc *virtualServiceSource) endpointsFromTemplate(ctx context.Context, virtu
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ttl, err := getTTLFromAnnotations(virtualService.Annotations)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
resource := fmt.Sprintf("virtualservice/%s/%s", virtualService.Namespace, virtualService.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(virtualService.Annotations, resource)
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(virtualService.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("virtualservice/%s/%s", virtualService.Namespace, virtualService.Name)
|
||||
|
||||
var endpoints []*endpoint.Endpoint
|
||||
for _, hostname := range hostnames {
|
||||
targets, err := sc.targetsFromVirtualService(ctx, virtualService, hostname)
|
||||
@ -312,18 +309,16 @@ func (sc *virtualServiceSource) targetsFromVirtualService(ctx context.Context, v
|
||||
// endpointsFromVirtualService extracts the endpoints from an Istio VirtualService Config object
|
||||
func (sc *virtualServiceSource) endpointsFromVirtualService(ctx context.Context, virtualservice *networkingv1alpha3.VirtualService) ([]*endpoint.Endpoint, error) {
|
||||
var endpoints []*endpoint.Endpoint
|
||||
var err error
|
||||
|
||||
ttl, err := getTTLFromAnnotations(virtualservice.Annotations)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
resource := fmt.Sprintf("virtualservice/%s/%s", virtualservice.Namespace, virtualservice.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(virtualservice.Annotations, resource)
|
||||
|
||||
targetsFromAnnotation := getTargetsFromTargetAnnotation(virtualservice.Annotations)
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(virtualservice.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("virtualservice/%s/%s", virtualservice.Namespace, virtualservice.Name)
|
||||
|
||||
for _, host := range virtualservice.Spec.Hosts {
|
||||
if host == "" || host == "*" {
|
||||
continue
|
||||
|
@ -204,15 +204,12 @@ func (sc *kongTCPIngressSource) setDualstackLabel(tcpIngress *TCPIngress, endpoi
|
||||
func (sc *kongTCPIngressSource) endpointsFromTCPIngress(tcpIngress *TCPIngress, targets endpoint.Targets) ([]*endpoint.Endpoint, error) {
|
||||
var endpoints []*endpoint.Endpoint
|
||||
|
||||
ttl, err := getTTLFromAnnotations(tcpIngress.Annotations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resource := fmt.Sprintf("tcpingress/%s/%s", tcpIngress.Namespace, tcpIngress.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(tcpIngress.Annotations, resource)
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(tcpIngress.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("tcpingress/%s/%s", tcpIngress.Namespace, tcpIngress.Name)
|
||||
|
||||
hostnameList := getHostnamesFromAnnotations(tcpIngress.Annotations)
|
||||
for _, hostname := range hostnameList {
|
||||
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
|
||||
|
@ -104,10 +104,7 @@ func (ns *nodeSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, erro
|
||||
|
||||
log.Debugf("creating endpoint for node %s", node.Name)
|
||||
|
||||
ttl, err := getTTLFromAnnotations(node.Annotations)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
ttl := getTTLFromAnnotations(node.Annotations, fmt.Sprintf("node/%s", node.Name))
|
||||
|
||||
// create new endpoint with the information we already have
|
||||
ep := &endpoint.Endpoint{
|
||||
|
@ -174,10 +174,9 @@ func (ors *ocpRouteSource) endpointsFromTemplate(ocpRoute *routev1.Route) ([]*en
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ttl, err := getTTLFromAnnotations(ocpRoute.Annotations)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
resource := fmt.Sprintf("route/%s/%s", ocpRoute.Namespace, ocpRoute.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(ocpRoute.Annotations, resource)
|
||||
|
||||
targets := getTargetsFromTargetAnnotation(ocpRoute.Annotations)
|
||||
if len(targets) == 0 {
|
||||
@ -187,8 +186,6 @@ func (ors *ocpRouteSource) endpointsFromTemplate(ocpRoute *routev1.Route) ([]*en
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ocpRoute.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("route/%s/%s", ocpRoute.Namespace, ocpRoute.Name)
|
||||
|
||||
var endpoints []*endpoint.Endpoint
|
||||
for _, hostname := range hostnames {
|
||||
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
|
||||
@ -230,10 +227,9 @@ func (ors *ocpRouteSource) filterByAnnotations(ocpRoutes []*routev1.Route) ([]*r
|
||||
func (ors *ocpRouteSource) endpointsFromOcpRoute(ocpRoute *routev1.Route, ignoreHostnameAnnotation bool) []*endpoint.Endpoint {
|
||||
var endpoints []*endpoint.Endpoint
|
||||
|
||||
ttl, err := getTTLFromAnnotations(ocpRoute.Annotations)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
resource := fmt.Sprintf("route/%s/%s", ocpRoute.Namespace, ocpRoute.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(ocpRoute.Annotations, resource)
|
||||
|
||||
targets := getTargetsFromTargetAnnotation(ocpRoute.Annotations)
|
||||
targetsFromRoute, host := ors.getTargetsFromRouteStatus(ocpRoute.Status)
|
||||
@ -244,8 +240,6 @@ func (ors *ocpRouteSource) endpointsFromOcpRoute(ocpRoute *routev1.Route, ignore
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ocpRoute.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("route/%s/%s", ocpRoute.Namespace, ocpRoute.Name)
|
||||
|
||||
if host != "" {
|
||||
endpoints = append(endpoints, endpointsForHostname(host, targets, ttl, providerSpecific, setIdentifier, resource)...)
|
||||
}
|
||||
|
@ -460,10 +460,7 @@ func (sc *serviceSource) setResourceLabel(service *v1.Service, endpoints []*endp
|
||||
|
||||
func (sc *serviceSource) generateEndpoints(svc *v1.Service, hostname string, providerSpecific endpoint.ProviderSpecific, setIdentifier string, useClusterIP bool) []*endpoint.Endpoint {
|
||||
hostname = strings.TrimSuffix(hostname, ".")
|
||||
ttl, err := getTTLFromAnnotations(svc.Annotations)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
ttl := getTTLFromAnnotations(svc.Annotations, fmt.Sprintf("service/%s/%s", svc.Namespace, svc.Name))
|
||||
|
||||
epA := &endpoint.Endpoint{
|
||||
RecordTTL: ttl,
|
||||
@ -510,6 +507,7 @@ func (sc *serviceSource) generateEndpoints(svc *v1.Service, hostname string, pro
|
||||
}
|
||||
case v1.ServiceTypeNodePort:
|
||||
// add the nodeTargets and extract an SRV endpoint
|
||||
var err error
|
||||
targets, err = sc.extractNodePortTargets(svc)
|
||||
if err != nil {
|
||||
log.Errorf("Unable to extract targets from service %s/%s error: %v", svc.Namespace, svc.Name, err)
|
||||
|
@ -300,8 +300,10 @@ func (sc *routeGroupSource) endpointsFromTemplate(rg *routeGroup) ([]*endpoint.E
|
||||
|
||||
hostnames := buf.String()
|
||||
|
||||
resource := fmt.Sprintf("routegroup/%s/%s", rg.Metadata.Namespace, rg.Metadata.Name)
|
||||
|
||||
// error handled in endpointsFromRouteGroup(), otherwise duplicate log
|
||||
ttl, _ := getTTLFromAnnotations(rg.Metadata.Annotations)
|
||||
ttl := getTTLFromAnnotations(rg.Metadata.Annotations, resource)
|
||||
|
||||
targets := getTargetsFromTargetAnnotation(rg.Metadata.Annotations)
|
||||
|
||||
@ -311,8 +313,6 @@ func (sc *routeGroupSource) endpointsFromTemplate(rg *routeGroup) ([]*endpoint.E
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(rg.Metadata.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("routegroup/%s/%s", rg.Metadata.Namespace, rg.Metadata.Name)
|
||||
|
||||
var endpoints []*endpoint.Endpoint
|
||||
// splits the FQDN template and removes the trailing periods
|
||||
hostnameList := strings.Split(strings.Replace(hostnames, " ", "", -1), ",")
|
||||
@ -336,10 +336,10 @@ func (sc *routeGroupSource) setRouteGroupDualstackLabel(rg *routeGroup, eps []*e
|
||||
// annotation logic ported from source/ingress.go without Spec.TLS part, because it'S not supported in RouteGroup
|
||||
func (sc *routeGroupSource) endpointsFromRouteGroup(rg *routeGroup) []*endpoint.Endpoint {
|
||||
endpoints := []*endpoint.Endpoint{}
|
||||
ttl, err := getTTLFromAnnotations(rg.Metadata.Annotations)
|
||||
if err != nil {
|
||||
log.Warnf("Failed to get TTL from annotation: %v", err)
|
||||
}
|
||||
|
||||
resource := fmt.Sprintf("routegroup/%s/%s", rg.Metadata.Namespace, rg.Metadata.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(rg.Metadata.Annotations, resource)
|
||||
|
||||
targets := getTargetsFromTargetAnnotation(rg.Metadata.Annotations)
|
||||
if len(targets) == 0 {
|
||||
@ -355,8 +355,6 @@ func (sc *routeGroupSource) endpointsFromRouteGroup(rg *routeGroup) []*endpoint.
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(rg.Metadata.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("routegroup/%s/%s", rg.Metadata.Namespace, rg.Metadata.Name)
|
||||
|
||||
for _, src := range rg.Spec.Hosts {
|
||||
if src == "" {
|
||||
continue
|
||||
|
@ -29,6 +29,7 @@ import (
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@ -86,20 +87,22 @@ type Source interface {
|
||||
AddEventHandler(context.Context, func())
|
||||
}
|
||||
|
||||
func getTTLFromAnnotations(annotations map[string]string) (endpoint.TTL, error) {
|
||||
func getTTLFromAnnotations(annotations map[string]string, resource string) endpoint.TTL {
|
||||
ttlNotConfigured := endpoint.TTL(0)
|
||||
ttlAnnotation, exists := annotations[ttlAnnotationKey]
|
||||
if !exists {
|
||||
return ttlNotConfigured, nil
|
||||
return ttlNotConfigured
|
||||
}
|
||||
ttlValue, err := parseTTL(ttlAnnotation)
|
||||
if err != nil {
|
||||
return ttlNotConfigured, fmt.Errorf("\"%v\" is not a valid TTL value", ttlAnnotation)
|
||||
log.Warnf("%s: \"%v\" is not a valid TTL value: %v", resource, ttlAnnotation, err)
|
||||
return ttlNotConfigured
|
||||
}
|
||||
if ttlValue < ttlMinimum || ttlValue > ttlMaximum {
|
||||
return ttlNotConfigured, fmt.Errorf("TTL value must be between [%d, %d]", ttlMinimum, ttlMaximum)
|
||||
log.Warnf("TTL value %q must be between [%d, %d]", ttlValue, ttlMinimum, ttlMaximum)
|
||||
return ttlNotConfigured
|
||||
}
|
||||
return endpoint.TTL(ttlValue), nil
|
||||
return endpoint.TTL(ttlValue)
|
||||
}
|
||||
|
||||
// parseTTL parses TTL from string, returning duration in seconds.
|
||||
@ -109,9 +112,13 @@ func getTTLFromAnnotations(annotations map[string]string) (endpoint.TTL, error)
|
||||
// Note: for durations like "1.5s" the fraction is omitted (resulting in 1 second
|
||||
// for the example).
|
||||
func parseTTL(s string) (ttlSeconds int64, err error) {
|
||||
ttlDuration, err := time.ParseDuration(s)
|
||||
if err != nil {
|
||||
return strconv.ParseInt(s, 10, 64)
|
||||
ttlDuration, errDuration := time.ParseDuration(s)
|
||||
if errDuration != nil {
|
||||
ttlInt, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return 0, errDuration
|
||||
}
|
||||
return ttlInt, nil
|
||||
}
|
||||
|
||||
return int64(ttlDuration.Seconds()), nil
|
||||
|
@ -30,61 +30,51 @@ func TestGetTTLFromAnnotations(t *testing.T) {
|
||||
title string
|
||||
annotations map[string]string
|
||||
expectedTTL endpoint.TTL
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
title: "TTL annotation not present",
|
||||
annotations: map[string]string{"foo": "bar"},
|
||||
expectedTTL: endpoint.TTL(0),
|
||||
expectedErr: nil,
|
||||
},
|
||||
{
|
||||
title: "TTL annotation value is not a number",
|
||||
annotations: map[string]string{ttlAnnotationKey: "foo"},
|
||||
expectedTTL: endpoint.TTL(0),
|
||||
expectedErr: fmt.Errorf("\"foo\" is not a valid TTL value"),
|
||||
},
|
||||
{
|
||||
title: "TTL annotation value is empty",
|
||||
annotations: map[string]string{ttlAnnotationKey: ""},
|
||||
expectedTTL: endpoint.TTL(0),
|
||||
expectedErr: fmt.Errorf("\"\" is not a valid TTL value"),
|
||||
},
|
||||
{
|
||||
title: "TTL annotation value is negative number",
|
||||
annotations: map[string]string{ttlAnnotationKey: "-1"},
|
||||
expectedTTL: endpoint.TTL(0),
|
||||
expectedErr: fmt.Errorf("TTL value must be between [%d, %d]", ttlMinimum, ttlMaximum),
|
||||
},
|
||||
{
|
||||
title: "TTL annotation value is too high",
|
||||
annotations: map[string]string{ttlAnnotationKey: fmt.Sprintf("%d", 1<<32)},
|
||||
expectedTTL: endpoint.TTL(0),
|
||||
expectedErr: fmt.Errorf("TTL value must be between [%d, %d]", ttlMinimum, ttlMaximum),
|
||||
},
|
||||
{
|
||||
title: "TTL annotation value is set correctly using integer",
|
||||
annotations: map[string]string{ttlAnnotationKey: "60"},
|
||||
expectedTTL: endpoint.TTL(60),
|
||||
expectedErr: nil,
|
||||
},
|
||||
{
|
||||
title: "TTL annotation value is set correctly using duration (whole)",
|
||||
annotations: map[string]string{ttlAnnotationKey: "10m"},
|
||||
expectedTTL: endpoint.TTL(600),
|
||||
expectedErr: nil,
|
||||
},
|
||||
{
|
||||
title: "TTL annotation value is set correctly using duration (fractional)",
|
||||
annotations: map[string]string{ttlAnnotationKey: "20.5s"},
|
||||
expectedTTL: endpoint.TTL(20),
|
||||
expectedErr: nil,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.title, func(t *testing.T) {
|
||||
ttl, err := getTTLFromAnnotations(tc.annotations)
|
||||
ttl := getTTLFromAnnotations(tc.annotations, "resource/test")
|
||||
assert.Equal(t, tc.expectedTTL, ttl)
|
||||
assert.Equal(t, tc.expectedErr, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -647,15 +647,12 @@ func (ts *traefikSource) setDualstackLabelIngressRouteUDP(ingressRoute *IngressR
|
||||
func (ts *traefikSource) endpointsFromIngressRoute(ingressRoute *IngressRoute, targets endpoint.Targets) ([]*endpoint.Endpoint, error) {
|
||||
var endpoints []*endpoint.Endpoint
|
||||
|
||||
ttl, err := getTTLFromAnnotations(ingressRoute.Annotations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resource := fmt.Sprintf("ingressroute/%s/%s", ingressRoute.Namespace, ingressRoute.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(ingressRoute.Annotations, resource)
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ingressRoute.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("ingressroute/%s/%s", ingressRoute.Namespace, ingressRoute.Name)
|
||||
|
||||
hostnameList := getHostnamesFromAnnotations(ingressRoute.Annotations)
|
||||
for _, hostname := range hostnameList {
|
||||
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
|
||||
@ -684,15 +681,12 @@ func (ts *traefikSource) endpointsFromIngressRoute(ingressRoute *IngressRoute, t
|
||||
func (ts *traefikSource) endpointsFromIngressRouteTCP(ingressRoute *IngressRouteTCP, targets endpoint.Targets) ([]*endpoint.Endpoint, error) {
|
||||
var endpoints []*endpoint.Endpoint
|
||||
|
||||
ttl, err := getTTLFromAnnotations(ingressRoute.Annotations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resource := fmt.Sprintf("ingressroutetcp/%s/%s", ingressRoute.Namespace, ingressRoute.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(ingressRoute.Annotations, resource)
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ingressRoute.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("ingressroutetcp/%s/%s", ingressRoute.Namespace, ingressRoute.Name)
|
||||
|
||||
hostnameList := getHostnamesFromAnnotations(ingressRoute.Annotations)
|
||||
for _, hostname := range hostnameList {
|
||||
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
|
||||
@ -722,15 +716,12 @@ func (ts *traefikSource) endpointsFromIngressRouteTCP(ingressRoute *IngressRoute
|
||||
func (ts *traefikSource) endpointsFromIngressRouteUDP(ingressRoute *IngressRouteUDP, targets endpoint.Targets) ([]*endpoint.Endpoint, error) {
|
||||
var endpoints []*endpoint.Endpoint
|
||||
|
||||
ttl, err := getTTLFromAnnotations(ingressRoute.Annotations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resource := fmt.Sprintf("ingressrouteudp/%s/%s", ingressRoute.Namespace, ingressRoute.Name)
|
||||
|
||||
ttl := getTTLFromAnnotations(ingressRoute.Annotations, resource)
|
||||
|
||||
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ingressRoute.Annotations)
|
||||
|
||||
resource := fmt.Sprintf("ingressrouteudp/%s/%s", ingressRoute.Namespace, ingressRoute.Name)
|
||||
|
||||
hostnameList := getHostnamesFromAnnotations(ingressRoute.Annotations)
|
||||
for _, hostname := range hostnameList {
|
||||
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
|
||||
|
Loading…
Reference in New Issue
Block a user