Skip to content

Commit

Permalink
fix: fix so that get version handles necessary errors
Browse files Browse the repository at this point in the history
Signed-off-by: Bob Melander <bob.melander@anchore.com>
  • Loading branch information
bobmel committed Oct 29, 2024
1 parent a14d8e6 commit 630671c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
22 changes: 19 additions & 3 deletions internal/anchore/anchoreclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/anchore/k8s-inventory/internal/tracker"
"github.com/h2non/gock"
"io"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -225,11 +226,26 @@ func ServerIsOffline(err error) bool {
return true
}

if errors.Is(err, syscall.ECONNREFUSED) {
return true
offlineErrors := []error{
syscall.ENETDOWN,
syscall.ENETUNREACH,
syscall.ENETRESET,
syscall.ECONNABORTED,
syscall.ECONNRESET,
syscall.ETIMEDOUT,
syscall.ECONNREFUSED,
syscall.EHOSTDOWN,
syscall.EHOSTUNREACH,
}

for _, e := range offlineErrors {
if errors.Is(err, e) {
return true
}
}

if errors.Is(err, syscall.ECONNRESET) {
var dnsError *net.DNSError
if errors.As(err, &dnsError) {
return true
}

Expand Down
67 changes: 67 additions & 0 deletions internal/anchore/anchoreclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,44 @@ var (
Err: &httpError{err: "net/http: timeout awaiting response headers", timeout: true},
}

networkDownError = url.Error{
Op: "Post",
URL: "http://127.0.0.1:8228/v2/system/integrations/registration",
Err: &net.OpError{
Op: "dial",
Net: "tcp",
Source: nil,
Addr: &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 8228,
Zone: "",
},
Err: &os.SyscallError{
Syscall: "connect",
Err: syscall.ENETDOWN,
},
},
}

networkUnreachableError = url.Error{
Op: "Post",
URL: "http://127.0.0.1:8228/v2/system/integrations/registration",
Err: &net.OpError{
Op: "dial",
Net: "tcp",
Source: nil,
Addr: &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 8228,
Zone: "",
},
Err: &os.SyscallError{
Syscall: "connect",
Err: syscall.ENETUNREACH,
},
},
}

connectionRefusedError = url.Error{
Op: "Post",
URL: "http://127.0.0.1:8228/v2/system/integrations/registration",
Expand Down Expand Up @@ -125,6 +163,20 @@ var (
},
}

dnsError = url.Error{
Op: "Post",
URL: "http://doesnotexist:8228/v2/system/integrations/registration",
Err: &net.OpError{
Op: "read",
Net: "tcp",
Err: &net.DNSError{
Err: "no such host",
Name: "http://doesnotexist:8228/v2/system/integrations/registration",
IsNotFound: true,
},
},
}

badGatewayError = APIClientError{
HTTPStatusCode: http.StatusBadGateway,
Message: "Bad Gateway",
Expand Down Expand Up @@ -502,6 +554,16 @@ func TestAnchoreIsOffline(t *testing.T) {
err error
want bool
}{
{
name: "Network unreachable returns true",
err: &networkUnreachableError,
want: true,
},
{
name: "Network down returns true",
err: &networkDownError,
want: true,
},
{
name: "Connection timeout returns true",
err: &connectionTimeoutError,
Expand All @@ -517,6 +579,11 @@ func TestAnchoreIsOffline(t *testing.T) {
err: &connectionResetError,
want: true,
},
{
name: "DNSError returns true",
err: &dnsError,
want: true,
},
{
name: "AnchoreAPIClientError with 502 http_status returns true",
err: &badGatewayError,
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ func (cfg *Application) Build() error {

cfg.handleBackwardsCompatibility()

if cfg.HealthReportIntervalSeconds < 30 || cfg.HealthReportIntervalSeconds > 600 {
return fmt.Errorf("health-report-interval-seconds must be between 30 and 600")
}

return nil
}

Expand Down

0 comments on commit 630671c

Please sign in to comment.