Skip to content

Commit

Permalink
Test dns before ping (#520)
Browse files Browse the repository at this point in the history
I get negative dns caches a lot here, this will help us ensure dns is there before we start pinging
  • Loading branch information
michaeljguarino authored Jun 22, 2024
1 parent ff4ca56 commit c780c78
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3
github.com/joho/godotenv v1.3.0
github.com/ktrysmt/go-bitbucket v0.9.55
github.com/likexian/doh v0.7.1
github.com/linode/linodego v1.26.0
github.com/microsoftgraph/msgraph-sdk-go v0.61.0
github.com/mikesmitty/edkey v0.0.0-20170222072505-3356ea4e686a
Expand Down Expand Up @@ -204,6 +205,7 @@ require (
github.com/leaanthony/go-ansi-parser v1.0.1 // indirect
github.com/leaanthony/gosod v1.0.3 // indirect
github.com/leaanthony/slicer v1.5.0 // indirect
github.com/likexian/gokit v0.25.15 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,10 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/likexian/doh v0.7.1 h1:0p75orMxXrfGNAOnOEtl2xZ5T9Ctqi++i/WhN6P6hs8=
github.com/likexian/doh v0.7.1/go.mod h1:aIEOK197o6nC82iRo5+5L5ws+X7xRCmuer3M4In91WA=
github.com/likexian/gokit v0.25.15 h1:QjospM1eXhdMMHwZRpMKKAHY/Wig9wgcREmLtf9NslY=
github.com/likexian/gokit v0.25.15/go.mod h1:S2QisdsxLEHWeD/XI0QMVeggp+jbxYqUxMvSBil7MRg=
github.com/linode/linodego v1.26.0 h1:2tOZ3Wxn4YvGBRgZi3Vz6dab+L16XUntJ9sJxh3ZBio=
github.com/linode/linodego v1.26.0/go.mod h1:kD7Bf1piWg/AXb9TA0ThAVwzR+GPf6r2PvbTbVk7PMA=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand Down
7 changes: 6 additions & 1 deletion pkg/up/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func (ctx *Context) Deploy(commit func() error) error {
return err
}

if err := ping(fmt.Sprintf("https://console.%s", ctx.Manifest.Network.Subdomain)); err != nil {
subdomain := ctx.Manifest.Network.Subdomain
if err := testDns(fmt.Sprintf("console.%s", subdomain)); err != nil {
return err
}

if err := ping(fmt.Sprintf("https://console.%s", subdomain)); err != nil {
return err
}

Expand Down
74 changes: 69 additions & 5 deletions pkg/up/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,87 @@ import (
"net/http"
"time"

"github.com/likexian/doh"
"github.com/likexian/doh/dns"

"github.com/pluralsh/plural-cli/pkg/utils"
)

func testDns(domain string) error {
ping := fmt.Sprintf("Querying %s...\n", domain)
success := "DNS fully resolved, testing if console is functional...\n"
return retrier(ping, success, func() error {
return doTestDns(domain)
})
}

func doTestDns(domain string) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

callDns := func(t dns.Type) error {
c := doh.Use(doh.CloudflareProvider, doh.GoogleProvider)
rsp, err := c.Query(ctx, dns.Domain(domain), t)
if err != nil {
return err
}

// close the client
c.Close()

// doh dns answer
answer := rsp.Answer
if len(answer) > 0 {
return nil
}

return fmt.Errorf("dns answer was empty")
}

if err := callDns(dns.TypeA); err == nil {
return nil
}

if err := callDns(dns.TypeCNAME); err == nil {
return nil
}

if err := callDns(dns.TypeAAAA); err == nil {
return nil
}

return fmt.Errorf("could not resolve %s dns domain, you likely need to wait for this to propagate", domain)
}

func ping(url string) error {
ping := fmt.Sprintf("Pinging %s...\n", url)
success := "Found status code 200, console up!\n"
return retrier(ping, success, func() error {
resp, err := http.Get(url)
if err == nil && resp.StatusCode == 200 {
return nil
}

return fmt.Errorf("Console failed to become ready after 5 minutes, you might want to inspect the resources in the plrl-console namespace")
})
}

func retrier(retryMsg, successMsg string, f func() error) error {
done := make(chan bool)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
var resErr error
defer cancel()

go func() {
for {
fmt.Printf("Pinging %s...\n", url)
resp, err := http.Get(url)
if err == nil && resp.StatusCode == 200 {
utils.Success("Found status code 200, console up!\n")
fmt.Printf(retryMsg)

Check failure on line 82 in pkg/up/ping.go

View workflow job for this annotation

GitHub Actions / Lint

SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)
err := f()
if err == nil {
utils.Success(successMsg)
done <- true
return
}
resErr = err
time.Sleep(10 * time.Second)
}
}()
Expand All @@ -31,6 +95,6 @@ func ping(url string) error {
case <-done:
return nil
case <-ctx.Done():
return fmt.Errorf("Console failed to become ready after 5 minutes, you might want to inspect the resources in the plrl-console namespace")
return resErr
}
}

0 comments on commit c780c78

Please sign in to comment.