github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadgets/params.go (about) 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 15 package gadgets 16 17 import ( 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 25 const ( 26 ParamInterval = "interval" 27 ParamSortBy = "sort" 28 ParamMaxRows = "max-rows" 29 ) 30 31 const ( 32 LocalContainer params.ValueHint = "local:container" 33 K8SNodeName params.ValueHint = "k8s:node" 34 K8SNodeList params.ValueHint = "k8s:node-list" 35 K8SPodName params.ValueHint = "k8s:pod" 36 K8SNamespace params.ValueHint = "k8s:namespace" 37 K8SContainerName params.ValueHint = "k8s:container" 38 K8SLabels params.ValueHint = "k8s:labels" 39 ) 40 41 // DefaultSort can be implemented in addition to the Gadget interface, to specify the default sorting columns 42 type DefaultSort interface { 43 SortByDefault() []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 49 func GadgetParams(gadget GadgetDesc, gType GadgetType, parser parser.Parser) params.ParamDescs { 50 p := params.ParamDescs{} 51 if gType.IsPeriodic() { 52 p.Add(IntervalParams()...) 53 } 54 if gType.CanSort() { 55 p.Add(SortableParams(gadget, parser)...) 56 } 57 return p 58 } 59 60 func IntervalParams() params.ParamDescs { 61 return params.ParamDescs{ 62 { 63 Key: ParamInterval, 64 Title: "Interval", 65 DefaultValue: "1", 66 TypeHint: params.TypeUint32, 67 Description: "Interval (in Seconds)", 68 }, 69 } 70 } 71 72 func SortableParams(gadget GadgetDesc, parser parser.Parser) params.ParamDescs { 73 if parser == nil { 74 return nil 75 } 76 77 var defaultSort []string 78 if sortInterface, ok := gadget.(DefaultSort); ok { 79 defaultSort = sortInterface.SortByDefault() 80 } 81 82 return params.ParamDescs{ 83 { 84 Key: ParamMaxRows, 85 Title: "Max Rows", 86 Alias: "m", 87 DefaultValue: "50", 88 TypeHint: params.TypeUint32, 89 Description: "Maximum number of rows to return", 90 }, 91 { 92 Key: ParamSortBy, 93 Title: "Sort By", 94 DefaultValue: strings.Join(defaultSort, ","), 95 Description: "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. 103 func ParamsFromMap( 104 paramMap map[string]string, 105 gadgetParams *params.Params, 106 runtimeParams *params.Params, 107 operatorParams params.Collection, 108 ) error { 109 err := gadgetParams.CopyFromMap(paramMap, "") 110 if err != nil { 111 return fmt.Errorf("setting gadget parameters: %w", err) 112 } 113 err = runtimeParams.CopyFromMap(paramMap, "runtime.") 114 if err != nil { 115 return fmt.Errorf("setting runtime parameters: %w", err) 116 } 117 err = operatorParams.CopyFromMap(paramMap, "operator.") 118 if err != nil { 119 return fmt.Errorf("setting operator parameters: %w", err) 120 } 121 return 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. 126 func ParamsToMap( 127 paramMap map[string]string, 128 gadgetParams *params.Params, 129 runtimeParams *params.Params, 130 operatorParams params.Collection, 131 ) { 132 gadgetParams.CopyToMap(paramMap, "") 133 runtimeParams.CopyToMap(paramMap, "runtime.") 134 operatorParams.CopyToMap(paramMap, "operator.") 135 }