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

Function NewCmd

internal/cmd/manager/walarchive/cmd.go:40–89  ·  view source on GitHub ↗

NewCmd creates the new cobra command

()

Source from the content-addressed store, hash-verified

38
39// NewCmd creates the new cobra command
40func NewCmd() *cobra.Command {
41 var podName string
42 var pgData string
43 cmd := cobra.Command{
44 Use: "wal-archive [name]",
45 SilenceErrors: true,
46 Args: cobra.ExactArgs(1),
47 RunE: func(cobraCmd *cobra.Command, args []string) error {
48 const logErrorMessage = "failed to run wal-archive command"
49
50 contextLog := log.WithName("wal-archive")
51 ctx := log.IntoContext(cobraCmd.Context(), contextLog)
52
53 if podName == "" {
54 err := fmt.Errorf("no pod-name value passed and failed to extract it from POD_NAME env variable")
55 contextLog.Error(err, logErrorMessage)
56 return err
57 }
58
59 localClient := local.NewClient()
60 cluster, errCluster := localClient.Cache().GetCluster()
61 if errCluster != nil {
62 return fmt.Errorf("failed to get cluster: %w", errCluster)
63 }
64
65 if err := archiver.Run(ctx, podName, pgData, cluster, args[0]); err != nil {
66 if errors.Is(err, errSwitchoverInProgress) {
67 contextLog.Warning("Refusing to archive WALs until the switchover is not completed",
68 "err", err)
69 } else {
70 contextLog.Error(err, logErrorMessage)
71 }
72 if reqErr := localClient.Cluster().SetWALArchiveStatusCondition(ctx, err.Error()); reqErr != nil {
73 contextLog.Error(reqErr, "while invoking the set wal archive condition endpoint")
74 }
75 return err
76 }
77
78 if err := localClient.Cluster().SetWALArchiveStatusCondition(ctx, ""); err != nil {
79 contextLog.Error(err, "while invoking the set wal archive condition endpoint")
80 }
81 return nil
82 },
83 }
84 cmd.Flags().StringVar(&podName, "pod-name", os.Getenv("POD_NAME"), "The name of the "+
85 "current pod in k8s")
86 cmd.Flags().StringVar(&pgData, "pg-data", os.Getenv("PGDATA"), "The PGDATA to be used")
87
88 return &cmd
89}

Callers 1

mainFunction · 0.92

Calls 9

CacheMethod · 0.95
ClusterMethod · 0.95
NewClientFunction · 0.92
RunFunction · 0.92
WithNameMethod · 0.80
WarningMethod · 0.80
GetClusterMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected