github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadgets/trace/dns/tracer/gadget.go (about)

     1  // Copyright 2022-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 tracer
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	gadgetregistry "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-registry"
    22  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
    23  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types"
    24  	"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
    25  	"github.com/inspektor-gadget/inspektor-gadget/pkg/parser"
    26  )
    27  
    28  const (
    29  	ParamDNSTimeout = "dns-timeout"
    30  	ParamPorts      = "ports"
    31  )
    32  
    33  type GadgetDesc struct{}
    34  
    35  func (g *GadgetDesc) Name() string {
    36  	return "dns"
    37  }
    38  
    39  func (g *GadgetDesc) Category() string {
    40  	return gadgets.CategoryTrace
    41  }
    42  
    43  func (g *GadgetDesc) Type() gadgets.GadgetType {
    44  	return gadgets.TypeTrace
    45  }
    46  
    47  func (g *GadgetDesc) Description() string {
    48  	return "Trace DNS requests"
    49  }
    50  
    51  func (g *GadgetDesc) ParamDescs() params.ParamDescs {
    52  	return params.ParamDescs{
    53  		{
    54  			Key:          ParamDNSTimeout,
    55  			Title:        "dns-timeout",
    56  			DefaultValue: "10s",
    57  			Description:  "Timeout waiting for a response to a DNS query (used to calculate latency)",
    58  			TypeHint:     params.TypeDuration,
    59  			Validator: func(value string) error {
    60  				d, err := time.ParseDuration(value)
    61  				if err != nil {
    62  					return err
    63  				}
    64  
    65  				if d <= 0 {
    66  					return fmt.Errorf("DNS timeout must be > 0")
    67  				}
    68  
    69  				return nil
    70  			},
    71  		},
    72  		{
    73  			Key:          ParamPorts,
    74  			Alias:        "P",
    75  			DefaultValue: "53,5353",
    76  			Description:  "Ports to trace DNS requests on",
    77  			Validator:    params.ValidateSlice(params.ValidateUintRange(1, 65535)),
    78  		},
    79  	}
    80  }
    81  
    82  func (g *GadgetDesc) Parser() parser.Parser {
    83  	return parser.NewParser[types.Event](types.GetColumns())
    84  }
    85  
    86  func (g *GadgetDesc) EventPrototype() any {
    87  	return &types.Event{}
    88  }
    89  
    90  func (g *GadgetDesc) SkipParams() []params.ValueHint {
    91  	return []params.ValueHint{gadgets.K8SContainerName}
    92  }
    93  
    94  func init() {
    95  	gadgetregistry.Register(&GadgetDesc{})
    96  }