configmap

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package configmap exists to facilitate consuming Kubernetes ConfigMap resources in various ways, including:

  • Watching them for changes over time, and
  • Loading them from a VolumeMount.

Index

Constants

View Source
const (
	// ExampleKey signifies a given example configuration in a ConfigMap.
	ExampleKey = "_example"

	// ExampleChecksumAnnotation is the annotation that stores the computed checksum.
	ExampleChecksumAnnotation = "knative.dev/example-checksum"
)

Variables

This section is empty.

Functions

func Checksum

func Checksum(value string) string

Checksum generates a checksum for the example value to be compared against a respective annotation. Leading and trailing spaces are ignored.

func Load

func Load(p string) (map[string]string, error)

Load reads the "Data" of a ConfigMap from a particular VolumeMount.

func Parse

func Parse(data map[string]string, parsers ...ParseFunc) error

Parse parses the given map using the parser functions passed in.

func TypeFilter

func TypeFilter(ts ...interface{}) func(func(string, interface{})) func(string, interface{})

TypeFilter accepts instances of types to check against and returns a function transformer that would only let the call to f through if value is assignable to any one of types of ts. Example:

F := configmap.TypeFilter(&config.Domain{})(f)

The result is a function F(name string, value interface{}) that will call the underlying function f(name, value) iff value is a *config.Domain

func ValidateConstructor

func ValidateConstructor(constructor interface{}) error

ValidateConstructor checks the type of the constructor it evaluates the constructor to be a function with correct signature.

The expectation is for the constructor to receive a single input parameter of type corev1.ConfigMap as the input and return two values with the second value being of type error

Types

type Constructors

type Constructors map[string]interface{}

Constructors is a map for specifying configmap names to their function constructors

The values of this map must be functions with the definition

func(*k8s.io/api/core/v1.ConfigMap) (... , error)

These functions can return any type along with an error

type DefaultingWatcher

type DefaultingWatcher interface {
	Watcher

	// WatchWithDefault is called to register callbacks to be notified when a named ConfigMap
	// changes. The provided default value is always observed before any real ConfigMap with that
	// name is. If the real ConfigMap with that name is deleted, then the default value is observed.
	WatchWithDefault(cm corev1.ConfigMap, o ...Observer)
}

DefaultingWatcher is similar to Watcher, but if a ConfigMap is absent, then a code provided default will be used.

type Logger

type Logger interface {
	Debugf(string, ...interface{})
	Infof(string, ...interface{})
	Fatalf(string, ...interface{})
	Errorf(string, ...interface{})
}

Logger is the interface that UntypedStore expects its logger to conform to. UntypedStore will log when updates succeed or fail.

type ManualWatcher

type ManualWatcher struct {
	Namespace string

	// Guards observers
	sync.RWMutex
	// contains filtered or unexported fields
}

ManualWatcher will notify Observers when a ConfigMap is manually reported as changed

func (*ManualWatcher) ForEach

func (w *ManualWatcher) ForEach(f func(string, []Observer) error) error

ForEach implements Watcher

func (*ManualWatcher) OnChange

func (w *ManualWatcher) OnChange(configMap *corev1.ConfigMap)

OnChange invokes the callbacks of all observers of the given ConfigMap.

func (*ManualWatcher) Start

func (w *ManualWatcher) Start(<-chan struct{}) error

Start implements Watcher

func (*ManualWatcher) Watch

func (w *ManualWatcher) Watch(name string, o ...Observer)

Watch implements Watcher

type Observer

type Observer func(*corev1.ConfigMap)

Observer is the signature of the callbacks that notify an observer of the latest state of a particular configuration. An observer should not modify the provided ConfigMap, and should `.DeepCopy()` it for persistence (or otherwise process its contents).

type ParseFunc

type ParseFunc func(map[string]string) error

ParseFunc is a function taking ConfigMap data and applying a parse operation to it.

func AsBool

func AsBool(key string, target *bool) ParseFunc

AsBool parses the value at key as a boolean into the target, if it exists.

func AsDuration

func AsDuration(key string, target *time.Duration) ParseFunc

AsDuration parses the value at key as a time.Duration into the target, if it exists.

func AsFloat64

func AsFloat64(key string, target *float64) ParseFunc

AsFloat64 parses the value at key as a float64 into the target, if it exists.

func AsInt

func AsInt(key string, target *int) ParseFunc

AsInt parses the value at key as an int into the target, if it exists.

func AsInt16

func AsInt16(key string, target *int16) ParseFunc

AsInt16 parses the value at key as an int16 into the target, if it exists.

