Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Orphan Volume judgment logic #821

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions pkg/recommendation/framework/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ type RecommendationContext struct {
Scale *autoscalingapiv1.Scale
// Pods in recommendation
Pods []corev1.Pod
// PVCs in recommendation
PVCs []corev1.PersistentVolumeClaim
// HPA Object
HPA *autoscalingv2.HorizontalPodAutoscaler
// HPA Object
Expand Down Expand Up @@ -180,15 +178,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
12 changes: 8 additions & 4 deletions pkg/recommendation/recommender/volumes/recommend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package volumes
import (
"fmt"

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

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

Expand All @@ -13,15 +15,17 @@ 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
var pv corev1.PersistentVolume
if err := framework.ObjectConversion(ctx.Object, &pv); err != nil {
return err
}
for _, pod := range ctx.Pods {
for _, volumeClaim := range pod.Spec.Volumes {
if volumeClaim.PersistentVolumeClaim == nil {
continue
}
for _, pvc := range ctx.PVCs {
if volumeClaim.PersistentVolumeClaim.ClaimName == pvc.Name {
isOrphanVolume = false
}
if volumeClaim.PersistentVolumeClaim.ClaimName == pv.Spec.ClaimRef.Name {
isOrphanVolume = false
}
}
}
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