T
- a Kubernetes resource type@Immutable @ThreadSafe public class Controller<T extends HasMetadata> extends Object implements Closeable
Reflector
, a VersionWatchable
and Listable
implementation, an
(internal) EventQueueCollection
, a Map
of known
Kubernetes resources and an EventQueue
Consumer
that mirrors Kubernetes cluster
events into a collection of
EventQueue
s and arranges for their consumption
and processing.
Starting a Controller
starts the
Consumer
supplied at construction time, and
starts the embedded
Reflector
. Closing a Controller
closes its embedded
Reflector
and causes the Consumer
supplied at construction time to stop receiving
Event
s.
Several protected
methods in this class exist to make
customization easier; none require overriding and their default
behavior is usually just fine.
Instances of this class are safe for concurrent use by multiple threads.
This class loosely models a combination of a Controller
type and a SharedIndexInformer
type as found in controller.go
and shared_informer.go
respectively.
Reflector
,
EventQueueCollection
,
ResourceTrackingEventQueueConsumer
,
start()
,
close()
Modifier and Type | Field and Description |
---|---|
protected Logger |
logger
A
Logger used by this Controller . |
Constructor and Description |
---|
Controller(X operation,
Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Creates a new
Controller but does not start it. |
Controller(X operation,
Duration synchronizationInterval,
Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Creates a new
Controller but does not start it. |
Controller(X operation,
Duration synchronizationInterval,
Map<Object,T> knownObjects,
Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Creates a new
Controller but does not start it. |
Controller(X operation,
Map<Object,T> knownObjects,
Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Creates a new
Controller but does not start it. |
Controller(X operation,
ScheduledExecutorService synchronizationExecutorService,
Duration synchronizationInterval,
Function<? super Throwable,Boolean> errorHandler,
Map<Object,T> knownObjects,
Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Creates a new
Controller but does not start it. |
Controller(X operation,
ScheduledExecutorService synchronizationExecutorService,
Duration synchronizationInterval,
Map<Object,T> knownObjects,
Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Creates a new
Controller but does not start it. |
Modifier and Type | Method and Description |
---|---|
void |
awaitEventCacheSynchronization()
|
@Blocking boolean |
awaitEventCacheSynchronization(long timeout,
TimeUnit timeUnit)
Blocks for the desired amount of time until the
EventQueueCollection affiliated with this Controller
has
synchronized or the amount of time has elapsed. |
void |
close()
Closes the embedded
Reflector and then closes the embedded
EventQueueCollection , handling exceptions
appropriately. |
protected Event<T> |
createEvent(Object source,
AbstractEvent.Type eventType,
T resource)
Creates a new
Event when invoked. |
protected EventQueue<T> |
createEventQueue(Object key)
Creates a new
EventQueue when invoked. |
protected Logger |
createLogger()
Returns a
Logger for use by this Controller . |
protected Object |
getKey(T resource)
Returns a key that can be used to identify the supplied
HasMetadata . |
protected void |
onClose()
|
protected boolean |
shouldSynchronize()
Returns if the embedded
Reflector should synchronize. |
void |
start()
|
protected final Logger logger
createLogger()
public Controller(X operation, Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Controller
but does not start it.X
- a Listable
and VersionWatchable
that
will be used by the embedded Reflector
; must not be
null
operation
- a Listable
and a VersionWatchable
that produces Kubernetes events; must not be
null
eventQueueConsumer
- the Consumer
that will process
each EventQueue
as it becomes ready; must not be null
NullPointerException
- if operation
or eventQueueConsumer
is null
Controller(Listable, ScheduledExecutorService, Duration,
Map, Consumer)
,
start()
public Controller(X operation, Map<Object,T> knownObjects, Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Controller
but does not start it.X
- a Listable
and VersionWatchable
that
will be used by the embedded Reflector
; must not be
null
operation
- a Listable
and a VersionWatchable
that produces Kubernetes events; must not be
null
knownObjects
- a Map
containing the last known state
of Kubernetes resources the embedded EventQueueCollection
is caching events for; may be null
if this Controller
is not interested in tracking deletions of objects;
if non-null
will be synchronized on by this
class during retrieval and traversal operationseventQueueConsumer
- the Consumer
that will process
each EventQueue
as it becomes ready; must not be null
NullPointerException
- if operation
or eventQueueConsumer
is null
Controller(Listable, ScheduledExecutorService, Duration,
Map, Consumer)
,
start()
public Controller(X operation, Duration synchronizationInterval, Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Controller
but does not start it.X
- a Listable
and VersionWatchable
that
will be used by the embedded Reflector
; must not be
null
operation
- a Listable
and a VersionWatchable
that produces Kubernetes events; must not be
null
synchronizationInterval
- a Duration
representing
the time in between one synchronization operation and another; may be null
in
which case no synchronization will occureventQueueConsumer
- the Consumer
that will process
each EventQueue
as it becomes ready; must not be null
NullPointerException
- if operation
or eventQueueConsumer
is null
Controller(Listable, ScheduledExecutorService, Duration,
Map, Consumer)
,
start()
public Controller(X operation, Duration synchronizationInterval, Map<Object,T> knownObjects, Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Controller
but does not start it.X
- a Listable
and VersionWatchable
that
will be used by the embedded Reflector
; must not be
null
operation
- a Listable
and a VersionWatchable
that produces Kubernetes events; must not be
null
synchronizationInterval
- a Duration
representing
the time in between one synchronization operation and another; may be null
in
which case no synchronization will occurknownObjects
- a Map
containing the last known state
of Kubernetes resources the embedded EventQueueCollection
is caching events for; may be null
if this Controller
is not interested in tracking deletions of objects;
if non-null
will be synchronized on by this
class during retrieval and traversal operationseventQueueConsumer
- the Consumer
that will process
each EventQueue
as it becomes ready; must not be null
NullPointerException
- if operation
or eventQueueConsumer
is null
Controller(Listable, ScheduledExecutorService, Duration,
Map, Consumer)
,
start()
public Controller(X operation, ScheduledExecutorService synchronizationExecutorService, Duration synchronizationInterval, Map<Object,T> knownObjects, Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Controller
but does not start it.X
- a Listable
and VersionWatchable
that
will be used by the embedded Reflector
; must not be
null
operation
- a Listable
and a VersionWatchable
that produces Kubernetes events; must not be
null
synchronizationExecutorService
- the ScheduledExecutorService
that will be passed to the Reflector
constructor; may be null
in which case a
default ScheduledExecutorService
may be used insteadsynchronizationInterval
- a Duration
representing
the time in between one synchronization operation and another; may be null
in
which case no synchronization will occurknownObjects
- a Map
containing the last known state
of Kubernetes resources the embedded EventQueueCollection
is caching events for; may be null
if this Controller
is not interested in tracking deletions of objects;
if non-null
will be synchronized on by this
class during retrieval and traversal operationseventQueueConsumer
- the Consumer
that will process
each EventQueue
as it becomes ready; must not be null
NullPointerException
- if operation
or eventQueueConsumer
is null
start()
public Controller(X operation, ScheduledExecutorService synchronizationExecutorService, Duration synchronizationInterval, Function<? super Throwable,Boolean> errorHandler, Map<Object,T> knownObjects, Consumer<? super EventQueue<? extends T>> eventQueueConsumer)
Controller
but does not start it.X
- a Listable
and VersionWatchable
that
will be used by the embedded Reflector
; must not be
null
operation
- a Listable
and a VersionWatchable
that produces Kubernetes events; must not be
null
synchronizationExecutorService
- the ScheduledExecutorService
that will be passed to the Reflector
constructor; may be null
in which case a
default ScheduledExecutorService
may be used insteadsynchronizationInterval
- a Duration
representing
the time in between one synchronization operation and another; may be null
in
which case no synchronization will occurerrorHandler
- a Function
that accepts a Throwable
and returns a Boolean
indicating whether the
error was handled or not; used to handle truly unanticipated
errors from within a ScheduledExecutorService
used
during synchronization and
event consumption activities; may be null
knownObjects
- a Map
containing the last known state
of Kubernetes resources the embedded EventQueueCollection
is caching events for; may be null
if this Controller
is not interested in tracking deletions of objects;
if non-null
will be synchronized on by this
class during retrieval and traversal operationseventQueueConsumer
- the Consumer
that will process
each EventQueue
as it becomes ready; must not be null
NullPointerException
- if operation
or eventQueueConsumer
is null
start()
protected Logger createLogger()
Logger
for use by this Controller
.
This method never returns null
.
Overrides of this method must not return null
.
null
Logger
@Blocking public final void awaitEventCacheSynchronization() throws InterruptedException
InterruptedException
- if the current Thread
was
interrupted@Blocking public final @Blocking boolean awaitEventCacheSynchronization(long timeout, TimeUnit timeUnit) throws InterruptedException
EventQueueCollection
affiliated with this Controller
has
synchronized or the amount of time has elapsed.timeout
- the amount of time to waittimeUnit
- the TimeUnit
designating the amount of
time to wait; must not be null
false
if the waiting time elapsed before the
event cache synchronized; true
otherwiseInterruptedException
- if the current Thread
was
interruptedNullPointerException
- if timeUnit
is null
EventQueueCollection.SynchronizationAwaitingPropertyChangeListener
@NonBlocking public final void start() throws IOException
EventQueueCollection
consumption machinery
and then starts the embedded
Reflector
.IOException
- if Reflector.start()
throws an
IOException
KubernetesClientException
- if the embedded Reflector
could not be startedEventQueueCollection.start(Consumer)
,
Reflector.start()
public final void close() throws IOException
Reflector
and then closes the embedded
EventQueueCollection
, handling exceptions
appropriately.close
in interface Closeable
close
in interface AutoCloseable
IOException
- if the Reflector
could not
close properlyReflector.close()
,
EventQueueCollection.close()
protected boolean shouldSynchronize()
true
if the embedded Reflector
should
synchronize; false
otherwiseprotected void onClose()
Reflector.close()
,
Reflector.onClose()
protected Object getKey(T resource)
HasMetadata
.
This method never returns null
.
Overrides of this method must not return null
.
The default implementation of this method returns the return
value of invoking the HasMetadatas.getKey(HasMetadata)
method.
resource
- the Kubernetes resource for which a key is
desired; must not be null
null
key for the supplied HasMetadata
NullPointerException
- if resource
is null
protected Event<T> createEvent(Object source, AbstractEvent.Type eventType, T resource)
Event
when invoked.
This method never returns null
.
Overrides of this method must not return null
.
Overrides of this method must return a new Event
or
subclass with each invocation.
source
- the source of the new Event
; must not be
null
eventType
- the Event.Type
for the new Event
; must not be null
resource
- the HasMetadata
that the new Event
concerns; must not be null
null
Event
NullPointerException
- if any of the parameters is
null
protected EventQueue<T> createEventQueue(Object key)
EventQueue
when invoked.
This method never returns null
.
Overrides of this method must not return null
.
Overrides of this method must return a new EventQueue
or subclass with each invocation.
key
- the key to create the new EventQueue
with;
must not be null
null
EventQueue
NullPointerException
- if key
is null
Copyright © 2017–2021, microBean™. All rights reserved.