plugin-barman-cloud/internal/cmd/healthcheck/main.go
Armando Ruocco 5fd9449b27
feat: add liveness and readiness probe support (#69)
Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
Signed-off-by: Francesco Canovai <francesco.canovai@enterprisedb.com>
Co-authored-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
Co-authored-by: Francesco Canovai <francesco.canovai@enterprisedb.com>
2024-12-02 14:51:32 +01:00

76 lines
1.8 KiB
Go

package healthcheck
import (
"fmt"
"os"
"path"
"github.com/cloudnative-pg/machinery/pkg/log"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
)
// NewCmd returns the healthcheck command
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "healthcheck",
Short: "healthcheck commands",
}
cmd.AddCommand(unixHealthCheck())
return cmd
}
func unixHealthCheck() *cobra.Command {
cmd := &cobra.Command{
Use: "unix",
Short: "executes the health check command on unix:///plugins/barman-cloud.cloudnative-pg.io",
RunE: func(cmd *cobra.Command, _ []string) error {
dialPath := fmt.Sprintf("unix://%s", path.Join("/plugins", metadata.PluginName))
cli, cliErr := grpc.NewClient(dialPath, grpc.WithTransportCredentials(insecure.NewCredentials()))
if cliErr != nil {
log.Error(cliErr, "while building the client")
return cliErr
}
healthCli := grpc_health_v1.NewHealthClient(cli)
res, healthErr := healthCli.Check(
cmd.Context(),
&grpc_health_v1.HealthCheckRequest{},
)
if healthErr != nil {
log.Error(healthErr, "while executing the healthcheck call")
return healthErr
}
if res.Status == grpc_health_v1.HealthCheckResponse_SERVING {
log.Trace("healthcheck response OK")
os.Exit(0)
return nil
}
log.Error(fmt.Errorf("unexpected healthcheck status: %v", res.Status),
"while processing healthcheck response")
// exit code 1 is returned when we exit from the function with an error
switch res.Status {
case grpc_health_v1.HealthCheckResponse_UNKNOWN:
os.Exit(2)
case grpc_health_v1.HealthCheckResponse_NOT_SERVING:
os.Exit(3)
default:
os.Exit(125)
}
return nil
},
}
return cmd
}