Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ping command to server to allow container healthchecks #2030

Merged
merged 6 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions cmd/server/health.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func main() {
app.Version = version.String()
app.Usage = "woodpecker server"
app.Action = run
app.Commands = []*cli.Command{
{
Name: "ping",
Usage: "ping the server",
Action: pinger,
},
}
app.Flags = flags

setupSwaggerStaticConfig()
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile.server.alpine.multiarch
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ EXPOSE 8000 9000 80 443

COPY dist/server/${TARGETOS}/${TARGETARCH}/woodpecker-server /bin/

HEALTHCHECK CMD ["/bin/woodpecker-server", "ping"]
ENTRYPOINT ["/bin/woodpecker-server"]
1 change: 1 addition & 0 deletions docker/Dockerfile.server.multiarch
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certifica
# copy server binary
COPY dist/server/${TARGETOS}/${TARGETARCH}/woodpecker-server /bin/

HEALTHCHECK CMD ["/bin/woodpecker-server", "ping"]
ENTRYPOINT ["/bin/woodpecker-server"]