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

     1  // Copyright 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 simple
    16  
    17  import (
    18  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-service/api"
    19  	"github.com/inspektor-gadget/inspektor-gadget/pkg/operators"
    20  	"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
    21  )
    22  
    23  type simpleOperator struct {
    24  	name     string
    25  	onInit   func(gadgetCtx operators.GadgetContext) error
    26  	onStart  func(gadgetCtx operators.GadgetContext) error
    27  	onStop   func(gadgetCtx operators.GadgetContext) error
    28  	priority int
    29  }
    30  
    31  func New(name string, options ...Option) operators.DataOperator {
    32  	s := &simpleOperator{
    33  		name: name,
    34  	}
    35  	for _, o := range options {
    36  		o(s)
    37  	}
    38  	return s
    39  }
    40  
    41  func (s *simpleOperator) Name() string {
    42  	return s.name
    43  }
    44  
    45  func (s *simpleOperator) Init(params *params.Params) error {
    46  	return nil
    47  }
    48  
    49  func (s *simpleOperator) GlobalParams() api.Params {
    50  	return nil
    51  }
    52  
    53  func (s *simpleOperator) InstanceParams() api.Params {
    54  	return nil
    55  }
    56  
    57  func (s *simpleOperator) InstantiateDataOperator(gadgetCtx operators.GadgetContext, instanceParamValues api.ParamValues) (operators.DataOperatorInstance, error) {
    58  	if s.onInit != nil {
    59  		err := s.onInit(gadgetCtx)
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  	}
    64  	return s, nil
    65  }
    66  
    67  func (s *simpleOperator) Priority() int {
    68  	return s.priority
    69  }
    70  
    71  func (s *simpleOperator) Start(gadgetCtx operators.GadgetContext) error {
    72  	if s.onStart != nil {
    73  		return s.onStart(gadgetCtx)
    74  	}
    75  	return nil
    76  }
    77  
    78  func (s *simpleOperator) Stop(gadgetCtx operators.GadgetContext) error {
    79  	if s.onStart != nil {
    80  		return s.onStop(gadgetCtx)
    81  	}
    82  	return nil
    83  }