mirror of
https://github.com/vmware-tanzu/velero.git
synced 2025-12-23 06:15:21 +00:00
Get pod list once per namespace in pvc IBA
Signed-off-by: Scott Seago <sseago@redhat.com>
This commit is contained in:
1
changelogs/unreleased/9226-sseago
Normal file
1
changelogs/unreleased/9226-sseago
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Get pod list once per namespace in pvc IBA
|
||||||
@@ -39,6 +39,8 @@ import (
|
|||||||
type PVCAction struct {
|
type PVCAction struct {
|
||||||
log logrus.FieldLogger
|
log logrus.FieldLogger
|
||||||
crClient crclient.Client
|
crClient crclient.Client
|
||||||
|
// map[namespace]->[map[pvcVolumes]->[]podName]
|
||||||
|
nsPVCs map[string]map[string][]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPVCAction(f client.Factory) plugincommon.HandlerInitializer {
|
func NewPVCAction(f client.Factory) plugincommon.HandlerInitializer {
|
||||||
@@ -78,31 +80,18 @@ func (a *PVCAction) GetRelatedItems(item runtime.Unstructured, backup *v1.Backup
|
|||||||
|
|
||||||
// Adds pods mounting this PVC to ensure that multiple pods mounting the same RWX
|
// Adds pods mounting this PVC to ensure that multiple pods mounting the same RWX
|
||||||
// volume get backed up together.
|
// volume get backed up together.
|
||||||
pods := new(corev1api.PodList)
|
pvcs, err := a.getPVCList(pvc.Namespace)
|
||||||
err := a.crClient.List(context.Background(), pods, crclient.InNamespace(pvc.Namespace))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to list pods")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range pods.Items {
|
for _, pod := range pvcs[pvc.Name] {
|
||||||
for _, volume := range pods.Items[i].Spec.Volumes {
|
a.log.Infof("Adding related Pod %s to PVC %s", pod, pvc.Name)
|
||||||
if volume.VolumeSource.PersistentVolumeClaim == nil {
|
relatedItems = append(relatedItems, velero.ResourceIdentifier{
|
||||||
continue
|
GroupResource: kuberesource.Pods,
|
||||||
}
|
Namespace: pvc.Namespace,
|
||||||
if volume.PersistentVolumeClaim.ClaimName == pvc.Name {
|
Name: pod,
|
||||||
if kube.IsPodRunning(&pods.Items[i]) != nil {
|
})
|
||||||
a.log.Infof("Related pod %s is not running, not adding to ItemBlock for PVC %s", pods.Items[i].Name, pvc.Name)
|
|
||||||
} else {
|
|
||||||
a.log.Infof("Adding related Pod %s to PVC %s", pods.Items[i].Name, pvc.Name)
|
|
||||||
relatedItems = append(relatedItems, velero.ResourceIdentifier{
|
|
||||||
GroupResource: kuberesource.Pods,
|
|
||||||
Namespace: pods.Items[i].Namespace,
|
|
||||||
Name: pods.Items[i].Name,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gather groupedPVCs based on VGS label provided in the backup
|
// Gather groupedPVCs based on VGS label provided in the backup
|
||||||
@@ -117,6 +106,35 @@ func (a *PVCAction) GetRelatedItems(item runtime.Unstructured, backup *v1.Backup
|
|||||||
return relatedItems, nil
|
return relatedItems, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *PVCAction) getPVCList(ns string) (map[string][]string, error) {
|
||||||
|
if a.nsPVCs == nil {
|
||||||
|
a.nsPVCs = make(map[string]map[string][]string)
|
||||||
|
}
|
||||||
|
pvcList, ok := a.nsPVCs[ns]
|
||||||
|
if ok {
|
||||||
|
return pvcList, nil
|
||||||
|
}
|
||||||
|
pvcList = make(map[string][]string)
|
||||||
|
pods := new(corev1api.PodList)
|
||||||
|
err := a.crClient.List(context.Background(), pods, crclient.InNamespace(ns))
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed to list pods")
|
||||||
|
}
|
||||||
|
for i := range pods.Items {
|
||||||
|
if kube.IsPodRunning(&pods.Items[i]) != nil {
|
||||||
|
a.log.Debugf("Pod %s is not running, not adding to Pod list for PVC IBA plugin", pods.Items[i].Name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, volume := range pods.Items[i].Spec.Volumes {
|
||||||
|
if volume.VolumeSource.PersistentVolumeClaim != nil {
|
||||||
|
pvcList[volume.VolumeSource.PersistentVolumeClaim.ClaimName] = append(pvcList[volume.VolumeSource.PersistentVolumeClaim.ClaimName], pods.Items[i].Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a.nsPVCs[ns] = pvcList
|
||||||
|
return pvcList, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (a *PVCAction) Name() string {
|
func (a *PVCAction) Name() string {
|
||||||
return "PVCItemBlockAction"
|
return "PVCItemBlockAction"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user