Skip to content

Commit

Permalink
Improve log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed Sep 28, 2023
1 parent 1924dca commit a961e39
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 70 deletions.
2 changes: 1 addition & 1 deletion source/ambassador_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (sc *ambassadorHostSource) endpointsFromHost(ctx context.Context, host *amb
resource := fmt.Sprintf("host/%s/%s", host.Namespace, host.Name)

annotations := host.Annotations
ttl := getTTLFromAnnotations(annotations)
ttl := getTTLFromAnnotations(annotations, resource)

if host.Spec != nil {
hostname := host.Spec.Hostname
Expand Down
12 changes: 6 additions & 6 deletions source/contour_httpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ func (sc *httpProxySource) endpointsFromTemplate(httpProxy *projectcontour.HTTPP
return nil, err
}

ttl := getTTLFromAnnotations(httpProxy.Annotations)
resource := fmt.Sprintf("HTTPProxy/%s/%s", httpProxy.Namespace, httpProxy.Name)

ttl := getTTLFromAnnotations(httpProxy.Annotations, resource)

targets := getTargetsFromTargetAnnotation(httpProxy.Annotations)
if len(targets) == 0 {
Expand All @@ -202,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)...)
Expand Down Expand Up @@ -249,7 +249,9 @@ func (sc *httpProxySource) endpointsFromHTTPProxy(httpProxy *projectcontour.HTTP
return nil, nil
}

ttl := getTTLFromAnnotations(httpProxy.Annotations)
resource := fmt.Sprintf("HTTPProxy/%s/%s", httpProxy.Namespace, httpProxy.Name)

ttl := getTTLFromAnnotations(httpProxy.Annotations, resource)

targets := getTargetsFromTargetAnnotation(httpProxy.Annotations)

Expand All @@ -266,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 {
Expand Down
12 changes: 7 additions & 5 deletions source/f5_virtualserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ func (vs *f5VirtualServerSource) endpointsFromVirtualServers(virtualServers []*f
var endpoints []*endpoint.Endpoint

for _, virtualServer := range virtualServers {
ttl := getTTLFromAnnotations(virtualServer.Annotations)
resource := fmt.Sprintf("f5-virtualserver/%s/%s", virtualServer.Namespace, virtualServer.Name)

ttl := getTTLFromAnnotations(virtualServer.Annotations, resource)

if virtualServer.Spec.VirtualServerAddress != "" {
ep := &endpoint.Endpoint{
Expand All @@ -160,7 +162,7 @@ func (vs *f5VirtualServerSource) endpointsFromVirtualServers(virtualServers []*f
RecordTTL: ttl,
}

vs.setResourceLabel(virtualServer, ep)
vs.setResourceLabel(ep, resource)
endpoints = append(endpoints, ep)
continue
}
Expand All @@ -176,7 +178,7 @@ func (vs *f5VirtualServerSource) endpointsFromVirtualServers(virtualServers []*f
RecordTTL: ttl,
}

vs.setResourceLabel(virtualServer, ep)
vs.setResourceLabel(ep, resource)
endpoints = append(endpoints, ep)
continue
}
Expand Down Expand Up @@ -232,6 +234,6 @@ func (vs *f5VirtualServerSource) filterByAnnotations(virtualServers []*f5.Virtua
return filteredList, nil
}

func (vs *f5VirtualServerSource) setResourceLabel(virtualServer *f5.VirtualServer, ep *endpoint.Endpoint) {
ep.Labels[endpoint.ResourceLabelKey] = fmt.Sprintf("f5-virtualserver/%s/%s", virtualServer.Namespace, virtualServer.Name)
func (vs *f5VirtualServerSource) setResourceLabel(ep *endpoint.Endpoint, resource string) {
ep.Labels[endpoint.ResourceLabelKey] = resource
}
2 changes: 1 addition & 1 deletion source/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +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 := getTTLFromAnnotations(annots)
ttl := getTTLFromAnnotations(annots, resource)
for host, targets := range hostTargets {
endpoints = append(endpoints, endpointsForHostname(host, targets, ttl, providerSpecific, setIdentifier, resource)...)
}
Expand Down
6 changes: 5 additions & 1 deletion source/gloo_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package source
import (
"context"
"encoding/json"
"fmt"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -150,13 +151,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 := getTTLFromAnnotations(annotations)
ttl := getTTLFromAnnotations(annotations, resource)
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annotations)
for _, domain := range virtualHost.Domains {
endpoints = append(endpoints, endpointsForHostname(strings.TrimSuffix(domain, "."), targets, ttl, providerSpecific, setIdentifier, "")...)
Expand Down
12 changes: 6 additions & 6 deletions source/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ func (sc *ingressSource) endpointsFromTemplate(ing *networkv1.Ingress) ([]*endpo
return nil, err
}

ttl := getTTLFromAnnotations(ing.Annotations)
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)

ttl := getTTLFromAnnotations(ing.Annotations, resource)

targets := getTargetsFromTargetAnnotation(ing.Annotations)
if len(targets) == 0 {
Expand All @@ -197,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)...)
Expand Down Expand Up @@ -286,7 +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 := getTTLFromAnnotations(ing.Annotations)
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)

ttl := getTTLFromAnnotations(ing.Annotations, resource)

targets := getTargetsFromTargetAnnotation(ing.Annotations)

Expand All @@ -296,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
Expand Down
6 changes: 3 additions & 3 deletions source/istio_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,10 @@ func (sc *gatewaySource) endpointsFromGateway(ctx context.Context, hostnames []s
var endpoints []*endpoint.Endpoint
var err error

resource := fmt.Sprintf("gateway/%s/%s", gateway.Namespace, gateway.Name)

annotations := gateway.Annotations
ttl := getTTLFromAnnotations(annotations)
ttl := getTTLFromAnnotations(annotations, resource)

targets := getTargetsFromTargetAnnotation(annotations)
if len(targets) == 0 {
Expand All @@ -319,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)...)
}
Expand Down
12 changes: 6 additions & 6 deletions source/istio_virtualservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ func (sc *virtualServiceSource) endpointsFromTemplate(ctx context.Context, virtu
return nil, err
}

ttl := getTTLFromAnnotations(virtualService.Annotations)
resource := fmt.Sprintf("virtualservice/%s/%s", virtualService.Namespace, virtualService.Name)

providerSpecific, setIdentifier := getProviderSpecificAnnotations(virtualService.Annotations)
ttl := getTTLFromAnnotations(virtualService.Annotations, resource)

resource := fmt.Sprintf("virtualservice/%s/%s", virtualService.Namespace, virtualService.Name)
providerSpecific, setIdentifier := getProviderSpecificAnnotations(virtualService.Annotations)

var endpoints []*endpoint.Endpoint
for _, hostname := range hostnames {
Expand Down Expand Up @@ -311,14 +311,14 @@ func (sc *virtualServiceSource) endpointsFromVirtualService(ctx context.Context,
var endpoints []*endpoint.Endpoint
var err error

ttl := getTTLFromAnnotations(virtualservice.Annotations)
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
Expand Down
6 changes: 3 additions & 3 deletions source/kong_tcpingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ func (sc *kongTCPIngressSource) setDualstackLabel(tcpIngress *TCPIngress, endpoi
func (sc *kongTCPIngressSource) endpointsFromTCPIngress(tcpIngress *TCPIngress, targets endpoint.Targets) ([]*endpoint.Endpoint, error) {
var endpoints []*endpoint.Endpoint

ttl := getTTLFromAnnotations(tcpIngress.Annotations)
resource := fmt.Sprintf("tcpingress/%s/%s", tcpIngress.Namespace, tcpIngress.Name)

providerSpecific, setIdentifier := getProviderSpecificAnnotations(tcpIngress.Annotations)
ttl := getTTLFromAnnotations(tcpIngress.Annotations, resource)

resource := fmt.Sprintf("tcpingress/%s/%s", tcpIngress.Namespace, tcpIngress.Name)
providerSpecific, setIdentifier := getProviderSpecificAnnotations(tcpIngress.Annotations)

hostnameList := getHostnamesFromAnnotations(tcpIngress.Annotations)
for _, hostname := range hostnameList {
Expand Down
2 changes: 1 addition & 1 deletion source/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (ns *nodeSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, erro

log.Debugf("creating endpoint for node %s", node.Name)

ttl := getTTLFromAnnotations(node.Annotations)
ttl := getTTLFromAnnotations(node.Annotations, fmt.Sprintf("node/%s", node.Name))

// create new endpoint with the information we already have
ep := &endpoint.Endpoint{
Expand Down
12 changes: 6 additions & 6 deletions source/openshift_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ func (ors *ocpRouteSource) endpointsFromTemplate(ocpRoute *routev1.Route) ([]*en
return nil, err
}

ttl := getTTLFromAnnotations(ocpRoute.Annotations)
resource := fmt.Sprintf("route/%s/%s", ocpRoute.Namespace, ocpRoute.Name)

ttl := getTTLFromAnnotations(ocpRoute.Annotations, resource)

targets := getTargetsFromTargetAnnotation(ocpRoute.Annotations)
if len(targets) == 0 {
Expand All @@ -184,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)...)
Expand Down Expand Up @@ -227,7 +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 := getTTLFromAnnotations(ocpRoute.Annotations)
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)
Expand All @@ -238,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)...)
}
Expand Down
2 changes: 1 addition & 1 deletion source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +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 := getTTLFromAnnotations(svc.Annotations)
ttl := getTTLFromAnnotations(svc.Annotations, fmt.Sprintf("service/%s/%s", svc.Namespace, svc.Name))

epA := &endpoint.Endpoint{
RecordTTL: ttl,
Expand Down
13 changes: 7 additions & 6 deletions source/skipper_routegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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), ",")
Expand All @@ -336,7 +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 := getTTLFromAnnotations(rg.Metadata.Annotations)

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 {
Expand All @@ -352,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
Expand Down
14 changes: 9 additions & 5 deletions source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ type Source interface {
AddEventHandler(context.Context, func())
}

func getTTLFromAnnotations(annotations map[string]string) endpoint.TTL {
func getTTLFromAnnotations(annotations map[string]string, resource string) endpoint.TTL {
ttlNotConfigured := endpoint.TTL(0)
ttlAnnotation, exists := annotations[ttlAnnotationKey]
if !exists {
return ttlNotConfigured
}
ttlValue, err := parseTTL(ttlAnnotation)
if err != nil {
log.Warnf("\"%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 {
Expand All @@ -112,9 +112,13 @@ func getTTLFromAnnotations(annotations map[string]string) endpoint.TTL {
// 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
Expand Down
Loading

0 comments on commit a961e39

Please sign in to comment.