generated from cloudnative-pg/cnpg-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
9404772
commit 5fd9449
Showing
8 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package healthcheck contains the logic to execute an healthcheck on the plugin through a command | ||
package healthcheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cloudnative-pg/machinery/pkg/log" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/health/grpc_health_v1" | ||
) | ||
|
||
// AddHealthCheck adds a health check service to the gRPC server with the tag 'plugin-barman-cloud' | ||
func AddHealthCheck(server *grpc.Server) { | ||
grpc_health_v1.RegisterHealthServer(server, &healthServer{}) // replaces default registration | ||
} | ||
|
||
type healthServer struct { | ||
grpc_health_v1.UnimplementedHealthServer | ||
} | ||
|
||
// Check is the response handle for the healthcheck request | ||
func (h healthServer) Check( | ||
ctx context.Context, | ||
_ *grpc_health_v1.HealthCheckRequest, | ||
) (*grpc_health_v1.HealthCheckResponse, error) { | ||
contextLogger := log.FromContext(ctx) | ||
contextLogger.Trace("serving health check response") | ||
return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters