Skip to content

Commit 17e9637

Browse files
authored
Refactor getTTLFromAnnotations() to not return error (#3939)
* Refactor getTTLFromAnnotations() to not return error * Improve log messages
1 parent 091ae32 commit 17e9637

16 files changed

+77
-132
lines changed

source/ambassador_host.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,7 @@ func (sc *ambassadorHostSource) endpointsFromHost(ctx context.Context, host *amb
173173
resource := fmt.Sprintf("host/%s/%s", host.Namespace, host.Name)
174174

175175
annotations := host.Annotations
176-
ttl, err := getTTLFromAnnotations(annotations)
177-
if err != nil {
178-
return nil, err
179-
}
176+
ttl := getTTLFromAnnotations(annotations, resource)
180177

181178
if host.Spec != nil {
182179
hostname := host.Spec.Hostname

source/contour_httpproxy.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,9 @@ func (sc *httpProxySource) endpointsFromTemplate(httpProxy *projectcontour.HTTPP
186186
return nil, err
187187
}
188188

189-
ttl, err := getTTLFromAnnotations(httpProxy.Annotations)
190-
if err != nil {
191-
log.Warn(err)
192-
}
189+
resource := fmt.Sprintf("HTTPProxy/%s/%s", httpProxy.Namespace, httpProxy.Name)
190+
191+
ttl := getTTLFromAnnotations(httpProxy.Annotations, resource)
193192

194193
targets := getTargetsFromTargetAnnotation(httpProxy.Annotations)
195194
if len(targets) == 0 {
@@ -205,8 +204,6 @@ func (sc *httpProxySource) endpointsFromTemplate(httpProxy *projectcontour.HTTPP
205204

206205
providerSpecific, setIdentifier := getProviderSpecificAnnotations(httpProxy.Annotations)
207206

208-
resource := fmt.Sprintf("HTTPProxy/%s/%s", httpProxy.Namespace, httpProxy.Name)
209-
210207
var endpoints []*endpoint.Endpoint
211208
for _, hostname := range hostnames {
212209
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
@@ -252,10 +249,9 @@ func (sc *httpProxySource) endpointsFromHTTPProxy(httpProxy *projectcontour.HTTP
252249
return nil, nil
253250
}
254251

255-
ttl, err := getTTLFromAnnotations(httpProxy.Annotations)
256-
if err != nil {
257-
log.Warn(err)
258-
}
252+
resource := fmt.Sprintf("HTTPProxy/%s/%s", httpProxy.Namespace, httpProxy.Name)
253+
254+
ttl := getTTLFromAnnotations(httpProxy.Annotations, resource)
259255

260256
targets := getTargetsFromTargetAnnotation(httpProxy.Annotations)
261257

@@ -272,8 +268,6 @@ func (sc *httpProxySource) endpointsFromHTTPProxy(httpProxy *projectcontour.HTTP
272268

273269
providerSpecific, setIdentifier := getProviderSpecificAnnotations(httpProxy.Annotations)
274270

275-
resource := fmt.Sprintf("HTTPProxy/%s/%s", httpProxy.Namespace, httpProxy.Name)
276-
277271
var endpoints []*endpoint.Endpoint
278272

279273
if virtualHost := httpProxy.Spec.VirtualHost; virtualHost != nil {

source/f5_virtualserver.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,9 @@ func (vs *f5VirtualServerSource) endpointsFromVirtualServers(virtualServers []*f
147147
var endpoints []*endpoint.Endpoint
148148

149149
for _, virtualServer := range virtualServers {
150-
ttl, err := getTTLFromAnnotations(virtualServer.Annotations)
151-
if err != nil {
152-
return nil, err
153-
}
150+
resource := fmt.Sprintf("f5-virtualserver/%s/%s", virtualServer.Namespace, virtualServer.Name)
151+
152+
ttl := getTTLFromAnnotations(virtualServer.Annotations, resource)
154153

155154
targets := getTargetsFromTargetAnnotation(virtualServer.Annotations)
156155
if len(targets) == 0 && virtualServer.Spec.VirtualServerAddress != "" {
@@ -160,7 +159,6 @@ func (vs *f5VirtualServerSource) endpointsFromVirtualServers(virtualServers []*f
160159
targets = append(targets, virtualServer.Status.VSAddress)
161160
}
162161

163-
resource := fmt.Sprintf("f5-virtualserver/%s/%s", virtualServer.Namespace, virtualServer.Name)
164162
endpoints = append(endpoints, endpointsForHostname(virtualServer.Spec.Host, targets, ttl, nil, "", resource)...)
165163
}
166164

source/gateway.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,7 @@ func (src *gatewayRouteSource) Endpoints(ctx context.Context) ([]*endpoint.Endpo
230230
// Create endpoints from hostnames and targets.
231231
resource := fmt.Sprintf("%s/%s/%s", kind, meta.Namespace, meta.Name)
232232
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annots)
233-
ttl, err := getTTLFromAnnotations(annots)
234-
if err != nil {
235-
log.Warn(err)
236-
}
233+
ttl := getTTLFromAnnotations(annots, resource)
237234
for host, targets := range hostTargets {
238235
endpoints = append(endpoints, endpointsForHostname(host, targets, ttl, providerSpecific, setIdentifier, resource)...)
239236
}

source/gloo_proxy.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package source
1919
import (
2020
"context"
2121
"encoding/json"
22+
"fmt"
2223
"strings"
2324

2425
log "github.com/sirupsen/logrus"
@@ -155,16 +156,16 @@ func (gs *glooSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, erro
155156

156157
func (gs *glooSource) generateEndpointsFromProxy(ctx context.Context, proxy *proxy, targets endpoint.Targets) ([]*endpoint.Endpoint, error) {
157158
endpoints := []*endpoint.Endpoint{}
159+
160+
resource := fmt.Sprintf("proxy/%s/%s", proxy.Metadata.Namespace, proxy.Metadata.Name)
161+
158162
for _, listener := range proxy.Spec.Listeners {
159163
for _, virtualHost := range listener.HTTPListener.VirtualHosts {
160164
annotations, err := gs.annotationsFromProxySource(ctx, virtualHost)
161165
if err != nil {
162166
return nil, err
163167
}
164-
ttl, err := getTTLFromAnnotations(annotations)
165-
if err != nil {
166-
return nil, err
167-
}
168+
ttl := getTTLFromAnnotations(annotations, resource)
168169
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annotations)
169170
for _, domain := range virtualHost.Domains {
170171
endpoints = append(endpoints, endpointsForHostname(strings.TrimSuffix(domain, "."), targets, ttl, providerSpecific, setIdentifier, "")...)

source/ingress.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,9 @@ func (sc *ingressSource) endpointsFromTemplate(ing *networkv1.Ingress) ([]*endpo
188188
return nil, err
189189
}
190190

191-
ttl, err := getTTLFromAnnotations(ing.Annotations)
192-
if err != nil {
193-
log.Warn(err)
194-
}
191+
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)
192+
193+
ttl := getTTLFromAnnotations(ing.Annotations, resource)
195194

196195
targets := getTargetsFromTargetAnnotation(ing.Annotations)
197196
if len(targets) == 0 {
@@ -200,8 +199,6 @@ func (sc *ingressSource) endpointsFromTemplate(ing *networkv1.Ingress) ([]*endpo
200199

201200
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ing.Annotations)
202201

203-
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)
204-
205202
var endpoints []*endpoint.Endpoint
206203
for _, hostname := range hostnames {
207204
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
@@ -289,10 +286,9 @@ func (sc *ingressSource) setDualstackLabel(ingress *networkv1.Ingress, endpoints
289286

290287
// endpointsFromIngress extracts the endpoints from ingress object
291288
func endpointsFromIngress(ing *networkv1.Ingress, ignoreHostnameAnnotation bool, ignoreIngressTLSSpec bool, ignoreIngressRulesSpec bool) []*endpoint.Endpoint {
292-
ttl, err := getTTLFromAnnotations(ing.Annotations)
293-
if err != nil {
294-
log.Warn(err)
295-
}
289+
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)
290+
291+
ttl := getTTLFromAnnotations(ing.Annotations, resource)
296292

297293
targets := getTargetsFromTargetAnnotation(ing.Annotations)
298294

@@ -302,8 +298,6 @@ func endpointsFromIngress(ing *networkv1.Ingress, ignoreHostnameAnnotation bool,
302298

303299
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ing.Annotations)
304300

305-
resource := fmt.Sprintf("ingress/%s/%s", ing.Namespace, ing.Name)
306-
307301
// Gather endpoints defined on hosts sections of the ingress
308302
var definedHostsEndpoints []*endpoint.Endpoint
309303
// Skip endpoints if we do not want entries from Rules section

source/istio_gateway.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,14 @@ func (sc *gatewaySource) targetsFromGateway(ctx context.Context, gateway *networ
304304
// endpointsFromGatewayConfig extracts the endpoints from an Istio Gateway Config object
305305
func (sc *gatewaySource) endpointsFromGateway(ctx context.Context, hostnames []string, gateway *networkingv1alpha3.Gateway) ([]*endpoint.Endpoint, error) {
306306
var endpoints []*endpoint.Endpoint
307+
var err error
308+
309+
resource := fmt.Sprintf("gateway/%s/%s", gateway.Namespace, gateway.Name)
307310

308311
annotations := gateway.Annotations
309-
ttl, err := getTTLFromAnnotations(annotations)
310-
if err != nil {
311-
log.Warn(err)
312-
}
312+
ttl := getTTLFromAnnotations(annotations, resource)
313313

314314
targets := getTargetsFromTargetAnnotation(annotations)
315-
316315
if len(targets) == 0 {
317316
targets, err = sc.targetsFromGateway(ctx, gateway)
318317
if err != nil {
@@ -322,8 +321,6 @@ func (sc *gatewaySource) endpointsFromGateway(ctx context.Context, hostnames []s
322321

323322
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annotations)
324323

325-
resource := fmt.Sprintf("gateway/%s/%s", gateway.Namespace, gateway.Name)
326-
327324
for _, host := range hostnames {
328325
endpoints = append(endpoints, endpointsForHostname(host, targets, ttl, providerSpecific, setIdentifier, resource)...)
329326
}

source/istio_virtualservice.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,11 @@ func (sc *virtualServiceSource) endpointsFromTemplate(ctx context.Context, virtu
222222
return nil, err
223223
}
224224

225-
ttl, err := getTTLFromAnnotations(virtualService.Annotations)
226-
if err != nil {
227-
log.Warn(err)
228-
}
225+
resource := fmt.Sprintf("virtualservice/%s/%s", virtualService.Namespace, virtualService.Name)
229226

230-
providerSpecific, setIdentifier := getProviderSpecificAnnotations(virtualService.Annotations)
227+
ttl := getTTLFromAnnotations(virtualService.Annotations, resource)
231228

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

234231
var endpoints []*endpoint.Endpoint
235232
for _, hostname := range hostnames {
@@ -312,18 +309,16 @@ func (sc *virtualServiceSource) targetsFromVirtualService(ctx context.Context, v
312309
// endpointsFromVirtualService extracts the endpoints from an Istio VirtualService Config object
313310
func (sc *virtualServiceSource) endpointsFromVirtualService(ctx context.Context, virtualservice *networkingv1alpha3.VirtualService) ([]*endpoint.Endpoint, error) {
314311
var endpoints []*endpoint.Endpoint
312+
var err error
315313

316-
ttl, err := getTTLFromAnnotations(virtualservice.Annotations)
317-
if err != nil {
318-
log.Warn(err)
319-
}
314+
resource := fmt.Sprintf("virtualservice/%s/%s", virtualservice.Namespace, virtualservice.Name)
315+
316+
ttl := getTTLFromAnnotations(virtualservice.Annotations, resource)
320317

321318
targetsFromAnnotation := getTargetsFromTargetAnnotation(virtualservice.Annotations)
322319

323320
providerSpecific, setIdentifier := getProviderSpecificAnnotations(virtualservice.Annotations)
324321

325-
resource := fmt.Sprintf("virtualservice/%s/%s", virtualservice.Namespace, virtualservice.Name)
326-
327322
for _, host := range virtualservice.Spec.Hosts {
328323
if host == "" || host == "*" {
329324
continue

source/kong_tcpingress.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,11 @@ func (sc *kongTCPIngressSource) setDualstackLabel(tcpIngress *TCPIngress, endpoi
204204
func (sc *kongTCPIngressSource) endpointsFromTCPIngress(tcpIngress *TCPIngress, targets endpoint.Targets) ([]*endpoint.Endpoint, error) {
205205
var endpoints []*endpoint.Endpoint
206206

207-
ttl, err := getTTLFromAnnotations(tcpIngress.Annotations)
208-
if err != nil {
209-
return nil, err
210-
}
207+
resource := fmt.Sprintf("tcpingress/%s/%s", tcpIngress.Namespace, tcpIngress.Name)
211208

212-
providerSpecific, setIdentifier := getProviderSpecificAnnotations(tcpIngress.Annotations)
209+
ttl := getTTLFromAnnotations(tcpIngress.Annotations, resource)
213210

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

216213
hostnameList := getHostnamesFromAnnotations(tcpIngress.Annotations)
217214
for _, hostname := range hostnameList {

source/node.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ func (ns *nodeSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, erro
104104

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

107-
ttl, err := getTTLFromAnnotations(node.Annotations)
108-
if err != nil {
109-
log.Warn(err)
110-
}
107+
ttl := getTTLFromAnnotations(node.Annotations, fmt.Sprintf("node/%s", node.Name))
111108

112109
// create new endpoint with the information we already have
113110
ep := &endpoint.Endpoint{

source/openshift_route.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,9 @@ func (ors *ocpRouteSource) endpointsFromTemplate(ocpRoute *routev1.Route) ([]*en
174174
return nil, err
175175
}
176176

177-
ttl, err := getTTLFromAnnotations(ocpRoute.Annotations)
178-
if err != nil {
179-
log.Warn(err)
180-
}
177+
resource := fmt.Sprintf("route/%s/%s", ocpRoute.Namespace, ocpRoute.Name)
178+
179+
ttl := getTTLFromAnnotations(ocpRoute.Annotations, resource)
181180

182181
targets := getTargetsFromTargetAnnotation(ocpRoute.Annotations)
183182
if len(targets) == 0 {
@@ -187,8 +186,6 @@ func (ors *ocpRouteSource) endpointsFromTemplate(ocpRoute *routev1.Route) ([]*en
187186

188187
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ocpRoute.Annotations)
189188

190-
resource := fmt.Sprintf("route/%s/%s", ocpRoute.Namespace, ocpRoute.Name)
191-
192189
var endpoints []*endpoint.Endpoint
193190
for _, hostname := range hostnames {
194191
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
@@ -230,10 +227,9 @@ func (ors *ocpRouteSource) filterByAnnotations(ocpRoutes []*routev1.Route) ([]*r
230227
func (ors *ocpRouteSource) endpointsFromOcpRoute(ocpRoute *routev1.Route, ignoreHostnameAnnotation bool) []*endpoint.Endpoint {
231228
var endpoints []*endpoint.Endpoint
232229

233-
ttl, err := getTTLFromAnnotations(ocpRoute.Annotations)
234-
if err != nil {
235-
log.Warn(err)
236-
}
230+
resource := fmt.Sprintf("route/%s/%s", ocpRoute.Namespace, ocpRoute.Name)
231+
232+
ttl := getTTLFromAnnotations(ocpRoute.Annotations, resource)
237233

238234
targets := getTargetsFromTargetAnnotation(ocpRoute.Annotations)
239235
targetsFromRoute, host := ors.getTargetsFromRouteStatus(ocpRoute.Status)
@@ -244,8 +240,6 @@ func (ors *ocpRouteSource) endpointsFromOcpRoute(ocpRoute *routev1.Route, ignore
244240

245241
providerSpecific, setIdentifier := getProviderSpecificAnnotations(ocpRoute.Annotations)
246242

247-
resource := fmt.Sprintf("route/%s/%s", ocpRoute.Namespace, ocpRoute.Name)
248-
249243
if host != "" {
250244
endpoints = append(endpoints, endpointsForHostname(host, targets, ttl, providerSpecific, setIdentifier, resource)...)
251245
}

source/service.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,7 @@ func (sc *serviceSource) setResourceLabel(service *v1.Service, endpoints []*endp
460460

461461
func (sc *serviceSource) generateEndpoints(svc *v1.Service, hostname string, providerSpecific endpoint.ProviderSpecific, setIdentifier string, useClusterIP bool) []*endpoint.Endpoint {
462462
hostname = strings.TrimSuffix(hostname, ".")
463-
ttl, err := getTTLFromAnnotations(svc.Annotations)
464-
if err != nil {
465-
log.Warn(err)
466-
}
463+
ttl := getTTLFromAnnotations(svc.Annotations, fmt.Sprintf("service/%s/%s", svc.Namespace, svc.Name))
467464

468465
epA := &endpoint.Endpoint{
469466
RecordTTL: ttl,
@@ -510,6 +507,7 @@ func (sc *serviceSource) generateEndpoints(svc *v1.Service, hostname string, pro
510507
}
511508
case v1.ServiceTypeNodePort:
512509
// add the nodeTargets and extract an SRV endpoint
510+
var err error
513511
targets, err = sc.extractNodePortTargets(svc)
514512
if err != nil {
515513
log.Errorf("Unable to extract targets from service %s/%s error: %v", svc.Namespace, svc.Name, err)

source/skipper_routegroup.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,10 @@ func (sc *routeGroupSource) endpointsFromTemplate(rg *routeGroup) ([]*endpoint.E
300300

301301
hostnames := buf.String()
302302

303+
resource := fmt.Sprintf("routegroup/%s/%s", rg.Metadata.Namespace, rg.Metadata.Name)
304+
303305
// error handled in endpointsFromRouteGroup(), otherwise duplicate log
304-
ttl, _ := getTTLFromAnnotations(rg.Metadata.Annotations)
306+
ttl := getTTLFromAnnotations(rg.Metadata.Annotations, resource)
305307

306308
targets := getTargetsFromTargetAnnotation(rg.Metadata.Annotations)
307309

@@ -311,8 +313,6 @@ func (sc *routeGroupSource) endpointsFromTemplate(rg *routeGroup) ([]*endpoint.E
311313

312314
providerSpecific, setIdentifier := getProviderSpecificAnnotations(rg.Metadata.Annotations)
313315

314-
resource := fmt.Sprintf("routegroup/%s/%s", rg.Metadata.Namespace, rg.Metadata.Name)
315-
316316
var endpoints []*endpoint.Endpoint
317317
// splits the FQDN template and removes the trailing periods
318318
hostnameList := strings.Split(strings.Replace(hostnames, " ", "", -1), ",")
@@ -336,10 +336,10 @@ func (sc *routeGroupSource) setRouteGroupDualstackLabel(rg *routeGroup, eps []*e
336336
// annotation logic ported from source/ingress.go without Spec.TLS part, because it'S not supported in RouteGroup
337337
func (sc *routeGroupSource) endpointsFromRouteGroup(rg *routeGroup) []*endpoint.Endpoint {
338338
endpoints := []*endpoint.Endpoint{}
339-
ttl, err := getTTLFromAnnotations(rg.Metadata.Annotations)
340-
if err != nil {
341-
log.Warnf("Failed to get TTL from annotation: %v", err)
342-
}
339+
340+
resource := fmt.Sprintf("routegroup/%s/%s", rg.Metadata.Namespace, rg.Metadata.Name)
341+
342+
ttl := getTTLFromAnnotations(rg.Metadata.Annotations, resource)
343343

344344
targets := getTargetsFromTargetAnnotation(rg.Metadata.Annotations)
345345
if len(targets) == 0 {
@@ -355,8 +355,6 @@ func (sc *routeGroupSource) endpointsFromRouteGroup(rg *routeGroup) []*endpoint.
355355

356356
providerSpecific, setIdentifier := getProviderSpecificAnnotations(rg.Metadata.Annotations)
357357

358-
resource := fmt.Sprintf("routegroup/%s/%s", rg.Metadata.Namespace, rg.Metadata.Name)
359-
360358
for _, src := range rg.Spec.Hosts {
361359
if src == "" {
362360
continue

0 commit comments

Comments
 (0)