Skip to content

Commit

Permalink
Merge pull request inspektor-gadget#2551 from inspektor-gadget/michae…
Browse files Browse the repository at this point in the history
…l/refactoring-helpers-4

move metadata for image based gadgets to a versioned location and remove dependencies
  • Loading branch information
flyth authored Mar 25, 2024
2 parents ef25d34 + 37ea34f commit b586c01
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 373 deletions.
6 changes: 3 additions & 3 deletions pkg/gadgets/run/tracer/helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The Inspektor Gadget authors
// Copyright 2023-2024 The Inspektor Gadget authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,8 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/run/types"
"github.com/inspektor-gadget/inspektor-gadget/pkg/k8sutil"
metadatav1 "github.com/inspektor-gadget/inspektor-gadget/pkg/metadata/v1"
)

// getAnyMapElem returns any element of a map. If the map is empty, it returns nil, nil.
Expand All @@ -34,7 +34,7 @@ func getAnyMapElem[K comparable, V any](m map[K]V) (*K, *V) {
return nil, nil
}

func getEventTypeBTF(progContent []byte, metadata *types.GadgetMetadata) (*btf.Struct, error) {
func getEventTypeBTF(progContent []byte, metadata *metadatav1.GadgetMetadata) (*btf.Struct, error) {
spec, err := loadSpec(progContent)
if err != nil {
return nil, err
Expand Down
27 changes: 14 additions & 13 deletions pkg/gadgets/run/tracer/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/run/types"
"github.com/inspektor-gadget/inspektor-gadget/pkg/logger"
metadatav1 "github.com/inspektor-gadget/inspektor-gadget/pkg/metadata/v1"
"github.com/inspektor-gadget/inspektor-gadget/pkg/oci"
"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
"github.com/inspektor-gadget/inspektor-gadget/pkg/parser"
Expand Down Expand Up @@ -127,7 +128,7 @@ func (g *GadgetDesc) Experimental() bool {

// getGadgetType returns the type of the gadget according to the gadget being run.
func getGadgetType(spec *ebpf.CollectionSpec,
gadgetMetadata *types.GadgetMetadata,
gadgetMetadata *metadatav1.GadgetMetadata,
) (gadgets.GadgetType, error) {
switch {
case len(gadgetMetadata.Structs) == 0:
Expand Down Expand Up @@ -156,7 +157,7 @@ func getGadgetInfo(params *params.Params, args []string, secretBytes []byte, log

ret := &types.GadgetInfo{
ProgContent: gadget.EbpfObject,
GadgetMetadata: &types.GadgetMetadata{},
GadgetMetadata: &metadatav1.GadgetMetadata{},
}

spec, err := loadSpec(ret.ProgContent)
Expand All @@ -166,7 +167,7 @@ func getGadgetInfo(params *params.Params, args []string, secretBytes []byte, log

if bytes.Equal(gadget.Metadata, ocispec.DescriptorEmptyJSON.Data) {
// metadata is not present. synthesize something on the fly from the spec
if err := ret.GadgetMetadata.Populate(spec); err != nil {
if err := types.Populate(ret.GadgetMetadata, spec); err != nil {
return nil, err
}
} else {
Expand All @@ -176,7 +177,7 @@ func getGadgetInfo(params *params.Params, args []string, secretBytes []byte, log
return nil, fmt.Errorf("unmarshaling metadata: %w", err)
}

if err := ret.GadgetMetadata.Validate(spec); err != nil {
if err := types.Validate(ret.GadgetMetadata, spec); err != nil {
if !validate {
logger.Warnf("gadget metadata is not valid: %v", err)
} else {
Expand Down Expand Up @@ -258,7 +259,7 @@ func getTypeHint(typ btf.Type) params.TypeHint {

// fillTypeHints fills the TypeHint field in the ebpf parameters according to the BTF information
// about those constants.
func fillTypeHints(spec *ebpf.CollectionSpec, params map[string]types.EBPFParam) error {
func fillTypeHints(spec *ebpf.CollectionSpec, params map[string]metadatav1.EBPFParam) error {
for varName, p := range params {
var btfVar *btf.Var
err := spec.Types.TypeByName(varName, &btfVar)
Expand Down Expand Up @@ -414,7 +415,7 @@ func addL4EndpointColumns(
})
}

func field2ColumnAttrs(field *types.Field) columns.Attributes {
func field2ColumnAttrs(field *metadatav1.Field) columns.Attributes {
fieldAttrs := field.Attributes

defaultOpts := columns.GetDefault()
Expand All @@ -441,18 +442,18 @@ func field2ColumnAttrs(field *types.Field) columns.Attributes {
}

switch fieldAttrs.Alignment {
case types.AlignmentLeft:
case metadatav1.AlignmentLeft:
attrs.Alignment = columns.AlignLeft
case types.AlignmentRight:
case metadatav1.AlignmentRight:
attrs.Alignment = columns.AlignRight
}

switch fieldAttrs.Ellipsis {
case types.EllipsisStart:
case metadatav1.EllipsisStart:
attrs.EllipsisType = ellipsis.Start
case types.EllipsisMiddle:
case metadatav1.EllipsisMiddle:
attrs.EllipsisType = ellipsis.Middle
case types.EllipsisEnd:
case metadatav1.EllipsisEnd:
attrs.EllipsisType = ellipsis.End
}

Expand All @@ -474,7 +475,7 @@ func (g *GadgetDesc) getColumns(info *types.GadgetInfo) (*columns.Columns[types.
l4endpointCounter := 0
timestampsCounter := 0

fields := map[string]types.Field{}
fields := map[string]metadatav1.Field{}
for _, field := range eventStruct.Fields {
fields[field.Name] = field
}
Expand Down Expand Up @@ -778,7 +779,7 @@ func simpleTypeFromBTF(typ btf.Type) *types.Type {

func calculateColumnsForClient(
eventFactory *types.EventFactory,
gadgetMetadata *types.GadgetMetadata,
gadgetMetadata *metadatav1.GadgetMetadata,
progContent []byte,
logger logger.Logger,
) ([]types.ColumnDesc, error) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/gadgets/run/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
gadgetcontext "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-context"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/run/types"
metadatav1 "github.com/inspektor-gadget/inspektor-gadget/pkg/metadata/v1"
"github.com/inspektor-gadget/inspektor-gadget/pkg/netnsenter"
"github.com/inspektor-gadget/inspektor-gadget/pkg/networktracer"
"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
Expand All @@ -69,7 +70,7 @@ type l4EndpointT struct {

type Config struct {
ProgContent []byte
Metadata *types.GadgetMetadata
Metadata *metadatav1.GadgetMetadata
MountnsMap *ebpf.Map

// constants to replace in the ebpf program
Expand Down Expand Up @@ -854,7 +855,7 @@ func (t *Tracer) runTracers(gadgetCtx gadgets.GadgetContext) {
}
}

func (t *Tracer) setEBPFParameters(ebpfParams map[string]types.EBPFParam, gadgetParams *params.Params) {
func (t *Tracer) setEBPFParameters(ebpfParams map[string]metadatav1.EBPFParam, gadgetParams *params.Params) {
t.config.Consts = make(map[string]interface{})
for varName, paramDef := range ebpfParams {
p := gadgetParams.Get(paramDef.Key)
Expand Down
Loading

0 comments on commit b586c01

Please sign in to comment.