Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enricher.Instance() returns nil instead of e if e is not initialized #53

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pkg/enricher/enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
)

var (
e *Enricher
once sync.Once
rbtr marked this conversation as resolved.
Show resolved Hide resolved
e *Enricher
once sync.Once
initialized bool
)

type Enricher struct {
Expand Down Expand Up @@ -50,7 +51,7 @@ func New(ctx context.Context, cache cache.CacheInterface) *Enricher {
Reader: container.NewRingReader(ir, ir.OldestWrite()),
outputRing: container.NewRing(container.Capacity1023),
}
e.init()
initialized = true
})

return e
Expand All @@ -60,8 +61,8 @@ func Instance() *Enricher {
return e
}

func (e *Enricher) init() {
// todo
func IsInitialized() bool {
return initialized
}

func (e *Enricher) Run() {
Expand Down
7 changes: 4 additions & 3 deletions pkg/plugin/dns/dns_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ func (d *dns) Init() error {

func (d *dns) Start(ctx context.Context) error {
if d.cfg.EnablePodLevel {
d.enricher = enricher.Instance()
if d.enricher == nil {
d.l.Warn("Failed to get enricher instance")
if enricher.IsInitialized() {
d.enricher = enricher.Instance()
} else {
d.l.Warn("retina enricher is not initialized")
}
}
if err := d.tracer.Attach(d.pid); err != nil {
Expand Down
9 changes: 5 additions & 4 deletions pkg/plugin/dropreason/dropreason_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,11 @@ func (dr *dropReason) Start(ctx context.Context) error {
dr.recordsChannel = make(chan perf.Record, buffer)

dr.l.Info("setting up enricher since pod level is enabled")
// Setup enricher.
dr.enricher = enricher.Instance()
if dr.enricher == nil {
dr.l.Warn("Retina enricher is nil")
// Set up enricher.
if enricher.IsInitialized() {
dr.enricher = enricher.Instance()
} else {
dr.l.Warn("retina enricher is not initialized")
}
} else {
dr.l.Info("will not set up enricher since pod level is disabled")
Expand Down
10 changes: 5 additions & 5 deletions pkg/plugin/packetparser/packetparser_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ func (p *packetParser) Start(ctx context.Context) error {
p.l.Info("Starting packet parser")

p.l.Info("setting up enricher since pod level is enabled")
// Setup enricher.
p.enricher = enricher.Instance()
if p.enricher == nil {
p.l.Warn("Retina enricher is nil")
// return errors.New("enricher is nil")
// Set up enricher.
if enricher.IsInitialized() {
p.enricher = enricher.Instance()
} else {
p.l.Warn("retina enricher is not initialized")
}

// Get Pubsub instance.
Expand Down
11 changes: 6 additions & 5 deletions pkg/plugin/tcpretrans/tcpretrans_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/microsoft/retina/pkg/log"
"github.com/microsoft/retina/pkg/plugin/api"
"github.com/microsoft/retina/pkg/utils"
"github.com/pkg/errors"
"go.uber.org/zap"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -66,10 +65,12 @@ func (t *tcpretrans) Start(ctx context.Context) error {
t.l.Warn("tcpretrans will not start because pod level is disabled")
return nil
}
t.enricher = enricher.Instance()
if t.enricher == nil {
t.l.Error("Failed to get enricher instance")
return errors.New("failed to get enricher instance")
// Set up enricher
if enricher.IsInitialized() {
t.enricher = enricher.Instance()
} else {
t.l.Error(errEnricherNotInitialized.Error())
return errEnricherNotInitialized
}
t.gadgetCtx = gadgetcontext.New(ctx, "tcpretrans", nil, nil, nil, nil, nil, nil, nil, nil, 0, nil)

Expand Down
4 changes: 4 additions & 0 deletions pkg/plugin/tcpretrans/types_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package tcpretrans

import (
"errors"

gadgetcontext "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-context"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/tcpretrans/tracer"
kcfg "github.com/microsoft/retina/pkg/config"
Expand All @@ -22,3 +24,5 @@ type tcpretrans struct {
gadgetCtx *gadgetcontext.GadgetContext
enricher enricher.EnricherInterface
}

var errEnricherNotInitialized = errors.New("enricher not initialized")
Loading