Skip to content

Commit

Permalink
tetragon: Move policyLister under sensor manager handler
Browse files Browse the repository at this point in the history
Moving policyLister under sensor manager handler to keep
all the handler routines together now that we don't have
the go routine.

Signed-off-by: Jiri Olsa <[email protected]>
  • Loading branch information
olsajiri committed Oct 31, 2024
1 parent 2c144ae commit 4ee1a05
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 75 deletions.
62 changes: 0 additions & 62 deletions pkg/sensors/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,65 +143,3 @@ func (c *collection) destroy() {
s.Destroy()
}
}

func (cm *collectionMap) listOverheads() ([]ProgOverhead, error) {
cm.mu.RLock()
defer cm.mu.RUnlock()

overheads := []ProgOverhead{}

for ck, col := range cm.c {
for _, s := range col.sensors {
ret, ok := s.Overhead()
if !ok {
continue
}
for _, ovh := range ret {
ovh.Namespace = ck.namespace
ovh.Policy = ck.name
overheads = append(overheads, ovh)
}
}
}

return overheads, nil
}

func (cm *collectionMap) listPolicies() []*tetragon.TracingPolicyStatus {
cm.mu.RLock()
defer cm.mu.RUnlock()
collections := cm.c

ret := make([]*tetragon.TracingPolicyStatus, 0, len(collections))
for ck, col := range collections {
if col.tracingpolicy == nil {
continue
}

pol := tetragon.TracingPolicyStatus{
Id: col.tracingpolicyID,
Name: ck.name,
Enabled: col.state == EnabledState,
FilterId: col.policyfilterID,
State: col.state.ToTetragonState(),
}

if col.err != nil {
pol.Error = col.err.Error()
}

pol.Namespace = ""
if tpNs, ok := col.tracingpolicy.(tracingpolicy.TracingPolicyNamespaced); ok {
pol.Namespace = tpNs.TpNamespace()
}

for _, sens := range col.sensors {
pol.Sensors = append(pol.Sensors, sens.GetName())
pol.KernelMemoryBytes += uint64(sens.TotalMemlock())
}

ret = append(ret, &pol)
}

return ret
}
64 changes: 64 additions & 0 deletions pkg/sensors/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"

slimv1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/meta/v1"
"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/pkg/policyfilter"
"github.com/cilium/tetragon/pkg/tracingpolicy"
)
Expand Down Expand Up @@ -345,6 +346,69 @@ func (h *handler) listSensors(op *sensorList) error {
return nil
}

func (h *handler) listOverheads() ([]ProgOverhead, error) {
h.collections.mu.RLock()
defer h.collections.mu.RUnlock()
collections := h.collections.c

overheads := []ProgOverhead{}

for ck, col := range collections {
for _, s := range col.sensors {
ret, ok := s.Overhead()
if !ok {
continue
}
for _, ovh := range ret {
ovh.Namespace = ck.namespace
ovh.Policy = ck.name
overheads = append(overheads, ovh)
}
}
}

return overheads, nil
}

func (h *handler) listPolicies() []*tetragon.TracingPolicyStatus {
h.collections.mu.RLock()
defer h.collections.mu.RUnlock()
collections := h.collections.c

ret := make([]*tetragon.TracingPolicyStatus, 0, len(collections))
for ck, col := range collections {
if col.tracingpolicy == nil {
continue
}

pol := tetragon.TracingPolicyStatus{
Id: col.tracingpolicyID,
Name: ck.name,
Enabled: col.state == EnabledState,
FilterId: col.policyfilterID,
State: col.state.ToTetragonState(),
}

if col.err != nil {
pol.Error = col.err.Error()
}

pol.Namespace = ""
if tpNs, ok := col.tracingpolicy.(tracingpolicy.TracingPolicyNamespaced); ok {
pol.Namespace = tpNs.TpNamespace()
}

for _, sens := range col.sensors {
pol.Sensors = append(pol.Sensors, sens.GetName())
pol.KernelMemoryBytes += uint64(sens.TotalMemlock())
}

ret = append(ret, &pol)
}

return ret
}

func sensorsFromPolicyHandlers(tp tracingpolicy.TracingPolicy, filterID policyfilter.PolicyID) ([]SensorIface, error) {
var sensors []SensorIface
for n, s := range registeredPolicyHandlers {
Expand Down
16 changes: 3 additions & 13 deletions pkg/sensors/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func StartSensorManagerWithPF(
}

m := Manager{
policyLister: handler.collections,
handler: handler,
handler: handler,
}
return &m, nil
}
Expand Down Expand Up @@ -173,12 +172,12 @@ func (h *Manager) DisableTracingPolicy(ctx context.Context, name, namespace stri
// ListTracingPolicies returns a list of the active tracing policies
func (h *Manager) ListTracingPolicies(_ context.Context) (*tetragon.ListTracingPoliciesResponse, error) {
ret := &tetragon.ListTracingPoliciesResponse{}
ret.Policies = h.listPolicies()
ret.Policies = h.handler.listPolicies()
return ret, nil
}

func (h *Manager) ListOverheads() ([]ProgOverhead, error) {
return h.listOverheads()
return h.handler.listOverheads()
}

func (h *Manager) RemoveSensor(ctx context.Context, sensorName string) error {
Expand Down Expand Up @@ -229,20 +228,11 @@ 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 {
listPolicies() []*tetragon.TracingPolicyStatus
listOverheads() ([]ProgOverhead, error)
}

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

// There are 6 commands that can be passed to the controller goroutine:
Expand Down

0 comments on commit 4ee1a05

Please sign in to comment.