diff --git a/collection.go b/collection.go index a7b6cb31b..98ade553c 100644 --- a/collection.go +++ b/collection.go @@ -252,7 +252,11 @@ func (cs *CollectionSpec) Assign(to interface{}) error { // Returns an error if any of the fields can't be found, or // if the same Map or Program is assigned multiple times. func (cs *CollectionSpec) LoadAndAssign(to interface{}, opts *CollectionOptions) error { - loader, err := newCollectionLoader(cs, opts) + if opts == nil { + opts = &CollectionOptions{} + } + + loader, err := newCollectionLoader(cs, *opts) if err != nil { return err } @@ -351,7 +355,7 @@ func NewCollection(spec *CollectionSpec) (*Collection, error) { // Omitting Collection.Close() during application shutdown is an error. // See the package documentation for details around Map and Program lifecycle. func NewCollectionWithOptions(spec *CollectionSpec, opts CollectionOptions) (*Collection, error) { - loader, err := newCollectionLoader(spec, &opts) + loader, err := newCollectionLoader(spec, opts) if err != nil { return nil, err } @@ -405,9 +409,13 @@ type collectionLoader struct { vars map[string]*Variable } -func newCollectionLoader(coll *CollectionSpec, opts *CollectionOptions) (*collectionLoader, error) { - if opts == nil { - opts = &CollectionOptions{} +func newCollectionLoader(coll *CollectionSpec, opts CollectionOptions) (*collectionLoader, error) { + if opts.Programs.KernelTypes == nil { + kernelSpec, err := btf.LoadKernelSpec() + if err != nil { + return nil, fmt.Errorf("cannot load kernel spec: %w", err) + } + opts.Programs.KernelTypes = kernelSpec } // Check for existing MapSpecs in the CollectionSpec for all provided replacement maps. @@ -423,7 +431,7 @@ func newCollectionLoader(coll *CollectionSpec, opts *CollectionOptions) (*collec return &collectionLoader{ coll, - opts, + &opts, make(map[string]*Map), make(map[string]*Program), make(map[string]*Variable),