Project: gocrane (Observability and Analysis / Continuous Optimization)
File: how-to-develop-recommender.md

title: "How to develop Recommender" description: "Introduce how to develop and extend Recommender based on framework" weight: 100 Recommendation Framework provides an extensible framework for Recommender and supports several built-in Recommender. Users can implement a self-defined Recommender or modify the existing Recommenders. ```go type Recommender interface { Name() string framework.Filter framework.PrePrepare framework.Prepare framework.PostPrepare framework.PreRecommend framework.Recommend framework.PostRecommend framework.Observe } // Phase: Filter // Filter interface type Filter interface { // The Filter will filter resource can`t be recommended via target recommender. Filter(ctx *RecommendationContext) error } // Phase: Prepare // PrePrepare interface type PrePrepare interface { CheckDataProviders(ctx *RecommendationContext) error } // Prepare interface type Prepare interface { CollectData(ctx *RecommendationContext) error } type PostPrepare interface { PostProcessing(ctx *RecommendationContext) error } // PreRecommend interface type PreRecommend interface { PreRecommend(ctx *RecommendationContext) error } // Phase: Recommend // Recommend interface type Recommend interface { Recommend(ctx *RecommendationContext) error } // PostRecommend interface type PostRecommend interface { Policy(ctx *RecommendationContext) error } // Phase: Observe // Observe interface type Observe interface { Observe(ctx *RecommendationContext) error } ``` Recommender interface defines four stages and eight extension points that need to be implemented in recommender. These extension points are called sequentially during the recommendation process. Some of these extension points can change recommendation decisions, while others are only give information. The whole recommendation process is divided into four phases: FilterPrepareRecommendObservePhase's input is the Kubernetes resource to analysisoutput is the recommendation advise. Let's begin to introduce the inputs, outputs, and capabilities of each phase. `RecommendationContext` saved the context for a recommended process, including recommended target, RecommendationConfiguration etc., the user can add more content as needed. The Filter phase is used to preprocess the recommendation data. In general, it is necessary to decide whether the recommendation target matches Recommender during preprocessing. For example, the Resource Recommender only supports handling Workload (Deployment, StatefulSet). In addition, it can also determine whether the recommended target state is suitable for recommendation, such as whether it is being deleted or just created. The recommendation will be terminated when return error. BaseRecommender implements basic preprocessing functions and users can call it to inherit related functions. The Prepare phase is used for data preparation, requesting an external monitoring system and saving the timing data in the context. PrePrepare extension point used to check the connection status of the monitoring system. Prepare extension point used to query time series data. The PostPrepare extension point is used to process time series data, such as abnormal cold start data, partial data loss, data aggregation, and clearing abnormal data. The Recommend phase is used to optimize recommendations based on timing data and resource allocation. The type of optimization recommendation depends on the type of recommendation. For example, if it is a resource recommendation, then the output is the resource configuration for the kubernetes workload. The Recommend extension point is used to analyze and calculate the data using Crane's algorithm module, and the analysis result is finally processed in the PostRecommend stage. Users can customize it by implement their Recommend phase. The Observe phase is used to observe the recommendation result. For example, when recommending a resource, the information about the optimization proposal is saved to the monitoring system via Metric, and the revenue generated by the optimization proposal is observed through the Dashboard.