Skip to content

Commit

Permalink
feat: add label-backup-name flag to snapshot command (cloudnative-p…
Browse files Browse the repository at this point in the history
…g#2653)

Closes cloudnative-pg#2652

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Tao Li <[email protected]>
Co-authored-by: Tao Li <[email protected]>
  • Loading branch information
armru and litaocdl authored Sep 6, 2023
1 parent bdd0656 commit 2a104f9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
27 changes: 24 additions & 3 deletions internal/cmd/plugin/snapshot/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"regexp"

volumesnapshot "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
"github.com/spf13/cobra"
Expand All @@ -34,6 +35,9 @@ import (
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils/snapshot"
)

// label value regular expression
var labelValueRegex = regexp.MustCompile("^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]")

// NewCmd implements the `snapshot` subcommand
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -57,17 +61,18 @@ The other replicas will continue working.`,

snapshotClassName, _ := cmd.Flags().GetString("volume-snapshot-class-name")
snapshotNameSuffix, _ := cmd.Flags().GetString("volume-snapshot-suffix")
backupNameLabel, _ := cmd.Flags().GetString("label-backup-name")

return execute(cmd.Context(), clusterName, snapshotClassName, snapshotNameSuffix)
return execute(cmd.Context(), clusterName, snapshotClassName, snapshotNameSuffix, backupNameLabel)
},
}

cmd.Flags().StringP(
"volume-snapshot-class-name",
"c",
"",
`The VolumeSnapshotClass name to be used for the snapshot
(defaults to empty, which will make use of the default VolumeSnapshotClass)`)
`The VolumeSnapshotClass name to be used for the snapshot.
Defaults to empty string, which will make use of the default VolumeSnapshotClass`)

cmd.Flags().StringP("volume-snapshot-suffix",
"x",
Expand All @@ -76,6 +81,12 @@ The other replicas will continue working.`,
"Defaults to the snapshot time expressed as unix timestamp",
)

cmd.Flags().StringP("label-backup-name",
"l",
"",
`Specifies the value for the label 'cnpg.io/backupName'.
Defaults to empty, which will cause the label to not be present on the created VolumeSnapshot resources`)

return cmd
}

Expand All @@ -85,7 +96,13 @@ func execute(
clusterName string,
snapshotClassName string,
snapshotSuffix string,
backupNameLabel string,
) error {
if backupNameLabel != "" && !labelValueRegex.MatchString(backupNameLabel) {
return fmt.Errorf("invalid label value. A valid label must be an empty string or consist of " +
"alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character")
}

// Get the Cluster object
var cluster apiv1.Cluster
err := plugin.Client.Get(
Expand Down Expand Up @@ -130,6 +147,10 @@ func execute(
}

enrichFunc := func(vs *volumesnapshot.VolumeSnapshot) {
if backupNameLabel != "" {
vs.Labels[utils.BackupNameLabelName] = backupNameLabel
}

vs.Annotations[utils.ClusterManifestAnnotationName] = string(rawCluster)

pgControlData, err := plugin.GetPGControlData(ctx, *targetPod)
Expand Down
7 changes: 6 additions & 1 deletion tests/e2e/volume_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ var _ = Describe("Verify Volume Snapshot",
namespace,
clusterName,
"",
"",
)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -97,12 +98,14 @@ var _ = Describe("Verify Volume Snapshot",
}).Should(Succeed())
})

It("using the kubectl cnpg plugin with a custom suffix", func() {
It("using the kubectl cnpg plugin with a custom suffix and label name", func() {
const backupName = "backup-name"
err := testUtils.CreateVolumeSnapshotBackup(
volumeSnapshotClassName,
namespace,
clusterName,
snapshotSuffix,
backupName,
)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -111,6 +114,7 @@ var _ = Describe("Verify Volume Snapshot",
for _, snapshot := range snapshotList.Items {
if strings.Contains(snapshot.Name, snapshotSuffix) {
g.Expect(snapshot.Name).To(ContainSubstring(clusterName))
g.Expect(snapshot.Labels[utils.BackupNameLabelName]).To(BeEquivalentTo(backupName))
}
}
}).Should(Succeed())
Expand Down Expand Up @@ -227,6 +231,7 @@ var _ = Describe("Verify Volume Snapshot",
namespace,
clusterToSnapshotName,
suffix,
"",
)
Expect(err).ToNot(HaveOccurred())
})
Expand Down
6 changes: 5 additions & 1 deletion tests/utils/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ func CreateVolumeSnapshotBackup(
volumeSnapshotClass,
namespace,
clusterName,
snapshotSuffix string,
snapshotSuffix,
backupName string,
) error {
command := fmt.Sprintf("kubectl cnpg snapshot %v -n %v", clusterName, namespace)
if volumeSnapshotClass != "" {
Expand All @@ -507,6 +508,9 @@ func CreateVolumeSnapshotBackup(
if snapshotSuffix != "" {
command = fmt.Sprintf("%v -x %v", command, snapshotSuffix)
}
if backupName != "" {
command = fmt.Sprintf("%v -l %v", command, backupName)
}

_, _, err := Run(command)
return err
Expand Down

0 comments on commit 2a104f9

Please sign in to comment.