Skip to content

Commit

Permalink
pkg/podvolume/configs/configs.go
Browse files Browse the repository at this point in the history
before
```go
❯ find ./pkg/cmd/server/config -maxdepth 1 -type f | grep .go | xargs -I {} sh -c "echo && echo && echo {} && go list -f {{.Deps}} {} | sed 's/ /\n/g' | grep -E 'github.com/vmware-tanzu/velero/pkg/podvolume'"

./pkg/cmd/server/config/config.go
github.com/vmware-tanzu/velero/pkg/podvolume

./pkg/cmd/server/config/config_test.go
```

after
```go
~/git/velero refactor-pluginframeworkdep
❯ find ./pkg/cmd/server/config -maxdepth 1 -type f | grep .go | xargs -I {} sh -c "echo && echo && echo {} && go list -f {{.Deps}} {} | sed 's/ /\n/g' | grep -E 'github.com/vmware-tanzu/velero/pkg/podvolume'"

./pkg/cmd/server/config/config.go
github.com/vmware-tanzu/velero/pkg/podvolume/configs

./pkg/cmd/server/config/config_test.go
```

Signed-off-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
kaovilai committed Sep 12, 2024
1 parent cd76cee commit 639e69e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
4 changes: 2 additions & 2 deletions pkg/cmd/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/vmware-tanzu/velero/pkg/cmd/util/flag"
"github.com/vmware-tanzu/velero/pkg/constant"
"github.com/vmware-tanzu/velero/pkg/podvolume"
podvolumeconfigs "github.com/vmware-tanzu/velero/pkg/podvolume/configs"
"github.com/vmware-tanzu/velero/pkg/types"
"github.com/vmware-tanzu/velero/pkg/uploader"
"github.com/vmware-tanzu/velero/pkg/util/kube"
Expand Down Expand Up @@ -198,7 +198,7 @@ func GetDefaultConfig() *Config {
ResourceTerminatingTimeout: defaultResourceTerminatingTimeout,
LogLevel: logging.LogLevelFlag(logrus.InfoLevel),
LogFormat: logging.NewFormatFlag(),
DefaultVolumesToFsBackup: podvolume.DefaultVolumesToFsBackup,
DefaultVolumesToFsBackup: podvolumeconfigs.DefaultVolumesToFsBackup,
UploaderType: uploader.ResticType,
MaxConcurrentK8SConnections: defaultMaxConcurrentK8SConnections,
DefaultSnapshotMoveData: false,
Expand Down
3 changes: 2 additions & 1 deletion pkg/podvolume/backupper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
veleroclient "github.com/vmware-tanzu/velero/pkg/client"
"github.com/vmware-tanzu/velero/pkg/label"
"github.com/vmware-tanzu/velero/pkg/nodeagent"
"github.com/vmware-tanzu/velero/pkg/podvolume/configs"
"github.com/vmware-tanzu/velero/pkg/repository"
"github.com/vmware-tanzu/velero/pkg/uploader"
uploaderutil "github.com/vmware-tanzu/velero/pkg/uploader/util"
Expand Down Expand Up @@ -419,7 +420,7 @@ func newPodVolumeBackup(backup *velerov1api.Backup, pod *corev1api.Pod, volume c
// this annotation is used in pkg/restore to identify if a PVC
// has a pod volume backup.
pvb.Annotations = map[string]string{
PVCNameAnnotation: pvc.Name,
configs.PVCNameAnnotation: pvc.Name,
}

// this label is used by the pod volume backup controller to tell
Expand Down
16 changes: 16 additions & 0 deletions pkg/podvolume/configs/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package configs

const (
// PVCNameAnnotation is the key for the annotation added to
// pod volume backups when they're for a PVC.
PVCNameAnnotation = "velero.io/pvc-name"

// Deprecated.
//
// TODO(2.0): remove
PodAnnotationPrefix = "snapshot.velero.io/"

// DefaultVolumesToFsBackup specifies whether pod volume backup should be used, by default, to
// take backup of all pod volumes.
DefaultVolumesToFsBackup = false
)
20 changes: 3 additions & 17 deletions pkg/podvolume/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/podvolume/configs"
repotypes "github.com/vmware-tanzu/velero/pkg/repository/types"
"github.com/vmware-tanzu/velero/pkg/uploader"
)

const (
// PVCNameAnnotation is the key for the annotation added to
// pod volume backups when they're for a PVC.
PVCNameAnnotation = "velero.io/pvc-name"

// Deprecated.
//
// TODO(2.0): remove
podAnnotationPrefix = "snapshot.velero.io/"

// DefaultVolumesToFsBackup specifies whether pod volume backup should be used, by default, to
// take backup of all pod volumes.
DefaultVolumesToFsBackup = false
)

// volumeBackupInfo describes the backup info of a volume backed up by PodVolumeBackups
type volumeBackupInfo struct {
snapshotID string
Expand Down Expand Up @@ -212,8 +198,8 @@ func getPodSnapshotAnnotations(obj metav1.Object) map[string]string {
}

for k, v := range obj.GetAnnotations() {
if strings.HasPrefix(k, podAnnotationPrefix) {
insertSafe(k[len(podAnnotationPrefix):], v)
if strings.HasPrefix(k, configs.PodAnnotationPrefix) {
insertSafe(k[len(configs.PodAnnotationPrefix):], v)
}
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/podvolume/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/builder"
"github.com/vmware-tanzu/velero/pkg/podvolume/configs"
)

func TestGetVolumeBackupsForPod(t *testing.T) {
Expand Down Expand Up @@ -53,17 +54,17 @@ func TestGetVolumeBackupsForPod(t *testing.T) {
},
{
name: "pod annotation with only snapshot annotation prefix, results in volume backup with empty volume key",
podAnnotations: map[string]string{podAnnotationPrefix: "snapshotID"},
podAnnotations: map[string]string{configs.PodAnnotationPrefix: "snapshotID"},
expected: map[string]string{"": "snapshotID"},
},
{
name: "pod annotation with snapshot annotation prefix results in volume backup with volume name and snapshot ID",
podAnnotations: map[string]string{podAnnotationPrefix + "volume": "snapshotID"},
podAnnotations: map[string]string{configs.PodAnnotationPrefix + "volume": "snapshotID"},
expected: map[string]string{"volume": "snapshotID"},
},
{
name: "only pod annotations with snapshot annotation prefix are considered",
podAnnotations: map[string]string{"x": "y", podAnnotationPrefix + "volume1": "snapshot1", podAnnotationPrefix + "volume2": "snapshot2"},
podAnnotations: map[string]string{"x": "y", configs.PodAnnotationPrefix + "volume1": "snapshot1", configs.PodAnnotationPrefix + "volume2": "snapshot2"},
expected: map[string]string{"volume1": "snapshot1", "volume2": "snapshot2"},
},
{
Expand All @@ -74,7 +75,7 @@ func TestGetVolumeBackupsForPod(t *testing.T) {
},
podName: "TestPod",
sourcePodNs: "TestNS",
podAnnotations: map[string]string{"x": "y", podAnnotationPrefix + "foo": "bar", podAnnotationPrefix + "abc": "123"},
podAnnotations: map[string]string{"x": "y", configs.PodAnnotationPrefix + "foo": "bar", configs.PodAnnotationPrefix + "abc": "123"},
expected: map[string]string{"pvbtest1-foo": "snapshot1", "pvbtest2-abc": "snapshot2"},
},
{
Expand Down
3 changes: 2 additions & 1 deletion pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import (
vsv1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/volumesnapshotter/v1"
"github.com/vmware-tanzu/velero/pkg/podexec"
"github.com/vmware-tanzu/velero/pkg/podvolume"
"github.com/vmware-tanzu/velero/pkg/podvolume/configs"
"github.com/vmware-tanzu/velero/pkg/types"
"github.com/vmware-tanzu/velero/pkg/util/boolptr"
"github.com/vmware-tanzu/velero/pkg/util/collections"
Expand Down Expand Up @@ -2020,7 +2021,7 @@ func hasPodVolumeBackup(unstructuredPV *unstructured.Unstructured, ctx *restoreC

var found bool
for _, pvb := range ctx.podVolumeBackups {
if pvb.Spec.Pod.Namespace == pv.Spec.ClaimRef.Namespace && pvb.GetAnnotations()[podvolume.PVCNameAnnotation] == pv.Spec.ClaimRef.Name {
if pvb.Spec.Pod.Namespace == pv.Spec.ClaimRef.Namespace && pvb.GetAnnotations()[configs.PVCNameAnnotation] == pv.Spec.ClaimRef.Name {
found = true
break
}
Expand Down

0 comments on commit 639e69e

Please sign in to comment.