Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only skip PVC creation if --namespace-mapping flag is not used #565

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pkg/plugin/restore_pvc_action_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package plugin
import (
"context"
"fmt"
"os"

"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/cmd"
"k8s.io/client-go/kubernetes"
"os"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
backupdriverv1 "github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/apis/backupdriver/v1alpha1"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/backuprepository"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/constants"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
backupdriverTypedV1 "github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/generated/clientset/versioned/typed/backupdriver/v1alpha1"
pluginItem "github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/plugin/util"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/snapshotUtils"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/utils"
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -173,12 +174,14 @@ func (p *NewPVCRestoreItemAction) Execute(input *velero.RestoreItemActionExecute
return nil, errors.WithStack(err)
}

bSkipPVC, err := pluginItem.SkipPVCCreation(ctx, restConfig, &pvc, p.Log)
bSkipPVC, err := pluginItem.SkipPVCCreation(ctx, restConfig, &pvc, targetNamespace, p.Log)
if err != nil {
return nil, errors.WithStack(err)
} else if bSkipPVC {
}
if bSkipPVC {
// Skip PVCRestoreItemAction for PVC creation
// as it already exists
// and Restore is not remapping namespace
return &velero.RestoreItemActionExecuteOutput{
UpdatedItem: item,
}, nil
Expand Down
22 changes: 12 additions & 10 deletions pkg/plugin/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
backupdriverv1 "github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/apis/backupdriver/v1alpha1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/cmd"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/constants"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/utils"
"github.com/vmware-tanzu/velero/pkg/podvolume"
corev1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"strconv"
"strings"
)

func GetSnapshotFromPVCAnnotation(snapshotAnnotation string, itemSnapshot interface{}) error {
Expand Down Expand Up @@ -400,33 +401,34 @@ func IsPVCBackedUpByFsBackup(pvcNamespace, pvcName string, podClient corev1clien
// If PVC already exists, it returns true and nil.
// If PVC is not found, it returns false and nil to indicate
// PVC should be created.
func SkipPVCCreation(ctx context.Context, config *rest.Config, pvc *corev1.PersistentVolumeClaim, logger logrus.FieldLogger) (bool, error) {
func SkipPVCCreation(ctx context.Context, config *rest.Config, pvc *corev1.PersistentVolumeClaim, targetNamespace string, logger logrus.FieldLogger) (bool, error) {
if pvc == nil {
errMsg := "Input PVC cannot be nil"
logger.Error(errMsg)
return true, errors.New(errMsg)
}
logger.Infof("Check if PVC %s/%s creation should be skipped", pvc.Namespace, pvc.Name)
logger.Infof("Check if PVC %s/%s creation should be skipped", targetNamespace, pvc.Name)
kubeClient, err := GetKubeClient(config, logger)
if err != nil {
logger.Error("Failed to get clientset from given config")
return true, err
}

getPvc, err := kubeClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(ctx, pvc.Name, metav1.GetOptions{})
getPvc, err := kubeClient.CoreV1().PersistentVolumeClaims(targetNamespace).Get(ctx, pvc.Name, metav1.GetOptions{})
if err != nil && !apierrs.IsNotFound(err) {
logger.Errorf("Error occurred when trying to get PVC %s/%s: %v", pvc.Namespace, pvc.Name, err)
logger.Errorf("Error occurred when trying to get PVC %s/%s: %v", targetNamespace, pvc.Name, err)
return true, err
} else if getPvc != nil && getPvc.Namespace == pvc.Namespace && getPvc.Name == pvc.Name {
} else if getPvc != nil && getPvc.Namespace == targetNamespace && getPvc.Name == pvc.Name {
// NOTE: If PVC does not exist, "Get" may return an empty PersistentVolumeClaim object, not nil; so we need to check Namespace/Name match here
// NOTE: Need to skip it here in the plugin to avoid hanging as Velero skips restoring already existing resources
logger.Warnf("Skipping PVC %s/%s creation since it already exists.", pvc.Namespace, pvc.Name)
logger.Warnf("Skipping PVC %s/%s creation since it already exists.", targetNamespace, pvc.Name)
return true, nil
} else if err != nil && apierrs.IsNotFound(err) {
logger.Debugf("PVC %s/%s is not found. Create a new one.", pvc.Namespace, pvc.Name)
logger.Debugf("PVC %s/%s is not found. Create a new one.", targetNamespace, pvc.Name)
}

// Create a new PVC
logger.Infof("Creating PVC as it does not exist %s/%s", targetNamespace, pvc.Name)
return false, nil
}

Expand Down
Loading