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

use Load and Run function in director #1941

Merged
merged 4 commits into from
Dec 8, 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
47 changes: 26 additions & 21 deletions odiglet/pkg/ebpf/director.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import (

// This interface should be implemented by all ebpf sdks
// for example, the go auto instrumentation sdk implements it
// Deprecated: this will be removed once we fully move to the generic instrumentation manager
type OtelEbpfSdk interface {
// this is temporary until we move to the generic instrumentation manager
Load(ctx context.Context) error
// Run starts the eBPF instrumentation.
// It should block until the instrumentation is stopped or the context is canceled or an error occurs.
Run(ctx context.Context) error
Expand All @@ -42,6 +45,7 @@ type ConfigurableOtelEbpfSdk interface {
}

// users can use different eBPF otel SDKs by returning them from this function
// Deprecated: this will be removed once we fully move to the generic instrumentation manager
type InstrumentationFactory[T OtelEbpfSdk] interface {
CreateEbpfInstrumentation(ctx context.Context, pid int, serviceName string, podWorkload *workload.PodWorkload, containerName string, podName string, loadedIndicator chan struct{}) (T, error)
}
Expand Down Expand Up @@ -301,46 +305,47 @@ func (d *EbpfDirector[T]) Instrument(ctx context.Context, pid int, pod types.Nam
d.workloadToPods[*podWorkload][pod] = struct{}{}

ip.runOnce.Do(func() {
loadedIndicator := make(chan struct{})
loadedCtx, loadedObserverCancel := context.WithCancel(ctx)
// launch an observer for successful loading of the eBPF probes
go func() {
select {
case <-loadedCtx.Done():
return
case <-loadedIndicator:
inst, err := d.instrumentationFactory.CreateEbpfInstrumentation(ctx, pid, appName, podWorkload, containerName, pod.Name, nil)
if err != nil {
d.instrumentationStatusChan <- instrumentationStatus{
Healthy: true,
Message: "Successfully loaded eBPF probes to pod: " + pod.String(),
Healthy: false,
Message: err.Error(),
Workload: *podWorkload,
Reason: LoadedSuccessfully,
Reason: FailedToInitialize,
PodName: pod,
ContainerName: containerName,
Pid: pid,
}
return
}
}()

go func() {
// once the instrumentation finished running (either by error or successful exit), we can cancel the 'loaded' observer for this instrumentation
defer loadedObserverCancel()
inst, err := d.instrumentationFactory.CreateEbpfInstrumentation(ctx, pid, appName, podWorkload, containerName, pod.Name, loadedIndicator)
if ip.closed.Load() {
log.Logger.Info("Instrumentation already closed before running, stopping instrumentation", "pid", pid)
return
}

err = inst.Load(ctx)
if err != nil {
d.instrumentationStatusChan <- instrumentationStatus{
Healthy: false,
Message: err.Error(),
Workload: *podWorkload,
Reason: FailedToInitialize,
Reason: FailedToLoad,
PodName: pod,
ContainerName: containerName,
Pid: pid,
}
return
}

if ip.closed.Load() {
log.Logger.Info("Instrumentation already closed before running, stopping instrumentation", "pid", pid)
return
d.instrumentationStatusChan <- instrumentationStatus{
Healthy: true,
Message: "Successfully loaded eBPF probes to pod: " + pod.String(),
Workload: *podWorkload,
Reason: LoadedSuccessfully,
PodName: pod,
ContainerName: containerName,
Pid: pid,
}

ip.inst = inst
Expand All @@ -352,7 +357,7 @@ func (d *EbpfDirector[T]) Instrument(ctx context.Context, pid int, pod types.Nam
Healthy: false,
Message: err.Error(),
Workload: *podWorkload,
Reason: FailedToLoad,
Reason: FailedToRun,
PodName: pod,
ContainerName: containerName,
Pid: pid,
Expand Down
7 changes: 4 additions & 3 deletions odiglet/pkg/ebpf/director_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
)

type FakeEbpfSdk struct {
loadedIndicator chan struct{}
running bool
pid int

Expand All @@ -47,10 +46,13 @@ func (f *FakeEbpfSdk) Close(_ context.Context) error {
return nil
}

func (f *FakeEbpfSdk) Load(ctx context.Context) error {
return nil
}

func (f *FakeEbpfSdk) Run(ctx context.Context) error {
// To simulate a real instrumentation, Run is blocking until the context is cancelled
f.running = true
close(f.loadedIndicator)

cancelCtx, cancel := context.WithCancel(ctx)
f.cancel = cancel
Expand All @@ -76,7 +78,6 @@ func NewFakeInstrumentationFactory(kubeclient client.Client, setupDuration time.
func (f *FakeInstrumentationFactory) CreateEbpfInstrumentation(ctx context.Context, pid int, serviceName string, podWorkload *workload.PodWorkload, containerName string, podName string, loadedIndicator chan struct{}) (*FakeEbpfSdk, error) {
<-time.After(f.timeToSetup)
return &FakeEbpfSdk{
loadedIndicator: loadedIndicator,
pid: pid,
}, nil
}
Expand Down
Loading