Stubbing out metric converters.

This commit is contained in:
Tony Compton 2018-06-28 22:08:21 -04:00
parent b3d99534a8
commit a69a5cbbcc
5 changed files with 213 additions and 30 deletions

View file

@ -1,29 +1,14 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package provider
import (
"context"
"fmt"
"time"
pmodel "github.com/prometheus/common/model"
conv "github.com/directxman12/k8s-prometheus-adapter/pkg/custom-provider/metric-converter"
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
apimeta "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/dynamic"
@ -32,16 +17,22 @@ import (
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
)
//TODO: Make sure everything has the proper licensing disclosure at the top.
//TODO: I'd like to move these files into another directory, but the compiler was giving me
//some static around unexported types. I'm going to leave things as-is for now, but it
//might be worthwhile to, once the shared components are discovered, move some things around.
//TODO: Some of these members may not be necessary.
//Some of them are definitely duplicated between the
//external and custom providers. They should probably share
//the same instances of these objects (especially the SeriesRegistry)
//to cut down on unnecessary chatter/bookkeeping.
type externalPrometheusProvider struct {
mapper apimeta.RESTMapper
kubeClient dynamic.Interface
promClient prom.Client
queryBuilder ExternalMetricQueryBuilder
mapper apimeta.RESTMapper
kubeClient dynamic.Interface
promClient prom.Client
queryBuilder ExternalMetricQueryBuilder
metricConverter conv.MetricConverter
SeriesRegistry
}
@ -52,7 +43,8 @@ type externalPrometheusProvider struct {
//Just glancing at start.go looks like it would be much more straightforward
//to do one of those two things instead of trying to run the two providers
//independently.
func NewExternalPrometheusProvider(mapper apimeta.RESTMapper, kubeClient dynamic.Interface, promClient prom.Client, namers []MetricNamer, updateInterval time.Duration, queryBuilder ExternalMetricQueryBuilder) (provider.ExternalMetricsProvider, Runnable) {
func NewExternalPrometheusProvider(mapper apimeta.RESTMapper, kubeClient dynamic.Interface, promClient prom.Client, namers []MetricNamer, updateInterval time.Duration, queryBuilder ExternalMetricQueryBuilder, metricConverter conv.MetricConverter) (provider.ExternalMetricsProvider, Runnable) {
lister := &cachingMetricsLister{
updateInterval: updateInterval,
promClient: promClient,
@ -64,9 +56,10 @@ func NewExternalPrometheusProvider(mapper apimeta.RESTMapper, kubeClient dynamic
}
return &externalPrometheusProvider{
mapper: mapper,
kubeClient: kubeClient,
promClient: promClient,
mapper: mapper,
kubeClient: kubeClient,
promClient: promClient,
metricConverter: metricConverter,
SeriesRegistry: lister,
}, lister
@ -79,12 +72,13 @@ func (p *externalPrometheusProvider) GetExternalMetric(namespace string, metricN
//TODO: I don't yet know what a context is, but apparently I should use a real one.
queryResults, err := p.promClient.Query(context.TODO(), pmodel.Now(), selector)
//TODO: Only here to stop compiler issues in this incomplete code.
fmt.Printf("%s, %s", queryResults, err)
if err != nil {
//TODO: Is this how folks normally deal w/ errors? Just propagate them upwards?
//I should go look at what the customProvider does.
return nil, err
}
//TODO: Check for errors. See what the custromPrometheusProvider does for errors.
//TODO: Adapt the results in queryResults to the appropriate return type.
return nil, nil
return p.metricConverter.Convert(queryResults)
}
func (p *externalPrometheusProvider) ListAllExternalMetrics() []provider.ExternalMetricInfo {