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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions pkg/ebpf/helper_call_patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,7 @@ 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
}
23 changes: 23 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,10 @@ 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
}

// InitWithOptions is a wrapper around ebpf-manager.Manager.InitWithOptions
Expand Down Expand Up @@ -100,3 +105,21 @@ 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.Warnf("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))
}

return errs
}
8 changes: 7 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,11 @@ 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 TestNewManagerWithDefault(t *testing.T) {
type args struct {
mgr *manager.Manager
Expand Down
5 changes: 5 additions & 0 deletions pkg/ebpf/printk_patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,8 @@ 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
}
5 changes: 5 additions & 0 deletions pkg/ebpf/telemetry/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,8 @@ 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
}
Loading