From 44f0f172fc450bf9ea7d4f16348c2103f8d0b5b7 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 1 Jan 2024 23:56:32 +0100 Subject: [PATCH] migrate healthcheck to go closes #72 --- Dockerfile | 2 +- cmd/healthcheck.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 cmd/healthcheck.go diff --git a/Dockerfile b/Dockerfile index 7f277cf..54436f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,5 +6,5 @@ COPY oidc-test-client / EXPOSE 9009 WORKDIR /web-root ENV OIDC_BIND=0.0.0.0:9009 -HEALTHCHECK --interval=5s --start-period=1s CMD [ "wget", "--spider", "http://localhost:9009/health" ] +HEALTHCHECK --interval=5s --start-period=1s CMD [ "/oidc-test-client", "healthcheck" ] ENTRYPOINT [ "/oidc-test-client" ] diff --git a/cmd/healthcheck.go b/cmd/healthcheck.go new file mode 100644 index 0000000..b7d1804 --- /dev/null +++ b/cmd/healthcheck.go @@ -0,0 +1,38 @@ +package cmd + +import ( + "fmt" + "net/http" + "os" + + "beryju.io/oidc-test-client/pkg" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var healthcheckCmd = &cobra.Command{ + Use: "healthcheck", + Run: func(cmd *cobra.Command, args []string) { + os.Exit(check()) + }, +} + +func check() int { + root := pkg.Env("OIDC_ROOT_URL", "http://localhost:9009") + url := fmt.Sprintf("%s/health", root) + res, err := http.DefaultClient.Head(url) + if err != nil { + log.WithError(err).Warning("failed to send healthcheck request") + return 1 + } + if res.StatusCode >= 400 { + log.WithField("status", res.StatusCode).Warning("unhealthy status code") + return 1 + } + log.Debug("successfully checked health") + return 0 +} + +func init() { + rootCmd.AddCommand(healthcheckCmd) +}