diff --git a/changelogs/unreleased/6863-qiuming-best b/changelogs/unreleased/6863-qiuming-best new file mode 100644 index 00000000000..12e0d7356b8 --- /dev/null +++ b/changelogs/unreleased/6863-qiuming-best @@ -0,0 +1 @@ +Add volume types filter in resource policies diff --git a/internal/resourcepolicies/resource_policies.go b/internal/resourcepolicies/resource_policies.go index 956a06753c5..45b58c12c32 100644 --- a/internal/resourcepolicies/resource_policies.go +++ b/internal/resourcepolicies/resource_policies.go @@ -70,6 +70,7 @@ func (p *Policies) buildPolicy(resPolicies *resourcePolicies) error { volP.conditions = append(volP.conditions, &storageClassCondition{storageClass: con.StorageClass}) volP.conditions = append(volP.conditions, &nfsCondition{nfs: con.NFS}) volP.conditions = append(volP.conditions, &csiCondition{csi: con.CSI}) + volP.conditions = append(volP.conditions, &volumeTypeCondition{volumeTypes: con.VolumeTypes}) p.volumePolicies = append(p.volumePolicies, volP) } diff --git a/internal/resourcepolicies/resource_policies_test.go b/internal/resourcepolicies/resource_policies_test.go index 51b2a4f75f4..9126d1387af 100644 --- a/internal/resourcepolicies/resource_policies_test.go +++ b/internal/resourcepolicies/resource_policies_test.go @@ -355,6 +355,51 @@ volumePolicies: }, skip: false, }, + { + name: "match volume by types", + yamlData: `version: v1 +volumePolicies: +- conditions: + capacity: "0,100Gi" + volumeTypes: + - local + - hostPath + action: + type: skip`, + vol: &v1.PersistentVolume{ + Spec: v1.PersistentVolumeSpec{ + Capacity: v1.ResourceList{ + v1.ResourceStorage: resource.MustParse("1Gi"), + }, + PersistentVolumeSource: v1.PersistentVolumeSource{ + HostPath: &v1.HostPathVolumeSource{Path: "/mnt/data"}, + }, + }, + }, + skip: true, + }, + { + name: "dismatch volume by types", + yamlData: `version: v1 +volumePolicies: +- conditions: + capacity: "0,100Gi" + volumeTypes: + - local + action: + type: skip`, + vol: &v1.PersistentVolume{ + Spec: v1.PersistentVolumeSpec{ + Capacity: v1.ResourceList{ + v1.ResourceStorage: resource.MustParse("1Gi"), + }, + PersistentVolumeSource: v1.PersistentVolumeSource{ + HostPath: &v1.HostPathVolumeSource{Path: "/mnt/data"}, + }, + }, + }, + skip: false, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { diff --git a/internal/resourcepolicies/volume_resources.go b/internal/resourcepolicies/volume_resources.go index fc6fdb7d53a..ede8df3ef54 100644 --- a/internal/resourcepolicies/volume_resources.go +++ b/internal/resourcepolicies/volume_resources.go @@ -32,6 +32,7 @@ type structuredVolume struct { storageClass string nfs *nFSVolumeSource csi *csiVolumeSource + volumeType SupportedVolume } func (s *structuredVolume) parsePV(pv *corev1api.PersistentVolume) { @@ -46,6 +47,8 @@ func (s *structuredVolume) parsePV(pv *corev1api.PersistentVolume) { if csi != nil { s.csi = &csiVolumeSource{Driver: csi.Driver} } + + s.volumeType = getVolumeTypeFromPV(pv) } func (s *structuredVolume) parsePodVolume(vol *corev1api.Volume) { @@ -58,6 +61,8 @@ func (s *structuredVolume) parsePodVolume(vol *corev1api.Volume) { if csi != nil { s.csi = &csiVolumeSource{Driver: csi.Driver} } + + s.volumeType = getVolumeTypeFromVolume(vol) } type capacityCondition struct { diff --git a/internal/resourcepolicies/volume_resources_validator.go b/internal/resourcepolicies/volume_resources_validator.go index 7b4d0b815d4..720afccece9 100644 --- a/internal/resourcepolicies/volume_resources_validator.go +++ b/internal/resourcepolicies/volume_resources_validator.go @@ -23,10 +23,11 @@ type nFSVolumeSource struct { // volumeConditions defined the current format of conditions we parsed type volumeConditions struct { - Capacity string `yaml:"capacity,omitempty"` - StorageClass []string `yaml:"storageClass,omitempty"` - NFS *nFSVolumeSource `yaml:"nfs,omitempty"` - CSI *csiVolumeSource `yaml:"csi,omitempty"` + Capacity string `yaml:"capacity,omitempty"` + StorageClass []string `yaml:"storageClass,omitempty"` + NFS *nFSVolumeSource `yaml:"nfs,omitempty"` + CSI *csiVolumeSource `yaml:"csi,omitempty"` + VolumeTypes []SupportedVolume `yaml:"volumeTypes,omitempty"` } func (c *capacityCondition) validate() error { diff --git a/internal/resourcepolicies/volume_types_conditions.go b/internal/resourcepolicies/volume_types_conditions.go new file mode 100644 index 00000000000..df048886974 --- /dev/null +++ b/internal/resourcepolicies/volume_types_conditions.go @@ -0,0 +1,247 @@ +/* +Copyright the Velero contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package resourcepolicies + +import ( + corev1api "k8s.io/api/core/v1" +) + +type volumeTypeCondition struct { + volumeTypes []SupportedVolume +} + +type SupportedVolume string + +const ( + AWSAzureDisk SupportedVolume = "awsAzureDisk" + AWSElasticBlockStore SupportedVolume = "awsElasticBlockStore" + AzureDisk SupportedVolume = "azureDisk" + AzureFile SupportedVolume = "azureFile" + Cinder SupportedVolume = "cinder" + CephFS SupportedVolume = "cephfs" + ConfigMap SupportedVolume = "configMap" + CSI SupportedVolume = "csi" + DownwardAPI SupportedVolume = "downwardAPI" + EmptyDir SupportedVolume = "emptyDir" + Ephemeral SupportedVolume = "ephemeral" + FC SupportedVolume = "fc" + Flocker SupportedVolume = "flocker" + FlexVolume SupportedVolume = "flexVolume" + GitRepo SupportedVolume = "gitRepo" + Glusterfs SupportedVolume = "glusterfs" + GCEPersistentDisk SupportedVolume = "gcePersistentDisk" + HostPath SupportedVolume = "hostPath" + ISCSI SupportedVolume = "iscsi" + Local SupportedVolume = "local" + NFS SupportedVolume = "nfs" + PhotonPersistentDisk SupportedVolume = "photonPersistentDisk" + PortworxVolume SupportedVolume = "portworxVolume" + Projected SupportedVolume = "projected" + Quobyte SupportedVolume = "quobyte" + RBD SupportedVolume = "rbd" + ScaleIO SupportedVolume = "scaleIO" + Secret SupportedVolume = "secret" + StorageOS SupportedVolume = "storageOS" + VsphereVolume SupportedVolume = "vsphereVolume" +) + +func (v *volumeTypeCondition) match(s *structuredVolume) bool { + if len(v.volumeTypes) == 0 { + return true + } + + for _, vt := range v.volumeTypes { + if vt == s.volumeType { + return true + } + } + return false +} + +func (s *volumeTypeCondition) validate() error { + // validate by yamlv3 + return nil +} + +func getVolumeTypeFromPV(pv *corev1api.PersistentVolume) SupportedVolume { + if pv == nil { + return "" + } + + if pv.Spec.AWSElasticBlockStore != nil { + return AWSElasticBlockStore + } + if pv.Spec.AzureDisk != nil { + return AzureDisk + } + if pv.Spec.AzureFile != nil { + return AzureFile + } + if pv.Spec.CephFS != nil { + return CephFS + } + if pv.Spec.Cinder != nil { + return Cinder + } + if pv.Spec.CSI != nil { + return CSI + } + if pv.Spec.FC != nil { + return FC + } + if pv.Spec.Flocker != nil { + return Flocker + } + if pv.Spec.FlexVolume != nil { + return FlexVolume + } + if pv.Spec.GCEPersistentDisk != nil { + return GCEPersistentDisk + } + if pv.Spec.Glusterfs != nil { + return Glusterfs + } + if pv.Spec.HostPath != nil { + return HostPath + } + if pv.Spec.ISCSI != nil { + return ISCSI + } + if pv.Spec.Local != nil { + return Local + } + if pv.Spec.NFS != nil { + return NFS + } + if pv.Spec.PhotonPersistentDisk != nil { + return PhotonPersistentDisk + } + if pv.Spec.PortworxVolume != nil { + return PortworxVolume + } + if pv.Spec.Quobyte != nil { + return Quobyte + } + if pv.Spec.RBD != nil { + return RBD + } + if pv.Spec.ScaleIO != nil { + return ScaleIO + } + if pv.Spec.StorageOS != nil { + return StorageOS + } + if pv.Spec.VsphereVolume != nil { + return VsphereVolume + } + return "" +} + +func getVolumeTypeFromVolume(vol *corev1api.Volume) SupportedVolume { + if vol == nil { + return "" + } + + if vol.AWSElasticBlockStore != nil { + return AWSElasticBlockStore + } + if vol.AzureDisk != nil { + return AzureDisk + } + if vol.AzureFile != nil { + return AzureFile + } + if vol.CephFS != nil { + return CephFS + } + if vol.Cinder != nil { + return Cinder + } + if vol.CSI != nil { + return CSI + } + if vol.FC != nil { + return FC + } + if vol.Flocker != nil { + return Flocker + } + if vol.FlexVolume != nil { + return FlexVolume + } + if vol.GCEPersistentDisk != nil { + return GCEPersistentDisk + } + if vol.GitRepo != nil { + return GitRepo + } + if vol.Glusterfs != nil { + return Glusterfs + } + if vol.ISCSI != nil { + return ISCSI + } + if vol.NFS != nil { + return NFS + } + if vol.Secret != nil { + return Secret + } + if vol.RBD != nil { + return RBD + } + if vol.DownwardAPI != nil { + return DownwardAPI + } + if vol.ConfigMap != nil { + return ConfigMap + } + if vol.Projected != nil { + return Projected + } + if vol.Ephemeral != nil { + return Ephemeral + } + if vol.FC != nil { + return FC + } + if vol.PhotonPersistentDisk != nil { + return PhotonPersistentDisk + } + if vol.PortworxVolume != nil { + return PortworxVolume + } + if vol.Quobyte != nil { + return Quobyte + } + if vol.ScaleIO != nil { + return ScaleIO + } + if vol.StorageOS != nil { + return StorageOS + } + if vol.VsphereVolume != nil { + return VsphereVolume + } + if vol.HostPath != nil { + return HostPath + } + if vol.EmptyDir != nil { + return EmptyDir + } + return "" +} diff --git a/internal/resourcepolicies/volume_types_conditions_test.go b/internal/resourcepolicies/volume_types_conditions_test.go new file mode 100644 index 00000000000..7b7be97ee1f --- /dev/null +++ b/internal/resourcepolicies/volume_types_conditions_test.go @@ -0,0 +1,576 @@ +/* +Copyright the Velero contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package resourcepolicies + +import ( + "testing" + + corev1api "k8s.io/api/core/v1" +) + +func TestGetVolumeTypeFromPV(t *testing.T) { + testCases := []struct { + name string + inputPV *corev1api.PersistentVolume + expected SupportedVolume + }{ + { + name: "nil PersistentVolume", + inputPV: nil, + expected: "", + }, + { + name: "Test GCEPersistentDisk", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + GCEPersistentDisk: &corev1api.GCEPersistentDiskVolumeSource{}, + }, + }, + }, + expected: GCEPersistentDisk, + }, + { + name: "Test AWSElasticBlockStore", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + AWSElasticBlockStore: &corev1api.AWSElasticBlockStoreVolumeSource{}, + }, + }, + }, + expected: AWSElasticBlockStore, + }, + { + name: "Test HostPath", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + HostPath: &corev1api.HostPathVolumeSource{}, + }, + }, + }, + expected: HostPath, + }, + { + name: "Test Glusterfs", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + Glusterfs: &corev1api.GlusterfsPersistentVolumeSource{}, + }, + }, + }, + expected: Glusterfs, + }, + { + name: "Test NFS", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + NFS: &corev1api.NFSVolumeSource{}, + }, + }, + }, + expected: NFS, + }, + { + name: "Test RBD", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + RBD: &corev1api.RBDPersistentVolumeSource{}, + }, + }, + }, + expected: RBD, + }, + { + name: "Test ISCSI", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + ISCSI: &corev1api.ISCSIPersistentVolumeSource{}, + }, + }, + }, + expected: ISCSI, + }, + { + name: "Test Cinder", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + Cinder: &corev1api.CinderPersistentVolumeSource{}, + }, + }, + }, + expected: Cinder, + }, + { + name: "Test CephFS", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + CephFS: &corev1api.CephFSPersistentVolumeSource{}, + }, + }, + }, + expected: CephFS, + }, + { + name: "Test FC", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + FC: &corev1api.FCVolumeSource{}, + }, + }, + }, + expected: FC, + }, + { + name: "Test Flocker", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + Flocker: &corev1api.FlockerVolumeSource{}, + }, + }, + }, + expected: Flocker, + }, + { + name: "Test FlexVolume", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + FlexVolume: &corev1api.FlexPersistentVolumeSource{}, + }, + }, + }, + expected: FlexVolume, + }, + { + name: "Test AzureFile", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + AzureFile: &corev1api.AzureFilePersistentVolumeSource{}, + }, + }, + }, + expected: AzureFile, + }, + { + name: "Test VsphereVolume", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + VsphereVolume: &corev1api.VsphereVirtualDiskVolumeSource{}, + }, + }, + }, + expected: VsphereVolume, + }, + { + name: "Test Quobyte", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + Quobyte: &corev1api.QuobyteVolumeSource{}, + }, + }, + }, + expected: Quobyte, + }, + { + name: "Test AzureDisk", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + AzureDisk: &corev1api.AzureDiskVolumeSource{}, + }, + }, + }, + expected: AzureDisk, + }, + { + name: "Test PhotonPersistentDisk", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + PhotonPersistentDisk: &corev1api.PhotonPersistentDiskVolumeSource{}, + }, + }, + }, + expected: PhotonPersistentDisk, + }, + { + name: "Test PortworxVolume", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + PortworxVolume: &corev1api.PortworxVolumeSource{}, + }, + }, + }, + expected: PortworxVolume, + }, + { + name: "Test ScaleIO", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + ScaleIO: &corev1api.ScaleIOPersistentVolumeSource{}, + }, + }, + }, + expected: ScaleIO, + }, + { + name: "Test Local", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + Local: &corev1api.LocalVolumeSource{}, + }, + }, + }, + expected: Local, + }, + { + name: "Test StorageOS", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + StorageOS: &corev1api.StorageOSPersistentVolumeSource{}, + }, + }, + }, + expected: StorageOS, + }, + { + name: "Test CSI", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{ + PersistentVolumeSource: corev1api.PersistentVolumeSource{ + CSI: &corev1api.CSIPersistentVolumeSource{}, + }, + }, + }, + expected: CSI, + }, + { + name: "Test Unknown Source", + inputPV: &corev1api.PersistentVolume{ + Spec: corev1api.PersistentVolumeSpec{}, + }, + expected: "", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := getVolumeTypeFromPV(tc.inputPV) + if result != tc.expected { + t.Errorf("Expected %s, but got %s", tc.expected, result) + } + }) + } +} + +func TestGetVolumeTypeFromVolume(t *testing.T) { + testCases := []struct { + name string + inputVol *corev1api.Volume + expected SupportedVolume + }{ + { + name: "nil Volume", + inputVol: nil, + expected: "", + }, + { + name: "Test Unknown Source", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{}, + }, + expected: "", + }, + { + name: "Test HostPath", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + HostPath: &corev1api.HostPathVolumeSource{}, + }, + }, + expected: HostPath, + }, + { + name: "Test EmptyDir", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + EmptyDir: &corev1api.EmptyDirVolumeSource{}, + }, + }, + expected: EmptyDir, + }, + { + name: "Test GCEPersistentDisk", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + GCEPersistentDisk: &corev1api.GCEPersistentDiskVolumeSource{}, + }, + }, + expected: GCEPersistentDisk, + }, + { + name: "Test AWSElasticBlockStore", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + AWSElasticBlockStore: &corev1api.AWSElasticBlockStoreVolumeSource{}, + }, + }, + expected: AWSElasticBlockStore, + }, + { + name: "Test GitRepo", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + GitRepo: &corev1api.GitRepoVolumeSource{}, + }, + }, + expected: GitRepo, + }, + { + name: "Test Secret", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + Secret: &corev1api.SecretVolumeSource{}, + }, + }, + expected: Secret, + }, + { + name: "Test NFS", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + NFS: &corev1api.NFSVolumeSource{}, + }, + }, + expected: NFS, + }, + { + name: "Test ISCSI", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + ISCSI: &corev1api.ISCSIVolumeSource{}, + }, + }, + expected: ISCSI, + }, + { + name: "Test Glusterfs", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + Glusterfs: &corev1api.GlusterfsVolumeSource{}, + }, + }, + expected: Glusterfs, + }, + { + name: "Test RBD", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + RBD: &corev1api.RBDVolumeSource{}, + }, + }, + expected: RBD, + }, + { + name: "Test FlexVolume", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + FlexVolume: &corev1api.FlexVolumeSource{}, + }, + }, + expected: FlexVolume, + }, + { + name: "Test Cinder", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + Cinder: &corev1api.CinderVolumeSource{}, + }, + }, + expected: Cinder, + }, + { + name: "Test CephFS", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + CephFS: &corev1api.CephFSVolumeSource{}, + }, + }, + expected: CephFS, + }, + { + name: "Test Flocker", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + Flocker: &corev1api.FlockerVolumeSource{}, + }, + }, + expected: Flocker, + }, + { + name: "Test DownwardAPI", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + DownwardAPI: &corev1api.DownwardAPIVolumeSource{}, + }, + }, + expected: DownwardAPI, + }, + { + name: "Test FC", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + FC: &corev1api.FCVolumeSource{}, + }, + }, + expected: FC, + }, + { + name: "Test AzureFile", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + AzureFile: &corev1api.AzureFileVolumeSource{}, + }, + }, + expected: AzureFile, + }, + { + name: "Test ConfigMap", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + ConfigMap: &corev1api.ConfigMapVolumeSource{}, + }, + }, + expected: ConfigMap, + }, + { + name: "Test VsphereVolume", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + VsphereVolume: &corev1api.VsphereVirtualDiskVolumeSource{}, + }, + }, + expected: VsphereVolume, + }, + { + name: "Test Quobyte", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + Quobyte: &corev1api.QuobyteVolumeSource{}, + }, + }, + expected: Quobyte, + }, + { + name: "Test AzureDisk", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + AzureDisk: &corev1api.AzureDiskVolumeSource{}, + }, + }, + expected: AzureDisk, + }, + { + name: "Test PhotonPersistentDisk", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + PhotonPersistentDisk: &corev1api.PhotonPersistentDiskVolumeSource{}, + }, + }, + expected: PhotonPersistentDisk, + }, + { + name: "Test Projected", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + Projected: &corev1api.ProjectedVolumeSource{}, + }, + }, + expected: Projected, + }, + { + name: "Test PortworxVolume", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + PortworxVolume: &corev1api.PortworxVolumeSource{}, + }, + }, + expected: PortworxVolume, + }, + { + name: "Test ScaleIO", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + ScaleIO: &corev1api.ScaleIOVolumeSource{}, + }, + }, + expected: ScaleIO, + }, + { + name: "Test StorageOS", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + StorageOS: &corev1api.StorageOSVolumeSource{}, + }, + }, + expected: StorageOS, + }, + { + name: "Test CSI", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + CSI: &corev1api.CSIVolumeSource{}, + }, + }, + expected: CSI, + }, + { + name: "Test Ephemeral", + inputVol: &corev1api.Volume{ + VolumeSource: corev1api.VolumeSource{ + Ephemeral: &corev1api.EphemeralVolumeSource{}, + }, + }, + expected: Ephemeral, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := getVolumeTypeFromVolume(tc.inputVol) + if result != tc.expected { + t.Errorf("Expected %s, but got %s", tc.expected, result) + } + }) + } +} diff --git a/site/content/docs/main/resource-filtering.md b/site/content/docs/main/resource-filtering.md index 3edf69b12b3..3a33887812e 100644 --- a/site/content/docs/main/resource-filtering.md +++ b/site/content/docs/main/resource-filtering.md @@ -244,8 +244,8 @@ Velero only support volume resource policies currently, other kinds of resource driver: aws.ebs.csi.driver # pv matches one of the storage class list storageClass: - - gp2 - - standard + - gp2 + - standard action: type: skip - conditions: @@ -271,6 +271,14 @@ Velero only support volume resource policies currently, other kinds of resource csi: {} action: type: skip + - conditions: + volumeTypes: + - emptyDir + - downwardAPI + - configmap + - cinder + action: + type: skip ``` **Supported conditions** @@ -318,6 +326,19 @@ Velero supported conditions and format listed below: ``` For volume provisioned by [Persistent Volumes](https://kubernetes.io/docs/concepts/storage/persistent-volumes) support all above attributes, but for pod [Volume](https://kubernetes.io/docs/concepts/storage/volumes) only support filtered by volume source. +- volume types + + Support filter volumes by types + ```yaml + volumeTypes: + # matches volumes listed below + - emptyDir + - downwardAPI + - configmap + - cinder + ``` + Volume types could be found in [Persistent Volumes](https://kubernetes.io/docs/concepts/storage/persistent-volumes) and pod [Volume](https://kubernetes.io/docs/concepts/storage/volumes) + **Resource policies rules** - Velero already has lots of include or exclude filters. the resource policies are the final filters after others include or exclude filters in one backup processing workflow. So if use a defined similar filter like the opt-in approach to backup one pod volume but skip backup of the same pod volume in resource policies, as resource policies are the final filters that are applied, the volume will not be backed up. - If volume resource policies conflict with themselves the first matched policy will be respected when many policies are defined. \ No newline at end of file