Skip to content

Commit

Permalink
refactor volume recommender
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-song committed May 23, 2023
1 parent 4489184 commit 7c3e187
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
23 changes: 14 additions & 9 deletions pkg/recommendation/framework/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"

jsonpatch "github.com/evanphx/json-patch"
Expand Down Expand Up @@ -73,8 +74,6 @@ type RecommendationContext struct {
HPA *autoscalingv2.HorizontalPodAutoscaler
// HPA Object
EHPA *autoscalingapi.EffectiveHorizontalPodAutoscaler
// Orphan Volumes
Volumes []corev1.PersistentVolume
}

func NewRecommendationContext(context context.Context, identity ObjectIdentity, recommendationRule *v1alpha1.RecommendationRule, predictorMgr predictormgr.Manager, dataProviders map[providers.DataSourceType]providers.History, recommendation *v1alpha1.Recommendation, client client.Client, scaleClient scale.ScalesGetter, oomRecorder oom.Recorder) RecommendationContext {
Expand Down Expand Up @@ -180,6 +179,19 @@ func RetrieveScale(ctx *RecommendationContext) error {
return nil
}

func RetrieveVolumes(ctx *RecommendationContext) error {
if ctx.Recommendation.Spec.TargetRef.Kind == "PersistentVolume" {
volumes, err := utils.GetOrphanVolumes(ctx.Client)
if len(volumes) == 0 {
return err
}
str := strings.Join(volumes, ",")
ctx.Recommendation.Status.RecommendedValue = str
return err
}
return nil
}

func RetrievePods(ctx *RecommendationContext) error {
if ctx.Recommendation.Spec.TargetRef.Kind == "Node" {
pods, err := utils.GetNodePods(ctx.Client, ctx.Recommendation.Spec.TargetRef.Name)
Expand All @@ -194,13 +206,6 @@ func RetrievePods(ctx *RecommendationContext) error {
pods, err := utils.GetDaemonSetPods(ctx.Client, ctx.Recommendation.Spec.TargetRef.Namespace, ctx.Recommendation.Spec.TargetRef.Name)
ctx.Pods = pods
return err
} else if ctx.Recommendation.Spec.TargetRef.Kind == "PersistentVolume" {
volumes, err := utils.GetOrphanVolumes(ctx.Client)
if err != nil {
return err
}
ctx.Volumes = volumes
return nil
} else {
pods, err := utils.GetPodsFromScale(ctx.Client, ctx.Scale)
ctx.Pods = pods
Expand Down
6 changes: 3 additions & 3 deletions pkg/recommendation/recommender/volumes/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
)

// Filter out k8s resources that are not supported by the recommender.
func (rr *VolumesRecommender) Filter(ctx *framework.RecommendationContext) error {
func (vr *VolumesRecommender) Filter(ctx *framework.RecommendationContext) error {
var err error

// filter resource that not match objectIdentity
if err = rr.BaseRecommender.Filter(ctx); err != nil {
if err = vr.BaseRecommender.Filter(ctx); err != nil {
return err
}

if err = framework.RetrievePods(ctx); err != nil {
if err = framework.RetrieveVolumes(ctx); err != nil {
return err
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/recommendation/recommender/volumes/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ type VolumesRecommender struct {
base.BaseRecommender
}

func init() {
recommender.RegisterRecommenderProvider(recommender.VolumesRecommender, NewVolumesRecommender)
}

func (s *VolumesRecommender) Name() string {
return recommender.VolumesRecommender
}

// NewVolumesRecommender create a new Volumes recommender.
func NewServiceRecommender(recommender apis.Recommender, recommendationRule analysisv1alph1.RecommendationRule) (*VolumesRecommender, error) {
func NewVolumesRecommender(recommender apis.Recommender, recommendationRule analysisv1alph1.RecommendationRule) (recommender.Recommender, error) {
recommender = config.MergeRecommenderConfigFromRule(recommender, recommendationRule)
return &VolumesRecommender{
*base.NewBaseRecommender(recommender),
Expand Down
8 changes: 4 additions & 4 deletions pkg/utils/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func GetNodePods(kubeClient client.Client, nodeName string) ([]corev1.Pod, error
}

// GetOrphanVolumes returns Orphan Volumes
func GetOrphanVolumes(kubeClient client.Client) ([]corev1.PersistentVolume, error) {
func GetOrphanVolumes(kubeClient client.Client) ([]string, error) {
// Get a list of all volumes
volumes := &corev1.PersistentVolumeList{}
if err := kubeClient.List(context.Background(), volumes); err != nil {
Expand All @@ -327,14 +327,14 @@ func GetOrphanVolumes(kubeClient client.Client) ([]corev1.PersistentVolume, erro
}

// Check if each volume is being used by any pods
orphanVolumes := []corev1.PersistentVolume{}
orphanVolumesName := []string{}
for _, volume := range volumes.Items {
if isOrphanVolume(&volume, pods) {
orphanVolumes = append(orphanVolumes, volume)
orphanVolumesName = append(orphanVolumesName, volume.Spec.ClaimRef.Name)
}
}

return orphanVolumes, nil
return orphanVolumesName, nil
}

// volume is not being used by any pod
Expand Down

0 comments on commit 7c3e187

Please sign in to comment.