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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 6 additions & 12 deletions
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

Lines changed: 3 additions & 5 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 6 additions & 12 deletions
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

Lines changed: 4 additions & 7 deletions
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

Lines changed: 7 additions & 12 deletions
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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 1 addition & 4 deletions
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{

0 commit comments

Comments
 (0)