github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/internal/uid/monitor.go (about)

     1  package uidmonitor
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"regexp"
     7  
     8  	"go.aporeto.io/trireme-lib/common"
     9  	"go.aporeto.io/trireme-lib/monitor/config"
    10  	"go.aporeto.io/trireme-lib/monitor/registerer"
    11  	"go.aporeto.io/trireme-lib/utils/cache"
    12  	"go.aporeto.io/trireme-lib/utils/cgnetcls"
    13  )
    14  
    15  // UIDMonitor captures all the monitor processor information for a UIDLoginPU
    16  // It implements the EventProcessor interface of the rpc monitor
    17  type UIDMonitor struct {
    18  	proc *uidProcessor
    19  }
    20  
    21  // New returns a new implmentation of a monitor implmentation
    22  func New() *UIDMonitor {
    23  
    24  	return &UIDMonitor{
    25  		proc: &uidProcessor{},
    26  	}
    27  }
    28  
    29  // Run implements Implementation interface
    30  func (u *UIDMonitor) Run(ctx context.Context) error {
    31  
    32  	if err := u.proc.config.IsComplete(); err != nil {
    33  		return fmt.Errorf("uid: %s", err)
    34  	}
    35  
    36  	return u.Resync(ctx)
    37  }
    38  
    39  // SetupConfig provides a configuration to implmentations. Every implmentation
    40  // can have its own config type.
    41  func (u *UIDMonitor) SetupConfig(registerer registerer.Registerer, cfg interface{}) error {
    42  
    43  	defaultConfig := DefaultConfig()
    44  	if cfg == nil {
    45  		cfg = defaultConfig
    46  	}
    47  
    48  	uidConfig, ok := cfg.(*Config)
    49  	if !ok {
    50  		return fmt.Errorf("Invalid configuration specified")
    51  	}
    52  
    53  	if registerer != nil {
    54  		if err := registerer.RegisterProcessor(common.UIDLoginPU, u.proc); err != nil {
    55  			return err
    56  		}
    57  	}
    58  
    59  	// Setup defaults
    60  	uidConfig = SetupDefaultConfig(uidConfig)
    61  
    62  	// Setup config
    63  	u.proc.netcls = cgnetcls.NewCgroupNetController(common.TriremeUIDCgroupPath, uidConfig.ReleasePath)
    64  	u.proc.regStart = regexp.MustCompile("^[a-zA-Z0-9_]{1,11}$")
    65  	u.proc.regStop = regexp.MustCompile("^/trireme/[a-zA-Z0-9_]{1,11}$")
    66  	u.proc.putoPidMap = cache.NewCache("putoPidMap")
    67  	u.proc.pidToPU = cache.NewCache("pidToPU")
    68  	u.proc.metadataExtractor = uidConfig.EventMetadataExtractor
    69  	if u.proc.metadataExtractor == nil {
    70  		return fmt.Errorf("Unable to setup a metadata extractor")
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  // SetupHandlers sets up handlers for monitors to invoke for various events such as
    77  // processing unit events and synchronization events. This will be called before Start()
    78  // by the consumer of the monitor
    79  func (u *UIDMonitor) SetupHandlers(m *config.ProcessorConfig) {
    80  
    81  	u.proc.config = m
    82  }
    83  
    84  // Resync asks the monitor to do a resync
    85  func (u *UIDMonitor) Resync(ctx context.Context) error {
    86  	return u.proc.Resync(ctx, nil)
    87  }