github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/runtime/runtime.go (about)

     1  // Copyright 2022-2024 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 runtime
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"github.com/inspektor-gadget/inspektor-gadget/pkg/datasource"
    22  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-service/api"
    23  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
    24  	"github.com/inspektor-gadget/inspektor-gadget/pkg/logger"
    25  	"github.com/inspektor-gadget/inspektor-gadget/pkg/operators"
    26  	"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
    27  	"github.com/inspektor-gadget/inspektor-gadget/pkg/parser"
    28  )
    29  
    30  type GadgetContext interface {
    31  	ID() string
    32  	Parser() parser.Parser
    33  	GadgetDesc() gadgets.GadgetDesc
    34  	Context() context.Context
    35  	Operators() operators.Operators
    36  	Logger() logger.Logger
    37  	RuntimeParams() *params.Params
    38  	GadgetParams() *params.Params
    39  	Args() []string
    40  	OperatorsParamCollection() params.Collection
    41  	Timeout() time.Duration
    42  
    43  	Cancel()
    44  	ImageName() string
    45  	RegisterDataSource(datasource.Type, string) (datasource.DataSource, error)
    46  	GetDataSources() map[string]datasource.DataSource
    47  	SetVar(string, any)
    48  	GetVar(string) (any, bool)
    49  	SerializeGadgetInfo() (*api.GadgetInfo, error)
    50  	LoadGadgetInfo(info *api.GadgetInfo, paramValues api.ParamValues, run bool) error
    51  	Params() []*api.Param
    52  	SetMetadata([]byte)
    53  	SetParams([]*api.Param)
    54  	DataOperators() []operators.DataOperator
    55  
    56  	Run(paramValues api.ParamValues) error
    57  	PrepareGadgetInfo(paramValues api.ParamValues) error
    58  }
    59  
    60  // GadgetResult contains the (optional) payload and error of a gadget run for a node
    61  type GadgetResult struct {
    62  	Payload []byte
    63  	Error   error
    64  }
    65  
    66  type CombinedGadgetResult map[string]*GadgetResult
    67  
    68  func (r CombinedGadgetResult) Err() error {
    69  	c := &combinedErrors{}
    70  	for _, result := range r {
    71  		if result != nil && result.Error != nil {
    72  			c.errs = append(c.errs, result.Error)
    73  		}
    74  	}
    75  	if len(c.errs) > 0 {
    76  		return c
    77  	}
    78  	return nil
    79  }
    80  
    81  type combinedErrors struct {
    82  	errs []error
    83  }
    84  
    85  func (e *combinedErrors) Error() string {
    86  	var b []byte
    87  	for i, err := range e.errs {
    88  		if i > 0 {
    89  			b = append(b, '\n')
    90  		}
    91  		b = append(b, err.Error()...)
    92  	}
    93  	return string(b)
    94  }
    95  
    96  func (e *combinedErrors) Unwrap() []error {
    97  	return e.errs
    98  }
    99  
   100  // Runtime is the interface for gadget runtimes. Runtimes are used to control the lifecycle of gadgets either locally
   101  // or remotely.
   102  type Runtime interface {
   103  	Init(globalRuntimeParams *params.Params) error
   104  	Close() error
   105  	GlobalParamDescs() params.ParamDescs
   106  	ParamDescs() params.ParamDescs
   107  
   108  	// GetGadgetInfo returns information about the gadget and used operators; this info potentially comes
   109  	// from a cache
   110  	GetGadgetInfo(gadgetCtx GadgetContext, runtimeParams *params.Params, paramValueMap api.ParamValues) (*api.GadgetInfo, error)
   111  
   112  	RunBuiltInGadget(gadgetCtx GadgetContext) (CombinedGadgetResult, error)
   113  	RunGadget(gadgetCtx GadgetContext, runtimeParams *params.Params, paramValueMap api.ParamValues) error
   114  	GetCatalog() (*Catalog, error)
   115  	SetDefaultValue(params.ValueHint, string)
   116  	GetDefaultValue(params.ValueHint) (string, bool)
   117  }