volcano.sh/volcano@v1.9.0/pkg/scheduler/metrics/source/metrics_client.go (about)

     1  /*
     2   Copyright 2023 The Volcano 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 source
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	"k8s.io/client-go/rest"
    25  	"k8s.io/klog/v2"
    26  )
    27  
    28  const (
    29  	NODE_METRICS_PERIOD             = "10m"
    30  	Metrics_Type_Prometheus_Adaptor = "prometheus_adaptor"
    31  	Metrics_Tpye_Prometheus         = "prometheus"
    32  	Metrics_Type_Elasticsearch      = "elasticsearch"
    33  )
    34  
    35  type NodeMetrics struct {
    36  	MetricsTime time.Time
    37  	CPU         float64
    38  	Memory      float64
    39  }
    40  
    41  type MetricsClient interface {
    42  	NodesMetricsAvg(ctx context.Context, nodeMetricsMap map[string]*NodeMetrics) error
    43  }
    44  
    45  func NewMetricsClient(restConfig *rest.Config, metricsConf map[string]string) (MetricsClient, error) {
    46  	klog.V(3).Infof("New metrics client begin, metricsConf is %v", metricsConf)
    47  	metricsType := metricsConf["type"]
    48  	if metricsType == Metrics_Type_Elasticsearch {
    49  		return NewElasticsearchMetricsClient(metricsConf)
    50  	} else if metricsType == Metrics_Tpye_Prometheus {
    51  		return NewPrometheusMetricsClient(metricsConf)
    52  	} else if metricsType == Metrics_Type_Prometheus_Adaptor {
    53  		return NewCustomMetricsClient(restConfig)
    54  	} else {
    55  		return nil, fmt.Errorf("Data cannot be collected from the %s monitoring system. "+
    56  			"The supported monitoring systems are %s, %s, and %s.",
    57  			metricsType, Metrics_Type_Elasticsearch, Metrics_Tpye_Prometheus, Metrics_Type_Prometheus_Adaptor)
    58  	}
    59  }