-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathmetrics_source.go
145 lines (125 loc) · 5.06 KB
/
metrics_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Copyright 2023 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 metrics
import (
"context"
"time"
k8sapiv1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"k8s.io/metrics/pkg/apis/metrics/v1beta1"
resourceclient "k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1"
"k8s.io/metrics/pkg/client/external_metrics"
)
// PodMetricsLister wraps both metrics-client and External Metrics
type PodMetricsLister interface {
List(ctx context.Context, namespace string, opts v1.ListOptions) (*v1beta1.PodMetricsList, error)
}
// podMetricsSource is the metrics-client source of metrics.
type podMetricsSource struct {
metricsGetter resourceclient.PodMetricsesGetter
}
// NewPodMetricsesSource Returns a Source-wrapper around PodMetricsesGetter.
func NewPodMetricsesSource(source resourceclient.PodMetricsesGetter) PodMetricsLister {
return podMetricsSource{metricsGetter: source}
}
func (s podMetricsSource) List(ctx context.Context, namespace string, opts v1.ListOptions) (*v1beta1.PodMetricsList, error) {
podMetricsInterface := s.metricsGetter.PodMetricses(namespace)
return podMetricsInterface.List(ctx, opts)
}
// externalMetricsClient is the External Metrics source of metrics.
type externalMetricsClient struct {
externalClient external_metrics.ExternalMetricsClient
options ExternalClientOptions
clusterState *model.ClusterState
}
// ExternalClientOptions specifies parameters for using an External Metrics Client.
type ExternalClientOptions struct {
ResourceMetrics map[k8sapiv1.ResourceName]string
// Label to use for the container name.
ContainerNameLabel string
}
// NewExternalClient returns a Source for an External Metrics Client.
func NewExternalClient(c *rest.Config, clusterState *model.ClusterState, options ExternalClientOptions) PodMetricsLister {
extClient, err := external_metrics.NewForConfig(c)
if err != nil {
klog.Fatalf("Failed initializing external metrics client: %v", err)
}
return &externalMetricsClient{
externalClient: extClient,
options: options,
clusterState: clusterState,
}
}
func (s *externalMetricsClient) List(ctx context.Context, namespace string, opts v1.ListOptions) (*v1beta1.PodMetricsList, error) {
result := v1beta1.PodMetricsList{}
for _, vpa := range s.clusterState.Vpas {
if vpa.PodCount == 0 {
continue
}
if namespace != "" && vpa.ID.Namespace != namespace {
continue
}
nsClient := s.externalClient.NamespacedMetrics(vpa.ID.Namespace)
pods := s.clusterState.GetMatchingPods(vpa)
for _, pod := range pods {
podNameReq, err := labels.NewRequirement("pod", selection.Equals, []string{pod.PodName})
if err != nil {
return nil, err
}
selector := vpa.PodSelector.Add(*podNameReq)
podMets := v1beta1.PodMetrics{
TypeMeta: v1.TypeMeta{},
ObjectMeta: v1.ObjectMeta{Namespace: vpa.ID.Namespace, Name: pod.PodName},
Window: v1.Duration{},
Containers: make([]v1beta1.ContainerMetrics, 0),
}
// Query each resource in turn, then assemble back to a single []ContainerMetrics.
containerMetrics := make(map[string]k8sapiv1.ResourceList)
for resourceName, metricName := range s.options.ResourceMetrics {
m, err := nsClient.List(metricName, selector)
if err != nil {
return nil, err
}
if m == nil || len(m.Items) == 0 {
klog.V(4).Infof("External Metrics Query for VPA %s: resource %+v, metric %+v, No items,", klog.KRef(vpa.ID.Namespace, vpa.ID.VpaName), resourceName, metricName)
continue
}
klog.V(4).Infof("External Metrics Query for VPA %s: resource %+v, metric %+v, %d items, item[0]: %+v", klog.KRef(vpa.ID.Namespace, vpa.ID.VpaName), resourceName, metricName, len(m.Items), m.Items[0])
podMets.Timestamp = m.Items[0].Timestamp
if m.Items[0].WindowSeconds != nil {
podMets.Window = v1.Duration{Duration: time.Duration(*m.Items[0].WindowSeconds) * time.Second}
}
for _, val := range m.Items {
ctrName, hasCtrName := val.MetricLabels[s.options.ContainerNameLabel]
if !hasCtrName {
continue
}
if containerMetrics[ctrName] == nil {
containerMetrics[ctrName] = make(k8sapiv1.ResourceList)
}
containerMetrics[ctrName][resourceName] = val.Value
}
}
for cname, res := range containerMetrics {
podMets.Containers = append(podMets.Containers, v1beta1.ContainerMetrics{Name: cname, Usage: res})
}
result.Items = append(result.Items, podMets)
}
}
return &result, nil
}