Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crictl: add metricsp command #1228

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/crictl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func main() {
configCommand,
statsCommand,
podStatsCommand,
podMetricsCommand,
completionCommand,
checkpointContainerCommand,
runtimeConfigCommand,
Expand Down
122 changes: 122 additions & 0 deletions cmd/crictl/pod_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
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 main

import (
"fmt"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"golang.org/x/net/context"
cri "k8s.io/cri-api/pkg/apis"
pb "k8s.io/cri-api/pkg/apis/runtime/v1"
)

type podMetricsOptions struct {
// output format
output string

// live watch
watch bool
}

var podMetricsCommand = &cli.Command{
Name: "metricsp",
Usage: "List pod metrics. Metrics are unstructured key/value pairs gathered by CRI meant to replace cAdvisor's /metrics/cadvisor endpoint.",
UseShortOptionHandling: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "Output format, One of: json|yaml",
},
&cli.BoolFlag{
Name: "watch",
Aliases: []string{"w"},
Usage: "Watch pod metrics",
},
},
Action: func(c *cli.Context) error {
if c.NArg() > 0 {
return cli.ShowSubcommandHelp(c)
}

client, err := getRuntimeService(c, 0)
if err != nil {
return fmt.Errorf("get runtime service: %w", err)
}

opts := podMetricsOptions{
output: c.String("output"),
watch: c.Bool("watch"),
}

switch opts.output {
case "json", "yaml", "":
default:
return cli.ShowSubcommandHelp(c)
}

if err := podMetrics(c.Context, client, opts); err != nil {
return fmt.Errorf("get pod metrics: %w", err)
}

return nil
},
}

func podMetrics(
c context.Context,
client cri.RuntimeService,
opts podMetricsOptions,
) error {
d := podMetricsDisplayer{opts}
return handleDisplay(c, client, opts.watch, d.displayPodMetrics)
}

type podMetricsDisplayer struct {
opts podMetricsOptions
}

func (p *podMetricsDisplayer) displayPodMetrics(
c context.Context,
client cri.RuntimeService,
) error {
metrics, err := podSandboxMetrics(client)
if err != nil {
return err
}

response := &pb.ListPodSandboxMetricsResponse{PodMetrics: metrics}
switch p.opts.output {
case "json", "":
return outputProtobufObjAsJSON(response)
case "yaml":
return outputProtobufObjAsYAML(response)
}
return nil
}

func podSandboxMetrics(client cri.RuntimeService) ([]*pb.PodSandboxMetrics, error) {
metrics, err := client.ListPodSandboxMetrics(context.TODO())
if err != nil {
return nil, fmt.Errorf("list pod sandbox metrics: %w", err)
}
logrus.Debugf("PodMetrics: %v", metrics)

return metrics, nil
}
2 changes: 1 addition & 1 deletion cmd/crictl/pod_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type podStatsOptions struct {

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