Change healtcheck port into address format, redo #1197 (#1423)

As discussed in the comments in PR #1197. Also add documenation
accordingly.

One thing I'm not sure about is the simple check in health.go if the
address is usable in the GET request or not. From reading
https://pkg.go.dev/net#Dial it seems that the only non-standard address
format that would work in the `net` package but not in a GET url would
likely only be `:port`, as the others listed here are actually also
valid urls:

`For TCP, UDP and IP networks, if the host is empty or a literal
unspecified IP address, as in ":80", "0.0.0.0:80" or "[::]:80" for TCP
and UDP, "", "0.0.0.0" or "::" for IP, the local system is assumed.`

One additional thing I noticed is that while `WOODPECKER_SERVER_ADDR`
and `WOODPECKER_SERVER_ADDR` use the default value format of `:PORT`,
`WOODPECKER_SERVER` actually uses `localhost:9000`. I guess it makes a
bit of sense, considering the server might not be local to the agent,
but it looks a bit inconsistent this way. I don't think it would hurt to
make the `WOODPECKER_HEALTHCHECK_ADDR` in this format too, but then it's
different from the server flags again... :-)
This commit is contained in:
velsinki 2022-11-19 12:06:51 +01:00 committed by GitHub
parent 1786c0554a
commit dda4998261
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 9 deletions

View file

@ -17,7 +17,6 @@ package main
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"fmt"
"net/http" "net/http"
"os" "os"
"runtime" "runtime"
@ -90,8 +89,8 @@ func loop(c *cli.Context) error {
if c.Bool("healthcheck") { if c.Bool("healthcheck") {
go func() { go func() {
if err := http.ListenAndServe(fmt.Sprintf(":%d", c.Int("healthcheck-port")), nil); err != nil { if err := http.ListenAndServe(c.String("healthcheck-addr"), nil); err != nil {
log.Error().Msgf("can not listen on port 3000: %v", err) log.Error().Msgf("cannot listen on address %s: %v", c.String("healthcheck-addr"), err)
} }
}() }()
} }

View file

@ -90,11 +90,11 @@ var flags = []cli.Flag{
Usage: "enable healthcheck endpoint", Usage: "enable healthcheck endpoint",
Value: true, Value: true,
}, },
&cli.IntFlag{ &cli.StringFlag{
EnvVars: []string{"WOODPECKER_HEALTHCHECK_PORT"}, EnvVars: []string{"WOODPECKER_HEALTHCHECK_ADDR"},
Name: "healthcheck-port", Name: "healthcheck-addr",
Usage: "port used for healthcheck endpoint", Usage: "healthcheck endpoint address",
Value: 3000, Value: ":3000",
}, },
&cli.DurationFlag{ &cli.DurationFlag{
EnvVars: []string{"WOODPECKER_KEEPALIVE_TIME"}, EnvVars: []string{"WOODPECKER_KEEPALIVE_TIME"},

View file

@ -18,6 +18,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -78,7 +79,12 @@ var counter = &agent.State{
// handles pinging the endpoint and returns an error if the // handles pinging the endpoint and returns an error if the
// agent is in an unhealthy state. // agent is in an unhealthy state.
func pinger(c *cli.Context) error { func pinger(c *cli.Context) error {
resp, err := http.Get("http://localhost:3000/healthz") healthcheckAddress := c.String("healthcheck-addr")
if strings.HasPrefix(healthcheckAddress, ":") {
// this seems sufficient according to https://pkg.go.dev/net#Dial
healthcheckAddress = "localhost" + healthcheckAddress
}
resp, err := http.Get("http://" + healthcheckAddress + "/healthz")
if err != nil { if err != nil {
return err return err
} }

View file

@ -95,6 +95,11 @@ Configures labels to filter pipeline pick up. Use a list of key-value pairs like
Enable healthcheck endpoint. Enable healthcheck endpoint.
### `WOODPECKER_HEALTHCHECK_ADDR`
> Default: `:3000`
Configures healthcheck endpoint address.
### `WOODPECKER_KEEPALIVE_TIME` ### `WOODPECKER_KEEPALIVE_TIME`
> Default: empty > Default: empty