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

tetragon: Load base sensor via sensor manager #3045

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 1 addition & 4 deletions cmd/tetragon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,9 @@ func tetragonExecute() error {

// load base sensor
initialSensor := base.GetInitialSensor()
if err := initialSensor.Load(observerDir); err != nil {
if err := observer.GetSensorManager().LoadSensor(initialSensor); err != nil {
return err
}
defer func() {
initialSensor.Unload()
}()

cgrouprate.NewCgroupRate(ctx, pm, base.CgroupRateMap, &option.Config.CgroupRate)
cgrouprate.Config(base.CgroupRateOptionsMap)
Expand Down
32 changes: 28 additions & 4 deletions pkg/sensors/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ type collection struct {

type collectionMap struct {
// map of sensor collections: name, namespace -> collection
c map[collectionKey]*collection
mu sync.RWMutex
c map[collectionKey]*collection
mu sync.RWMutex
bpfDir string
}

func newCollectionMap() *collectionMap {
func newCollectionMap(bpfDir string) *collectionMap {
return &collectionMap{
c: map[collectionKey]*collection{},
c: map[collectionKey]*collection{},
bpfDir: bpfDir,
}
}

Expand Down Expand Up @@ -144,6 +146,28 @@ func (c *collection) destroy() {
}
}

func (cm *collectionMap) loadSensor(s *Sensor) error {
cm.mu.Lock()
defer cm.mu.Unlock()

collections := cm.c
// Treat sensors as cluster-wide operations
ck := collectionKey{s.Name, ""}
if _, exists := collections[ck]; exists {
return fmt.Errorf("sensor %s already exists", ck)
}
col := &collection{
sensors: []SensorIface{s},
name: s.Name,
}
if err := col.load(cm.bpfDir); err != nil {
return err
}
col.state = EnabledState
collections[ck] = col
return nil
}

func (cm *collectionMap) listPolicies() []*tetragon.TracingPolicyStatus {
cm.mu.RLock()
defer cm.mu.RUnlock()
Expand Down
21 changes: 13 additions & 8 deletions pkg/sensors/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func StartSensorManagerWithPF(
waitChan chan struct{},
pfState policyfilter.State,
) (*Manager, error) {
colMap := newCollectionMap()
colMap := newCollectionMap(bpfDir)

handler, err := newHandler(pfState, colMap, bpfDir)
if err != nil {
Expand All @@ -62,13 +62,13 @@ func StartSensorManagerWithPF(

func startSensorManager(
handler *handler,
policyLister policyLister,
directAccess directAccess,
waitChan chan struct{},
) (*Manager, error) {
c := make(chan sensorOp)
m := Manager{
sensorCtl: c,
policyLister: policyLister,
directAccess: directAccess,
}

go func() {
Expand Down Expand Up @@ -267,6 +267,10 @@ func (h *Manager) ListTracingPolicies(_ context.Context) (*tetragon.ListTracingP
return ret, nil
}

func (h *Manager) LoadSensor(s *Sensor) error {
return h.loadSensor(s)
}

func (h *Manager) RemoveSensor(ctx context.Context, sensorName string) error {
retc := make(chan error)
op := &sensorRemove{
Expand Down Expand Up @@ -332,19 +336,20 @@ func (h *Manager) LogSensorsAndProbes(ctx context.Context) {
log.WithField("types", strings.Join(names, ", ")).Info("Registered probe types")
}

// policyLister allows read-only access to the collections map
type policyLister interface {
// directAccess allows access to the collections map
type directAccess interface {
listPolicies() []*tetragon.TracingPolicyStatus
loadSensor(s *Sensor) error
}

// Manager handles dynamic sensor management, such as adding / removing sensors
// at runtime.
type Manager struct {
// channel to communicate with the controller goroutine
sensorCtl sensorCtlHandle
// policyLister is used to list policies without going via the controller goroutine by
// directly accessing the collection.
policyLister
// directAccess is used to access collection directly without
// going via the controller goroutine
directAccess
}

// There are 6 commands that can be passed to the controller goroutine:
Expand Down
2 changes: 1 addition & 1 deletion pkg/sensors/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestPolicyFilterDisabled(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

handler, err := newHandler(policyfilter.DisabledState(), newCollectionMap(), "")
handler, err := newHandler(policyfilter.DisabledState(), newCollectionMap(""), "")
assert.NoError(t, err)
mgr, err := startSensorManager(handler, handler.collections, nil)
assert.NoError(t, err)
Expand Down
Loading