|
| 1 | +/* Copyright © 2026 Broadcom, Inc. All Rights Reserved. |
| 2 | + SPDX-License-Identifier: Apache-2.0 */ |
| 3 | + |
| 4 | +package gateway |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/types" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 13 | + |
| 14 | + servicecommon "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common" |
| 15 | + "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/dns" |
| 16 | + extdns "github.com/vmware-tanzu/nsx-operator/pkg/third_party/externaldns/endpoint" |
| 17 | + extdnssrc "github.com/vmware-tanzu/nsx-operator/pkg/third_party/externaldns/source" |
| 18 | +) |
| 19 | + |
| 20 | +// Route DNS: hostnames + ipCache → extdns.Endpoint; Gateway annotation DNS → owner-scoped batches (no ListenerSet annotation DNS). |
| 21 | +func (r *GatewayReconciler) buildEndpointsFromAnnotations(obj client.Object, targets extdns.Targets, owner *dns.ResourceRef, gwNN types.NamespacedName) (*dns.AggregatedDNSEndpoints, map[string]string, error) { |
| 22 | + parseHostnamesFromAnnotation := func(rawAnno map[string]string) []string { |
| 23 | + if rawAnno == nil { |
| 24 | + return nil |
| 25 | + } |
| 26 | + hostnames := rawAnno[servicecommon.AnnotationDNSHostnameKey] |
| 27 | + if strings.TrimSpace(hostnames) == "" { |
| 28 | + return nil |
| 29 | + } |
| 30 | + anno := map[string]string{ |
| 31 | + servicecommon.AnnotationDNSHostnameSourceKey: extdnssrc.GatewayHostnameSourceAnnotationOnly, |
| 32 | + servicecommon.AnnotationDNSHostnameKey: hostnames, |
| 33 | + } |
| 34 | + hosts := extdnssrc.RouteHostnames(&metav1.ObjectMeta{Annotations: anno}, nil, |
| 35 | + servicecommon.AnnotationDNSHostnameSourceKey, servicecommon.AnnotationDNSHostnameKey, false) |
| 36 | + return hosts |
| 37 | + } |
| 38 | + |
| 39 | + hosts := parseHostnamesFromAnnotation(obj.GetAnnotations()) |
| 40 | + if len(hosts) == 0 { |
| 41 | + return nil, nil, nil |
| 42 | + } |
| 43 | + var eps []*extdns.Endpoint |
| 44 | + buildEndpoints(&eps, hosts, targets, gwNN.String()) |
| 45 | + if len(eps) == 0 { |
| 46 | + return nil, nil, nil |
| 47 | + } |
| 48 | + endpointRows, allowed, err := r.DNS.ValidateEndpointsByZone(obj.GetNamespace(), owner, eps) |
| 49 | + if err != nil { |
| 50 | + return nil, allowed, err |
| 51 | + } |
| 52 | + return dns.NewOwnerScopedAggregatedRouteDNS(owner, endpointRows), allowed, nil |
| 53 | +} |
| 54 | + |
| 55 | +type parentGatewayMatch struct { |
| 56 | + nn types.NamespacedName |
| 57 | + filter string |
| 58 | + ips extdns.Targets |
| 59 | +} |
| 60 | + |
| 61 | +func mergeTargetsUnion(a, b extdns.Targets) extdns.Targets { |
| 62 | + return extdns.NewTargets(append(append([]string(nil), a...), b...)...) |
| 63 | +} |
| 64 | + |
| 65 | +// buildRouteDNSMergedEndpoints builds owner-scoped Route DNS rows (Gateway or ListenerSet→root Gateway, same rules as aggregation). |
| 66 | +func (a routeReconcilerAdapter[PT, T]) buildRouteDNSMergedEndpoints(route PT) (*dns.AggregatedDNSEndpoints, map[string]string, error) { |
| 67 | + owner := route.GetResourceRef() |
| 68 | + eps, allowed, err := a.reconciler.buildRouteDNSEndpointsForAggregation(route.GetNamespace(), owner, route.GetParentRefs(), route.GetRouteParentStatus(), route.GetObjectMeta(), route.GetSpecHostnames()) |
| 69 | + if err != nil || len(eps) == 0 { |
| 70 | + return nil, allowed, err |
| 71 | + } |
| 72 | + return dns.NewOwnerScopedAggregatedRouteDNS(owner, eps), allowed, nil |
| 73 | +} |
| 74 | + |
| 75 | +// buildEndpoints appends extdns endpoints per hostname; sets EndpointLabelParentGateway to gatewayLabel (comma-separated if merged). |
| 76 | +func buildEndpoints(out *[]*extdns.Endpoint, hostnames []string, targets extdns.Targets, parentGatewayLabel string) { |
| 77 | + ttl := extdns.TTL(0) |
| 78 | + for _, h := range hostnames { |
| 79 | + if h == "" { |
| 80 | + continue |
| 81 | + } |
| 82 | + for _, ep := range extdns.EndpointsForHostname(h, targets, ttl) { |
| 83 | + if ep == nil { |
| 84 | + continue |
| 85 | + } |
| 86 | + if parentGatewayLabel != "" { |
| 87 | + ep.WithLabel(dns.EndpointLabelParentGateway, parentGatewayLabel) |
| 88 | + } |
| 89 | + *out = append(*out, ep) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// collectGatewayEndpointsByAnnotation builds Gateway owner DNS from hostname annotation + targets. |
| 95 | +func (r *GatewayReconciler) collectGatewayEndpointsByAnnotation(gw *gatewayv1.Gateway, targets extdns.Targets) (*dns.AggregatedDNSEndpoints, map[string]string, error) { |
| 96 | + owner := &dns.ResourceRef{Kind: dns.ResourceKindGateway, Object: gw.GetObjectMeta()} |
| 97 | + gwNN := types.NamespacedName{Namespace: gw.Namespace, Name: gw.Name} |
| 98 | + out, allowed, err := r.buildEndpointsFromAnnotations(gw, targets, owner, gwNN) |
| 99 | + if err != nil { |
| 100 | + log.Error(err, "Failed to build DNS Endpoints for Gateway annotations", "Gateway", gwNN.String()) |
| 101 | + return nil, allowed, err |
| 102 | + } |
| 103 | + return out, allowed, nil |
| 104 | +} |
0 commit comments