@@ -189,8 +189,8 @@ func (cs *CollectionSpec) RewriteConstants(consts map[string]interface{}) error
189
189
// Returns an error if any of the eBPF objects can't be found, or
190
190
// if the same MapSpec or ProgramSpec is assigned multiple times.
191
191
func (cs * CollectionSpec ) Assign (to interface {}) error {
192
- // Assign() only supports assigning ProgramSpecs and MapSpecs,
193
- // so doesn't load any resources into the kernel.
192
+ // Assign() only supports assigning ProgramSpecs, MapSpecs
193
+ // and VariableSpecs, so doesn't load any resources into the kernel.
194
194
getValue := func (typ reflect.Type , name string ) (interface {}, error ) {
195
195
switch typ {
196
196
@@ -206,6 +206,12 @@ func (cs *CollectionSpec) Assign(to interface{}) error {
206
206
}
207
207
return nil , fmt .Errorf ("missing map %q" , name )
208
208
209
+ case reflect .TypeOf ((* VariableSpec )(nil )):
210
+ if m := cs .Variables [name ]; m != nil {
211
+ return m , nil
212
+ }
213
+ return nil , fmt .Errorf ("missing variable %q" , name )
214
+
209
215
default :
210
216
return nil , fmt .Errorf ("unsupported type %s" , typ )
211
217
}
@@ -269,6 +275,11 @@ func (cs *CollectionSpec) LoadAndAssign(to interface{}, opts *CollectionOptions)
269
275
return loader .loadVariable (name )
270
276
271
277
default :
278
+ if typ .Kind () == reflect .Pointer && typ .Elem ().ConvertibleTo (reflect .TypeOf ((* Variable )(nil )).Elem ()) {
279
+ assignedVars [name ] = true
280
+ return loader .loadVariable (name )
281
+ }
282
+
272
283
return nil , fmt .Errorf ("unsupported type %s" , typ )
273
284
}
274
285
}
@@ -752,8 +763,8 @@ func (coll *Collection) Assign(to interface{}) error {
752
763
assignedProgs := make (map [string ]bool )
753
764
assignedVars := make (map [string ]bool )
754
765
755
- // Assign() only transfers already-loaded Maps and Programs. No extra
756
- // loading is done.
766
+ // Assign() only transfers already-loaded Maps, Programs and Variablees.
767
+ // No extra loading is done.
757
768
getValue := func (typ reflect.Type , name string ) (interface {}, error ) {
758
769
switch typ {
759
770
@@ -779,6 +790,15 @@ func (coll *Collection) Assign(to interface{}) error {
779
790
return nil , fmt .Errorf ("missing variable %q" , name )
780
791
781
792
default :
793
+ if typ .Kind () == reflect .Pointer && typ .Elem ().ConvertibleTo (reflect .TypeOf ((* Variable )(nil )).Elem ()) {
794
+ assignedVars [name ] = true
795
+ if v := coll .Variables [name ]; v != nil {
796
+ assignedVars [name ] = true
797
+ return v , nil
798
+ }
799
+ return nil , fmt .Errorf ("missing variable %q" , name )
800
+ }
801
+
782
802
return nil , fmt .Errorf ("unsupported type %s" , typ )
783
803
}
784
804
}
0 commit comments