inspektor-gadget
135 строк · 4.0 Кб
1// Copyright 2023 The Inspektor Gadget authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package gadgets
16
17import (
18"fmt"
19"strings"
20
21"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
22"github.com/inspektor-gadget/inspektor-gadget/pkg/parser"
23)
24
25const (
26ParamInterval = "interval"
27ParamSortBy = "sort"
28ParamMaxRows = "max-rows"
29)
30
31const (
32LocalContainer params.ValueHint = "local:container"
33K8SNodeName params.ValueHint = "k8s:node"
34K8SNodeList params.ValueHint = "k8s:node-list"
35K8SPodName params.ValueHint = "k8s:pod"
36K8SNamespace params.ValueHint = "k8s:namespace"
37K8SContainerName params.ValueHint = "k8s:container"
38K8SLabels params.ValueHint = "k8s:labels"
39)
40
41// DefaultSort can be implemented in addition to the Gadget interface, to specify the default sorting columns
42type DefaultSort interface {
43SortByDefault() []string
44}
45
46// GadgetParams returns params specific to the gadgets' type - for example, it returns
47// parameters for 'sort' and 'max-rows' for gadgets with sortable results, and 'interval'
48// for periodically called gadgets
49func GadgetParams(gadget GadgetDesc, gType GadgetType, parser parser.Parser) params.ParamDescs {
50p := params.ParamDescs{}
51if gType.IsPeriodic() {
52p.Add(IntervalParams()...)
53}
54if gType.CanSort() {
55p.Add(SortableParams(gadget, parser)...)
56}
57return p
58}
59
60func IntervalParams() params.ParamDescs {
61return params.ParamDescs{
62{
63Key: ParamInterval,
64Title: "Interval",
65DefaultValue: "1",
66TypeHint: params.TypeUint32,
67Description: "Interval (in Seconds)",
68},
69}
70}
71
72func SortableParams(gadget GadgetDesc, parser parser.Parser) params.ParamDescs {
73if parser == nil {
74return nil
75}
76
77var defaultSort []string
78if sortInterface, ok := gadget.(DefaultSort); ok {
79defaultSort = sortInterface.SortByDefault()
80}
81
82return params.ParamDescs{
83{
84Key: ParamMaxRows,
85Title: "Max Rows",
86Alias: "m",
87DefaultValue: "50",
88TypeHint: params.TypeUint32,
89Description: "Maximum number of rows to return",
90},
91{
92Key: ParamSortBy,
93Title: "Sort By",
94DefaultValue: strings.Join(defaultSort, ","),
95Description: "Sort by columns. Join multiple columns with ','. Prefix a column with '-' to sort in descending order.",
96},
97}
98}
99
100// ParamsFromMap fills the given params (gadget, runtime and operator) using values from `paramMap`. It looks up
101// values using prefixes (see also `ParamsToMap`) and applies verification. If verification for a field fails, an
102// error will be returned.
103func ParamsFromMap(
104paramMap map[string]string,
105gadgetParams *params.Params,
106runtimeParams *params.Params,
107operatorParams params.Collection,
108) error {
109err := gadgetParams.CopyFromMap(paramMap, "")
110if err != nil {
111return fmt.Errorf("setting gadget parameters: %w", err)
112}
113err = runtimeParams.CopyFromMap(paramMap, "runtime.")
114if err != nil {
115return fmt.Errorf("setting runtime parameters: %w", err)
116}
117err = operatorParams.CopyFromMap(paramMap, "operator.")
118if err != nil {
119return fmt.Errorf("setting operator parameters: %w", err)
120}
121return nil
122}
123
124// ParamsToMap adds the given params (gadget, runtime and operator) to the paramMap. It uses prefixes to ensure
125// the keys remain unique.
126func ParamsToMap(
127paramMap map[string]string,
128gadgetParams *params.Params,
129runtimeParams *params.Params,
130operatorParams params.Collection,
131) {
132gadgetParams.CopyToMap(paramMap, "")
133runtimeParams.CopyToMap(paramMap, "runtime.")
134operatorParams.CopyToMap(paramMap, "operator.")
135}
136