Skip to content

Commit

Permalink
Merge pull request #653 from saikey0379/main
Browse files Browse the repository at this point in the history
Add prometheus-adapter-extension-labels for global prometheus expression
  • Loading branch information
qmhu authored Dec 15, 2022
2 parents f0e61c9 + 06e3c29 commit 2fcdce0
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 69 deletions.
3 changes: 2 additions & 1 deletion cmd/craned/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,13 @@ func initControllers(oomRecorder oom.Recorder, mgr ctrl.Manager, opts *options.O
}).SetupWithManager(mgr); err != nil {
klog.Exit(err, "unable to create controller", "controller", "PromAdapterConfigMapController")
}
prometheus_adapter.SetExtensionLabels(opts.DataSourcePromConfig.AdapterExtensionLabels)
} else if opts.DataSourcePromConfig.AdapterConfig != "" {
// PrometheusAdapterConfigFetcher
pac := &prometheus_adapter.PrometheusAdapterConfigFetcher{
AdapterConfig: opts.DataSourcePromConfig.AdapterConfig,
}

prometheus_adapter.SetExtensionLabels(opts.DataSourcePromConfig.AdapterExtensionLabels)
go pac.Reload()
}

Expand Down
1 change: 1 addition & 0 deletions cmd/craned/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (o *Options) AddFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.DataSourcePromConfig.AdapterConfigMapName, "prometheus-adapter-configmap-name", "", "prometheus adapter-configmap name")
flags.StringVar(&o.DataSourcePromConfig.AdapterConfigMapKey, "prometheus-adapter-configmap-key", "", "prometheus adapter-configmap key")
flags.StringVar(&o.DataSourcePromConfig.AdapterConfig, "prometheus-adapter-config", "", "prometheus adapter-config path")
flags.StringVar(&o.DataSourcePromConfig.AdapterExtensionLabels, "prometheus-adapter-extension-labels", "", "prometheus adapter extension-labels for expressionQuery")
flags.StringVar(&o.DataSourcePromConfig.Auth.Username, "prometheus-auth-username", "", "prometheus auth username")
flags.StringVar(&o.DataSourcePromConfig.Auth.Password, "prometheus-auth-password", "", "prometheus auth password")
flags.StringVar(&o.DataSourcePromConfig.Auth.BearerToken, "prometheus-auth-bearertoken", "", "prometheus auth bearertoken")
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/ehpa/predict.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (c *EffectiveHPAController) NewPredictionObject(ehpa *autoscalingapi.Effect
if metricRule != nil {
// Second priority: get default expressionQuery
var err error
expressionQuery, err = metricRule.QueryForSeries(ehpa.Namespace, matchLabels)
expressionQuery, err = metricRule.QueryForSeries(ehpa.Namespace, append(mrs.ExtensionLabels, matchLabels...))
if err != nil {
klog.Errorf("Got promSelector prometheus-adapter %v", err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ensurance/cm/cpumanager/cpu_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This code is directly lifted from the distribution codebase as they could't been
For reference: https://github.com/kubernetes/kubernetes/blob/release-1.25/pkg/kubelet/cm/cpumanager/cpu_assignment.go
*/

//nolint
// nolint
package cpumanager

import (
Expand Down
25 changes: 16 additions & 9 deletions pkg/prometheus-adapter/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type MetricRules struct {
MetricRulesResource []MetricRule
MetricRulesCustomer []MetricRule
MetricRulesExternal []MetricRule
ExtensionLabels []string
}

type MetricRule struct {
Expand All @@ -55,11 +56,6 @@ func GetMetricRules() *MetricRules {
return metricRules
}

// get MetricRulesExternal from config.MetricsDiscoveryConfig
func GetMetricRulesExternal() []MetricRule {
return metricRules.MetricRulesExternal
}

// ParsingResourceRules from config.MetricsDiscoveryConfig
func ParsingResourceRules(mc config.MetricsDiscoveryConfig) (err error) {
metricRules.MetricRulesResource, err = GetMetricRulesFromResourceRules(*mc.ResourceRules)
Expand All @@ -86,6 +82,15 @@ func ParsingExternalRules(mc config.MetricsDiscoveryConfig) (err error) {
return err
}

// SetExtensionLabels from opts.DataSourcePromConfig.AdapterExtensionLabels
func SetExtensionLabels(extensionLabels string) {
if extensionLabels != "" {
for _, label := range strings.Split(extensionLabels, ",") {
metricRules.ExtensionLabels = append(metricRules.ExtensionLabels, label)
}
}
}

// GetMetricRuleResourceFromRules produces a MetricNamer for each rule in the given config.
func GetMetricRulesFromResourceRules(cfg config.ResourceRules) ([]MetricRule, error) {
var metricRules []MetricRule
Expand Down Expand Up @@ -252,10 +257,12 @@ func GetLabelMatchersFromDiscoveryRule(rule config.DiscoveryRule) []string {

// add SeriesQueryLabels
regLabelMatchers := regexp.MustCompile("{(.*?)}")
SeriesMatchers := regLabelMatchers.FindStringSubmatch(rule.SeriesQuery)[1]
if SeriesMatchers != "" {
for _, seriesMatcher := range strings.Split(SeriesMatchers, ",") {
labelMatchers = append(labelMatchers, seriesMatcher)
if len(regLabelMatchers.FindStringSubmatch(rule.SeriesQuery)) > 1 {
SeriesMatchers := regLabelMatchers.FindStringSubmatch(rule.SeriesQuery)[1]
if SeriesMatchers != "" {
for _, seriesMatcher := range strings.Split(SeriesMatchers, ",") {
labelMatchers = append(labelMatchers, seriesMatcher)
}
}
}

Expand Down
19 changes: 10 additions & 9 deletions pkg/providers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (

// PromConfig represents the config of prometheus
type PromConfig struct {
Address string
AdapterConfigMapNS string
AdapterConfigMapName string
AdapterConfigMapKey string
AdapterConfig string
Timeout time.Duration
KeepAlive time.Duration
InsecureSkipVerify bool
Auth ClientAuth
Address string
AdapterConfigMapNS string
AdapterConfigMapName string
AdapterConfigMapKey string
AdapterConfig string
AdapterExtensionLabels string
Timeout time.Duration
KeepAlive time.Duration
InsecureSkipVerify bool
Auth ClientAuth

QueryConcurrency int
BRateLimit bool
Expand Down
32 changes: 16 additions & 16 deletions pkg/querybuilder-providers/prometheus/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ func (b *builder) workloadQuery(metric *metricquery.Metric) (query *metricquery.
if metric.Workload == nil {
return nil, fmt.Errorf("metric type %v, but no WorkloadNamerInfo provided", metric.Type)
}
mrs := prometheus_adapter.GetMetricRulesExternal()
mrs := prometheus_adapter.GetMetricRules()
var metricRule *prometheus_adapter.MetricRule
var queryExpr string

switch strings.ToLower(metric.MetricName) {
case v1.ResourceCPU.String():
metricRule = prometheus_adapter.MatchMetricRule(mrs, prometheus_adapter.WorkloadCpuUsageExpression)
metricRule = prometheus_adapter.MatchMetricRule(mrs.MetricRulesExternal, prometheus_adapter.WorkloadCpuUsageExpression)
if metricRule == nil {
queryExpr = utils.GetWorkloadCpuUsageExpression(metric.Workload.Namespace, metric.Workload.Name, "")
}
case v1.ResourceMemory.String():
metricRule = prometheus_adapter.MatchMetricRule(mrs, prometheus_adapter.WorkloadMemUsageExpression)
metricRule = prometheus_adapter.MatchMetricRule(mrs.MetricRulesExternal, prometheus_adapter.WorkloadMemUsageExpression)
if metricRule == nil {
queryExpr = utils.GetWorkloadMemUsageExpression(metric.Workload.Namespace, metric.Workload.Name, "")
}
Expand All @@ -71,7 +71,7 @@ func (b *builder) workloadQuery(metric *metricquery.Metric) (query *metricquery.
}

if queryExpr == "" {
queryExpr, err = metricRule.QueryForSeries(metric.Workload.Namespace, []string{fmt.Sprintf("pod=~\"%s\"", utils.GetPodNameReg(metric.Workload.Name, metric.Workload.Kind))})
queryExpr, err = metricRule.QueryForSeries(metric.Workload.Namespace, append(mrs.ExtensionLabels, fmt.Sprintf("pod=~\"%s\"", utils.GetPodNameReg(metric.Workload.Name, metric.Workload.Kind))))
}
return promQuery(&metricquery.PrometheusQuery{Query: queryExpr}), err
}
Expand All @@ -80,18 +80,18 @@ func (b *builder) nodeQuery(metric *metricquery.Metric) (query *metricquery.Quer
if metric.Node == nil {
return nil, fmt.Errorf("metric type %v, but no NodeNamerInfo provided", metric.Type)
}
mrs := prometheus_adapter.GetMetricRulesExternal()
mrs := prometheus_adapter.GetMetricRules()
var metricRule *prometheus_adapter.MetricRule
var queryExpr string

switch strings.ToLower(metric.MetricName) {
case v1.ResourceCPU.String():
metricRule = prometheus_adapter.MatchMetricRule(mrs, prometheus_adapter.NodeCpuUsageExpression)
metricRule = prometheus_adapter.MatchMetricRule(mrs.MetricRulesExternal, prometheus_adapter.NodeCpuUsageExpression)
if metricRule == nil {
queryExpr = utils.GetNodeCpuUsageExpression(metric.Node.Name)
}
case v1.ResourceMemory.String():
metricRule = prometheus_adapter.MatchMetricRule(mrs, prometheus_adapter.NodeMemUsageExpression)
metricRule = prometheus_adapter.MatchMetricRule(mrs.MetricRulesExternal, prometheus_adapter.NodeMemUsageExpression)
if metricRule == nil {
queryExpr = utils.GetNodeMemUsageExpression(metric.Node.Name)
}
Expand All @@ -100,7 +100,7 @@ func (b *builder) nodeQuery(metric *metricquery.Metric) (query *metricquery.Quer
}

if queryExpr == "" {
queryExpr, err = metricRule.QueryForSeries("", []string{fmt.Sprintf("instance=~\"(%s)(:\\d+)?\"", metric.Node.Name)})
queryExpr, err = metricRule.QueryForSeries("", append(mrs.ExtensionLabels, fmt.Sprintf("instance=~\"(%s)(:\\d+)?\"", metric.Node.Name)))
}

return promQuery(&metricquery.PrometheusQuery{Query: queryExpr}), err
Expand All @@ -110,18 +110,18 @@ func (b *builder) containerQuery(metric *metricquery.Metric) (query *metricquery
if metric.Container == nil {
return nil, fmt.Errorf("metric type %v, but no ContainerNamerInfo provided", metric.Type)
}
mrs := prometheus_adapter.GetMetricRulesExternal()
mrs := prometheus_adapter.GetMetricRules()
var metricRule *prometheus_adapter.MetricRule
var queryExpr string

switch strings.ToLower(metric.MetricName) {
case v1.ResourceCPU.String():
metricRule = prometheus_adapter.MatchMetricRule(mrs, prometheus_adapter.ContainerCpuUsageExpression)
metricRule = prometheus_adapter.MatchMetricRule(mrs.MetricRulesExternal, prometheus_adapter.ContainerCpuUsageExpression)
if metricRule == nil {
queryExpr = utils.GetContainerCpuUsageExpression(metric.Container.Namespace, metric.Container.WorkloadName, "", metric.Container.Name)
}
case v1.ResourceMemory.String():
metricRule = prometheus_adapter.MatchMetricRule(mrs, prometheus_adapter.ContainerMemUsageExpression)
metricRule = prometheus_adapter.MatchMetricRule(mrs.MetricRulesExternal, prometheus_adapter.ContainerMemUsageExpression)
if metricRule == nil {
queryExpr = utils.GetContainerMemUsageExpression(metric.Container.Namespace, metric.Container.WorkloadName, "", metric.Container.Name)
}
Expand All @@ -130,7 +130,7 @@ func (b *builder) containerQuery(metric *metricquery.Metric) (query *metricquery
}

if queryExpr == "" {
queryExpr, err = metricRule.QueryForSeries(metric.Container.Namespace, []string{fmt.Sprintf("pod=~\"%s\"", utils.GetPodNameReg(metric.Container.WorkloadName, metric.Container.WorkloadKind)), fmt.Sprintf("container=\"%s\"", metric.Container.Name)})
queryExpr, err = metricRule.QueryForSeries(metric.Container.Namespace, append(mrs.ExtensionLabels, []string{fmt.Sprintf("pod=~\"%s\"", utils.GetPodNameReg(metric.Container.WorkloadName, metric.Container.WorkloadKind)), fmt.Sprintf("container=\"%s\"", metric.Container.Name)}...))
}
return promQuery(&metricquery.PrometheusQuery{
Query: queryExpr,
Expand All @@ -141,18 +141,18 @@ func (b *builder) podQuery(metric *metricquery.Metric) (query *metricquery.Query
if metric.Pod == nil {
return nil, fmt.Errorf("metric type %v, but no PodNamerInfo provided", metric.Type)
}
mrs := prometheus_adapter.GetMetricRulesExternal()
mrs := prometheus_adapter.GetMetricRules()
var metricRule *prometheus_adapter.MetricRule
var queryExpr string

switch strings.ToLower(metric.MetricName) {
case v1.ResourceCPU.String():
metricRule = prometheus_adapter.MatchMetricRule(mrs, prometheus_adapter.PodCpuUsageExpression)
metricRule = prometheus_adapter.MatchMetricRule(mrs.MetricRulesExternal, prometheus_adapter.PodCpuUsageExpression)
if metricRule == nil {
queryExpr = utils.GetPodCpuUsageExpression(metric.Pod.Namespace, metric.Pod.Name)
}
case v1.ResourceMemory.String():
metricRule = prometheus_adapter.MatchMetricRule(mrs, prometheus_adapter.PodMemUsageExpression)
metricRule = prometheus_adapter.MatchMetricRule(mrs.MetricRulesExternal, prometheus_adapter.PodMemUsageExpression)
if metricRule == nil {
queryExpr = utils.GetPodMemUsageExpression(metric.Pod.Namespace, metric.Pod.Name)
}
Expand All @@ -161,7 +161,7 @@ func (b *builder) podQuery(metric *metricquery.Metric) (query *metricquery.Query
}

if queryExpr == "" {
queryExpr, err = metricRule.QueryForSeries(metric.Pod.Namespace, []string{fmt.Sprintf("pod=\"%s\"", metric.Pod.Name)})
queryExpr, err = metricRule.QueryForSeries(metric.Pod.Namespace, append(mrs.ExtensionLabels, fmt.Sprintf("pod=\"%s\"", metric.Pod.Name)))
}
return promQuery(&metricquery.PrometheusQuery{
Query: queryExpr,
Expand Down
Loading

0 comments on commit 2fcdce0

Please sign in to comment.