From 7a817424aaeb18f94f2cb2576ba04fc40fc68dd6 Mon Sep 17 00:00:00 2001 From: hugoShaka Date: Wed, 27 Nov 2024 18:42:47 -0500 Subject: [PATCH 1/3] migrate to slog. add more details in logs --- tool/teleport/common/wait.go | 43 ++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/tool/teleport/common/wait.go b/tool/teleport/common/wait.go index f81cd0f6ee929..485c1bbb70884 100644 --- a/tool/teleport/common/wait.go +++ b/tool/teleport/common/wait.go @@ -21,6 +21,7 @@ package common import ( "context" "errors" + "fmt" "log/slog" "net" "os" @@ -29,7 +30,6 @@ import ( "time" "github.com/gravitational/trace" - log "github.com/sirupsen/logrus" "github.com/gravitational/teleport/api/utils/retryutils" "github.com/gravitational/teleport/lib/utils" @@ -92,6 +92,8 @@ func waitNoResolve(ctx context.Context, domain string, period, timeout time.Dura if timeout == 0 { return trace.BadParameter("no timeout provided") } + log := slog.With("domain", domain) + log.InfoContext(ctx, "waiting until the domain stops resolving to ensure that every auth server running the previous major version has been updated/terminated") var err error ctx, cancel := context.WithTimeout(ctx, timeout) @@ -124,44 +126,57 @@ func waitNoResolve(ctx context.Context, domain string, period, timeout time.Dura return trace.Wrap(err) case <-periodic.Next(): - exit, err = checkDomainNoResolve(domain) + exit, err = checkDomainNoResolve(ctx, domain, log) if err != nil { return trace.Wrap(err) } } } - log.Info("no endpoints found, exiting with success code") + log.InfoContext(ctx, "no endpoints found, exiting with success code") return nil } -func checkDomainNoResolve(domainName string) (exit bool, err error) { - endpoints, err := countEndpoints(domainName) +func checkDomainNoResolve(ctx context.Context, domainName string, log *slog.Logger) (exit bool, err error) { + endpoints, err := resolveEndpoints(domainName) if err != nil { var dnsErr *net.DNSError if !errors.As(trace.Unwrap(err), &dnsErr) { - log.Errorf("unexpected error when resolving domain %s : %s", domainName, err) + log.ErrorContext(ctx, "unexpected error when resolving domain", "error", err) return false, trace.Wrap(err) } + dnsErrorDetails := slog.Group("dns_error", + "name", dnsErr.Name, + "server", dnsErr.Server, + "is_timeout", dnsErr.IsTimeout, + "is_temporary", dnsErr.IsTemporary, + "is_not_found", dnsErr.IsNotFound, + // Logging the error type can help understanding where the error comes from + "wrapped_error_type", fmt.Sprintf("%T", dnsErr.Unwrap()), + ) if dnsErr.Temporary() { - log.Warnf("temporary error when resolving domain %s : %s", domainName, err) + log.WarnContext(ctx, "temporary error when resolving domain", "error", err, dnsErrorDetails) return false, nil } if dnsErr.IsNotFound { - log.Infof("domain %s not found", domainName) + log.InfoContext(ctx, "domain not found") return true, nil } - log.Errorf("error when resolving domain %s : %s", domainName, err) + log.ErrorContext(ctx, "error when resolving domain", "error", err, dnsErrorDetails) return false, nil } - log.Infof("%d endpoints found when resolving domain %s", endpoints, domainName) - return endpoints == 0, nil + if len(endpoints) == 0 { + log.InfoContext(ctx, "domain found and resolution returned no endpoints") + return true, nil + } + log.InfoContext(ctx, "endpoints found when resolving domain", "endpoints", endpoints) + return false, nil } -func countEndpoints(serviceName string) (int, error) { +func resolveEndpoints(serviceName string) ([]net.IP, error) { ips, err := net.LookupIP(serviceName) if err != nil { - return 0, trace.Wrap(err) + return nil, trace.Wrap(err) } - return len(ips), nil + return ips, nil } From 36b8cff708fcd7bc3d49a4e914c213112ae57829 Mon Sep 17 00:00:00 2001 From: hugoShaka Date: Thu, 28 Nov 2024 18:19:44 -0500 Subject: [PATCH 2/3] make linter happy --- tool/teleport/common/wait.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tool/teleport/common/wait.go b/tool/teleport/common/wait.go index 485c1bbb70884..a9895f263bbf9 100644 --- a/tool/teleport/common/wait.go +++ b/tool/teleport/common/wait.go @@ -145,7 +145,14 @@ func checkDomainNoResolve(ctx context.Context, domainName string, log *slog.Logg log.ErrorContext(ctx, "unexpected error when resolving domain", "error", err) return false, trace.Wrap(err) } - dnsErrorDetails := slog.Group("dns_error", + + if dnsErr.IsNotFound { + log.InfoContext(ctx, "domain not found") + return true, nil + } + + // Creating a new logger because the linter doesn't want both key/value and slog.Attr in the same log write. + log := log.With(slog.Group("dns_error", "name", dnsErr.Name, "server", dnsErr.Server, "is_timeout", dnsErr.IsTimeout, @@ -153,16 +160,12 @@ func checkDomainNoResolve(ctx context.Context, domainName string, log *slog.Logg "is_not_found", dnsErr.IsNotFound, // Logging the error type can help understanding where the error comes from "wrapped_error_type", fmt.Sprintf("%T", dnsErr.Unwrap()), - ) + )) if dnsErr.Temporary() { - log.WarnContext(ctx, "temporary error when resolving domain", "error", err, dnsErrorDetails) + log.WarnContext(ctx, "temporary error when resolving domain", "error", err) return false, nil } - if dnsErr.IsNotFound { - log.InfoContext(ctx, "domain not found") - return true, nil - } - log.ErrorContext(ctx, "error when resolving domain", "error", err, dnsErrorDetails) + log.ErrorContext(ctx, "error when resolving domain", "error", err) return false, nil } if len(endpoints) == 0 { From 10a581d9eb9a6c2df52ca2e66985407040c68f59 Mon Sep 17 00:00:00 2001 From: hugoShaka Date: Fri, 29 Nov 2024 10:12:28 -0500 Subject: [PATCH 3/3] remove unsupported error unwrap --- tool/teleport/common/wait.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/tool/teleport/common/wait.go b/tool/teleport/common/wait.go index a9895f263bbf9..5b73f8a209b16 100644 --- a/tool/teleport/common/wait.go +++ b/tool/teleport/common/wait.go @@ -21,7 +21,6 @@ package common import ( "context" "errors" - "fmt" "log/slog" "net" "os" @@ -158,8 +157,6 @@ func checkDomainNoResolve(ctx context.Context, domainName string, log *slog.Logg "is_timeout", dnsErr.IsTimeout, "is_temporary", dnsErr.IsTemporary, "is_not_found", dnsErr.IsNotFound, - // Logging the error type can help understanding where the error comes from - "wrapped_error_type", fmt.Sprintf("%T", dnsErr.Unwrap()), )) if dnsErr.Temporary() { log.WarnContext(ctx, "temporary error when resolving domain", "error", err)