monitor

package
v0.0.0-...-2b915d2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2024 License: Apache-2.0 Imports: 13 Imported by: 20

README

Monitor

This package provides the ability to populate a crd.Controller dynamically via a function that provides additional config. The monitor will acquire snapshots of additional changes and populate the crd.Controller as needed.

Creating a Monitor

To create a monitor, you should provide the crd.Controller, a polling interval, and a function that returns []*model.Config.

monitor := file.NewMonitor(
    controller,      // The crd controller holding the store and event handlers
    1*time.Second,   // How quickly the monitor requests new snapshots
    getSnapshotFunc) // The function used to acquire new config

Running a Monitor

Once created, you simply run the monitor, providing a stop channel.

stop := make(chan struct{})
...
monitor.Start(stop)

The Start method will kick off an asynchronous polling loop and will return immediately.

Example

To configure and run a monitor that watches for file changes to update an in-memory config store:

// Configure the config store
store := memory.Make(configDescriptor)
controller = memory.NewController(store)
// Create an object that will take snapshots of config
fileSnapshot := configmonitor.NewFileSnapshot(args.Config.FileDir, configDescriptor)
// Provide snapshot func to monitor
fileMonitor := configmonitor.NewMonitor(controller, 100*time.Millisecond, fileSnapshot.ReadFile)

// Run the controller and monitor
stop := make(chan struct{})
go controller.run(stop)
monitor.Start(stop)

See monitor_test.go and file_snapshot_test.go for more examples.

Notes

Always use a Controller

While the API supports any model.ConfigStore, it is recommended to always use a crd.Controller so that other system components can be notified of changes via controller.RegisterEventHandler().

Start behavior

The Start method will immediately check the provided getSnapshotFunc and update the controller appropriately before returning. This helps to simplify tests that rely on starting in a particular state.

After performing an initial update, the Start method then forks an asynchronous polling loop for update/termination.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileSnapshot

type FileSnapshot struct {
	// contains filtered or unexported fields
}

FileSnapshot holds a reference to a file directory that contains crd config and filter criteria for which of those configs will be parsed.

func NewFileSnapshot

func NewFileSnapshot(root string, schemas collection.Schemas, domainSuffix string) *FileSnapshot

NewFileSnapshot returns a snapshotter. If no types are provided in the descriptor, all Istio types will be allowed.

func (*FileSnapshot) ReadConfigFiles

func (f *FileSnapshot) ReadConfigFiles() ([]*config.Config, error)

ReadConfigFiles parses files in the root directory and returns a sorted slice of eligible model.Config. This can be used as a configFunc when creating a Monitor.

type Monitor

type Monitor struct {
	// contains filtered or unexported fields
}

Monitor will poll a config function in order to update a ConfigStore as changes are found.

func NewMonitor

func NewMonitor(name string, delegateStore model.ConfigStore, getSnapshotFunc func() ([]*config.Config, error), root string) *Monitor

NewMonitor creates a Monitor and will delegate to a passed in controller. The controller holds a reference to the actual store. Any func that returns a []*model.Config can be used with the Monitor

func (*Monitor) Start

func (m *Monitor) Start(stop <-chan struct{})

Start starts a new Monitor. Immediately checks the Monitor getSnapshotFunc and updates the controller. It then kicks off an asynchronous event loop that periodically polls the getSnapshotFunc for changes until a close event is sent.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL