snowdrop-zen / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
1 stars 0 forks source link

kubernetes client: set-up watchers using annotations #372

Open snowdrop-bot opened 3 years ago

snowdrop-bot commented 3 years ago

Description

When using the Kubernetes client to watch resource related events, some boilerplate code is needed. Inspired by the reactive route support that exists for Vert.x, I think it would be very beneficial if watch events can be expressed using annotations.

Implementation ideas

As example, some annotations and classes can be introduced:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.Method)
public @interface LabelSelector {
    String key()),
    String operation() default "In"),
    String[] values()
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.Method)
public @interface ResourceWatcher {
    /*
     * An optional namespace, if not set, watch all namespaces.
     * Can be defined using a placeholder
     */
    String namespace() default ALL_NAMESPACES,

    /*
     * An optional ID of an informer, if not defined, the watcher
     * is a plain watcher.
     * Can be defined using a placeholder
     */
    String informer() default NO_INCORMER, 

    /*
     * A list of selectors used to fine tune the resources that
     * should be watched
     */
    LabelSelector[] selectors() default {}

    /*
     * The acions for which this watcher should be notified. 
     * Default is all actions
     */
    Action[] actions() default { Action.ADDED, Action.MODIFIED, Action.DELETED, Action.ERROR }
}

interface WatchEvent<T> {
    T resource();
    Action action();
    Throwable error();
}

Those annotations can be then leveraged as:

class MyPodHandler {
    @ResourceWatcher(
        selectors = { 
            @LabelSelector(key = "application", values = "MyApp")
        },
        actions = { 
            Action.ADDED, 
            Action.MODIFIED, 
            Action.DELETED 
        }
    )
    void handlePodEvents(WatchEvent<Pod> event) {
    }
}

Behind the scenes, quarkus should:


https://github.com/quarkusio/quarkus/issues/18526


$upstream:18526$