Skip to content

Commit

Permalink
Add labels as a criteria for volume policy
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
shubham-pampattiwar committed Feb 21, 2025
1 parent 9235fe1 commit e6493fc
Show file tree
Hide file tree
Showing 10 changed files with 521 additions and 34 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
28 changes: 26 additions & 2 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 All @@ -122,16 +135,27 @@ func (p *Policies) match(res *structuredVolume) *Action {
return nil
}

func (p *Policies) GetMatchAction(res any) (*Action, error) {
func (p *Policies) GetMatchAction(volumeRes any, pvcRes any) (*Action, error) {
volume := &structuredVolume{}
switch obj := res.(type) {
switch obj := volumeRes.(type) {
case *v1.PersistentVolume:
volume.parsePV(obj)
case *v1.Volume:
volume.parsePodVolume(obj)
default:
return nil, errors.New("failed to convert object")
}

// If a PVC is provided, set the pvcLabels on structured volume
if pvcRes != nil {
pvc, ok := pvcRes.(*v1.PersistentVolumeClaim)
if !ok {
return nil, errors.New("failed to convert object")
}
if pvc != nil && len(pvc.GetLabels()) > 0 {
volume.pvcLabels = pvc.Labels
}
}
return p.match(volume), nil
}

Expand Down
Loading

0 comments on commit e6493fc

Please sign in to comment.