Bump SDK + change Evaluate signature
This commit is contained in:
parent
1a66c3a9d8
commit
cc7da2c6f7
3 changed files with 42 additions and 44 deletions
|
|
@ -3,7 +3,6 @@ package checker
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
sdk "git.happydns.org/checker-sdk-go/checker"
|
||||
)
|
||||
|
|
@ -64,66 +63,65 @@ type singleCheckRule struct {
|
|||
func (r *singleCheckRule) Name() string { return r.ruleName }
|
||||
func (r *singleCheckRule) Description() string { return r.description }
|
||||
|
||||
func (r *singleCheckRule) Evaluate(ctx context.Context, obs sdk.ObservationGetter, opts sdk.CheckerOptions) sdk.CheckState {
|
||||
func (r *singleCheckRule) Evaluate(ctx context.Context, obs sdk.ObservationGetter, opts sdk.CheckerOptions) []sdk.CheckState {
|
||||
var report NSRestrictionsReport
|
||||
if err := obs.Get(ctx, ObservationKeyNSRestrictions, &report); err != nil {
|
||||
return sdk.CheckState{
|
||||
return []sdk.CheckState{{
|
||||
Status: sdk.StatusError,
|
||||
Message: fmt.Sprintf("Failed to get NS restrictions data: %v", err),
|
||||
Code: r.code + "_error",
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
status := sdk.StatusOK
|
||||
var summaryParts []string
|
||||
failingServers := make([]map[string]string, 0)
|
||||
checked := false
|
||||
|
||||
out := make([]sdk.CheckState, 0, len(report.Servers))
|
||||
for _, srv := range report.Servers {
|
||||
item, found := findCheck(srv.Checks, r.checkName)
|
||||
if !found {
|
||||
// The collect step did not run this check on this server
|
||||
// (e.g. IPv6 unreachable, DNS resolution failure). Surface
|
||||
// the reason from whichever entry the server does have.
|
||||
if len(srv.Checks) > 0 {
|
||||
summaryParts = append(summaryParts, fmt.Sprintf("%s: skipped (%s)", serverLabel(srv), srv.Checks[0].Detail))
|
||||
} else {
|
||||
summaryParts = append(summaryParts, fmt.Sprintf("%s: skipped", serverLabel(srv)))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
checked = true
|
||||
|
||||
if item.OK {
|
||||
summaryParts = append(summaryParts, fmt.Sprintf("%s: OK", serverLabel(srv)))
|
||||
continue
|
||||
}
|
||||
|
||||
if status < r.failStatus {
|
||||
status = r.failStatus
|
||||
}
|
||||
summaryParts = append(summaryParts, fmt.Sprintf("%s: FAIL (%s)", serverLabel(srv), item.Detail))
|
||||
failingServers = append(failingServers, map[string]string{
|
||||
meta := map[string]any{
|
||||
"check": r.checkName,
|
||||
"name": srv.Name,
|
||||
"address": srv.Address,
|
||||
"detail": item.Detail,
|
||||
})
|
||||
}
|
||||
|
||||
item, found := findCheck(srv.Checks, r.checkName)
|
||||
if !found {
|
||||
message := "check not performed"
|
||||
if len(srv.Checks) > 0 {
|
||||
message = fmt.Sprintf("skipped: %s", srv.Checks[0].Detail)
|
||||
}
|
||||
out = append(out, sdk.CheckState{
|
||||
Status: sdk.StatusUnknown,
|
||||
Message: message,
|
||||
Code: r.code + "_skipped",
|
||||
Subject: serverLabel(srv),
|
||||
Meta: meta,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
state := sdk.CheckState{
|
||||
Code: r.code + "_result",
|
||||
Subject: serverLabel(srv),
|
||||
Meta: meta,
|
||||
Message: item.Detail,
|
||||
}
|
||||
if item.OK {
|
||||
state.Status = sdk.StatusOK
|
||||
if state.Message == "" {
|
||||
state.Message = "OK"
|
||||
}
|
||||
} else {
|
||||
state.Status = r.failStatus
|
||||
}
|
||||
out = append(out, state)
|
||||
}
|
||||
|
||||
if !checked {
|
||||
status = sdk.StatusUnknown
|
||||
}
|
||||
|
||||
return sdk.CheckState{
|
||||
Status: status,
|
||||
Message: strings.Join(summaryParts, " | "),
|
||||
Code: r.code + "_result",
|
||||
Meta: map[string]any{
|
||||
"check": r.checkName,
|
||||
"failing_servers": failingServers,
|
||||
},
|
||||
if len(out) == 0 {
|
||||
return []sdk.CheckState{{
|
||||
Status: sdk.StatusUnknown,
|
||||
Message: "no nameserver to evaluate",
|
||||
Code: r.code + "_result",
|
||||
}}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func findCheck(items []NSCheckItem, name string) (NSCheckItem, bool) {
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -3,7 +3,7 @@ module git.happydns.org/checker-ns-restrictions
|
|||
go 1.25.0
|
||||
|
||||
require (
|
||||
git.happydns.org/checker-sdk-go v0.0.1
|
||||
git.happydns.org/checker-sdk-go v1.2.0
|
||||
github.com/miekg/dns v1.1.72
|
||||
)
|
||||
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -1,5 +1,5 @@
|
|||
git.happydns.org/checker-sdk-go v0.0.1 h1:4RxCJr73HWKxjOyU/6NJMO8lXJmH0gMLA68EzTqLbQI=
|
||||
git.happydns.org/checker-sdk-go v0.0.1/go.mod h1:aNAcfYFfbhvH9kJhE0Njp5GX0dQbxdRB0rJ0KvSC5nI=
|
||||
git.happydns.org/checker-sdk-go v1.2.0 h1:v4MpKAz0W3PwP+bxx3pya8w893sVH5xTD1of1cc0TV8=
|
||||
git.happydns.org/checker-sdk-go v1.2.0/go.mod h1:aNAcfYFfbhvH9kJhE0Njp5GX0dQbxdRB0rJ0KvSC5nI=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/miekg/dns v1.1.72 h1:vhmr+TF2A3tuoGNkLDFK9zi36F2LS+hKTRW0Uf8kbzI=
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue