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

Allow ipv6 address for sslip.io in default-domain Job #15328

Merged
merged 3 commits into from
Jun 18, 2024
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
47 changes: 47 additions & 0 deletions cmd/default-domain/domain_test.go
Original file line number Diff line number Diff line change
@@ -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
tcnghia marked this conversation as resolved.
Show resolved Hide resolved

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)
}
})
}
}
15 changes: 12 additions & 3 deletions cmd/default-domain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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))
Expand Down
Loading