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

     1  package cnimonitor
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"go.aporeto.io/trireme-lib/common"
     8  	"go.aporeto.io/trireme-lib/monitor/config"
     9  	"go.aporeto.io/trireme-lib/monitor/extractors"
    10  	"go.aporeto.io/trireme-lib/monitor/registerer"
    11  )
    12  
    13  // Config is the configuration options to start a CNI monitor
    14  type Config struct {
    15  	EventMetadataExtractor extractors.EventMetadataExtractor
    16  }
    17  
    18  // DefaultConfig provides a default configuration
    19  func DefaultConfig() *Config {
    20  	return &Config{
    21  		EventMetadataExtractor: DockerMetadataExtractor,
    22  	}
    23  }
    24  
    25  // SetupDefaultConfig adds defaults to a partial configuration
    26  func SetupDefaultConfig(cniConfig *Config) *Config {
    27  
    28  	defaultConfig := DefaultConfig()
    29  
    30  	if cniConfig.EventMetadataExtractor == nil {
    31  		cniConfig.EventMetadataExtractor = defaultConfig.EventMetadataExtractor
    32  	}
    33  
    34  	return cniConfig
    35  }
    36  
    37  // CniMonitor captures all the monitor processor information
    38  // It implements the EventProcessor interface of the rpc monitor
    39  type CniMonitor struct {
    40  	proc *cniProcessor
    41  }
    42  
    43  // New returns a new implmentation of a monitor implmentation
    44  func New() *CniMonitor {
    45  
    46  	return &CniMonitor{
    47  		proc: &cniProcessor{},
    48  	}
    49  }
    50  
    51  // Run implements Implementation interface
    52  func (c *CniMonitor) Run(ctx context.Context) error {
    53  
    54  	return c.proc.config.IsComplete()
    55  }
    56  
    57  // SetupConfig provides a configuration to implmentations. Every implmentation
    58  // can have its own config type.
    59  func (c *CniMonitor) SetupConfig(registerer registerer.Registerer, cfg interface{}) error {
    60  
    61  	defaultConfig := DefaultConfig()
    62  	if cfg == nil {
    63  		cfg = defaultConfig
    64  	}
    65  
    66  	cniConfig, ok := cfg.(*Config)
    67  	if !ok {
    68  		return fmt.Errorf("Invalid configuration specified")
    69  	}
    70  
    71  	if registerer != nil {
    72  		if err := registerer.RegisterProcessor(common.KubernetesPU, c.proc); err != nil {
    73  			return err
    74  		}
    75  	}
    76  
    77  	// Setup defaults
    78  	cniConfig = SetupDefaultConfig(cniConfig)
    79  
    80  	// Setup configuration
    81  	c.proc.metadataExtractor = cniConfig.EventMetadataExtractor
    82  	if c.proc.metadataExtractor == nil {
    83  		return fmt.Errorf("Unable to setup a metadata extractor")
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  // SetupHandlers sets up handlers for monitors to invoke for various events such as
    90  // processing unit events and synchronization events. This will be called before Start()
    91  // by the consumer of the monitor
    92  func (c *CniMonitor) SetupHandlers(m *config.ProcessorConfig) {
    93  
    94  	c.proc.config = m
    95  }
    96  
    97  // Resync instructs the monitor to do a resync.
    98  func (c *CniMonitor) Resync(ctx context.Context) error {
    99  
   100  	// TODO: Implement resync
   101  	return fmt.Errorf("resync not implemented")
   102  }