mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-05 07:08:41 +00:00
Add ping command to server to allow container healthchecks (#2030)
Fixes: https://github.com/woodpecker-ci/woodpecker/issues/1943 Note: Kubernetes ignores the container `HEALTHCHECK` by default. --------- Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
parent
6b59143c44
commit
84b597478f
4 changed files with 72 additions and 0 deletions
63
cmd/server/health.go
Normal file
63
cmd/server/health.go
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
// Copyright 2023 Woodpecker Authors
|
||||||
|
// Copyright 2018 Drone.IO Inc.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
const pingTimeout = 1 * time.Second
|
||||||
|
|
||||||
|
// handles pinging the endpoint and returns an error if the
|
||||||
|
// server is in an unhealthy state.
|
||||||
|
func pinger(c *cli.Context) error {
|
||||||
|
scheme := "http"
|
||||||
|
serverAddr := c.String("server-addr")
|
||||||
|
if strings.HasPrefix(serverAddr, ":") {
|
||||||
|
// this seems sufficient according to https://pkg.go.dev/net#Dial
|
||||||
|
serverAddr = "localhost" + serverAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
// if woodpecker do ssl on it's own
|
||||||
|
if c.String("server-cert") != "" || c.Bool("lets-encrypt") {
|
||||||
|
scheme = "https"
|
||||||
|
}
|
||||||
|
|
||||||
|
// create the health url
|
||||||
|
healthURL := fmt.Sprintf("%s://%s/healthz", scheme, serverAddr)
|
||||||
|
log.Trace().Msgf("try to ping with url '%s'", healthURL)
|
||||||
|
|
||||||
|
// ask server if all is healthy
|
||||||
|
client := http.Client{Timeout: pingTimeout}
|
||||||
|
resp, err := client.Get(healthURL)
|
||||||
|
if err != nil {
|
||||||
|
if strings.Contains(err.Error(), "deadline exceeded") {
|
||||||
|
return fmt.Errorf("ping timeout reached after %s", pingTimeout)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return fmt.Errorf("server returned non-200 status code")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -31,6 +31,13 @@ func main() {
|
||||||
app.Version = version.String()
|
app.Version = version.String()
|
||||||
app.Usage = "woodpecker server"
|
app.Usage = "woodpecker server"
|
||||||
app.Action = run
|
app.Action = run
|
||||||
|
app.Commands = []*cli.Command{
|
||||||
|
{
|
||||||
|
Name: "ping",
|
||||||
|
Usage: "ping the server",
|
||||||
|
Action: pinger,
|
||||||
|
},
|
||||||
|
}
|
||||||
app.Flags = flags
|
app.Flags = flags
|
||||||
|
|
||||||
setupSwaggerStaticConfig()
|
setupSwaggerStaticConfig()
|
||||||
|
|
|
@ -11,4 +11,5 @@ EXPOSE 8000 9000 80 443
|
||||||
|
|
||||||
COPY dist/server/${TARGETOS}/${TARGETARCH}/woodpecker-server /bin/
|
COPY dist/server/${TARGETOS}/${TARGETARCH}/woodpecker-server /bin/
|
||||||
|
|
||||||
|
HEALTHCHECK CMD ["/bin/woodpecker-server", "ping"]
|
||||||
ENTRYPOINT ["/bin/woodpecker-server"]
|
ENTRYPOINT ["/bin/woodpecker-server"]
|
||||||
|
|
|
@ -14,4 +14,5 @@ COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certifica
|
||||||
# copy server binary
|
# copy server binary
|
||||||
COPY dist/server/${TARGETOS}/${TARGETARCH}/woodpecker-server /bin/
|
COPY dist/server/${TARGETOS}/${TARGETARCH}/woodpecker-server /bin/
|
||||||
|
|
||||||
|
HEALTHCHECK CMD ["/bin/woodpecker-server", "ping"]
|
||||||
ENTRYPOINT ["/bin/woodpecker-server"]
|
ENTRYPOINT ["/bin/woodpecker-server"]
|
||||||
|
|
Loading…
Reference in a new issue