|
| 1 | +/* |
| 2 | +Copyright 2025 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 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/sirupsen/logrus" |
| 24 | + "github.com/urfave/cli/v2" |
| 25 | + cri "k8s.io/cri-api/pkg/apis" |
| 26 | + pb "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 27 | +) |
| 28 | + |
| 29 | +type metricDescriptorsOptions struct { |
| 30 | + // output format |
| 31 | + output string |
| 32 | +} |
| 33 | + |
| 34 | +var metricDescriptorsCommand = &cli.Command{ |
| 35 | + Name: "metricdescs", |
| 36 | + Usage: "List metric descriptors. Returns information about the metrics available through the CRI.", |
| 37 | + UseShortOptionHandling: true, |
| 38 | + Flags: []cli.Flag{ |
| 39 | + &cli.StringFlag{ |
| 40 | + Name: "output", |
| 41 | + Aliases: []string{"o"}, |
| 42 | + Usage: "Output format, One of: json|yaml", |
| 43 | + }, |
| 44 | + }, |
| 45 | + Action: func(c *cli.Context) error { |
| 46 | + if c.NArg() > 0 { |
| 47 | + return cli.ShowSubcommandHelp(c) |
| 48 | + } |
| 49 | + |
| 50 | + client, err := getRuntimeService(c, 0) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("get runtime service: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + opts := metricDescriptorsOptions{ |
| 56 | + output: c.String("output"), |
| 57 | + } |
| 58 | + |
| 59 | + switch opts.output { |
| 60 | + case outputTypeJSON, outputTypeYAML, "": |
| 61 | + default: |
| 62 | + return cli.ShowSubcommandHelp(c) |
| 63 | + } |
| 64 | + |
| 65 | + if err := metricDescriptors(c.Context, client, opts); err != nil { |
| 66 | + return fmt.Errorf("get metric descriptors: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | + }, |
| 71 | +} |
| 72 | + |
| 73 | +func metricDescriptors( |
| 74 | + c context.Context, |
| 75 | + client cri.RuntimeService, |
| 76 | + opts metricDescriptorsOptions, |
| 77 | +) error { |
| 78 | + d := metricDescriptorsDisplayer{opts} |
| 79 | + |
| 80 | + return d.displayMetricDescriptors(c, client) |
| 81 | +} |
| 82 | + |
| 83 | +type metricDescriptorsDisplayer struct { |
| 84 | + opts metricDescriptorsOptions |
| 85 | +} |
| 86 | + |
| 87 | +func (m *metricDescriptorsDisplayer) displayMetricDescriptors( |
| 88 | + c context.Context, |
| 89 | + client cri.RuntimeService, |
| 90 | +) error { |
| 91 | + descriptors, err := listMetricDescriptors(c, client) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + response := &pb.ListMetricDescriptorsResponse{Descriptors: descriptors} |
| 97 | + |
| 98 | + switch m.opts.output { |
| 99 | + case outputTypeJSON, "": |
| 100 | + return outputProtobufObjAsJSON(response) |
| 101 | + case outputTypeYAML: |
| 102 | + return outputProtobufObjAsYAML(response) |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func listMetricDescriptors(ctx context.Context, client cri.RuntimeService) ([]*pb.MetricDescriptor, error) { |
| 109 | + descriptors, err := InterruptableRPC(ctx, func(ctx context.Context) ([]*pb.MetricDescriptor, error) { |
| 110 | + return client.ListMetricDescriptors(ctx) |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + return nil, fmt.Errorf("list metric descriptors: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + logrus.Debugf("MetricDescriptors: %v", descriptors) |
| 117 | + |
| 118 | + return descriptors, nil |
| 119 | +} |
0 commit comments