Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cherry-pick for release-1.8]Avoid repeatedly creating links to obtain node metrics #3229

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/scheduler/metrics/source/metrics_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type MetricsClient interface {
}

func NewMetricsClient(restConfig *rest.Config, metricsConf map[string]string) (MetricsClient, error) {
klog.V(3).Infof("New metrics client begin, resconfig is %v, metricsConf is %v", restConfig, metricsConf)
klog.V(3).Infof("New metrics client begin, metricsConf is %v", metricsConf)
metricsType := metricsConf["type"]
if metricsType == Metrics_Type_Elasticsearch {
return NewElasticsearchMetricsClient(metricsConf)
Expand Down
33 changes: 21 additions & 12 deletions pkg/scheduler/metrics/source/metrics_client_prometheus_adapt.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,40 @@ const (
CustomNodeMemUsageAvg = "node_memory_usage_avg"
)

type CustomMetricsClient struct {
config *rest.Config
type KMetricsClient struct {
customMetricsCli customclient.CustomMetricsClient
}

func NewCustomMetricsClient(restConfig *rest.Config) (*CustomMetricsClient, error) {
klog.V(3).Infof("NewCustomMetricsClient begin")
return &CustomMetricsClient{config: restConfig}, nil
}
var kMetricsClient *KMetricsClient

func (c *CustomMetricsClient) NodesMetricsAvg(ctx context.Context, nodeMetricsMap map[string]*NodeMetrics) error {
klog.V(5).Infof("Get node metrics from Custom Metrics")
discoveryClient := discovery.NewDiscoveryClientForConfigOrDie(c.config)
func NewCustomMetricsClient(cfg *rest.Config) (*KMetricsClient, error) {
if kMetricsClient != nil {
return kMetricsClient, nil
}

klog.V(3).Infof("Create custom metrics api client")
discoveryClient := discovery.NewDiscoveryClientForConfigOrDie(cfg)
cachedDiscoClient := cacheddiscovery.NewMemCacheClient(discoveryClient)
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(cachedDiscoClient)
restMapper.Reset()
apiVersionsGetter := customclient.NewAvailableAPIsGetter(discoveryClient)
customMetricsClient := customclient.NewForConfig(c.config, restMapper, apiVersionsGetter)
customMetricsClient := customclient.NewForConfig(cfg, restMapper, apiVersionsGetter)

kMetricsClient = &KMetricsClient{
customMetricsCli: customMetricsClient,
}
return kMetricsClient, nil
}

func (km *KMetricsClient) NodesMetricsAvg(ctx context.Context, nodeMetricsMap map[string]*NodeMetrics) error {
klog.V(5).Infof("Get node metrics from Custom Metrics")

groupKind := schema.GroupKind{
Group: "",
Kind: "Node",
}

for _, metricName := range []string{CustomNodeCPUUsageAvg, CustomNodeMemUsageAvg} {
metricsValue, err := customMetricsClient.RootScopedMetrics().GetForObjects(groupKind, labels.NewSelector(), metricName, labels.NewSelector())
metricsValue, err := km.customMetricsCli.RootScopedMetrics().GetForObjects(groupKind, labels.NewSelector(), metricName, labels.NewSelector())
if err != nil {
klog.Errorf("Failed to query the indicator %s, error is: %v.", metricName, err)
return err
Expand Down
Loading