diff --git a/pkg/recommendation/framework/context.go b/pkg/recommendation/framework/context.go index 63c185104..ab6b600c4 100644 --- a/pkg/recommendation/framework/context.go +++ b/pkg/recommendation/framework/context.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "strings" "sync" jsonpatch "github.com/evanphx/json-patch" @@ -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 { @@ -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) @@ -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 diff --git a/pkg/recommendation/recommender/volumes/filter.go b/pkg/recommendation/recommender/volumes/filter.go index c116704bd..087399bed 100644 --- a/pkg/recommendation/recommender/volumes/filter.go +++ b/pkg/recommendation/recommender/volumes/filter.go @@ -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 } diff --git a/pkg/recommendation/recommender/volumes/registry.go b/pkg/recommendation/recommender/volumes/registry.go index a215c446e..be242a8d6 100644 --- a/pkg/recommendation/recommender/volumes/registry.go +++ b/pkg/recommendation/recommender/volumes/registry.go @@ -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), diff --git a/pkg/utils/pod.go b/pkg/utils/pod.go index cd3574408..505ec3c9d 100644 --- a/pkg/utils/pod.go +++ b/pkg/utils/pod.go @@ -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 { @@ -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