mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 01:38:10 +00:00
vendor: Update vendor logic
This commit is contained in:
parent
c6ac5cbc87
commit
4ca64b85f0
1540 changed files with 265304 additions and 91616 deletions
61
vendor/sigs.k8s.io/metrics-server/pkg/api/install.go
generated
vendored
Normal file
61
vendor/sigs.k8s.io/metrics-server/pkg/api/install.go
generated
vendored
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
// Copyright 2018 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 api
|
||||
|
||||
import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
genericapiserver "k8s.io/apiserver/pkg/server"
|
||||
coreinf "k8s.io/client-go/informers/core/v1"
|
||||
"k8s.io/metrics/pkg/apis/metrics"
|
||||
"k8s.io/metrics/pkg/apis/metrics/install"
|
||||
"k8s.io/metrics/pkg/apis/metrics/v1beta1"
|
||||
)
|
||||
|
||||
var (
|
||||
// Scheme contains the types needed by the resource metrics API.
|
||||
Scheme = runtime.NewScheme()
|
||||
// Codecs is a codec factory for serving the resource metrics API.
|
||||
Codecs = serializer.NewCodecFactory(Scheme)
|
||||
)
|
||||
|
||||
func init() {
|
||||
install.Install(Scheme)
|
||||
metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
|
||||
}
|
||||
|
||||
// Build constructs APIGroupInfo the metrics.k8s.io API group using the given getters.
|
||||
func Build(m MetricsGetter, informers coreinf.Interface) genericapiserver.APIGroupInfo {
|
||||
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(metrics.GroupName, Scheme, metav1.ParameterCodec, Codecs)
|
||||
|
||||
node := newNodeMetrics(metrics.Resource("nodemetrics"), m, informers.Nodes().Lister())
|
||||
pod := newPodMetrics(metrics.Resource("podmetrics"), m, informers.Pods().Lister())
|
||||
metricsServerResources := map[string]rest.Storage{
|
||||
"nodes": node,
|
||||
"pods": pod,
|
||||
}
|
||||
apiGroupInfo.VersionedResourcesStorageMap[v1beta1.SchemeGroupVersion.Version] = metricsServerResources
|
||||
|
||||
return apiGroupInfo
|
||||
}
|
||||
|
||||
// InstallStorage builds the metrics for the metrics.k8s.io API, and then installs it into the given API metrics-server.
|
||||
func Install(metrics MetricsGetter, informers coreinf.Interface, server *genericapiserver.GenericAPIServer) error {
|
||||
info := Build(metrics, informers)
|
||||
return server.InstallAPIGroup(&info)
|
||||
}
|
||||
63
vendor/sigs.k8s.io/metrics-server/pkg/api/interfaces.go
generated
vendored
Normal file
63
vendor/sigs.k8s.io/metrics-server/pkg/api/interfaces.go
generated
vendored
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
// Copyright 2018 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 api
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apitypes "k8s.io/apimachinery/pkg/types"
|
||||
metrics "k8s.io/metrics/pkg/apis/metrics"
|
||||
)
|
||||
|
||||
// MetricsGetter is both a PodMetricsGetter and a NodeMetricsGetter
|
||||
type MetricsGetter interface {
|
||||
PodMetricsGetter
|
||||
NodeMetricsGetter
|
||||
}
|
||||
|
||||
// TimeInfo represents the timing information for a metric, which was
|
||||
// potentially calculated over some window of time (e.g. for CPU usage rate).
|
||||
type TimeInfo struct {
|
||||
// NB: we consider the earliest timestamp amongst multiple containers
|
||||
// for the purposes of determining if a metric is tained by a time
|
||||
// period, like pod startup (used by things like the HPA).
|
||||
|
||||
// Timestamp is the time at which the metrics were initially collected.
|
||||
// In the case of a rate metric, it should be the timestamp of the last
|
||||
// data point used in the calculation. If it represents multiple metric
|
||||
// points, it should be the earliest such timestamp from all of the points.
|
||||
Timestamp time.Time
|
||||
|
||||
// Window represents the window used to calculate rate metrics associated
|
||||
// with this timestamp.
|
||||
Window time.Duration
|
||||
}
|
||||
|
||||
// PodMetricsGetter knows how to fetch metrics for the containers in a pod.
|
||||
type PodMetricsGetter interface {
|
||||
// GetContainerMetrics gets the latest metrics for all containers in each listed pod,
|
||||
// returning both the metrics and the associated collection timestamp.
|
||||
// If a pod is missing, the container metrics should be nil for that pod.
|
||||
GetContainerMetrics(pods ...apitypes.NamespacedName) ([]TimeInfo, [][]metrics.ContainerMetrics, error)
|
||||
}
|
||||
|
||||
// NodeMetricsGetter knows how to fetch metrics for a node.
|
||||
type NodeMetricsGetter interface {
|
||||
// GetNodeMetrics gets the latest metrics for the given nodes,
|
||||
// returning both the metrics and the associated collection timestamp.
|
||||
// If a node is missing, the resourcelist should be nil for that node.
|
||||
GetNodeMetrics(nodes ...string) ([]TimeInfo, []corev1.ResourceList, error)
|
||||
}
|
||||
217
vendor/sigs.k8s.io/metrics-server/pkg/api/node.go
generated
vendored
Normal file
217
vendor/sigs.k8s.io/metrics-server/pkg/api/node.go
generated
vendored
Normal file
|
|
@ -0,0 +1,217 @@
|
|||
// Copyright 2018 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 api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
|
||||
"k8s.io/apimachinery/pkg/fields"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/registry/generic"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
v1listers "k8s.io/client-go/listers/core/v1"
|
||||
"k8s.io/klog"
|
||||
"k8s.io/metrics/pkg/apis/metrics"
|
||||
_ "k8s.io/metrics/pkg/apis/metrics/install"
|
||||
)
|
||||
|
||||
type nodeMetrics struct {
|
||||
groupResource schema.GroupResource
|
||||
metrics NodeMetricsGetter
|
||||
nodeLister v1listers.NodeLister
|
||||
}
|
||||
|
||||
var _ rest.KindProvider = &nodeMetrics{}
|
||||
var _ rest.Storage = &nodeMetrics{}
|
||||
var _ rest.Getter = &nodeMetrics{}
|
||||
var _ rest.Lister = &nodeMetrics{}
|
||||
var _ rest.Scoper = &nodeMetrics{}
|
||||
var _ rest.TableConvertor = &nodeMetrics{}
|
||||
|
||||
func newNodeMetrics(groupResource schema.GroupResource, metrics NodeMetricsGetter, nodeLister v1listers.NodeLister) *nodeMetrics {
|
||||
return &nodeMetrics{
|
||||
groupResource: groupResource,
|
||||
metrics: metrics,
|
||||
nodeLister: nodeLister,
|
||||
}
|
||||
}
|
||||
|
||||
// Storage interface
|
||||
func (m *nodeMetrics) New() runtime.Object {
|
||||
return &metrics.NodeMetrics{}
|
||||
}
|
||||
|
||||
// KindProvider interface
|
||||
func (m *nodeMetrics) Kind() string {
|
||||
return "NodeMetrics"
|
||||
}
|
||||
|
||||
// Lister interface
|
||||
func (m *nodeMetrics) NewList() runtime.Object {
|
||||
return &metrics.NodeMetricsList{}
|
||||
}
|
||||
|
||||
// Lister interface
|
||||
func (m *nodeMetrics) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
|
||||
labelSelector := labels.Everything()
|
||||
fieldSelector := fields.Everything()
|
||||
if options != nil && options.LabelSelector != nil {
|
||||
labelSelector = options.LabelSelector
|
||||
}
|
||||
if options != nil && options.FieldSelector != nil {
|
||||
fieldSelector = options.FieldSelector
|
||||
}
|
||||
nodes, err := m.nodeLister.ListWithPredicate(func(node *v1.Node) bool {
|
||||
if labelSelector.Empty() && fieldSelector.Empty() {
|
||||
return true
|
||||
}
|
||||
fieldsSet := generic.AddObjectMetaFieldsSet(make(fields.Set, 2), &node.ObjectMeta, true)
|
||||
return labelSelector.Matches(labels.Set(node.Labels)) && fieldSelector.Matches(fieldsSet)
|
||||
})
|
||||
if err != nil {
|
||||
errMsg := fmt.Errorf("Error while listing nodes for selector %v: %v", labelSelector, err)
|
||||
klog.Error(errMsg)
|
||||
return &metrics.NodeMetricsList{}, errMsg
|
||||
}
|
||||
|
||||
names := make([]string, len(nodes))
|
||||
for i, node := range nodes {
|
||||
names[i] = node.Name
|
||||
}
|
||||
// maintain the same ordering invariant as the Kube API would over nodes
|
||||
sort.Strings(names)
|
||||
|
||||
metricsItems, err := m.getNodeMetrics(names...)
|
||||
if err != nil {
|
||||
errMsg := fmt.Errorf("Error while fetching node metrics for selector %v: %v", labelSelector, err)
|
||||
klog.Error(errMsg)
|
||||
return &metrics.NodeMetricsList{}, errMsg
|
||||
}
|
||||
|
||||
return &metrics.NodeMetricsList{Items: metricsItems}, nil
|
||||
}
|
||||
|
||||
func (m *nodeMetrics) Get(ctx context.Context, name string, opts *metav1.GetOptions) (runtime.Object, error) {
|
||||
nodeMetrics, err := m.getNodeMetrics(name)
|
||||
if err == nil && len(nodeMetrics) == 0 {
|
||||
err = fmt.Errorf("no metrics known for node %q", name)
|
||||
}
|
||||
if err != nil {
|
||||
klog.Errorf("unable to fetch node metrics for node %q: %v", name, err)
|
||||
return nil, errors.NewNotFound(m.groupResource, name)
|
||||
}
|
||||
|
||||
return &nodeMetrics[0], nil
|
||||
}
|
||||
|
||||
func (m *nodeMetrics) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1beta1.Table, error) {
|
||||
var table metav1beta1.Table
|
||||
|
||||
switch t := object.(type) {
|
||||
case *metrics.NodeMetrics:
|
||||
table.ResourceVersion = t.ResourceVersion
|
||||
table.SelfLink = t.SelfLink
|
||||
addNodeMetricsToTable(&table, *t)
|
||||
case *metrics.NodeMetricsList:
|
||||
table.ResourceVersion = t.ResourceVersion
|
||||
table.SelfLink = t.SelfLink
|
||||
table.Continue = t.Continue
|
||||
addNodeMetricsToTable(&table, t.Items...)
|
||||
default:
|
||||
}
|
||||
|
||||
return &table, nil
|
||||
}
|
||||
|
||||
func addNodeMetricsToTable(table *metav1beta1.Table, nodes ...metrics.NodeMetrics) {
|
||||
var names []string
|
||||
for i, node := range nodes {
|
||||
if names == nil {
|
||||
for k := range node.Usage {
|
||||
names = append(names, string(k))
|
||||
}
|
||||
sort.Strings(names)
|
||||
|
||||
table.ColumnDefinitions = []metav1beta1.TableColumnDefinition{
|
||||
{Name: "Name", Type: "string", Format: "name", Description: "Name of the resource"},
|
||||
}
|
||||
for _, name := range names {
|
||||
table.ColumnDefinitions = append(table.ColumnDefinitions, metav1beta1.TableColumnDefinition{
|
||||
Name: name,
|
||||
Type: "string",
|
||||
Format: "quantity",
|
||||
})
|
||||
}
|
||||
table.ColumnDefinitions = append(table.ColumnDefinitions, metav1beta1.TableColumnDefinition{
|
||||
Name: "Window",
|
||||
Type: "string",
|
||||
Format: "duration",
|
||||
})
|
||||
}
|
||||
row := make([]interface{}, 0, len(names)+1)
|
||||
row = append(row, node.Name)
|
||||
for _, name := range names {
|
||||
v := node.Usage[v1.ResourceName(name)]
|
||||
row = append(row, v.String())
|
||||
}
|
||||
row = append(row, node.Window.Duration.String())
|
||||
table.Rows = append(table.Rows, metav1beta1.TableRow{
|
||||
Cells: row,
|
||||
Object: runtime.RawExtension{Object: &nodes[i]},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (m *nodeMetrics) getNodeMetrics(names ...string) ([]metrics.NodeMetrics, error) {
|
||||
timestamps, usages, err := m.metrics.GetNodeMetrics(names...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := make([]metrics.NodeMetrics, 0, len(names))
|
||||
|
||||
for i, name := range names {
|
||||
if usages[i] == nil {
|
||||
klog.Errorf("unable to fetch node metrics for node %q: no metrics known for node", name)
|
||||
|
||||
continue
|
||||
}
|
||||
res = append(res, metrics.NodeMetrics{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
CreationTimestamp: metav1.NewTime(time.Now()),
|
||||
},
|
||||
Timestamp: metav1.NewTime(timestamps[i].Timestamp),
|
||||
Window: metav1.Duration{Duration: timestamps[i].Window},
|
||||
Usage: usages[i],
|
||||
})
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (m *nodeMetrics) NamespaceScoped() bool {
|
||||
return false
|
||||
}
|
||||
266
vendor/sigs.k8s.io/metrics-server/pkg/api/pod.go
generated
vendored
Normal file
266
vendor/sigs.k8s.io/metrics-server/pkg/api/pod.go
generated
vendored
Normal file
|
|
@ -0,0 +1,266 @@
|
|||
// Copyright 2018 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 api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
|
||||
"k8s.io/apimachinery/pkg/fields"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
apitypes "k8s.io/apimachinery/pkg/types"
|
||||
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
||||
"k8s.io/apiserver/pkg/registry/generic"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
v1listers "k8s.io/client-go/listers/core/v1"
|
||||
"k8s.io/klog"
|
||||
"k8s.io/metrics/pkg/apis/metrics"
|
||||
_ "k8s.io/metrics/pkg/apis/metrics/install"
|
||||
)
|
||||
|
||||
type podMetrics struct {
|
||||
groupResource schema.GroupResource
|
||||
metrics PodMetricsGetter
|
||||
podLister v1listers.PodLister
|
||||
}
|
||||
|
||||
var _ rest.KindProvider = &podMetrics{}
|
||||
var _ rest.Storage = &podMetrics{}
|
||||
var _ rest.Getter = &podMetrics{}
|
||||
var _ rest.Lister = &podMetrics{}
|
||||
var _ rest.TableConvertor = &podMetrics{}
|
||||
|
||||
func newPodMetrics(groupResource schema.GroupResource, metrics PodMetricsGetter, podLister v1listers.PodLister) *podMetrics {
|
||||
return &podMetrics{
|
||||
groupResource: groupResource,
|
||||
metrics: metrics,
|
||||
podLister: podLister,
|
||||
}
|
||||
}
|
||||
|
||||
// Storage interface
|
||||
func (m *podMetrics) New() runtime.Object {
|
||||
return &metrics.PodMetrics{}
|
||||
}
|
||||
|
||||
// KindProvider interface
|
||||
func (m *podMetrics) Kind() string {
|
||||
return "PodMetrics"
|
||||
}
|
||||
|
||||
// Lister interface
|
||||
func (m *podMetrics) NewList() runtime.Object {
|
||||
return &metrics.PodMetricsList{}
|
||||
}
|
||||
|
||||
// Lister interface
|
||||
func (m *podMetrics) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
|
||||
labelSelector := labels.Everything()
|
||||
if options != nil && options.LabelSelector != nil {
|
||||
labelSelector = options.LabelSelector
|
||||
}
|
||||
|
||||
namespace := genericapirequest.NamespaceValue(ctx)
|
||||
pods, err := m.podLister.Pods(namespace).List(labelSelector)
|
||||
if err != nil {
|
||||
errMsg := fmt.Errorf("Error while listing pods for selector %v in namespace %q: %v", labelSelector, namespace, err)
|
||||
klog.Error(errMsg)
|
||||
return &metrics.PodMetricsList{}, errMsg
|
||||
}
|
||||
|
||||
// currently the PodLister API does not support filtering using FieldSelectors, we have to filter manually
|
||||
if options != nil && options.FieldSelector != nil {
|
||||
newPods := make([]*v1.Pod, 0, len(pods))
|
||||
fields := make(fields.Set, 2)
|
||||
for _, pod := range pods {
|
||||
for k := range fields {
|
||||
delete(fields, k)
|
||||
}
|
||||
fieldsSet := generic.AddObjectMetaFieldsSet(fields, &pod.ObjectMeta, true)
|
||||
if !options.FieldSelector.Matches(fieldsSet) {
|
||||
continue
|
||||
}
|
||||
newPods = append(newPods, pod)
|
||||
}
|
||||
pods = newPods
|
||||
}
|
||||
|
||||
// maintain the same ordering invariant as the Kube API would over pods
|
||||
sort.Slice(pods, func(i, j int) bool {
|
||||
if pods[i].Namespace != pods[j].Namespace {
|
||||
return pods[i].Namespace < pods[j].Namespace
|
||||
}
|
||||
return pods[i].Name < pods[j].Name
|
||||
})
|
||||
|
||||
metricsItems, err := m.getPodMetrics(pods...)
|
||||
if err != nil {
|
||||
errMsg := fmt.Errorf("Error while fetching pod metrics for selector %v in namespace %q: %v", labelSelector, namespace, err)
|
||||
klog.Error(errMsg)
|
||||
return &metrics.PodMetricsList{}, errMsg
|
||||
}
|
||||
|
||||
return &metrics.PodMetricsList{Items: metricsItems}, nil
|
||||
}
|
||||
|
||||
// Getter interface
|
||||
func (m *podMetrics) Get(ctx context.Context, name string, opts *metav1.GetOptions) (runtime.Object, error) {
|
||||
namespace := genericapirequest.NamespaceValue(ctx)
|
||||
|
||||
pod, err := m.podLister.Pods(namespace).Get(name)
|
||||
if err != nil {
|
||||
errMsg := fmt.Errorf("Error while getting pod %v: %v", name, err)
|
||||
klog.Error(errMsg)
|
||||
if errors.IsNotFound(err) {
|
||||
// return not-found errors directly
|
||||
return &metrics.PodMetrics{}, err
|
||||
}
|
||||
return &metrics.PodMetrics{}, errMsg
|
||||
}
|
||||
if pod == nil {
|
||||
return &metrics.PodMetrics{}, errors.NewNotFound(v1.Resource("pods"), fmt.Sprintf("%v/%v", namespace, name))
|
||||
}
|
||||
|
||||
podMetrics, err := m.getPodMetrics(pod)
|
||||
if err == nil && len(podMetrics) == 0 {
|
||||
err = fmt.Errorf("no metrics known for pod \"%s/%s\"", pod.Namespace, pod.Name)
|
||||
}
|
||||
if err != nil {
|
||||
klog.Errorf("unable to fetch pod metrics for pod %s/%s: %v", pod.Namespace, pod.Name, err)
|
||||
return nil, errors.NewNotFound(m.groupResource, fmt.Sprintf("%v/%v", namespace, name))
|
||||
}
|
||||
return &podMetrics[0], nil
|
||||
}
|
||||
|
||||
func (m *podMetrics) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1beta1.Table, error) {
|
||||
var table metav1beta1.Table
|
||||
|
||||
switch t := object.(type) {
|
||||
case *metrics.PodMetrics:
|
||||
table.ResourceVersion = t.ResourceVersion
|
||||
table.SelfLink = t.SelfLink
|
||||
addPodMetricsToTable(&table, *t)
|
||||
case *metrics.PodMetricsList:
|
||||
table.ResourceVersion = t.ResourceVersion
|
||||
table.SelfLink = t.SelfLink
|
||||
table.Continue = t.Continue
|
||||
addPodMetricsToTable(&table, t.Items...)
|
||||
default:
|
||||
}
|
||||
|
||||
return &table, nil
|
||||
}
|
||||
|
||||
func addPodMetricsToTable(table *metav1beta1.Table, pods ...metrics.PodMetrics) {
|
||||
usage := make(v1.ResourceList, 3)
|
||||
var names []string
|
||||
for i, pod := range pods {
|
||||
for k := range usage {
|
||||
delete(usage, k)
|
||||
}
|
||||
for _, container := range pod.Containers {
|
||||
for k, v := range container.Usage {
|
||||
u := usage[k]
|
||||
u.Add(v)
|
||||
usage[k] = u
|
||||
}
|
||||
}
|
||||
if names == nil {
|
||||
for k := range usage {
|
||||
names = append(names, string(k))
|
||||
}
|
||||
sort.Strings(names)
|
||||
|
||||
table.ColumnDefinitions = []metav1beta1.TableColumnDefinition{
|
||||
{Name: "Name", Type: "string", Format: "name", Description: "Name of the resource"},
|
||||
}
|
||||
for _, name := range names {
|
||||
table.ColumnDefinitions = append(table.ColumnDefinitions, metav1beta1.TableColumnDefinition{
|
||||
Name: name,
|
||||
Type: "string",
|
||||
Format: "quantity",
|
||||
})
|
||||
}
|
||||
table.ColumnDefinitions = append(table.ColumnDefinitions, metav1beta1.TableColumnDefinition{
|
||||
Name: "Window",
|
||||
Type: "string",
|
||||
Format: "duration",
|
||||
})
|
||||
}
|
||||
row := make([]interface{}, 0, len(names)+1)
|
||||
row = append(row, pod.Name)
|
||||
for _, name := range names {
|
||||
v := usage[v1.ResourceName(name)]
|
||||
row = append(row, v.String())
|
||||
}
|
||||
row = append(row, pod.Window.Duration.String())
|
||||
table.Rows = append(table.Rows, metav1beta1.TableRow{
|
||||
Cells: row,
|
||||
Object: runtime.RawExtension{Object: &pods[i]},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (m *podMetrics) getPodMetrics(pods ...*v1.Pod) ([]metrics.PodMetrics, error) {
|
||||
namespacedNames := make([]apitypes.NamespacedName, len(pods))
|
||||
for i, pod := range pods {
|
||||
namespacedNames[i] = apitypes.NamespacedName{
|
||||
Name: pod.Name,
|
||||
Namespace: pod.Namespace,
|
||||
}
|
||||
}
|
||||
timestamps, containerMetrics, err := m.metrics.GetContainerMetrics(namespacedNames...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := make([]metrics.PodMetrics, 0, len(pods))
|
||||
|
||||
for i, pod := range pods {
|
||||
if pod.Status.Phase != v1.PodRunning {
|
||||
// ignore pod not in Running phase
|
||||
continue
|
||||
}
|
||||
if containerMetrics[i] == nil {
|
||||
klog.Errorf("unable to fetch pod metrics for pod %s/%s: no metrics known for pod", pod.Namespace, pod.Name)
|
||||
continue
|
||||
}
|
||||
|
||||
res = append(res, metrics.PodMetrics{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: pod.Name,
|
||||
Namespace: pod.Namespace,
|
||||
CreationTimestamp: metav1.NewTime(time.Now()),
|
||||
},
|
||||
Timestamp: metav1.NewTime(timestamps[i].Timestamp),
|
||||
Window: metav1.Duration{Duration: timestamps[i].Window},
|
||||
Containers: containerMetrics[i],
|
||||
})
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (m *podMetrics) NamespaceScoped() bool {
|
||||
return true
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue