github.com/gocrane/crane@v0.11.0/pkg/recommendation/recommender/idlenode/recommend.go (about)

     1  package idlenode
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gocrane/crane/pkg/recommendation/framework"
     7  )
     8  
     9  func (inr *IdleNodeRecommender) PreRecommend(ctx *framework.RecommendationContext) error {
    10  	return nil
    11  }
    12  
    13  func (inr *IdleNodeRecommender) Recommend(ctx *framework.RecommendationContext) error {
    14  	// check if all pods in Node are owned by DaemonSet
    15  	allDaemonSetPod := true
    16  	for _, pod := range ctx.Pods {
    17  		for _, ownRef := range pod.OwnerReferences {
    18  			if ownRef.Kind != "DaemonSet" {
    19  				allDaemonSetPod = false
    20  			}
    21  		}
    22  	}
    23  
    24  	if allDaemonSetPod {
    25  		ctx.Recommendation.Status.Action = "Delete"
    26  		ctx.Recommendation.Status.Description = "Node is owned by DaemonSet"
    27  		return nil
    28  	}
    29  
    30  	if inr.cpuUsageUtilization == 0 && inr.memoryUsageUtilization == 0 && inr.cpuRequestUtilization == 0 && inr.memoryRequestUtilization == 0 {
    31  		return fmt.Errorf("Node %s is not a idle node ", ctx.Object.GetName())
    32  	}
    33  
    34  	// check if cpu usage utilization lt config value
    35  	if inr.cpuUsageUtilization != 0 {
    36  		cpuUsageUtilization, err := inr.BaseRecommender.GetPercentile(inr.cpuPercentile, ctx.InputValue(cpuUsageUtilizationKey))
    37  		if err != nil {
    38  			return err
    39  		}
    40  		if cpuUsageUtilization > inr.cpuUsageUtilization {
    41  			return fmt.Errorf("Node %s is not a idle node, because the config value is %f, but the node cpu usage utilization is %f ", ctx.Object.GetName(), inr.cpuUsageUtilization, cpuUsageUtilization)
    42  		}
    43  	}
    44  
    45  	// check if memory usage utilization lt config value
    46  	if inr.memoryUsageUtilization != 0 {
    47  		memoryUsageUtilization, err := inr.BaseRecommender.GetPercentile(inr.memoryPercentile, ctx.InputValue(memoryUsageUtilizationKey))
    48  		if err != nil {
    49  			return err
    50  		}
    51  		if memoryUsageUtilization > inr.memoryUsageUtilization {
    52  			return fmt.Errorf("Node %s is not a idle node, because the config value is %f, but the node memory usage utilization is %f ", ctx.Object.GetName(), inr.memoryUsageUtilization, memoryUsageUtilization)
    53  		}
    54  	}
    55  
    56  	// check if cpu request utilization lt config value
    57  	if inr.cpuRequestUtilization != 0 {
    58  		cpuRequestUtilization, err := inr.BaseRecommender.GetPercentile(inr.cpuPercentile, ctx.InputValue(cpuRequestUtilizationKey))
    59  		if err != nil {
    60  			return err
    61  		}
    62  		if cpuRequestUtilization > inr.cpuRequestUtilization {
    63  			return fmt.Errorf("Node %s is not a idle node, because the config value is %f, but the node cpu request utilization is %f ", ctx.Object.GetName(), inr.cpuRequestUtilization, cpuRequestUtilization)
    64  		}
    65  	}
    66  
    67  	// check if memory request utilization lt config value
    68  	if inr.memoryRequestUtilization != 0 {
    69  		memoryRequestUtilization, err := inr.BaseRecommender.GetPercentile(inr.memoryPercentile, ctx.InputValue(memoryRequestUtilizationKey))
    70  		if err != nil {
    71  			return err
    72  		}
    73  		if memoryRequestUtilization > inr.memoryRequestUtilization {
    74  			return fmt.Errorf("Node %s is not a idle node, because the config value is %f, but the node memory request utilization is %f ", ctx.Object.GetName(), inr.memoryRequestUtilization, memoryRequestUtilization)
    75  		}
    76  	}
    77  
    78  	ctx.Recommendation.Status.Action = "Delete"
    79  	ctx.Recommendation.Status.Description = "Node resource utilization is low"
    80  	return nil
    81  }
    82  
    83  // Policy add some logic for result of recommend phase.
    84  func (inr *IdleNodeRecommender) Policy(ctx *framework.RecommendationContext) error {
    85  	return nil
    86  }