Migrate to checker-sdk-go v1.3.0 with standalone build tag
The SDK split the HTTP server scaffolding into the new checker-sdk-go/checker/server subpackage. Update main.go to import server and call server.New, and isolate the interactive form code behind the standalone build tag so plugin/builtin builds skip net/http entirely.
This commit is contained in:
parent
a74c8d83aa
commit
b1ebac2198
10 changed files with 23 additions and 24 deletions
|
|
@ -6,7 +6,7 @@ WORKDIR /src
|
|||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 go build -ldflags "-X main.Version=${CHECKER_VERSION}" -o /checker-ping .
|
||||
RUN CGO_ENABLED=0 go build -tags standalone -ldflags "-X main.Version=${CHECKER_VERSION}" -o /checker-ping .
|
||||
|
||||
FROM scratch
|
||||
COPY --from=builder /checker-ping /checker-ping
|
||||
|
|
|
|||
7
Makefile
7
Makefile
|
|
@ -6,12 +6,12 @@ CHECKER_SOURCES := main.go $(wildcard checker/*.go)
|
|||
|
||||
GO_LDFLAGS := -X main.Version=$(CHECKER_VERSION)
|
||||
|
||||
.PHONY: all plugin docker clean
|
||||
.PHONY: all plugin docker test clean
|
||||
|
||||
all: $(CHECKER_NAME)
|
||||
|
||||
$(CHECKER_NAME): $(CHECKER_SOURCES)
|
||||
go build -ldflags "$(GO_LDFLAGS)" -o $@ .
|
||||
go build -tags standalone -ldflags "$(GO_LDFLAGS)" -o $@ .
|
||||
|
||||
plugin: $(CHECKER_NAME).so
|
||||
|
||||
|
|
@ -21,5 +21,8 @@ $(CHECKER_NAME).so: $(CHECKER_SOURCES) $(wildcard plugin/*.go)
|
|||
docker:
|
||||
docker build --build-arg CHECKER_VERSION=$(CHECKER_VERSION) -t $(CHECKER_IMAGE) .
|
||||
|
||||
test:
|
||||
go test -tags standalone ./...
|
||||
|
||||
clean:
|
||||
rm -f $(CHECKER_NAME) $(CHECKER_NAME).so
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ make
|
|||
|
||||
The server exposes:
|
||||
|
||||
- `GET /health` — health check
|
||||
- `POST /collect` — collect ping observations (happyDomain external checker protocol)
|
||||
- `GET /health`, health check
|
||||
- `POST /collect`, collect ping observations (happyDomain external checker protocol)
|
||||
|
||||
### Docker
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ import (
|
|||
var Version = "built-in"
|
||||
|
||||
// Definition returns the CheckerDefinition for the ping checker.
|
||||
func Definition() *happydns.CheckerDefinition {
|
||||
func (p *pingProvider) Definition() *happydns.CheckerDefinition {
|
||||
return &happydns.CheckerDefinition{
|
||||
ID: "ping",
|
||||
Name: "Ping (ICMP)",
|
||||
|
|
@ -89,9 +89,7 @@ func Definition() *happydns.CheckerDefinition {
|
|||
},
|
||||
},
|
||||
},
|
||||
Rules: []happydns.CheckRule{
|
||||
Rule(),
|
||||
},
|
||||
Rules: Rules(),
|
||||
Interval: &happydns.CheckIntervalSpec{
|
||||
Min: 1 * time.Minute,
|
||||
Max: 1 * time.Hour,
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//go:build standalone
|
||||
|
||||
package checker
|
||||
|
||||
import (
|
||||
|
|
@ -30,7 +32,7 @@ import (
|
|||
sdk "git.happydns.org/checker-sdk-go/checker"
|
||||
)
|
||||
|
||||
// RenderForm implements sdk.CheckerInteractive. It exposes a minimal form
|
||||
// RenderForm implements server.Interactive. It exposes a minimal form
|
||||
// letting a human submit one or more ping targets (hostnames or IPs) along
|
||||
// with the usual threshold knobs.
|
||||
func (p *pingProvider) RenderForm() []sdk.CheckerOptionField {
|
||||
|
|
@ -76,8 +78,8 @@ func (p *pingProvider) RenderForm() []sdk.CheckerOptionField {
|
|||
}
|
||||
}
|
||||
|
||||
// ParseForm implements sdk.CheckerInteractive. It converts the HTML form
|
||||
// inputs into a CheckerOptions that Collect can consume directly — pinging
|
||||
// ParseForm implements server.Interactive. It converts the HTML form
|
||||
// inputs into a CheckerOptions that Collect can consume directly, pinging
|
||||
// resolves hostnames on its own, so no extra lookups are needed here.
|
||||
func (p *pingProvider) ParseForm(r *http.Request) (sdk.CheckerOptions, error) {
|
||||
raw := strings.TrimSpace(r.FormValue("addresses"))
|
||||
|
|
|
|||
|
|
@ -47,11 +47,6 @@ func (p *pingProvider) Key() happydns.ObservationKey {
|
|||
return ObservationKeyPing
|
||||
}
|
||||
|
||||
// Definition implements happydns.CheckerDefinitionProvider.
|
||||
func (p *pingProvider) Definition() *happydns.CheckerDefinition {
|
||||
return Definition()
|
||||
}
|
||||
|
||||
// ExtractMetrics implements happydns.CheckerMetricsReporter.
|
||||
func (p *pingProvider) ExtractMetrics(ctx happydns.ReportContext, collectedAt time.Time) ([]happydns.CheckMetric, error) {
|
||||
var data PingData
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -3,7 +3,7 @@ module git.happydns.org/checker-ping
|
|||
go 1.25.0
|
||||
|
||||
require (
|
||||
git.happydns.org/checker-sdk-go v1.2.0
|
||||
git.happydns.org/checker-sdk-go v1.3.0
|
||||
git.happydns.org/happyDomain v0.7.0
|
||||
github.com/prometheus-community/pro-bing v0.8.0
|
||||
)
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -1,5 +1,5 @@
|
|||
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=
|
||||
git.happydns.org/checker-sdk-go v1.3.0 h1:FG2kIhlJCzI0m35EhxSgn4UWc9M4ha6aZTeoChu4l7A=
|
||||
git.happydns.org/checker-sdk-go v1.3.0/go.mod h1:aNAcfYFfbhvH9kJhE0Njp5GX0dQbxdRB0rJ0KvSC5nI=
|
||||
git.happydns.org/happyDomain v0.7.0 h1:NV82/NbcSeRm0+IUZqaK3Vu9Ovl5+vv4AigUJZMdwws=
|
||||
git.happydns.org/happyDomain v0.7.0/go.mod h1:5tgkmqFE65kK359rY49V++49wgZ0gco+Gh9X6tbL+bY=
|
||||
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
||||
|
|
|
|||
6
main.go
6
main.go
|
|
@ -26,7 +26,7 @@ import (
|
|||
"log"
|
||||
|
||||
ping "git.happydns.org/checker-ping/checker"
|
||||
sdk "git.happydns.org/checker-sdk-go/checker"
|
||||
"git.happydns.org/checker-sdk-go/checker/server"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -47,8 +47,8 @@ func main() {
|
|||
// CheckerDefinition.Version.
|
||||
ping.Version = Version
|
||||
|
||||
server := sdk.NewServer(ping.ProviderWithPrivileged(*privileged))
|
||||
if err := server.ListenAndServe(*listenAddr); err != nil {
|
||||
srv := server.New(ping.ProviderWithPrivileged(*privileged))
|
||||
if err := srv.ListenAndServe(*listenAddr); err != nil {
|
||||
log.Fatalf("server error: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,5 +22,6 @@ func NewCheckerPlugin() (*sdk.CheckerDefinition, sdk.ObservationProvider, error)
|
|||
// Propagate the plugin's version to the checker package so it shows up
|
||||
// in CheckerDefinition.Version.
|
||||
ping.Version = Version
|
||||
return ping.Definition(), ping.Provider(), nil
|
||||
prvd := ping.Provider()
|
||||
return prvd.(sdk.CheckerDefinitionProvider).Definition(), prvd, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue