|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/sirupsen/logrus" |
| 23 | + "github.com/urfave/cli/v2" |
| 24 | + "golang.org/x/net/context" |
| 25 | + cri "k8s.io/cri-api/pkg/apis" |
| 26 | + pb "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 27 | +) |
| 28 | + |
| 29 | +type podMetricsOptions struct { |
| 30 | + // output format |
| 31 | + output string |
| 32 | + |
| 33 | + // live watch |
| 34 | + watch bool |
| 35 | +} |
| 36 | + |
| 37 | +var podMetricsCommand = &cli.Command{ |
| 38 | + Name: "metricsp", |
| 39 | + Usage: "List pod metrics. Metrics are unstructured key/value pairs gathered by CRI meant to replace cAdvisor's /metrics/cadvisor endpoint.", |
| 40 | + UseShortOptionHandling: true, |
| 41 | + Flags: []cli.Flag{ |
| 42 | + &cli.StringFlag{ |
| 43 | + Name: "output", |
| 44 | + Aliases: []string{"o"}, |
| 45 | + Usage: "Output format, One of: json|yaml", |
| 46 | + }, |
| 47 | + &cli.BoolFlag{ |
| 48 | + Name: "watch", |
| 49 | + Aliases: []string{"w"}, |
| 50 | + Usage: "Watch pod metrics", |
| 51 | + }, |
| 52 | + }, |
| 53 | + Action: func(c *cli.Context) error { |
| 54 | + if c.NArg() > 0 { |
| 55 | + return cli.ShowSubcommandHelp(c) |
| 56 | + } |
| 57 | + |
| 58 | + client, err := getRuntimeService(c, 0) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("get runtime service: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + opts := podMetricsOptions{ |
| 64 | + output: c.String("output"), |
| 65 | + watch: c.Bool("watch"), |
| 66 | + } |
| 67 | + |
| 68 | + switch opts.output { |
| 69 | + case "json", "yaml", "": |
| 70 | + default: |
| 71 | + return cli.ShowSubcommandHelp(c) |
| 72 | + } |
| 73 | + |
| 74 | + if err := podMetrics(c.Context, client, opts); err != nil { |
| 75 | + return fmt.Errorf("get pod metrics: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | + }, |
| 80 | +} |
| 81 | + |
| 82 | +func podMetrics( |
| 83 | + c context.Context, |
| 84 | + client cri.RuntimeService, |
| 85 | + opts podMetricsOptions, |
| 86 | +) error { |
| 87 | + d := podMetricsDisplayer{opts} |
| 88 | + return handleDisplay(c, client, opts.watch, d.displayPodMetrics) |
| 89 | +} |
| 90 | + |
| 91 | +type podMetricsDisplayer struct { |
| 92 | + opts podMetricsOptions |
| 93 | +} |
| 94 | + |
| 95 | +func (p *podMetricsDisplayer) displayPodMetrics( |
| 96 | + c context.Context, |
| 97 | + client cri.RuntimeService, |
| 98 | +) error { |
| 99 | + metrics, err := podSandboxMetrics(client) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + response := &pb.ListPodSandboxMetricsResponse{PodMetrics: metrics} |
| 105 | + switch p.opts.output { |
| 106 | + case "json", "": |
| 107 | + return outputProtobufObjAsJSON(response) |
| 108 | + case "yaml": |
| 109 | + return outputProtobufObjAsYAML(response) |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func podSandboxMetrics(client cri.RuntimeService) ([]*pb.PodSandboxMetrics, error) { |
| 115 | + metrics, err := client.ListPodSandboxMetrics(context.TODO()) |
| 116 | + if err != nil { |
| 117 | + return nil, fmt.Errorf("list pod sandbox metrics: %w", err) |
| 118 | + } |
| 119 | + logrus.Debugf("PodMetrics: %v", metrics) |
| 120 | + |
| 121 | + return metrics, nil |
| 122 | +} |
0 commit comments