github.com/quay/claircore@v1.5.28/libvuln/updates/options.go (about)

     1  package updates
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/quay/claircore/libvuln/driver"
     7  )
     8  
     9  // ManagerOption specify optional configuration for a Manager.
    10  // Defaults will be used where options are not provided to the constructor.
    11  type ManagerOption func(m *Manager)
    12  
    13  // WithBatchSize sets the max number of parallel updaters that will run during an
    14  // update interval.
    15  func WithBatchSize(n int) ManagerOption {
    16  	return func(m *Manager) {
    17  		m.batchSize = n
    18  	}
    19  }
    20  
    21  // WithInterval configures the interval at which updaters will be ran.
    22  // The manager runs all configured updaters during an interval.
    23  // Setting this duration too low may cause missed update intervals.
    24  func WithInterval(interval time.Duration) ManagerOption {
    25  	return func(m *Manager) {
    26  		m.interval = interval
    27  	}
    28  }
    29  
    30  // WithEnabled configures the Manager to only run the specified
    31  // updater sets.
    32  //
    33  // If enabled == nil all default updater sets will run (same as not providing this option to the constructor at all).
    34  // If len(enabled) == 0 no default updater sets will run.
    35  // If len(enabled) > 0 only provided updater sets will be ran.
    36  func WithEnabled(enabled []string) ManagerOption {
    37  	return func(m *Manager) {
    38  		if enabled == nil {
    39  			return
    40  		}
    41  
    42  		factories := map[string]driver.UpdaterSetFactory{}
    43  		for _, enable := range enabled {
    44  			for name, factory := range m.factories {
    45  				if name == enable {
    46  					factories[name] = factory
    47  				}
    48  			}
    49  		}
    50  		m.factories = factories
    51  	}
    52  }
    53  
    54  // WithConfigs tells the Manager to configure each updater where
    55  // a configuration is provided.
    56  //
    57  // Configuration of individual updaters is delegated to updater/registry.go
    58  // Note: this option is optimal when ran after WithEnabled option. However,
    59  // this option has no strict depedency on others.
    60  func WithConfigs(cfgs Configs) ManagerOption {
    61  	return func(m *Manager) {
    62  		m.configs = cfgs
    63  	}
    64  }
    65  
    66  // WithOutOfTree allows callers to provide their own out-of-tree
    67  // updaters.
    68  //
    69  // note: currently we will never configure the outOfTree updater
    70  // factory. if this changes consider making this option a required
    71  // to avoid missing configuration
    72  func WithOutOfTree(outOfTree []driver.Updater) ManagerOption {
    73  	return func(m *Manager) {
    74  		us := driver.NewUpdaterSet()
    75  		for _, u := range outOfTree {
    76  			if err := us.Add(u); err != nil {
    77  				// duplicate updater, ignore.
    78  				continue
    79  			}
    80  		}
    81  		m.factories["outOfTree"] = driver.StaticSet(us)
    82  	}
    83  }
    84  
    85  // WithGC instructs the manager to run garbage collection
    86  // at the end of an update interval.
    87  //
    88  // The provided retention value informs the manager how many
    89  // update operations to keep.
    90  func WithGC(retention int) ManagerOption {
    91  	return func(m *Manager) {
    92  		m.updateRetention = retention
    93  	}
    94  }
    95  
    96  // WithFactories resets UpdaterSetFactories used by the Manager.
    97  func WithFactories(f map[string]driver.UpdaterSetFactory) ManagerOption {
    98  	return func(m *Manager) {
    99  		m.factories = f
   100  	}
   101  }