Skip to content

Commit

Permalink
Export client name with responses from metrics server
Browse files Browse the repository at this point in the history
In case we instantiate multiple clients and want to distinguish responses they're getting
  • Loading branch information
jbartosik committed Aug 24, 2022
1 parent bd0966b commit 2694bba
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
10 changes: 5 additions & 5 deletions vertical-pod-autoscaler/pkg/recommender/input/cluster_feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (
v1lister "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
klog "k8s.io/klog/v2"
resourceclient "k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1"
)

Expand Down Expand Up @@ -114,7 +114,7 @@ func (m ClusterStateFeederFactory) Make() *clusterStateFeeder {

// NewClusterStateFeeder creates new ClusterStateFeeder with internal data providers, based on kube client config.
// Deprecated; Use ClusterStateFeederFactory instead.
func NewClusterStateFeeder(config *rest.Config, clusterState *model.ClusterState, memorySave bool, namespace string) ClusterStateFeeder {
func NewClusterStateFeeder(config *rest.Config, clusterState *model.ClusterState, memorySave bool, namespace, metricsClientName string) ClusterStateFeeder {
kubeClient := kube_client.NewForConfigOrDie(config)
podLister, oomObserver := NewPodListerAndOOMObserver(kubeClient, namespace)
factory := informers.NewSharedInformerFactoryWithOptions(kubeClient, defaultResyncPeriod, informers.WithNamespace(namespace))
Expand All @@ -124,7 +124,7 @@ func NewClusterStateFeeder(config *rest.Config, clusterState *model.ClusterState
PodLister: podLister,
OOMObserver: oomObserver,
KubeClient: kubeClient,
MetricsClient: newMetricsClient(config, namespace),
MetricsClient: newMetricsClient(config, namespace, metricsClientName),
VpaCheckpointClient: vpa_clientset.NewForConfigOrDie(config).AutoscalingV1(),
VpaLister: vpa_api_util.NewVpasLister(vpa_clientset.NewForConfigOrDie(config), make(chan struct{}), namespace),
ClusterState: clusterState,
Expand All @@ -134,9 +134,9 @@ func NewClusterStateFeeder(config *rest.Config, clusterState *model.ClusterState
}.Make()
}

func newMetricsClient(config *rest.Config, namespace string) metrics.MetricsClient {
func newMetricsClient(config *rest.Config, namespace, clientName string) metrics.MetricsClient {
metricsGetter := resourceclient.NewForConfigOrDie(config)
return metrics.NewMetricsClient(metricsGetter, namespace)
return metrics.NewMetricsClient(metricsGetter, namespace, clientName)
}

// WatchEvictionEventsWithRetries watches new Events with reason=Evicted and passes them to the observer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ type MetricsClient interface {
type metricsClient struct {
metricsGetter resourceclient.PodMetricsesGetter
namespace string
clientName string
}

// NewMetricsClient creates new instance of MetricsClient, which is used by recommender.
// It requires an instance of PodMetricsesGetter, which is used for underlying communication with metrics server.
// namespace limits queries to particular namespace, use k8sapiv1.NamespaceAll to select all namespaces.
func NewMetricsClient(metricsGetter resourceclient.PodMetricsesGetter, namespace string) MetricsClient {
func NewMetricsClient(metricsGetter resourceclient.PodMetricsesGetter, namespace, clientName string) MetricsClient {
return &metricsClient{
metricsGetter: metricsGetter,
namespace: namespace,
clientName: clientName,
}
}

Expand All @@ -68,7 +70,7 @@ func (c *metricsClient) GetContainersMetrics() ([]*ContainerMetricsSnapshot, err

podMetricsInterface := c.metricsGetter.PodMetricses(c.namespace)
podMetricsList, err := podMetricsInterface.List(context.TODO(), metav1.ListOptions{})
recommender_metrics.RecordMetricsServerResponse(err)
recommender_metrics.RecordMetricsServerResponse(err, c.clientName)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (tc *metricsClientTestCase) createFakeMetricsClient() MetricsClient {
fakeMetricsGetter.AddReactor("list", "pods", func(action core.Action) (handled bool, ret runtime.Object, err error) {
return true, tc.getFakePodMetricsList(), nil
})
return NewMetricsClient(fakeMetricsGetter.MetricsV1beta1(), "")
return NewMetricsClient(fakeMetricsGetter.MetricsV1beta1(), "", "fake")
}

func (tc *metricsClientTestCase) getFakePodMetricsList() *metricsapi.PodMetricsList {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"k8s.io/client-go/informers"
kube_client "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
klog "k8s.io/klog/v2"
)

const (
Expand Down Expand Up @@ -261,7 +261,7 @@ func NewRecommender(config *rest.Config, checkpointsGCInterval time.Duration, us
controllerFetcher := controllerfetcher.NewControllerFetcher(config, kubeClient, factory, scaleCacheEntryFreshnessTime, scaleCacheEntryLifetime, scaleCacheEntryJitterFactor)
return RecommenderFactory{
ClusterState: clusterState,
ClusterStateFeeder: input.NewClusterStateFeeder(config, clusterState, *memorySaver, namespace),
ClusterStateFeeder: input.NewClusterStateFeeder(config, clusterState, *memorySaver, namespace, "default-metrics-client"),
ControllerFetcher: controllerFetcher,
CheckpointWriter: checkpoint.NewCheckpointWriter(clusterState, vpa_clientset.NewForConfigOrDie(config).AutoscalingV1()),
VpaClient: vpa_clientset.NewForConfigOrDie(config).AutoscalingV1(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var (
Namespace: metricsNamespace,
Name: "metric_server_responses",
Help: "Count of responses to queries to metrics server",
}, []string{"is_error"},
}, []string{"is_error", "client_name"},
)
)

Expand Down Expand Up @@ -116,8 +116,8 @@ func RecordAggregateContainerStatesCount(statesCount int) {
}

// RecordMetricsServerResponse records result of a query to metrics server
func RecordMetricsServerResponse(err error) {
metricServerResponses.WithLabelValues(strconv.FormatBool(err != nil)).Inc()
func RecordMetricsServerResponse(err error, clientName string) {
metricServerResponses.WithLabelValues(strconv.FormatBool(err != nil), clientName).Inc()
}

// NewObjectCounter creates a new helper to split VPA objects into buckets
Expand Down

0 comments on commit 2694bba

Please sign in to comment.