Merge pull request #80561 from seans3/move-metricsutil-staging
Move pkg/kubectl/metricsutil to staging
This commit is contained in:
@@ -85,7 +85,6 @@ filegroup(
|
||||
"//pkg/kubectl/cmd:all-srcs",
|
||||
"//pkg/kubectl/explain:all-srcs",
|
||||
"//pkg/kubectl/generate:all-srcs",
|
||||
"//pkg/kubectl/metricsutil:all-srcs",
|
||||
"//pkg/kubectl/polymorphichelpers:all-srcs",
|
||||
"//pkg/kubectl/proxy:all-srcs",
|
||||
],
|
||||
|
@@ -10,7 +10,6 @@ go_library(
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/top",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//pkg/kubectl/metricsutil:go_default_library",
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||
@@ -18,6 +17,7 @@ go_library(
|
||||
"//staging/src/k8s.io/client-go/discovery:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/cmd/util:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/metricsutil:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util/i18n:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util/templates:go_default_library",
|
||||
"//staging/src/k8s.io/metrics/pkg/apis/metrics:go_default_library",
|
||||
|
@@ -28,9 +28,9 @@ import (
|
||||
"k8s.io/client-go/discovery"
|
||||
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||
"k8s.io/kubectl/pkg/metricsutil"
|
||||
"k8s.io/kubectl/pkg/util/i18n"
|
||||
"k8s.io/kubectl/pkg/util/templates"
|
||||
"k8s.io/kubernetes/pkg/kubectl/metricsutil"
|
||||
metricsapi "k8s.io/metrics/pkg/apis/metrics"
|
||||
metricsV1beta1api "k8s.io/metrics/pkg/apis/metrics/v1beta1"
|
||||
metricsclientset "k8s.io/metrics/pkg/client/clientset/versioned"
|
||||
|
@@ -27,9 +27,9 @@ import (
|
||||
"k8s.io/client-go/discovery"
|
||||
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||
"k8s.io/kubectl/pkg/metricsutil"
|
||||
"k8s.io/kubectl/pkg/util/i18n"
|
||||
"k8s.io/kubectl/pkg/util/templates"
|
||||
"k8s.io/kubernetes/pkg/kubectl/metricsutil"
|
||||
metricsapi "k8s.io/metrics/pkg/apis/metrics"
|
||||
metricsv1beta1api "k8s.io/metrics/pkg/apis/metrics/v1beta1"
|
||||
metricsclientset "k8s.io/metrics/pkg/client/clientset/versioned"
|
||||
|
@@ -1,44 +0,0 @@
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"metrics_client.go",
|
||||
"metrics_printer.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/metricsutil",
|
||||
visibility = [
|
||||
"//build/visible_to:pkg_kubectl_metricsutil_CONSUMERS",
|
||||
],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/validation:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/scheme:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util/printers:go_default_library",
|
||||
"//staging/src/k8s.io/metrics/pkg/apis/metrics:go_default_library",
|
||||
"//staging/src/k8s.io/metrics/pkg/apis/metrics/v1alpha1:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = [
|
||||
"//build/visible_to:pkg_kubectl_metricsutil_CONSUMERS",
|
||||
],
|
||||
)
|
@@ -1,170 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 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 metricsutil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/validation"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
metricsapi "k8s.io/metrics/pkg/apis/metrics"
|
||||
metricsv1alpha1api "k8s.io/metrics/pkg/apis/metrics/v1alpha1"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultHeapsterNamespace = "kube-system"
|
||||
DefaultHeapsterScheme = "http"
|
||||
DefaultHeapsterService = "heapster"
|
||||
DefaultHeapsterPort = "" // use the first exposed port on the service
|
||||
)
|
||||
|
||||
var (
|
||||
prefix = "/apis"
|
||||
groupVersion = fmt.Sprintf("%s/%s", metricsGv.Group, metricsGv.Version)
|
||||
metricsRoot = fmt.Sprintf("%s/%s", prefix, groupVersion)
|
||||
|
||||
// TODO: get this from metrics api once it's finished
|
||||
metricsGv = schema.GroupVersion{Group: "metrics", Version: "v1alpha1"}
|
||||
)
|
||||
|
||||
type HeapsterMetricsClient struct {
|
||||
SVCClient corev1client.ServicesGetter
|
||||
HeapsterNamespace string
|
||||
HeapsterScheme string
|
||||
HeapsterService string
|
||||
HeapsterPort string
|
||||
}
|
||||
|
||||
func NewHeapsterMetricsClient(svcClient corev1client.ServicesGetter, namespace, scheme, service, port string) *HeapsterMetricsClient {
|
||||
return &HeapsterMetricsClient{
|
||||
SVCClient: svcClient,
|
||||
HeapsterNamespace: namespace,
|
||||
HeapsterScheme: scheme,
|
||||
HeapsterService: service,
|
||||
HeapsterPort: port,
|
||||
}
|
||||
}
|
||||
|
||||
func podMetricsURL(namespace string, name string) (string, error) {
|
||||
if namespace == metav1.NamespaceAll {
|
||||
return fmt.Sprintf("%s/pods", metricsRoot), nil
|
||||
}
|
||||
errs := validation.ValidateNamespaceName(namespace, false)
|
||||
if len(errs) > 0 {
|
||||
message := fmt.Sprintf("invalid namespace: %s - %v", namespace, errs)
|
||||
return "", errors.New(message)
|
||||
}
|
||||
if len(name) > 0 {
|
||||
errs = validation.NameIsDNSSubdomain(name, false)
|
||||
if len(errs) > 0 {
|
||||
message := fmt.Sprintf("invalid pod name: %s - %v", name, errs)
|
||||
return "", errors.New(message)
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s/namespaces/%s/pods/%s", metricsRoot, namespace, name), nil
|
||||
}
|
||||
|
||||
func nodeMetricsURL(name string) (string, error) {
|
||||
if len(name) > 0 {
|
||||
errs := validation.NameIsDNSSubdomain(name, false)
|
||||
if len(errs) > 0 {
|
||||
message := fmt.Sprintf("invalid node name: %s - %v", name, errs)
|
||||
return "", errors.New(message)
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s/nodes/%s", metricsRoot, name), nil
|
||||
}
|
||||
|
||||
func (cli *HeapsterMetricsClient) GetNodeMetrics(nodeName string, selector string) (*metricsapi.NodeMetricsList, error) {
|
||||
params := map[string]string{"labelSelector": selector}
|
||||
path, err := nodeMetricsURL(nodeName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resultRaw, err := GetHeapsterMetrics(cli, path, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
versionedMetrics := metricsv1alpha1api.NodeMetricsList{}
|
||||
if len(nodeName) == 0 {
|
||||
err = json.Unmarshal(resultRaw, &versionedMetrics)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshall heapster response: %v", err)
|
||||
}
|
||||
} else {
|
||||
var singleMetric metricsv1alpha1api.NodeMetrics
|
||||
err = json.Unmarshal(resultRaw, &singleMetric)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshall heapster response: %v", err)
|
||||
}
|
||||
versionedMetrics.Items = []metricsv1alpha1api.NodeMetrics{singleMetric}
|
||||
}
|
||||
metrics := &metricsapi.NodeMetricsList{}
|
||||
err = metricsv1alpha1api.Convert_v1alpha1_NodeMetricsList_To_metrics_NodeMetricsList(&versionedMetrics, metrics, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return metrics, nil
|
||||
}
|
||||
|
||||
func (cli *HeapsterMetricsClient) GetPodMetrics(namespace string, podName string, allNamespaces bool, selector labels.Selector) (*metricsapi.PodMetricsList, error) {
|
||||
if allNamespaces {
|
||||
namespace = metav1.NamespaceAll
|
||||
}
|
||||
path, err := podMetricsURL(namespace, podName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
params := map[string]string{"labelSelector": selector.String()}
|
||||
versionedMetrics := metricsv1alpha1api.PodMetricsList{}
|
||||
|
||||
resultRaw, err := GetHeapsterMetrics(cli, path, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(podName) == 0 {
|
||||
err = json.Unmarshal(resultRaw, &versionedMetrics)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshall heapster response: %v", err)
|
||||
}
|
||||
} else {
|
||||
var singleMetric metricsv1alpha1api.PodMetrics
|
||||
err = json.Unmarshal(resultRaw, &singleMetric)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshall heapster response: %v", err)
|
||||
}
|
||||
versionedMetrics.Items = []metricsv1alpha1api.PodMetrics{singleMetric}
|
||||
}
|
||||
metrics := &metricsapi.PodMetricsList{}
|
||||
err = metricsv1alpha1api.Convert_v1alpha1_PodMetricsList_To_metrics_PodMetricsList(&versionedMetrics, metrics, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return metrics, nil
|
||||
}
|
||||
|
||||
func GetHeapsterMetrics(cli *HeapsterMetricsClient, path string, params map[string]string) ([]byte, error) {
|
||||
return cli.SVCClient.Services(cli.HeapsterNamespace).
|
||||
ProxyGet(cli.HeapsterScheme, cli.HeapsterService, cli.HeapsterPort, path, params).
|
||||
DoRaw()
|
||||
}
|
@@ -1,323 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 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 metricsutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
"k8s.io/kubectl/pkg/util/printers"
|
||||
metricsapi "k8s.io/metrics/pkg/apis/metrics"
|
||||
)
|
||||
|
||||
var (
|
||||
MeasuredResources = []v1.ResourceName{
|
||||
v1.ResourceCPU,
|
||||
v1.ResourceMemory,
|
||||
}
|
||||
NodeColumns = []string{"NAME", "CPU(cores)", "CPU%", "MEMORY(bytes)", "MEMORY%"}
|
||||
PodColumns = []string{"NAME", "CPU(cores)", "MEMORY(bytes)"}
|
||||
NamespaceColumn = "NAMESPACE"
|
||||
PodColumn = "POD"
|
||||
)
|
||||
|
||||
type ResourceMetricsInfo struct {
|
||||
Name string
|
||||
Metrics v1.ResourceList
|
||||
Available v1.ResourceList
|
||||
}
|
||||
|
||||
type TopCmdPrinter struct {
|
||||
out io.Writer
|
||||
}
|
||||
|
||||
func NewTopCmdPrinter(out io.Writer) *TopCmdPrinter {
|
||||
return &TopCmdPrinter{out: out}
|
||||
}
|
||||
|
||||
type NodeMetricsSorter struct {
|
||||
metrics []metricsapi.NodeMetrics
|
||||
sortBy string
|
||||
usages []v1.ResourceList
|
||||
}
|
||||
|
||||
func (n *NodeMetricsSorter) Len() int {
|
||||
return len(n.metrics)
|
||||
}
|
||||
|
||||
func (n *NodeMetricsSorter) Swap(i, j int) {
|
||||
n.metrics[i], n.metrics[j] = n.metrics[j], n.metrics[i]
|
||||
}
|
||||
|
||||
func (n *NodeMetricsSorter) Less(i, j int) bool {
|
||||
switch n.sortBy {
|
||||
case "cpu":
|
||||
qi := n.usages[i][v1.ResourceCPU]
|
||||
qj := n.usages[j][v1.ResourceCPU]
|
||||
return qi.Value() > qj.Value()
|
||||
case "memory":
|
||||
qi := n.usages[i][v1.ResourceMemory]
|
||||
qj := n.usages[j][v1.ResourceMemory]
|
||||
return qi.Value() > qj.Value()
|
||||
default:
|
||||
return n.metrics[i].Name < n.metrics[j].Name
|
||||
}
|
||||
}
|
||||
|
||||
func NewNodeMetricsSorter(metrics []metricsapi.NodeMetrics, sortBy string) (*NodeMetricsSorter, error) {
|
||||
var usages = make([]v1.ResourceList, len(metrics))
|
||||
if len(sortBy) > 0 {
|
||||
for i, v := range metrics {
|
||||
if err := scheme.Scheme.Convert(&v.Usage, &usages[i], nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &NodeMetricsSorter{
|
||||
metrics: metrics,
|
||||
sortBy: sortBy,
|
||||
usages: usages,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type PodMetricsSorter struct {
|
||||
metrics []metricsapi.PodMetrics
|
||||
sortBy string
|
||||
withNamespace bool
|
||||
podMetrics []v1.ResourceList
|
||||
}
|
||||
|
||||
func (p *PodMetricsSorter) Len() int {
|
||||
return len(p.metrics)
|
||||
}
|
||||
|
||||
func (p *PodMetricsSorter) Swap(i, j int) {
|
||||
p.metrics[i], p.metrics[j] = p.metrics[j], p.metrics[i]
|
||||
}
|
||||
|
||||
func (p *PodMetricsSorter) Less(i, j int) bool {
|
||||
switch p.sortBy {
|
||||
case "cpu":
|
||||
qi := p.podMetrics[i][v1.ResourceCPU]
|
||||
qj := p.podMetrics[j][v1.ResourceCPU]
|
||||
return qi.Value() > qj.Value()
|
||||
case "memory":
|
||||
qi := p.podMetrics[i][v1.ResourceMemory]
|
||||
qj := p.podMetrics[j][v1.ResourceMemory]
|
||||
return qi.Value() > qj.Value()
|
||||
default:
|
||||
if p.withNamespace && p.metrics[i].Namespace != p.metrics[j].Namespace {
|
||||
return p.metrics[i].Namespace < p.metrics[j].Namespace
|
||||
}
|
||||
return p.metrics[i].Name < p.metrics[j].Name
|
||||
}
|
||||
}
|
||||
|
||||
func NewPodMetricsSorter(metrics []metricsapi.PodMetrics, printContainers bool, withNamespace bool, sortBy string) (*PodMetricsSorter, error) {
|
||||
var podMetrics = make([]v1.ResourceList, len(metrics))
|
||||
if len(sortBy) > 0 {
|
||||
for i, v := range metrics {
|
||||
podMetrics[i], _, _ = getPodMetrics(&v, printContainers)
|
||||
}
|
||||
}
|
||||
|
||||
return &PodMetricsSorter{
|
||||
metrics: metrics,
|
||||
sortBy: sortBy,
|
||||
withNamespace: withNamespace,
|
||||
podMetrics: podMetrics,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (printer *TopCmdPrinter) PrintNodeMetrics(metrics []metricsapi.NodeMetrics, availableResources map[string]v1.ResourceList, noHeaders bool, sortBy string) error {
|
||||
if len(metrics) == 0 {
|
||||
return nil
|
||||
}
|
||||
w := printers.GetNewTabWriter(printer.out)
|
||||
defer w.Flush()
|
||||
|
||||
n, err := NewNodeMetricsSorter(metrics, sortBy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Sort(n)
|
||||
|
||||
if !noHeaders {
|
||||
printColumnNames(w, NodeColumns)
|
||||
}
|
||||
var usage v1.ResourceList
|
||||
for _, m := range metrics {
|
||||
err := scheme.Scheme.Convert(&m.Usage, &usage, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printMetricsLine(w, &ResourceMetricsInfo{
|
||||
Name: m.Name,
|
||||
Metrics: usage,
|
||||
Available: availableResources[m.Name],
|
||||
})
|
||||
delete(availableResources, m.Name)
|
||||
}
|
||||
|
||||
// print lines for nodes of which the metrics is unreachable.
|
||||
for nodeName := range availableResources {
|
||||
printMissingMetricsNodeLine(w, nodeName)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (printer *TopCmdPrinter) PrintPodMetrics(metrics []metricsapi.PodMetrics, printContainers bool, withNamespace bool, noHeaders bool, sortBy string) error {
|
||||
if len(metrics) == 0 {
|
||||
return nil
|
||||
}
|
||||
w := printers.GetNewTabWriter(printer.out)
|
||||
defer w.Flush()
|
||||
if !noHeaders {
|
||||
if withNamespace {
|
||||
printValue(w, NamespaceColumn)
|
||||
}
|
||||
if printContainers {
|
||||
printValue(w, PodColumn)
|
||||
}
|
||||
printColumnNames(w, PodColumns)
|
||||
}
|
||||
|
||||
p, err := NewPodMetricsSorter(metrics, printContainers, withNamespace, sortBy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Sort(p)
|
||||
|
||||
for _, m := range metrics {
|
||||
err := printSinglePodMetrics(w, &m, printContainers, withNamespace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func printColumnNames(out io.Writer, names []string) {
|
||||
for _, name := range names {
|
||||
printValue(out, name)
|
||||
}
|
||||
fmt.Fprint(out, "\n")
|
||||
}
|
||||
|
||||
func printSinglePodMetrics(out io.Writer, m *metricsapi.PodMetrics, printContainersOnly bool, withNamespace bool) error {
|
||||
podMetrics, containers, err := getPodMetrics(m, printContainersOnly)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if printContainersOnly {
|
||||
for contName := range containers {
|
||||
if withNamespace {
|
||||
printValue(out, m.Namespace)
|
||||
}
|
||||
printValue(out, m.Name)
|
||||
printMetricsLine(out, &ResourceMetricsInfo{
|
||||
Name: contName,
|
||||
Metrics: containers[contName],
|
||||
Available: v1.ResourceList{},
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if withNamespace {
|
||||
printValue(out, m.Namespace)
|
||||
}
|
||||
printMetricsLine(out, &ResourceMetricsInfo{
|
||||
Name: m.Name,
|
||||
Metrics: podMetrics,
|
||||
Available: v1.ResourceList{},
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getPodMetrics(m *metricsapi.PodMetrics, printContainersOnly bool) (v1.ResourceList, map[string]v1.ResourceList, error) {
|
||||
containers := make(map[string]v1.ResourceList)
|
||||
podMetrics := make(v1.ResourceList)
|
||||
for _, res := range MeasuredResources {
|
||||
podMetrics[res], _ = resource.ParseQuantity("0")
|
||||
}
|
||||
|
||||
for _, c := range m.Containers {
|
||||
var usage v1.ResourceList
|
||||
if err := scheme.Scheme.Convert(&c.Usage, &usage, nil); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
containers[c.Name] = usage
|
||||
if !printContainersOnly {
|
||||
for _, res := range MeasuredResources {
|
||||
quantity := podMetrics[res]
|
||||
quantity.Add(usage[res])
|
||||
podMetrics[res] = quantity
|
||||
}
|
||||
}
|
||||
}
|
||||
return podMetrics, containers, nil
|
||||
}
|
||||
|
||||
func printMetricsLine(out io.Writer, metrics *ResourceMetricsInfo) {
|
||||
printValue(out, metrics.Name)
|
||||
printAllResourceUsages(out, metrics)
|
||||
fmt.Fprint(out, "\n")
|
||||
}
|
||||
|
||||
func printMissingMetricsNodeLine(out io.Writer, nodeName string) {
|
||||
printValue(out, nodeName)
|
||||
unknownMetricsStatus := "<unknown>"
|
||||
for i := 0; i < len(MeasuredResources); i++ {
|
||||
printValue(out, unknownMetricsStatus)
|
||||
printValue(out, "\t")
|
||||
printValue(out, unknownMetricsStatus)
|
||||
printValue(out, "\t")
|
||||
}
|
||||
fmt.Fprint(out, "\n")
|
||||
}
|
||||
|
||||
func printValue(out io.Writer, value interface{}) {
|
||||
fmt.Fprintf(out, "%v\t", value)
|
||||
}
|
||||
|
||||
func printAllResourceUsages(out io.Writer, metrics *ResourceMetricsInfo) {
|
||||
for _, res := range MeasuredResources {
|
||||
quantity := metrics.Metrics[res]
|
||||
printSingleResourceUsage(out, res, quantity)
|
||||
fmt.Fprint(out, "\t")
|
||||
if available, found := metrics.Available[res]; found {
|
||||
fraction := float64(quantity.MilliValue()) / float64(available.MilliValue()) * 100
|
||||
fmt.Fprintf(out, "%d%%\t", int64(fraction))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func printSingleResourceUsage(out io.Writer, resourceType v1.ResourceName, quantity resource.Quantity) {
|
||||
switch resourceType {
|
||||
case v1.ResourceCPU:
|
||||
fmt.Fprintf(out, "%vm", quantity.MilliValue())
|
||||
case v1.ResourceMemory:
|
||||
fmt.Fprintf(out, "%vMi", quantity.Value()/(1024*1024))
|
||||
default:
|
||||
fmt.Fprintf(out, "%v", quantity.Value())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user