Skip to content

Commit

Permalink
fix: enricher.Instance() returns nil instead of e if e is not initial…
Browse files Browse the repository at this point in the history
…ized
  • Loading branch information
nddq committed Mar 15, 2024
1 parent 016dce9 commit 12f7fcc
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
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
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")

0 comments on commit 12f7fcc

Please sign in to comment.