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

     1  package base
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/lithammer/fuzzysearch/fuzzy"
     9  
    10  	"github.com/gocrane/crane/pkg/providers"
    11  	"github.com/gocrane/crane/pkg/providers/prom"
    12  	"github.com/gocrane/crane/pkg/recommendation/config"
    13  	"github.com/gocrane/crane/pkg/recommendation/framework"
    14  )
    15  
    16  // CheckDataProviders in PrePrepare phase, will create data source provider via your recommendation config.
    17  func (br *BaseRecommender) CheckDataProviders(ctx *framework.RecommendationContext) error {
    18  	// 1. load data provider from recommendation config, override the default data source
    19  	configSet := br.Recommender.Config
    20  	// replicas recommender only need history data provider
    21  	// History data source
    22  	// metricserver can't collect history data
    23  	// default is prometheus, you can override the provider to grpc or override the prometheus config
    24  	// TODO(xieydd) Load cache data source provider if config is not changed.
    25  	configKeys := config.GetKeysOfMap(configSet)
    26  	promKeys := fuzzy.FindFold(string(providers.PrometheusDataSource), configKeys)
    27  	dataSourceKeys := fuzzy.FindFold(providers.DataSourceTypeKey, configKeys)
    28  	//grpcKeys := fuzzy.FindFold(string(providers.GrpcDataSource), configKeys)
    29  
    30  	if len(dataSourceKeys) == 0 {
    31  		return nil
    32  	}
    33  	dataSourceType := dataSourceKeys[0]
    34  
    35  	if dataSourceType != string(providers.PrometheusDataSource) {
    36  		return fmt.Errorf("in replicas recommender, only suppport prometheus history data source")
    37  	}
    38  
    39  	if len(promKeys) != 0 {
    40  		return fmt.Errorf("in replicas recommender, you need set prometheus config %v for history data provider", providers.PrometheusConfigKeys)
    41  	}
    42  
    43  	mustSetConfig := []string{"prometheus-address", "prometheus-auth-username", "prometheus-auth-password", "prometheus-auth-bearertoken"}
    44  	if config.SlicesContainSlice(promKeys, mustSetConfig) {
    45  		return fmt.Errorf("in replicas recommender, you need set prometheus config %v for history data provider", mustSetConfig)
    46  	}
    47  	timeOut := 3 * time.Minute
    48  	if value, ok := configSet["prometheus-timeout"]; ok {
    49  		timeOut, _ = time.ParseDuration(value)
    50  	}
    51  
    52  	aliveTime := 60 * time.Second
    53  	if value, ok := configSet["prometheus-keepalive"]; ok {
    54  		aliveTime, _ = time.ParseDuration(value)
    55  	}
    56  
    57  	concurrency := 10
    58  	if value, ok := configSet["prometheus-query-concurrency"]; ok {
    59  		concurrency, _ = strconv.Atoi(value)
    60  	}
    61  
    62  	maxPoints := 11000
    63  	if value, ok := configSet["prometheus-maxpoints"]; ok {
    64  		maxPoints, _ = strconv.Atoi(value)
    65  	}
    66  	promConfig := providers.PromConfig{
    67  		Address:            configSet["prometheus-address"],
    68  		Timeout:            timeOut,
    69  		KeepAlive:          aliveTime,
    70  		InsecureSkipVerify: configSet["prometheus-insecure-skip-verify"] == "true",
    71  		Auth: providers.ClientAuth{
    72  			Username:    configSet["prometheus-auth-username"],
    73  			Password:    configSet["prometheus-auth-password"],
    74  			BearerToken: configSet["prometheus-auth-bearertoken"],
    75  		},
    76  		QueryConcurrency:            concurrency,
    77  		BRateLimit:                  configSet["prometheus-bratelimit"] == "true",
    78  		MaxPointsLimitPerTimeSeries: maxPoints,
    79  	}
    80  	promDataProvider, err := prom.NewProvider(&promConfig)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	ctx.DataProviders = map[providers.DataSourceType]providers.History{
    85  		providers.PrometheusDataSource: promDataProvider,
    86  	}
    87  
    88  	// will be use in future.
    89  	// if no data provider config set, use default history data provider
    90  	/*
    91  		if len(dataSourceKeys) > 0 {
    92  			switch dataSourceType {
    93  			case string(providers.PrometheusDataSource):
    94  				if len(promKeys) != 0 {
    95  					return fmt.Errorf("in replicas recommender, you need set prometheus config %v for history data provider", providers.PrometheusConfigKeys)
    96  				}
    97  
    98  				mustSetConfig := []string{"prometheus-address", "prometheus-auth-username", "prometheus-auth-password", "prometheus-auth-bearertoken"}
    99  				if recommendation.SlicesContainSlice(promKeys, mustSetConfig) {
   100  					return fmt.Errorf("in replicas recommender, you need set prometheus config %v for history data provider", mustSetConfig)
   101  				}
   102  				timeOut := 3 * time.Minute
   103  				if value, ok := configSet["prometheus-timeout"]; ok {
   104  					timeOut, _ = time.ParseDuration(value)
   105  				}
   106  
   107  				aliveTime := 60 * time.Second
   108  				if value, ok := configSet["prometheus-keepalive"]; ok {
   109  					aliveTime, _ = time.ParseDuration(value)
   110  				}
   111  
   112  				concurrency := 10
   113  				if value, ok := configSet["prometheus-query-concurrency"]; ok {
   114  					concurrency, _ = strconv.Atoi(value)
   115  				}
   116  
   117  				maxPoints := 11000
   118  				if value, ok := configSet["prometheus-maxpoints"]; ok {
   119  					maxPoints, _ = strconv.Atoi(value)
   120  				}
   121  				promConfig := providers.PromConfig{
   122  					Address:            configSet["prometheus-address"],
   123  					Timeout:            timeOut,
   124  					KeepAlive:          aliveTime,
   125  					InsecureSkipVerify: configSet["prometheus-insecure-skip-verify"] == "true",
   126  					Auth: providers.ClientAuth{
   127  						Username:    configSet["prometheus-auth-username"],
   128  						Password:    configSet["prometheus-auth-password"],
   129  						BearerToken: configSet["prometheus-auth-bearertoken"],
   130  					},
   131  					QueryConcurrency:            concurrency,
   132  					BRateLimit:                  configSet["prometheus-bratelimit"] == "true",
   133  					MaxPointsLimitPerTimeSeries: maxPoints,
   134  				}
   135  				promDataProvider, err := prom.NewProvider(&promConfig)
   136  				if err != nil {
   137  					return err
   138  				}
   139  				ctx.DataProviders = map[providers.DataSourceType]providers.Interface{
   140  					providers.PrometheusDataSource: promDataProvider,
   141  				}
   142  			case string(providers.GrpcDataSource):
   143  				// not support grpc yet
   144  				if len(grpcKeys) != 0 {
   145  					return fmt.Errorf("in replicas recommender, you need set grpc config %v for history data provider", providers.PrometheusConfigKeys)
   146  				}
   147  
   148  				timeOut := time.Minute
   149  				if value, ok := configSet["grpc-ds-timeout"]; ok {
   150  					timeOut, _ = time.ParseDuration(value)
   151  				}
   152  
   153  				address := "localhost:50051"
   154  				if value, ok := configSet["grpc-ds-address"]; ok {
   155  					address = value
   156  				}
   157  
   158  				grpcConfig := providers.GrpcConfig{
   159  					Address: address,
   160  					Timeout: timeOut,
   161  				}
   162  				grpcDataProvider := grpc.NewProvider(&grpcConfig)
   163  				ctx.DataProviders = map[providers.DataSourceType]providers.Interface{
   164  					providers.GrpcDataSource: grpcDataProvider,
   165  				}
   166  			default:
   167  				return fmt.Errorf("replicas recommender only support %v and %v provider", providers.PrometheusDataSource, providers.GrpcDataSource)
   168  			}
   169  		}
   170  	*/
   171  
   172  	// 2. if not set data provider, will use default
   173  	// do nothing
   174  	return nil
   175  }
   176  
   177  func (br *BaseRecommender) CollectData(ctx *framework.RecommendationContext) error {
   178  	return nil
   179  }
   180  
   181  func (br *BaseRecommender) PostProcessing(ctx *framework.RecommendationContext) error {
   182  	return nil
   183  }