Skip to content

Commit

Permalink
issue 6668: start PVR when PVC is bound
Browse files Browse the repository at this point in the history
Signed-off-by: Lyndon-Li <[email protected]>
  • Loading branch information
Lyndon-Li committed Sep 27, 2023
1 parent b6b320c commit 2ee8d3a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/cli/nodeagent/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (s *nodeAgentServer) run() {
s.logger.Fatal(err, "unable to create controller", "controller", controller.PodVolumeBackup)
}

if err = controller.NewPodVolumeRestoreReconciler(s.mgr.GetClient(), repoEnsurer, credentialGetter, s.logger).SetupWithManager(s.mgr); err != nil {
if err = controller.NewPodVolumeRestoreReconciler(s.mgr.GetClient(), s.kubeClient, repoEnsurer, credentialGetter, s.logger).SetupWithManager(s.mgr); err != nil {
s.logger.WithError(err).Fatal("Unable to create the pod volume restore controller")
}

Expand Down
43 changes: 38 additions & 5 deletions pkg/controller/pod_volume_restore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
clocks "k8s.io/utils/clock"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -50,10 +51,11 @@ import (
"github.com/vmware-tanzu/velero/pkg/util/filesystem"
)

func NewPodVolumeRestoreReconciler(client client.Client, ensurer *repository.Ensurer,
func NewPodVolumeRestoreReconciler(client client.Client, kubeClient kubernetes.Interface, ensurer *repository.Ensurer,
credentialGetter *credentials.CredentialGetter, logger logrus.FieldLogger) *PodVolumeRestoreReconciler {
return &PodVolumeRestoreReconciler{
Client: client,
kubeClient: kubeClient,
logger: logger.WithField("controller", "PodVolumeRestore"),
repositoryEnsurer: ensurer,
credentialGetter: credentialGetter,
Expand All @@ -65,6 +67,7 @@ func NewPodVolumeRestoreReconciler(client client.Client, ensurer *repository.Ens

type PodVolumeRestoreReconciler struct {
client.Client
kubeClient kubernetes.Interface
logger logrus.FieldLogger
repositoryEnsurer *repository.Ensurer
credentialGetter *credentials.CredentialGetter
Expand Down Expand Up @@ -194,8 +197,38 @@ func (c *PodVolumeRestoreReconciler) shouldProcess(ctx context.Context, log logr
return false, nil, err
}

if !isInitContainerRunning(pod) {
log.Debug("Pod is not running restore-wait init container, skip")
if !isInitContainerReady(pod) {
log.Debug("Pod is not with a ready restore-wait init container, skip")
return false, nil, nil
}

pvcName := ""
for _, volume := range pod.Spec.Volumes {
if volume.Name == pvr.Spec.Volume {
pvcName = volume.PersistentVolumeClaim.ClaimName
break
}
}

if pvcName == "" {
err := errors.Errorf("pod doesn't have volume %s", pvr.Spec.Volume)
log.WithError(err).Error("Failed to find pod volume")
return false, nil, err
}

pvc, err := c.kubeClient.CoreV1().PersistentVolumeClaims(pod.Namespace).Get(ctx, pvcName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
log.WithField("PVC", pvcName).Debug("PVC not found skip")
return false, nil, nil
}

log.WithError(err).WithField("PVC", pvcName).Error("Unable to get PVC")
return false, nil, err
}

if pvc.Status.Phase != corev1api.ClaimBound {
log.WithField("PVC", pvcName).Debug("PVC is not bound")
return false, nil, nil
}

Expand Down Expand Up @@ -239,12 +272,12 @@ func isPVRNew(pvr *velerov1api.PodVolumeRestore) bool {
return pvr.Status.Phase == "" || pvr.Status.Phase == velerov1api.PodVolumeRestorePhaseNew
}

func isInitContainerRunning(pod *corev1api.Pod) bool {
func isInitContainerReady(pod *corev1api.Pod) bool {
// Pod volume wait container can be anywhere in the list of init containers, but must be running.
i := getInitContainerIndex(pod)
return i >= 0 &&
len(pod.Status.InitContainerStatuses)-1 >= i &&
pod.Status.InitContainerStatuses[i].State.Running != nil
(pod.Status.InitContainerStatuses[i].State.Running != nil || pod.Status.InitContainerStatuses[i].State.Waiting != nil)
}

func getInitContainerIndex(pod *corev1api.Pod) int {
Expand Down

0 comments on commit 2ee8d3a

Please sign in to comment.