diff --git a/cmd/default-domain/domain_test.go b/cmd/default-domain/domain_test.go new file mode 100644 index 000000000000..8663e3edd73e --- /dev/null +++ b/cmd/default-domain/domain_test.go @@ -0,0 +1,47 @@ +/* +Copyright 2024 The Knative Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import "testing" + +func TestBuildMagicDNSName(t *testing.T) { + for _, tt := range []struct { + name string + magicDNS string + ip string + want string + }{ + { + name: "ipv4", + magicDNS: "sslip.io", + ip: "1.1.1.1", + want: "1.1.1.1.sslip.io", + }, + { + name: "ipv6", + magicDNS: "sslip.io", + ip: "2a01:4f8:c17:b8f::2", + want: "2a01-4f8-c17-b8f--2.sslip.io", + }, + } { + t.Run(tt.name, func(t *testing.T) { + if got := buildMagicDNSName(tt.ip, tt.magicDNS); got != tt.want { + t.Errorf("buildMagicDNSName() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/cmd/default-domain/main.go b/cmd/default-domain/main.go index 00f6a86f7971..ec2f6f6cdd15 100644 --- a/cmd/default-domain/main.go +++ b/cmd/default-domain/main.go @@ -164,6 +164,13 @@ func findGatewayAddress(ctx context.Context, kubeclient kubernetes.Interface, cl return &svc.Status.LoadBalancer.Ingress[0], nil } +func buildMagicDNSName(ip, magicDNS string) string { + if magicDNS == "sslip.io" { + ip = strings.ReplaceAll(ip, ":", "-") + } + return fmt.Sprintf("%s.%s", ip, magicDNS) +} + func main() { flag.Parse() ctx := signals.NewContext() @@ -209,19 +216,21 @@ func main() { logger.Info("Gateway has neither IP nor hostname -- leaving default domain config intact") return } - ipAddr, err := net.ResolveIPAddr("ip4", address.Hostname) + ipAddr, err := net.ResolveIPAddr("ip", address.Hostname) if err != nil { logger.Fatalw("Error resolving the IP address of %q", address.Hostname, zap.Error(err)) } ip = ipAddr.String() } - // Use the IP (assumes IPv4) to set up a magic DNS name under a top-level Magic + // Use the IP to set up a magic DNS name under a top-level Magic // DNS service like sslip.io or nip.io, where: // 1.2.3.4.sslip.io ===(magically resolves to)===> 1.2.3.4 + // 2a01-4f8-c17-b8f--2.sslip.io ===(magically resolves to)===> 2a01:4f8:c17:b8f::2 + // // Add this magic DNS name without a label selector to the ConfigMap, // and send it back to the API server. - domain := fmt.Sprintf("%s.%s", ip, *magicDNS) + domain := buildMagicDNSName(ip, *magicDNS) domainCM.Data[domain] = "" if _, err = kubeClient.CoreV1().ConfigMaps(system.Namespace()).Update(ctx, domainCM, metav1.UpdateOptions{}); err != nil { logger.Fatalw("Error updating ConfigMap", zap.Error(err))