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

Refactor #2, Dig #257

Merged
merged 1 commit into from Jan 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 6 additions & 28 deletions controllers/dnsupdate.go
Expand Up @@ -9,13 +9,14 @@ import (
"strings"
"time"

"github.com/AbsaOSS/k8gb/controllers/internal/utils"

"github.com/AbsaOSS/k8gb/controllers/depresolver"

coreerrors "errors"

k8gbv1beta1 "github.com/AbsaOSS/k8gb/api/v1beta1"
ibclient "github.com/infobloxopen/infoblox-go-client"
"github.com/lixiangzhong/dnsutil"
"github.com/miekg/dns"
corev1 "k8s.io/api/core/v1"
v1beta1 "k8s.io/api/extensions/v1beta1"
Expand Down Expand Up @@ -52,8 +53,9 @@ func (r *GslbReconciler) getGslbIngressIPs(gslb *k8gbv1beta1.Gslb) ([]string, er
gslbIngressIPs = append(gslbIngressIPs, ip.IP)
}
if len(ip.Hostname) > 0 {
IPs, err := Dig(r.Config.EdgeDNSServer, ip.Hostname)
IPs, err := utils.Dig(r.Config.EdgeDNSServer, ip.Hostname)
if err != nil {
log.Info("Dig error: %s", err)
return nil, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as log.Info() was removed from Dig - we need to explicitly log it here, otherwise the failure of resolution will be hidden from the end user

Copy link
Collaborator Author

@kuritka kuritka Jan 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ytsarev thx for finding 👍 . Will log in caller function(s) as controllers package holds dependency to log (viz 1, 2).

}
gslbIngressIPs = append(gslbIngressIPs, IPs...)
Expand Down Expand Up @@ -346,30 +348,6 @@ func filterOutDelegateTo(delegateTo []ibclient.NameServer, fqdn string) []ibclie
return delegateTo
}

// Dig digs
func Dig(edgeDNSServer, fqdn string) ([]string, error) {
var dig dnsutil.Dig
if edgeDNSServer == "" {
return nil, fmt.Errorf("empty edgeDNSServer")
}
err := dig.SetDNS(edgeDNSServer)
if err != nil {
log.Info(fmt.Sprintf("Can't set query dns (%s) with error(%s)", edgeDNSServer, err))
return nil, err
}
a, err := dig.A(fqdn)
if err != nil {
log.Info(fmt.Sprintf("Can't dig fqdn(%s) with error(%s)", fqdn, err))
return nil, err
}
var IPs []string
for _, ip := range a {
IPs = append(IPs, fmt.Sprint(ip.A))
}
sort.Strings(IPs)
return IPs, nil
}

func (r *GslbReconciler) coreDNSExposedIPs() ([]string, error) {
coreDNSService := &corev1.Service{}

Expand All @@ -389,9 +367,9 @@ func (r *GslbReconciler) coreDNSExposedIPs() ([]string, error) {
err := coreerrors.New(errMessage)
return nil, err
}
IPs, err := Dig(r.Config.EdgeDNSServer, lbHostname)
IPs, err := utils.Dig(r.Config.EdgeDNSServer, lbHostname)
if err != nil {
log.Info(fmt.Sprintf("Can't dig k8gb-coredns-lb service loadbalancer fqdn %s", lbHostname))
log.Info(fmt.Sprintf("Can't dig k8gb-coredns-lb service loadbalancer fqdn %s (%s)", lbHostname, err))
return nil, err
}
return IPs, nil
Expand Down
32 changes: 32 additions & 0 deletions controllers/internal/utils/dns.go
@@ -0,0 +1,32 @@
package utils

import (
"fmt"
"sort"

"github.com/lixiangzhong/dnsutil"
)

// Dig retrieves list of tuple <IP address, A record > from edge DNS server for specific FQDN
func Dig(edgeDNSServer, fqdn string) ([]string, error) {
var dig dnsutil.Dig
if edgeDNSServer == "" {
return nil, fmt.Errorf("empty edgeDNSServer")
}
err := dig.SetDNS(edgeDNSServer)
if err != nil {
err = fmt.Errorf("dig error: can't set query dns (%s) with error(%s)", edgeDNSServer, err)
return nil, err
}
a, err := dig.A(fqdn)
if err != nil {
err = fmt.Errorf("dig error: can't dig fqdn(%s) with error(%s)", fqdn, err)
return nil, err
}
var IPs []string
for _, ip := range a {
IPs = append(IPs, fmt.Sprint(ip.A))
}
sort.Strings(IPs)
return IPs, nil
}
67 changes: 67 additions & 0 deletions controllers/internal/utils/dns_test.go
@@ -0,0 +1,67 @@
package utils

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestValidDig(t *testing.T) {
// arrange
if !connected() {
t.Skipf("no connectivity, skipping")
}
edgeDNSServer := "8.8.8.8"
fqdn := "google.com"
// act
result, err := Dig(edgeDNSServer, fqdn)
// assert
assert.NoError(t, err)
assert.NotEmpty(t, result)
assert.NotEmpty(t, result[0])
}

func TestEmptyFQDNButValidEdgeDNS(t *testing.T) {
// arrange
if !connected() {
t.Skipf("no connectivity, skipping")
}
edgeDNSServer := "8.8.8.8"
fqdn := ""
// act
result, err := Dig(edgeDNSServer, fqdn)
// assert
assert.NoError(t, err)
assert.Nil(t, result)
}

func TestEmptyEdgeDNS(t *testing.T) {
// arrange
edgeDNSServer := ""
fqdn := "whatever"
// act
result, err := Dig(edgeDNSServer, fqdn)
// assert
assert.Error(t, err)
assert.Nil(t, result)
}

func TestValidEdgeDNSButNonExistingFQDN(t *testing.T) {
// arrange
edgeDNSServer := "localhost"
fqdn := "some-valid-ip-fqdn-123"
// act
result, err := Dig(edgeDNSServer, fqdn)
// assert
assert.Error(t, err)
assert.Nil(t, result)
}

func connected() (ok bool) {
res, err := http.Get("http://google.com")
if err != nil {
return false
}
return res.Body.Close() == nil
}