func AsInt32

func AsInt32(key string, target *int32) ParseFunc

AsInt32 parses the value at key as an int32 into the target, if it exists.

func AsInt64

func AsInt64(key string, target *int64) ParseFunc

AsInt64 parses the value at key as an int64 into the target, if it exists.

func AsNamespacedName

func AsNamespacedName(key string, target *types.NamespacedName) ParseFunc

AsNamespacedName parses the value at key as a types.NamespacedName into the target, if it exists The namespace and name are both required and expected to be valid DNS labels

func AsOptionalNamespacedName

func AsOptionalNamespacedName(key string, target **types.NamespacedName) ParseFunc

AsOptionalNamespacedName parses the value at key as a types.NamespacedName into the target, if it exists The namespace and name are both required and expected to be valid DNS labels

func AsQuantity

func AsQuantity(key string, target **resource.Quantity) ParseFunc

AsQuantity parses the value at key as a *resource.Quantity into the target, if it exists

func AsString

func AsString(key string, target *string) ParseFunc

AsString passes the value at key through into the target, if it exists.

func AsStringSet

func AsStringSet(key string, target *sets.String) ParseFunc

AsStringSet parses the value at key as a sets.String (split by ',') into the target, if it exists.

func AsUint16

func AsUint16(key string, target *uint16) ParseFunc

AsUint16 parses the value at key as an uint16 into the target, if it exists.

func AsUint32

func AsUint32(key string, target *uint32) ParseFunc

AsUint32 parses the value at key as an uint32 into the target, if it exists.

func CollectMapEntriesWithPrefix

func CollectMapEntriesWithPrefix(prefix string, target *map[string]string) ParseFunc

CollectMapEntriesWithPrefix parses the data into the target as a map[string]string, if it exists. The map is represented as a list of key-value pairs with a common prefix.

type StaticWatcher

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

StaticWatcher is a Watcher with static ConfigMaps. Callbacks will occur when Watch is invoked for a specific Observer

func NewStaticWatcher

func NewStaticWatcher(cms ...*corev1.ConfigMap) *StaticWatcher

NewStaticWatcher returns an StaticWatcher that exposes a collection of ConfigMaps.

func (*StaticWatcher) Start

func (di *StaticWatcher) Start(<-chan struct{}) error

Start implements Watcher

func (*StaticWatcher) Watch

func (di *StaticWatcher) Watch(name string, o ...Observer)

Watch implements Watcher

type UntypedStore

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

An UntypedStore is a responsible for storing and constructing configs from Kubernetes ConfigMaps

WatchConfigs should be used with a configmap.Watcher in order for this store to remain up to date

func NewUntypedStore

func NewUntypedStore(
	name string,
	logger Logger,
	constructors Constructors,
	onAfterStore ...func(name string, value interface{})) *UntypedStore

NewUntypedStore creates an UntypedStore with given name, Logger and Constructors

The Logger must not be nil

The values in the Constructors map must be functions with the definition

func(*k8s.io/api/core/v1.ConfigMap) (... , error)

These functions can return any type along with an error. If the function definition differs then NewUntypedStore will panic.

onAfterStore is a variadic list of callbacks to run after the ConfigMap has been transformed (via the appropriate Constructor) and stored. These callbacks run sequentially (in the argument order) in a separate go-routine and are of type func(name string, value interface{}) where name is the config-map name and value is the object that has been constructed from the config-map and stored.

func (*UntypedStore) OnConfigChanged

func (s *UntypedStore) OnConfigChanged(c *corev1.ConfigMap)

OnConfigChanged will invoke the mapped constructor against a Kubernetes ConfigMap. If successful it will be stored. If construction fails during the first appearance the store will log a fatal error. If construction fails while updating the store will log an error message.

func (*UntypedStore) UntypedLoad

func (s *UntypedStore) UntypedLoad(name string) interface{}

UntypedLoad will return the constructed value for a given ConfigMap name

func (*UntypedStore) WatchConfigs

func (s *UntypedStore) WatchConfigs(w Watcher)

WatchConfigs uses the provided configmap.Watcher to setup watches for the config names provided in the Constructors map

type Watcher

type Watcher interface {
	// Watch is called to register callbacks to be notified when a named ConfigMap changes.
	Watch(string, ...Observer)

	// Start is called to initiate the watches and provide a channel to signal when we should
	// stop watching.  When Start returns, all registered Observers will be called with the
	// initial state of the ConfigMaps they are watching.
	Start(<-chan struct{}) error
}

Watcher defines the interface that a configmap implementation must implement.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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