Skip to content

Commit

Permalink
Add labels as a criteria for volume policy (#8713)
Browse files Browse the repository at this point in the history
* Add labels as a criteria for volume policy

Signed-off-by: Shubham Pampattiwar <[email protected]>

add changelog file

Signed-off-by: Shubham Pampattiwar <[email protected]>

handle err

Signed-off-by: Shubham Pampattiwar <[email protected]>

use labels selector.matches

Signed-off-by: Shubham Pampattiwar <[email protected]>

make update

Signed-off-by: Shubham Pampattiwar <[email protected]>

remove fetching pvc from volume policy filtering

Signed-off-by: Shubham Pampattiwar <[email protected]>

add more ut coverage

Signed-off-by: Shubham Pampattiwar <[email protected]>

* minor updates

Signed-off-by: Shubham Pampattiwar <[email protected]>

use VolumeFilterData struct in GetMatchAction func

Signed-off-by: Shubham Pampattiwar <[email protected]>

update parsePVC func and add more ut

Signed-off-by: Shubham Pampattiwar <[email protected]>

lint fix

Signed-off-by: Shubham Pampattiwar <[email protected]>

---------

Signed-off-by: Shubham Pampattiwar <[email protected]>
  • Loading branch information
shubham-pampattiwar authored Feb 26, 2025
1 parent a45c9f2 commit 0eb1040
Show file tree
Hide file tree
Showing 13 changed files with 861 additions and 37 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/8713-shubham-pampattiwar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add labels as a criteria for volume policy
35 changes: 30 additions & 5 deletions internal/resourcepolicies/resource_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ func unmarshalResourcePolicies(yamlData *string) (*ResourcePolicies, error) {
if err != nil {
return nil, fmt.Errorf("failed to decode yaml data into resource policies %v", err)
}

for _, vp := range resPolicies.VolumePolicies {
if raw, ok := vp.Conditions["pvcLabels"]; ok {
switch raw.(type) {
case map[string]any, map[string]string:
default:
return nil, fmt.Errorf("pvcLabels must be a map of string to string, got %T", raw)
}
}
}
return resPolicies, nil
}

Expand All @@ -96,6 +106,9 @@ func (p *Policies) BuildPolicy(resPolicies *ResourcePolicies) error {
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})
if con.PVCLabels != nil && len(con.PVCLabels) > 0 {
volP.conditions = append(volP.conditions, &pvcLabelsCondition{labels: con.PVCLabels})
}
p.volumePolicies = append(p.volumePolicies, volP)
}

Expand Down Expand Up @@ -123,15 +136,27 @@ func (p *Policies) match(res *structuredVolume) *Action {
}

func (p *Policies) GetMatchAction(res any) (*Action, error) {
data, ok := res.(VolumeFilterData)
if !ok {
return nil, errors.New("failed to convert input to VolumeFilterData")
}

volume := &structuredVolume{}
switch obj := res.(type) {
case *v1.PersistentVolume:
volume.parsePV(obj)
case *v1.Volume:
volume.parsePodVolume(obj)
switch {
case data.PersistentVolume != nil:
volume.parsePV(data.PersistentVolume)
if data.PVC != nil {
volume.parsePVC(data.PVC)
}
case data.PodVolume != nil:
volume.parsePodVolume(data.PodVolume)
if data.PVC != nil {
volume.parsePVC(data.PVC)
}
default:
return nil, errors.New("failed to convert object")
}

return p.match(volume), nil
}

Expand Down
Loading

0 comments on commit 0eb1040

Please sign in to comment.