Skip to content

Commit

Permalink
Allow ipv6 address for sslip.io in default-domain Job (#15328)
Browse files Browse the repository at this point in the history
* Allow ipv6 address for sslip.io

Signed-off-by: Nghia Tran <tcnghia@gmail.com>

* Update cmd/default-domain/domain_test.go

* Update cmd/default-domain/domain_test.go

---------

Signed-off-by: Nghia Tran <tcnghia@gmail.com>
  • Loading branch information
tcnghia committed Jun 18, 2024
1 parent f464e2d commit e56319d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
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

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

0 comments on commit e56319d

Please sign in to comment.