Skip to content

Commit b9442e6

Browse files
authored
Merge pull request #1228 from haircommander/metrics
crictl: add metricsp command
2 parents 1a88228 + a8a71d9 commit b9442e6

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

cmd/crictl/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func main() {
175175
configCommand,
176176
statsCommand,
177177
podStatsCommand,
178+
podMetricsCommand,
178179
completionCommand,
179180
checkpointContainerCommand,
180181
runtimeConfigCommand,

cmd/crictl/pod_metrics.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}

cmd/crictl/pod_stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type podStatsOptions struct {
4949

5050
var podStatsCommand = &cli.Command{
5151
Name: "statsp",
52-
Usage: "List pod resource usage statistics",
52+
Usage: "List pod statistics. Stats represent a structured API that will fulfill the Kubelet's /stats/summary endpoint.",
5353
UseShortOptionHandling: true,
5454
ArgsUsage: "[ID]",
5555
Flags: []cli.Flag{

0 commit comments

Comments
 (0)