Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ dns-api adds custom resources to your cluster:

The controller watches those resources and reconciles the real DNS provider state.

### Delegating DNS for Gateway listeners

A Gateway owner can set
`gateway.endpoint.dns.appthrust.io/excluded-listeners: "https-managed,http-managed"`
on the Gateway. These comma-separated listener section names are excluded from
Gateway EndpointRecordSet publication, including wildcard redirect routes, exact
HTTPRoute hosts, and previously admitted hostname retention. Other listeners are
unchanged. Apply the annotation before adding listeners whose DNS has another
owner, and deploy a dns-api version supporting this contract first; older versions
do not honour the annotation. The other writer owns its RecordSets and deletion.


## Install

The controller is distributed as a container image and a Helm chart on GitHub Container Registry. Choose the released version you want to run:
Expand Down
20 changes: 20 additions & 0 deletions internal/go/apps/gatewayendpoint/controllers/gateway/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
managedByLabel = "app.kubernetes.io/managed-by"
managedByValue = "dns-api-gateway-endpoint"
httpRouteGatewayIndex = "gateway.endpoint.dns.appthrust.io/parent-gateway"
excludedListenersAnnotation = "gateway.endpoint.dns.appthrust.io/excluded-listeners"
)

type Reconciler struct {
Expand Down Expand Up @@ -260,13 +261,17 @@ func (r *Reconciler) endpointRecordSetsForGateway(ctx context.Context, gateway *
hostnames := map[string]struct{}{}
retainedHostnames := map[string]struct{}{}
receiptBindings := map[string]admissionReceiptBinding{}
excluded := excludedListeners(gateway)
for i := range routes.Items {
route := &routes.Items[i]
if !route.DeletionTimestamp.IsZero() {
continue
}
fullyAccepted := allRouteParentRefsAccepted(route)
for _, binding := range routeGatewayBindings(route, gateway) {
if _, skip := excluded[binding.listener.Name]; skip {
continue
}
acceptance := routeParentAcceptance(route, binding.ref)
for _, hostname := range effectiveHostnamesForListener(route, binding.listener) {
match, hasReceipt := admissionReceiptBinding{}, false
Expand Down Expand Up @@ -344,6 +349,21 @@ func (r *Reconciler) endpointRecordSetsForGateway(ctx context.Context, gateway *
}}, nil
}

// The Gateway owner may delegate DNS for selected listener sections to a
// different writer. Apply this before both new admission and receipt retention.
func excludedListeners(gateway *gatewayv1.Gateway) map[gatewayv1.SectionName]struct{} {
var excluded map[gatewayv1.SectionName]struct{}
for name := range strings.SplitSeq(gateway.Annotations[excludedListenersAnnotation], ",") {
if name = strings.TrimSpace(name); name != "" {
if excluded == nil {
excluded = make(map[gatewayv1.SectionName]struct{})
}
excluded[gatewayv1.SectionName(name)] = struct{}{}
}
}
return excluded
}

func (r *Reconciler) applyGatewayEndpointRecordSets(ctx context.Context, namespace string, gatewayKey client.ObjectKey, desired []endpointv1alpha1.EndpointRecordSet) error {
desiredNames := map[string]struct{}{}
for _, item := range desired {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package gateway

import (
"reflect"
"testing"

endpoint "github.com/appthrust/dns-api/pkg/go/api/endpoint/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)

func TestManagedListenersDoNotPublishDNS(t *testing.T) {
ctx := t.Context()
wildcard := gatewayv1.Hostname("*.appthrust.app")
public := &gatewayv1.Gateway{ObjectMeta: metav1.ObjectMeta{Name: "public", Namespace: "platform", UID: "gateway", Generation: 1}, Spec: gatewayv1.GatewaySpec{
GatewayClassName: "envoy", Listeners: []gatewayv1.Listener{{Name: "https-platform"}, {Name: "https-managed", Hostname: &wildcard}, {Name: "http-managed", Hostname: &wildcard}},
}, Status: gatewayv1.GatewayStatus{Addresses: []gatewayv1.GatewayStatusAddress{{Value: "pmc.elb.example.test"}}}}
routes := []*gatewayv1.HTTPRoute{
acceptedHTTPRoute("platform", "console", "platform", "https-platform", "console.appthrust.com"),
acceptedHTTPRoute("platform", "redirect", "platform", "http-managed", "*.appthrust.app"),
acceptedHTTPRoute("dashboards", "acme", "platform", "https-managed", "acme.appthrust.app"),
}
objects := []client.Object{public}
for _, route := range routes {
route.UID, route.Generation = types.UID(route.Name), 1
route.Status.Parents[0].Conditions[0].ObservedGeneration = route.Generation
objects = append(objects, route)
}
r := &Reconciler{Client: fake.NewClientBuilder().WithScheme(testScheme(t)).WithObjects(objects...).Build()}
before, err := r.endpointRecordSetsForGateway(ctx, public, "dns", nil)
if err != nil || len(before) != 1 || len(before[0].Spec.Hostnames) != 3 {
t.Fatalf("baseline publication: %#v %v", before, err)
}
public.Annotations = map[string]string{excludedListenersAnnotation: " https-managed, http-managed,https-managed "}
for _, existing := range []*endpoint.EndpointRecordSet{nil, &before[0]} {
records, err := r.endpointRecordSetsForGateway(ctx, public, "dns", existing)
if err != nil || len(records) != 1 || !reflect.DeepEqual(records[0].Spec.Hostnames, []string{"console.appthrust.com"}) {
t.Fatalf("managed hosts must never be emitted or retained: %#v %v", records, err)
}
}
// Even during an address outage, prior admission cannot retain an excluded host.
public.Status.Addresses = nil
records, err := r.endpointRecordSetsForGateway(ctx, public, "dns", &before[0])
if err != nil || len(records) != 1 || !reflect.DeepEqual(records[0].Spec.Hostnames, []string{"console.appthrust.com"}) {
t.Fatalf("excluded receipt retained during outage: %#v %v", records, err)
}
public.Annotations[excludedListenersAnnotation] = "https-platform,https-managed,http-managed"
records, err = r.endpointRecordSetsForGateway(ctx, public, "dns", &before[0])
if err != nil || len(records) != 0 {
t.Fatalf("all excluded listeners must withdraw gateway publication: %#v %v", records, err)
}
}
Loading