Skip to content

Commit

Permalink
perf(volumes): optimize volumes recommender
Browse files Browse the repository at this point in the history
  • Loading branch information
lbbniu committed Jun 1, 2023
1 parent ed187a1 commit 2d85b53
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
11 changes: 10 additions & 1 deletion pkg/recommendation/recommender/volumes/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package volumes

import (
"github.com/gocrane/crane/pkg/recommendation/framework"
"github.com/gocrane/crane/pkg/utils"
)

// Filter out k8s resources that are not supported by the recommender.
Expand All @@ -13,7 +14,15 @@ func (vr *VolumesRecommender) Filter(ctx *framework.RecommendationContext) error
return err
}

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

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

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

Expand Down
14 changes: 14 additions & 0 deletions pkg/recommendation/recommender/volumes/recommend.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package volumes

import (
"fmt"

"github.com/gocrane/crane/pkg/recommendation/framework"
)

Expand All @@ -9,6 +11,18 @@ func (vr *VolumesRecommender) PreRecommend(ctx *framework.RecommendationContext)
}

func (vr *VolumesRecommender) Recommend(ctx *framework.RecommendationContext) error {
// Check if each volume is being used by any pods
isOrphanVolume := true
for _, pod := range ctx.Pods {
for _, volumeClaim := range pod.Spec.Volumes {
if volumeClaim.PersistentVolumeClaim != nil && volumeClaim.PersistentVolumeClaim.ClaimName == ctx.Object.GetName() {
isOrphanVolume = false
}
}
}
if !isOrphanVolume {
return fmt.Errorf("Volume %s is not an orphan volume ", ctx.Object.GetName())
}
ctx.Recommendation.Status.Action = "Delete"
ctx.Recommendation.Status.Description = "It is an Orphan Volumes"
return nil
Expand Down
45 changes: 17 additions & 28 deletions pkg/utils/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,41 +312,30 @@ func GetNodePods(kubeClient client.Client, nodeName string) ([]corev1.Pod, error
return podList.Items, nil
}

// GetOrphanVolumes returns Orphan Volumes
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 {
return nil, err
func GetNamespacePods(kubeClient client.Client, namespace string) ([]corev1.Pod, error) {
// Get a list of pods for a specified namespace
opts := []client.ListOption{
client.InNamespace(namespace),
}

// Get a list of all pods
pods := &corev1.PodList{}
if err := kubeClient.List(context.Background(), pods); err != nil {
if err := kubeClient.List(context.Background(), pods, opts...); err != nil {
return nil, err
}

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

return orphanVolumesName, nil
return pods.Items, nil
}

// volume is not being used by any pod
func isOrphanVolume(volume *corev1.PersistentVolume, pods *corev1.PodList) bool {
for _, pod := range pods.Items {
for _, volumeClaim := range pod.Spec.Volumes {
if volumeClaim.PersistentVolumeClaim != nil && volumeClaim.PersistentVolumeClaim.ClaimName == volume.Spec.ClaimRef.Name {
return false
}
}
// 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},
}
return true
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) {
Expand Down

0 comments on commit 2d85b53

Please sign in to comment.