MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / NewCmd

Function NewCmd

internal/cmd/manager/walrestore/cmd.go:65–108  ·  view source on GitHub ↗

NewCmd creates a new cobra command

()

Source from the content-addressed store, hash-verified

63
64// NewCmd creates a new cobra command
65func NewCmd() *cobra.Command {
66 var podName string
67 var pgData string
68
69 cmd := cobra.Command{
70 Use: "wal-restore [name]",
71 SilenceErrors: true,
72 Args: cobra.ExactArgs(2),
73 RunE: func(cobraCmd *cobra.Command, args []string) error {
74 // TODO: The command is triggered by PG, resulting in the loss of stdout logs.
75 // TODO: We need to implement a logpipe to prevent this.
76 contextLog := log.WithName("wal-restore")
77 ctx := log.IntoContext(cobraCmd.Context(), contextLog)
78 err := run(ctx, pgData, podName, args)
79 if err == nil {
80 return nil
81 }
82
83 switch {
84 case errors.Is(err, barmanRestorer.ErrWALNotFound):
85 // Nothing to log here. The failure has already been logged.
86 case errors.Is(err, ErrNoBackupConfigured):
87 contextLog.Debug("tried restoring WALs, but no backup was configured")
88 case errors.Is(err, ErrEndOfWALStreamReached):
89 contextLog.Info(
90 "end-of-wal-stream flag found." +
91 "Exiting with error once to let Postgres try switching to streaming replication")
92 return err
93 default:
94 contextLog.Info("wal-restore command failed", "error", err)
95 }
96
97 contextLog.Debug("There was an error in the previous wal-restore command. Waiting 100 ms before retrying.")
98 time.Sleep(100 * time.Millisecond)
99 return err
100 },
101 }
102
103 cmd.Flags().StringVar(&podName, "pod-name", os.Getenv("POD_NAME"), "The name of the "+
104 "current pod in k8s")
105 cmd.Flags().StringVar(&pgData, "pg-data", os.Getenv("PGDATA"), "The PGDATA to be used")
106
107 return &cmd
108}
109
110func run(ctx context.Context, pgData string, podName string, args []string) error {
111 contextLog := log.FromContext(ctx)

Callers 1

mainFunction · 0.92

Calls 4

WithNameMethod · 0.80
DebugMethod · 0.80
InfoMethod · 0.80
runFunction · 0.70

Tested by

no test coverage detected