Skip to content

Commit

Permalink
fix: Orphan Volume judgment logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lbbniu committed Jun 13, 2023
1 parent 1301c1e commit 2b802c9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 26 deletions.
9 changes: 0 additions & 9 deletions pkg/recommendation/framework/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,6 @@ func RetrieveScale(ctx *RecommendationContext) error {
return nil
}

func RetrievePersistentVolumeClaims(ctx *RecommendationContext) error {
if ctx.Recommendation.Spec.TargetRef.Kind == "PersistentVolume" {
pvcs, err := utils.GetPersistentVolumeClaims(ctx.Client, ctx.Recommendation.Spec.TargetRef.Name)
ctx.PVCs = pvcs
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 Down
9 changes: 6 additions & 3 deletions pkg/recommendation/recommender/volumes/filter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package volumes

import (
corev1 "k8s.io/api/core/v1"

"github.com/gocrane/crane/pkg/recommendation/framework"
"github.com/gocrane/crane/pkg/utils"
)
Expand All @@ -14,15 +16,16 @@ func (vr *VolumesRecommender) Filter(ctx *framework.RecommendationContext) error
return err
}

if err = framework.RetrievePersistentVolumeClaims(ctx); err != nil {
var pv corev1.PersistentVolume
if err = framework.ObjectConversion(ctx.Object, &pv); err != nil {
return err
}

if len(ctx.PVCs) == 0 {
if pv.Spec.ClaimRef == nil {
return nil
}

if ctx.Pods, err = utils.GetNamespacePods(ctx.Client, ctx.PVCs[0].GetNamespace()); err != nil {
if ctx.Pods, err = utils.GetNamespacePods(ctx.Client, pv.Spec.ClaimRef.Namespace); err != nil {
return err
}

Expand Down
14 changes: 0 additions & 14 deletions pkg/utils/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,20 +324,6 @@ func GetNamespacePods(kubeClient client.Client, namespace string) ([]corev1.Pod,
return pods.Items, nil
}

// GetPersistentVolumeClaims returns Persistent Volume Claims
func GetPersistentVolumeClaims(kubeClient client.Client, name string) ([]corev1.PersistentVolumeClaim, error) {
// Get a list of bind pvc
opts := []client.ListOption{
client.MatchingFields{"spec.volumeName": name},
}
pvcList := &corev1.PersistentVolumeClaimList{}
if err := kubeClient.List(context.Background(), pvcList, opts...); err != nil {
return nil, err
}

return pvcList.Items, nil
}

func GetServicePods(kubeClient client.Client, svc *corev1.Service) ([]corev1.Pod, error) {
opts := []client.ListOption{
client.InNamespace(svc.Namespace),
Expand Down

0 comments on commit 2b802c9

Please sign in to comment.