Skip to content

Commit

Permalink
dns: Move CheckCRCLocalDNSReachableFromHost from network to dns package
Browse files Browse the repository at this point in the history
The 'dns' package already contains the other CheckCRC*Reachable*
functions, so it makes sense to also have CheckCRCLocalDNSReachableFromHost
This remove the cyclic dependency introduced in the previous commit.
  • Loading branch information
cfergeau authored and openshift-merge-robot committed Nov 28, 2022
1 parent 123f612 commit ea74e2c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion pkg/crc/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)

// Check DNS lookup from host to VM
logging.Info("Check DNS query from host...")
if err := network.CheckCRCLocalDNSReachableFromHost(vm.bundle.GetAPIHostname(),
if err := dns.CheckCRCLocalDNSReachableFromHost(vm.bundle.GetAPIHostname(),
vm.bundle.GetAppHostname("foo"), vm.bundle.ClusterInfo.AppsDomain, instanceIP); err != nil {
if !client.useVSock() {
return nil, errors.Wrap(err, "Failed to query DNS from host")
Expand Down
48 changes: 0 additions & 48 deletions pkg/crc/network/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package network

import (
"fmt"
"net"
"net/url"
"runtime"

"github.com/crc-org/crc/pkg/crc/logging"
)

func URIStringForDisplay(uri string) (string, error) {
Expand All @@ -19,47 +15,3 @@ func URIStringForDisplay(uri string) (string, error) {
}
return uri, nil
}

func matchIP(ips []net.IP, expectedIP string) bool {
for _, ip := range ips {
if ip.String() == expectedIP {
return true
}
}

return false
}
func CheckCRCLocalDNSReachableFromHost(apiHostname, appsHostname, appsDomain, expectedIP string) error {
ip, err := net.LookupIP(apiHostname)
if err != nil {
return err
}
logging.Debugf("%s resolved to %s", apiHostname, ip)
if !matchIP(ip, expectedIP) {
logging.Warnf("%s resolved to %s but %s was expected", apiHostname, ip, expectedIP)
return fmt.Errorf("Invalid IP for %s", apiHostname)
}

if runtime.GOOS != "darwin" {
/* This check will fail with !CGO_ENABLED builds on darwin as
* in this case, /etc/resolver/ will not be used, so we won't
* have wildcard DNS for our domains
*/
ip, err = net.LookupIP(appsHostname)
if err != nil {
// Right now admin helper fallback is not implemented on windows so
// this check should still return an error.
if runtime.GOOS == "windows" {
return err
}
logging.Warnf("Wildcard DNS resolution for %s does not appear to be working", appsDomain)
return nil
}
logging.Debugf("%s resolved to %s", appsHostname, ip)
if !matchIP(ip, expectedIP) {
logging.Warnf("%s resolved to %s but %s was expected", appsHostname, ip, expectedIP)
return fmt.Errorf("Invalid IP for %s", appsHostname)
}
}
return nil
}
48 changes: 48 additions & 0 deletions pkg/crc/services/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package dns
import (
"context"
"fmt"
"net"
"runtime"
"time"

"github.com/crc-org/crc/pkg/crc/adminhelper"
"github.com/crc-org/crc/pkg/crc/constants"
"github.com/crc-org/crc/pkg/crc/errors"
"github.com/crc-org/crc/pkg/crc/logging"
"github.com/crc-org/crc/pkg/crc/network"
"github.com/crc-org/crc/pkg/crc/services"
"github.com/crc-org/crc/pkg/crc/systemd"
Expand Down Expand Up @@ -142,6 +145,51 @@ func CheckCRCPublicDNSReachable(serviceConfig services.ServicePostStartConfig) (
return stdout, err
}

func CheckCRCLocalDNSReachableFromHost(apiHostname, appsHostname, appsDomain, expectedIP string) error {
ip, err := net.LookupIP(apiHostname)
if err != nil {
return err
}
logging.Debugf("%s resolved to %s", apiHostname, ip)
if !matchIP(ip, expectedIP) {
logging.Warnf("%s resolved to %s but %s was expected", apiHostname, ip, expectedIP)
return fmt.Errorf("Invalid IP for %s", apiHostname)
}

if runtime.GOOS != "darwin" {
/* This check will fail with !CGO_ENABLED builds on darwin as
* in this case, /etc/resolver/ will not be used, so we won't
* have wildcard DNS for our domains
*/
ip, err = net.LookupIP(appsHostname)
if err != nil {
// Right now admin helper fallback is not implemented on windows so
// this check should still return an error.
if runtime.GOOS == "windows" {
return err
}
logging.Warnf("Wildcard DNS resolution for %s does not appear to be working", appsDomain)
return nil
}
logging.Debugf("%s resolved to %s", appsHostname, ip)
if !matchIP(ip, expectedIP) {
logging.Warnf("%s resolved to %s but %s was expected", appsHostname, ip, expectedIP)
return fmt.Errorf("Invalid IP for %s", appsHostname)
}
}
return nil
}

func matchIP(ips []net.IP, expectedIP string) bool {
for _, ip := range ips {
if ip.String() == expectedIP {
return true
}
}

return false
}

func addOpenShiftHosts(serviceConfig services.ServicePostStartConfig) error {
return adminhelper.UpdateHostsFile(serviceConfig.IP, serviceConfig.BundleMetadata.GetAPIHostname(),
serviceConfig.BundleMetadata.GetAppHostname("oauth-openshift"),
Expand Down

0 comments on commit ea74e2c

Please sign in to comment.