Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCPBUGS-15100: Create valid DNS names for Gateway API on GCP #949

Merged
merged 1 commit into from
Jun 26, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/operator/controller/gateway-service-dns/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gateway_service_dns
import (
"context"
"reflect"
"strings"

logf "github.com/openshift/cluster-ingress-operator/pkg/log"
operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller"
Expand Down Expand Up @@ -222,6 +223,10 @@ func (r *reconciler) ensureDNSRecordsForGateway(ctx context.Context, gateway *ga
var errs []error
for _, domain := range domains {
name := operatorcontroller.GatewayDNSRecordName(gateway, domain)
// If domain doesn't have a trailing dot, add it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// If domain doesn't have a trailing dot, add it
// If domain doesn't have a trailing dot, add it.

I would have added the trailing dot in desiredDNSRecord, but maybe this makes more sense.

if !strings.HasSuffix(domain, ".") {
domain = domain + "."
}
Comment on lines +226 to +229
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to account for the trailing dot in deleteStaleDNSRecordsForGateway too, or else it immediately deletes the DNSRecord CR that ensureDNSRecordsForGateway created. Better yet, add the trailing dot in getGatewayHostnames:

diff --git a/pkg/operator/controller/gateway-service-dns/controller.go b/pkg/operator/controller/gateway-service-dns/controller.go
index 1084d65c..919b8d32 100644
--- a/pkg/operator/controller/gateway-service-dns/controller.go
+++ b/pkg/operator/controller/gateway-service-dns/controller.go
@@ -199,7 +199,12 @@ func getGatewayHostnames(gateway *gatewayapiv1beta1.Gateway) sets.String {
 		if listener.Hostname == nil || len(*listener.Hostname) == 0 {
 			continue
 		}
-		domains.Insert(string(*listener.Hostname))
+		domain := string(*listener.Hostname)
+		// If domain doesn't have a trailing dot, add it.
+		if !strings.HasSuffix(domain, ".") {
+			domain = domain + "."
+		}
+		domains.Insert(domain)
 	}
 	return domains
 }
@@ -223,10 +228,6 @@ func (r *reconciler) ensureDNSRecordsForGateway(ctx context.Context, gateway *ga
 	var errs []error
 	for _, domain := range domains {
 		name := operatorcontroller.GatewayDNSRecordName(gateway, domain)
-		// If domain doesn't have a trailing dot, add it
-		if !strings.HasSuffix(domain, ".") {
-			domain = domain + "."
-		}
 		_, _, err := dnsrecord.EnsureDNSRecord(r.client, name, labels, ownerRef, domain, service)
 		errs = append(errs, err)
 	}

This also has the advantage that "foo.com" without a trailing "." and and "foo.com." with a trailing "." will get the same hash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #956

_, _, err := dnsrecord.EnsureDNSRecord(r.client, name, labels, ownerRef, domain, service)
errs = append(errs, err)
}
Expand Down
35 changes: 30 additions & 5 deletions pkg/operator/controller/gateway-service-dns/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ func Test_Reconcile(t *testing.T) {
},
reconcileRequest: req("openshift-ingress", "example-gateway"),
expectCreate: []client.Object{
dnsrecord("example-gateway-5bfc88bc87-wildcard", "*.prod.example.com", "lb.example.com"),
dnsrecord("example-gateway-57b76476b6-wildcard", "*.stage.example.com", "lb.example.com"),
dnsrecord("example-gateway-5bfc88bc87-wildcard", "*.prod.example.com.", "lb.example.com"),
dnsrecord("example-gateway-57b76476b6-wildcard", "*.stage.example.com.", "lb.example.com"),
},
expectUpdate: []client.Object{},
},
{
name: "gateway with two listeners and one dnsrecord with a stale target",
name: "gateway with two listeners and one dnsrecord with a stale target, hostname already has trailing dot",
existingObjects: []runtime.Object{
gw(
"example-gateway",
Expand All @@ -161,14 +161,39 @@ func Test_Reconcile(t *testing.T) {
},
ingHost("newlb.example.com"),
),
dnsrecord("example-gateway-55bcfdb97d-wildcard", "*.example.com", "oldlb.example.com"),
dnsrecord("example-gateway-55bcfdb97d-wildcard", "*.example.com.", "oldlb.example.com"),
},
reconcileRequest: req("openshift-ingress", "example-gateway"),
expectCreate: []client.Object{},
expectUpdate: []client.Object{
dnsrecord("example-gateway-55bcfdb97d-wildcard", "*.example.com", "newlb.example.com"),
dnsrecord("example-gateway-55bcfdb97d-wildcard", "*.example.com.", "newlb.example.com"),
},
},
{
name: "gateway with two listeners and one host name, no dnsrecords, name ends up with trailing dot",
existingObjects: []runtime.Object{
gw(
"example-gateway",
l("stage-http", "*.stage.example.com", 80),
l("stage-https", "*.stage.example.com", 443),
),
svc(
"example-gateway",
map[string]string{
"gateway.istio.io/managed": "example-gateway",
},
map[string]string{
"istio.io/gateway-name": "example-gateway",
},
ingHost("lb.example.com"),
),
},
reconcileRequest: req("openshift-ingress", "example-gateway"),
expectCreate: []client.Object{
dnsrecord("example-gateway-57b76476b6-wildcard", "*.stage.example.com.", "lb.example.com"),
},
expectUpdate: []client.Object{},
},
}

scheme := runtime.NewScheme()
Expand Down