mirror of
https://github.com/cloudnative-pg/plugin-barman-cloud.git
synced 2026-01-11 21:23:12 +01:00
feat: return proper gRPC error codes for expected conditions (#549)
The plugin now returns a 404 status code when a requested WAL file does not exist in the object store. This prevents misleading log entries such as "Error while handling gRPC request" for expected missing-file scenarios. The `ErrEndOfWALStreamReached` now returns `OutOfRange` error. The `ErrMissingPermissions` now returns `FailedPrecondition` error. Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com> Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com> Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com> Co-authored-by: Armando Ruocco <armando.ruocco@enterprisedb.com> Co-authored-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
This commit is contained in:
parent
65a0d11ec8
commit
08c3f1c232
@ -1,16 +1,25 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
// walNotFoundError is raised when a WAL file has not been found in the object store
|
import (
|
||||||
type walNotFoundError struct{}
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
)
|
||||||
|
|
||||||
func newWALNotFoundError() *walNotFoundError { return &walNotFoundError{} }
|
// ErrEndOfWALStreamReached is returned when end of WAL is detected in the cloud archive.
|
||||||
|
var ErrEndOfWALStreamReached = status.Errorf(codes.OutOfRange, "end of WAL reached")
|
||||||
|
|
||||||
// ShouldPrintStackTrace tells whether the sidecar log stream should contain the stack trace
|
// ErrMissingPermissions is raised when the sidecar has no
|
||||||
func (e walNotFoundError) ShouldPrintStackTrace() bool {
|
// permission to download the credentials needed to reach
|
||||||
return false
|
// the object storage.
|
||||||
}
|
// This will be fixed by the reconciliation loop in the
|
||||||
|
// operator plugin.
|
||||||
// Error implements the error interface
|
var ErrMissingPermissions = status.Errorf(codes.FailedPrecondition,
|
||||||
func (e walNotFoundError) Error() string {
|
"no permission to download the backup credentials, retrying")
|
||||||
return "WAL file not found"
|
|
||||||
|
// newWALNotFoundError returns a error that states that a
|
||||||
|
// certain WAL file has not been found. This error is
|
||||||
|
// compatible with GRPC status codes, resulting in a 404
|
||||||
|
// being used as a response code.
|
||||||
|
func newWALNotFoundError(walName string) error {
|
||||||
|
return status.Errorf(codes.NotFound, "wal %q not found", walName)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,13 +27,6 @@ import (
|
|||||||
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
|
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrMissingPermissions is raised when the sidecar has no
|
|
||||||
// permission to download the credentials needed to reach
|
|
||||||
// the object storage.
|
|
||||||
// This will be fixed by the reconciliation loop in the
|
|
||||||
// operator plugin.
|
|
||||||
var ErrMissingPermissions = fmt.Errorf("no permission to download the backup credentials, retrying")
|
|
||||||
|
|
||||||
// SpoolManagementError is raised when a spool management
|
// SpoolManagementError is raised when a spool management
|
||||||
// error has been detected
|
// error has been detected
|
||||||
type SpoolManagementError struct {
|
type SpoolManagementError struct {
|
||||||
@ -95,11 +88,14 @@ func (w WALServiceImplementation) Archive(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
request *wal.WALArchiveRequest,
|
request *wal.WALArchiveRequest,
|
||||||
) (*wal.WALArchiveResult, error) {
|
) (*wal.WALArchiveResult, error) {
|
||||||
contextLogger := log.FromContext(ctx)
|
|
||||||
contextLogger.Debug("starting wal archive")
|
|
||||||
|
|
||||||
baseWalName := path.Base(request.GetSourceFileName())
|
baseWalName := path.Base(request.GetSourceFileName())
|
||||||
|
|
||||||
|
contextLogger := log.FromContext(ctx)
|
||||||
|
contextLogger.Debug("wal archive start", "walName", baseWalName)
|
||||||
|
defer func() {
|
||||||
|
contextLogger.Debug("wal archive end", "walName", baseWalName)
|
||||||
|
}()
|
||||||
|
|
||||||
// Step 1: parse the configuration and get the environment variables needed
|
// Step 1: parse the configuration and get the environment variables needed
|
||||||
// for barman-cloud-wal-archive
|
// for barman-cloud-wal-archive
|
||||||
configuration, err := config.NewFromClusterJSON(request.ClusterDefinition)
|
configuration, err := config.NewFromClusterJSON(request.ClusterDefinition)
|
||||||
@ -122,6 +118,7 @@ func (w WALServiceImplementation) Archive(
|
|||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if apierrors.IsForbidden(err) {
|
if apierrors.IsForbidden(err) {
|
||||||
|
contextLogger.Info(ErrMissingPermissions.Error())
|
||||||
return nil, ErrMissingPermissions
|
return nil, ErrMissingPermissions
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -350,7 +347,7 @@ func (w WALServiceImplementation) restoreFromBarmanObjectStore(
|
|||||||
// The failure has already been logged in walRestorer.RestoreList method
|
// The failure has already been logged in walRestorer.RestoreList method
|
||||||
if walStatus[0].Err != nil {
|
if walStatus[0].Err != nil {
|
||||||
if errors.Is(walStatus[0].Err, barmanRestorer.ErrWALNotFound) {
|
if errors.Is(walStatus[0].Err, barmanRestorer.ErrWALNotFound) {
|
||||||
return newWALNotFoundError()
|
return newWALNotFoundError(walStatus[0].WalName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return walStatus[0].Err
|
return walStatus[0].Err
|
||||||
@ -457,9 +454,6 @@ func gatherWALFilesToRestore(walName string, parallel int) (walList []string, er
|
|||||||
return walList, err
|
return walList, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrEndOfWALStreamReached is returned when end of WAL is detected in the cloud archive.
|
|
||||||
var ErrEndOfWALStreamReached = errors.New("end of WAL reached")
|
|
||||||
|
|
||||||
// checkEndOfWALStreamFlag returns ErrEndOfWALStreamReached if the flag is set in the restorer.
|
// checkEndOfWALStreamFlag returns ErrEndOfWALStreamReached if the flag is set in the restorer.
|
||||||
func checkEndOfWALStreamFlag(walRestorer *barmanRestorer.WALRestorer) error {
|
func checkEndOfWALStreamFlag(walRestorer *barmanRestorer.WALRestorer) error {
|
||||||
contain, err := walRestorer.IsEndOfWALStream()
|
contain, err := walRestorer.IsEndOfWALStream()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user