github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-controller/app/options/resourcerecommender.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package options
    18  
    19  import (
    20  	"time"
    21  
    22  	cliflag "k8s.io/component-base/cli/flag"
    23  
    24  	"github.com/kubewharf/katalyst-core/pkg/config/controller"
    25  	"github.com/kubewharf/katalyst-core/pkg/controller/resource-recommend/datasource/prometheus"
    26  )
    27  
    28  type ResourceRecommenderOptions struct {
    29  	OOMRecordMaxNumber int `desc:"max number for oom record"`
    30  
    31  	HealthProbeBindPort string `desc:"The port the health probe binds to."`
    32  	MetricsBindPort     string `desc:"The port the metric endpoint binds to."`
    33  
    34  	// available datasource: prom
    35  	DataSource []string
    36  	// DataSourcePromConfig is the prometheus datasource config
    37  	DataSourcePromConfig prometheus.PromConfig
    38  
    39  	// LogVerbosityLevel to specify log verbosity level. (The default level is 4)
    40  	// Set it to something larger than 4 if more detailed logs are needed.
    41  	LogVerbosityLevel string
    42  }
    43  
    44  // NewResourceRecommenderOptions creates a new Options with a default config.
    45  func NewResourceRecommenderOptions() *ResourceRecommenderOptions {
    46  	return &ResourceRecommenderOptions{
    47  		OOMRecordMaxNumber:  5000,
    48  		HealthProbeBindPort: "8080",
    49  		MetricsBindPort:     "8081",
    50  		DataSource:          []string{"prom"},
    51  		DataSourcePromConfig: prometheus.PromConfig{
    52  			KeepAlive:                   60 * time.Second,
    53  			Timeout:                     3 * time.Minute,
    54  			BRateLimit:                  false,
    55  			MaxPointsLimitPerTimeSeries: 11000,
    56  		},
    57  		LogVerbosityLevel: "4",
    58  	}
    59  }
    60  
    61  // AddFlags adds flags to the specified FlagSet
    62  func (o *ResourceRecommenderOptions) AddFlags(fss *cliflag.NamedFlagSets) {
    63  	fs := fss.FlagSet("resource-recommend")
    64  	fs.IntVar(&o.OOMRecordMaxNumber, "oom-record-max-number", 5000, "Max number for oom records to store in configmap")
    65  
    66  	fs.StringVar(&o.HealthProbeBindPort, "resourcerecommend-health-probe-bind-port", "8080", "The port the health probe binds to.")
    67  	fs.StringVar(&o.MetricsBindPort, "resourcerecommend-metrics-bind-port", "8081", "The port the metric endpoint binds to.")
    68  
    69  	fs.StringSliceVar(&o.DataSource, "resourcerecommend-datasource", []string{"prom"}, "available datasource: prom")
    70  	fs.StringVar(&o.DataSourcePromConfig.Address, "resourcerecommend-prometheus-address", "", "prometheus address")
    71  	fs.StringVar(&o.DataSourcePromConfig.Auth.Type, "resourcerecommend-prometheus-auth-type", "", "prometheus auth type")
    72  	fs.StringVar(&o.DataSourcePromConfig.Auth.Username, "resourcerecommend-prometheus-auth-username", "", "prometheus auth username")
    73  	fs.StringVar(&o.DataSourcePromConfig.Auth.Password, "resourcerecommend-prometheus-auth-password", "", "prometheus auth password")
    74  	fs.StringVar(&o.DataSourcePromConfig.Auth.BearerToken, "resourcerecommend-prometheus-auth-bearertoken", "", "prometheus auth bearertoken")
    75  	fs.DurationVar(&o.DataSourcePromConfig.KeepAlive, "resourcerecommend-prometheus-keepalive", 60*time.Second, "prometheus keep alive")
    76  	fs.DurationVar(&o.DataSourcePromConfig.Timeout, "resourcerecommend-prometheus-timeout", 3*time.Minute, "prometheus timeout")
    77  	fs.BoolVar(&o.DataSourcePromConfig.BRateLimit, "resourcerecommend-prometheus-bratelimit", false, "prometheus bratelimit")
    78  	fs.IntVar(&o.DataSourcePromConfig.MaxPointsLimitPerTimeSeries, "resourcerecommend-prometheus-maxpoints", 11000, "prometheus max points limit per time series")
    79  	fs.StringVar(&o.DataSourcePromConfig.BaseFilter, "resourcerecommend-prometheus-promql-base-filter", "", ""+
    80  		"Get basic filters in promql for historical usage data. This filter is added to all promql statements. "+
    81  		"Supports filters format of promql, e.g: group=\\\"Katalyst\\\",cluster=\\\"cfeaf782fasdfe\\\"")
    82  }
    83  
    84  func (o *ResourceRecommenderOptions) ApplyTo(c *controller.ResourceRecommenderConfig) error {
    85  	c.OOMRecordMaxNumber = o.OOMRecordMaxNumber
    86  	c.HealthProbeBindPort = o.HealthProbeBindPort
    87  	c.MetricsBindPort = o.MetricsBindPort
    88  	c.DataSource = o.DataSource
    89  	c.DataSourcePromConfig = o.DataSourcePromConfig
    90  	c.LogVerbosityLevel = o.LogVerbosityLevel
    91  	return nil
    92  }
    93  
    94  func (o *ResourceRecommenderOptions) Config() (*controller.ResourceRecommenderConfig, error) {
    95  	c := &controller.ResourceRecommenderConfig{}
    96  	if err := o.ApplyTo(c); err != nil {
    97  		return nil, err
    98  	}
    99  	return c, nil
   100  }