Skip to content

Commit e3b16c5

Browse files
authored
Merge pull request #121 from datum-cloud/feat/human-hostnames
feat: make hostnames more human-friendly
2 parents e2abe52 + 3934f2e commit e3b16c5

File tree

5 files changed

+437
-4
lines changed

5 files changed

+437
-4
lines changed

internal/config/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"os"
88
"path/filepath"
99
"slices"
10-
"strings"
1110
"time"
1211

12+
words "go.datum.net/network-services-operator/internal/words"
13+
1314
corev1 "k8s.io/api/core/v1"
1415
"k8s.io/apimachinery/pkg/api/resource"
1516
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -632,7 +633,9 @@ func (c *GatewayConfig) ShouldDeleteErroredChallenges() bool {
632633
}
633634

634635
func (c *GatewayConfig) GatewayDNSAddress(gateway *gatewayv1.Gateway) string {
635-
return fmt.Sprintf("%s.%s", strings.ReplaceAll(string(gateway.UID), "-", ""), c.TargetDomain)
636+
seed := string(gateway.UID)
637+
suffix := fmt.Sprintf(".%s", c.TargetDomain)
638+
return words.WordsAndEntropy(suffix, seed)
636639
}
637640

638641
func (c *GatewayConfig) ConnectorTunnelListenerName() string {

internal/controller/gateway_controller.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,8 @@ func (r *GatewayReconciler) ensureHostnamesClaimed(
669669
// but is considered sufficient for now.
670670

671671
for _, hostname := range verifiedHostnames {
672-
// No accounting for the gateway DNS address hostname
673-
if hostname == r.Config.Gateway.GatewayDNSAddress(upstreamGateway) {
672+
// No accounting for platform-managed gateway hostnames.
673+
if r.isDatumManagedGatewayHostname(upstreamGateway, hostname) {
674674
claimedHostnames = append(claimedHostnames, hostname)
675675
continue
676676
}
@@ -750,6 +750,28 @@ func (r *GatewayReconciler) ensureHostnamesClaimed(
750750
return verifiedHostnames, claimedHostnames, notClaimedHostnames, nil
751751
}
752752

753+
func (r *GatewayReconciler) isDatumManagedGatewayHostname(upstreamGateway *gatewayv1.Gateway, hostname string) bool {
754+
targetDomain := r.Config.Gateway.TargetDomain
755+
gatewayUID := string(upstreamGateway.UID)
756+
legacyUIDWithoutDashes := strings.ReplaceAll(gatewayUID, "-", "")
757+
758+
managedBaseHostnames := []string{
759+
r.Config.Gateway.GatewayDNSAddress(upstreamGateway),
760+
fmt.Sprintf("%s.%s", legacyUIDWithoutDashes, targetDomain),
761+
fmt.Sprintf("%s.%s", gatewayUID, targetDomain),
762+
}
763+
764+
for _, managedHostname := range managedBaseHostnames {
765+
if hostname == managedHostname ||
766+
hostname == fmt.Sprintf("v4.%s", managedHostname) ||
767+
hostname == fmt.Sprintf("v6.%s", managedHostname) {
768+
return true
769+
}
770+
}
771+
772+
return false
773+
}
774+
753775
// isHostnameVerified returns hostnames found on listeners that are verified. A
754776
// hostname is considered verified if any verified Domain is found in the same
755777
// namespace with a `spec.domainName` value that matches the hostname exactly,

internal/controller/gateway_controller_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"slices"
7+
"strings"
78
"testing"
89

910
cmv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
@@ -14,6 +15,7 @@ import (
1415
apimeta "k8s.io/apimachinery/pkg/api/meta"
1516
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1617
"k8s.io/apimachinery/pkg/runtime"
18+
"k8s.io/apimachinery/pkg/types"
1719
"k8s.io/apimachinery/pkg/util/intstr"
1820
"k8s.io/apimachinery/pkg/util/uuid"
1921
"k8s.io/client-go/kubernetes/scheme"
@@ -1017,6 +1019,44 @@ func TestEnsureHostnamesClaimed(t *testing.T) {
10171019
assert.NoError(t, cl.Get(ctx, domainObjectKey, &networkingv1alpha.Domain{}), "expected to find a domain, but encountered an errro")
10181020
},
10191021
},
1022+
{
1023+
name: "legacy and current datum-managed hostnames bypass claiming",
1024+
upstreamGateway: func() *gatewayv1.Gateway {
1025+
legacyUID := types.UID("11111111-1111-1111-1111-111111111111")
1026+
legacyHostname := fmt.Sprintf("%s.%s", strings.ReplaceAll(string(legacyUID), "-", ""), testConfig.Gateway.TargetDomain)
1027+
gateway := newGateway(testConfig, upstreamNamespace.Name, "test", func(g *gatewayv1.Gateway) {
1028+
g.UID = legacyUID
1029+
g.Spec.Listeners = []gatewayv1.Listener{
1030+
{
1031+
Name: gatewayutil.DefaultHTTPListenerName,
1032+
Port: DefaultHTTPPort,
1033+
Protocol: gatewayv1.HTTPProtocolType,
1034+
Hostname: ptr.To(gatewayv1.Hostname(legacyHostname)),
1035+
},
1036+
}
1037+
})
1038+
gateway.Status.Addresses = []gatewayv1.GatewayStatusAddress{
1039+
{
1040+
Type: ptr.To(gatewayv1.HostnameAddressType),
1041+
Value: legacyHostname,
1042+
},
1043+
}
1044+
return gateway
1045+
}(),
1046+
existingDownstreamObjects: []client.Object{
1047+
&corev1.ConfigMap{
1048+
ObjectMeta: metav1.ObjectMeta{
1049+
Namespace: testConfig.Gateway.DownstreamHostnameAccountingNamespace,
1050+
Name: "11111111111111111111111111111111.test-suite.com",
1051+
},
1052+
Data: map[string]string{
1053+
"owner": "some/other/gateway",
1054+
},
1055+
},
1056+
},
1057+
expectedVerifiedHostnames: []string{"11111111111111111111111111111111.test-suite.com"},
1058+
expectedClaimedHostnames: []string{"11111111111111111111111111111111.test-suite.com"},
1059+
},
10201060
{
10211061
name: "hostname matches address",
10221062
upstreamGateway: newGateway(testConfig, upstreamNamespace.Name, "test", func(g *gatewayv1.Gateway) {

0 commit comments

Comments
 (0)