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

[EBPF] manager: add BeforeStop method to modifiers #32414

Closed
wants to merge 5 commits into from
Closed
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
8 changes: 8 additions & 0 deletions pkg/ebpf/helper_call_patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,11 @@ func (h *helperCallRemover) AfterInit(*manager.Manager, names.ModuleName, *manag
func (h *helperCallRemover) String() string {
return fmt.Sprintf("HelperCallRemover[%+v]", h.helpers)
}

func (h *helperCallRemover) BeforeStop(*manager.Manager, names.ModuleName) error {
return nil
}

func (h *helperCallRemover) AfterStop(*manager.Manager, names.ModuleName) error {
return nil
}
34 changes: 34 additions & 0 deletions pkg/ebpf/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package ebpf

import (
"errors"
"fmt"
"io"
"sync"
Expand Down Expand Up @@ -70,6 +71,14 @@ type Modifier interface {

// AfterInit is called after the ebpf.Manager.InitWithOptions call
AfterInit(*manager.Manager, names.ModuleName, *manager.Options) error

// BeforeStop is called after the ebpf.Manager.Stop call. Errors on these modifiers
// will not prevent the manager from stopping, but they will be reported.
BeforeStop(*manager.Manager, names.ModuleName) error

// AfterStop is called after the ebpf.Manager.Stop call. If the manager returns
// an error on stop, this function will still be called.
AfterStop(*manager.Manager, names.ModuleName) error
}

// InitWithOptions is a wrapper around ebpf-manager.Manager.InitWithOptions
Expand Down Expand Up @@ -100,3 +109,28 @@ func (m *Manager) InitWithOptions(bytecode io.ReaderAt, opts *manager.Options) e
}
return nil
}

// Stop is a wrapper around ebpf-manager.Manager.Stop
func (m *Manager) Stop(clean manager.MapCleanupType) error {
var errs error

for _, mod := range m.EnabledModifiers {
log.Tracef("Running %s manager modifier BeforeStop", mod)
if err := mod.BeforeStop(m.Manager, m.Name); err != nil {
errs = errors.Join(errs, fmt.Errorf("error running %s manager modifier BeforeStop(): %s", mod, err))
}
}

if err := m.Manager.Stop(clean); err != nil {
errs = errors.Join(errs, fmt.Errorf("failed to stop manager %w", err))
}

for _, mod := range m.EnabledModifiers {
log.Tracef("Running %s manager modifier AfterStop", mod)
if err := mod.AfterStop(m.Manager, m.Name); err != nil {
errs = errors.Join(errs, fmt.Errorf("error running %s manager modifier AfterStop(): %s", mod, err))
}
}

return errs
}
12 changes: 11 additions & 1 deletion pkg/ebpf/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ package ebpf
import (
"testing"

"github.com/DataDog/datadog-agent/pkg/ebpf/names"
manager "github.com/DataDog/ebpf-manager"
"github.com/stretchr/testify/assert"

"github.com/DataDog/datadog-agent/pkg/ebpf/names"
)

// PrintkPatcherModifier adds an InstructionPatcher to the manager that removes the newline character from log_debug calls if needed
Expand All @@ -34,6 +35,15 @@ func (t *dummyModifier) AfterInit(_ *manager.Manager, _ names.ModuleName, _ *man
return nil
}

// BeforeStop is a no-op for this modifier
func (t *dummyModifier) BeforeStop(_ *manager.Manager, _ names.ModuleName) error {
return nil
}

func (t *dummyModifier) AfterStop(_ *manager.Manager, _ names.ModuleName) error {
return nil
}

func TestNewManagerWithDefault(t *testing.T) {
type args struct {
mgr *manager.Manager
Expand Down
10 changes: 10 additions & 0 deletions pkg/ebpf/printk_patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,13 @@ func (t *PrintkPatcherModifier) BeforeInit(m *manager.Manager, _ names.ModuleNam
func (t *PrintkPatcherModifier) AfterInit(_ *manager.Manager, _ names.ModuleName, _ *manager.Options) error {
return nil
}

// BeforeStop is a no-op for this modifier
func (t *PrintkPatcherModifier) BeforeStop(_ *manager.Manager, _ names.ModuleName) error {
return nil
}

// AfterStop is a no-op for this modifier
func (t *PrintkPatcherModifier) AfterStop(_ *manager.Manager, _ names.ModuleName) error {
return nil
}
10 changes: 10 additions & 0 deletions pkg/ebpf/telemetry/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,13 @@ func (t *ErrorsTelemetryModifier) AfterInit(m *manager.Manager, module names.Mod

return nil
}

// BeforeStop is a no-op for now
func (t *ErrorsTelemetryModifier) BeforeStop(_ *manager.Manager, _ names.ModuleName) error {
return nil
}

// AfterStop is a no-op
func (t *ErrorsTelemetryModifier) AfterStop(_ *manager.Manager, _ names.ModuleName) error {
return nil
}
Loading