pkg/gadget-collection: Add Lookup() to BaseFactory.
Contrary to LookupAndCreate() this function just lookup and returns an error
either if the map is nil or there is no element corresponding to the key.
package gadgets
import (
+ "errors"
+ "fmt"
"sync"
"github.com/cilium/ebpf"
@@ -130,6 +132,22 @@ func (f *BaseFactory) LookupOrCreate(name string, newTrace func() interface{}) i
return trace
}
+func (f *BaseFactory) Lookup(name string) (interface{}, error) {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+
+ if f.traces == nil {
+ return nil, errors.New("traces map is nil")
+ }
+
+ trace, ok := f.traces[name]
+ if !ok {
+ return nil, fmt.Errorf("no trace for name %q", name)
+ }
+
+ return trace, nil
+}
+
func (f *BaseFactory) Delete(name string) {
log.Infof("Deleting %s", name)
f.mu.Lock()
