Skip to content

Commit

Permalink
fix modifier usage
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle committed Jan 6, 2025
1 parent c40923a commit 6daf999
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 41 deletions.
43 changes: 13 additions & 30 deletions pkg/ebpf/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ type ModifierAfterInit interface {
AfterInit(*manager.Manager, names.ModuleName, *manager.Options) error
}

// ModifierPreStart is a sub-interface of Modifier that exposes an PreStart method
type ModifierPreStart interface {
Modifier

// PreStart is called before the ebpf.Manager.Start call
PreStart(*manager.Manager, names.ModuleName) error
}

// ModifierBeforeStop is a sub-interface of Modifier that exposes a BeforeStop method
type ModifierBeforeStop interface {
Modifier
Expand Down Expand Up @@ -169,39 +177,14 @@ func (m *Manager) Stop(cleanupType manager.MapCleanupType) error {
return errors.Join(errs, err)
}

type modifierPreStart interface {
PreStart() error
}

// Start is a wrapper around ebpf-manager.Manager.Start
func (m *Manager) Start() error {
for _, mod := range m.EnabledModifiers {
if ps, ok := mod.(modifierPreStart); ok {
if err := ps.PreStart(); err != nil {
return fmt.Errorf("prestart %s manager modifier: %w", mod, err)
}
}
}
return m.Manager.Start()
}

type modifierAfterStop interface {
AfterStop(manager.MapCleanupType) error
}

// Stop is a wrapper around ebpf-manager.Manager.Stop
func (m *Manager) Stop(ct manager.MapCleanupType) error {
if err := m.Manager.Stop(ct); err != nil {
err := runModifiersOfType(m.EnabledModifiers, "PreStart", func(mod ModifierPreStart) error {
return mod.PreStart(m.Manager, m.Name)
})
if err != nil {
return err
}

for _, mod := range m.EnabledModifiers {
if as, ok := mod.(modifierAfterStop); ok {
if err := as.AfterStop(ct); err != nil {
return fmt.Errorf("afterstop %s manager modifier: %w", mod, err)
}
}
}

return nil
return m.Manager.Start()
}
18 changes: 7 additions & 11 deletions pkg/ebpf/perf/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ type Flusher interface {
Flush()
}

// compile time check to ensure this satisfies the Modifier interface
var _ ddebpf.Modifier = (*EventHandler)(nil)
// compile time check to ensure this satisfies the Modifier* interfaces
var _ ddebpf.ModifierPreStart = (*EventHandler)(nil)
var _ ddebpf.ModifierAfterStop = (*EventHandler)(nil)

// EventHandler abstracts consuming data from a perf buffer or ring buffer (depending on availability and options).
// It handles upgrading maps from a ring buffer if desired, and unmarshalling into the desired data type.
Expand Down Expand Up @@ -295,19 +296,14 @@ func (e *EventHandler) setupRingbufferWakeupConstant(mgrOpts *manager.Options) {
}
}

// AfterInit implements the Modifier interface
func (e *EventHandler) AfterInit(_ *manager.Manager, _ names.ModuleName, _ *manager.Options) error {
return nil
}

// PreStart implements the Modifier interface
func (e *EventHandler) PreStart() error {
// PreStart implements the ModifierPreStart interface
func (e *EventHandler) PreStart(_ *manager.Manager, _ names.ModuleName) error {
go e.readLoop()
return nil
}

// AfterStop implements the Modifier interface
func (e *EventHandler) AfterStop(_ manager.MapCleanupType) error {
// AfterStop implements the ModifierAfterStop interface
func (e *EventHandler) AfterStop(_ *manager.Manager, _ names.ModuleName, _ manager.MapCleanupType) error {
if e.perfChan != nil {
close(e.perfChan)
}
Expand Down

0 comments on commit 6daf999

Please sign in to comment.