|
| 1 | +// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl. |
| 3 | + |
| 4 | +package oracle.kubernetes.operator; |
| 5 | + |
| 6 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 7 | + |
| 8 | +import com.google.gson.reflect.TypeToken; |
| 9 | +import io.kubernetes.client.ApiException; |
| 10 | +import io.kubernetes.client.models.V1beta1Ingress; |
| 11 | +import io.kubernetes.client.util.Watch; |
| 12 | +import oracle.kubernetes.operator.helpers.ClientHelper; |
| 13 | +import oracle.kubernetes.operator.helpers.ClientHolder; |
| 14 | +import oracle.kubernetes.operator.watcher.Watcher; |
| 15 | +import oracle.kubernetes.operator.watcher.Watching; |
| 16 | +import oracle.kubernetes.operator.watcher.WatchingEventDestination; |
| 17 | + |
| 18 | +/** |
| 19 | + * This class handles Ingress watching. It receives Ingress change events and sends |
| 20 | + * them into the operator for processing. |
| 21 | + */ |
| 22 | +public class IngressWatcher implements Runnable { |
| 23 | + private final String ns; |
| 24 | + private final String initialResourceVersion; |
| 25 | + private final WatchingEventDestination<V1beta1Ingress> destination; |
| 26 | + private final AtomicBoolean isStopping; |
| 27 | + |
| 28 | + public static IngressWatcher create(String ns, String initialResourceVersion, WatchingEventDestination<V1beta1Ingress> destination, AtomicBoolean isStopping) { |
| 29 | + IngressWatcher dlw = new IngressWatcher(ns, initialResourceVersion, destination, isStopping); |
| 30 | + Thread thread = new Thread(dlw); |
| 31 | + thread.setName("Thread-IngressWatcher-" + ns); |
| 32 | + thread.setDaemon(true); |
| 33 | + thread.start(); |
| 34 | + return dlw; |
| 35 | + } |
| 36 | + |
| 37 | + private IngressWatcher(String ns, String initialResourceVersion, WatchingEventDestination<V1beta1Ingress> destination, AtomicBoolean isStopping) { |
| 38 | + this.ns = ns; |
| 39 | + this.initialResourceVersion = initialResourceVersion; |
| 40 | + this.destination = destination; |
| 41 | + this.isStopping = isStopping; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Polling loop. Get the next Ingress object event and process it. |
| 46 | + */ |
| 47 | + @Override |
| 48 | + public void run() { |
| 49 | + ClientHelper helper = ClientHelper.getInstance(); |
| 50 | + ClientHolder client = helper.take(); |
| 51 | + try { |
| 52 | + Watching<V1beta1Ingress> w = createWatching(client); |
| 53 | + Watcher<V1beta1Ingress> watcher = new Watcher<V1beta1Ingress>(w, null, initialResourceVersion); |
| 54 | + |
| 55 | + // invoke watch on current Thread. Won't return until watch stops |
| 56 | + watcher.doWatch(); |
| 57 | + |
| 58 | + } finally { |
| 59 | + helper.recycle(client); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + protected Watching<V1beta1Ingress> createWatching(ClientHolder client) { |
| 64 | + return new Watching<V1beta1Ingress>() { |
| 65 | + |
| 66 | + /** |
| 67 | + * Watcher callback to issue the list Ingress changes. It is driven by the |
| 68 | + * Watcher wrapper to issue repeated watch requests. |
| 69 | + * @param context user defined contact object or null |
| 70 | + * @param resourceVersion resource version to omit older events |
| 71 | + * @return Watch object or null if the operation should end |
| 72 | + * @throws ApiException if there is an API error. |
| 73 | + */ |
| 74 | + @Override |
| 75 | + public Watch<V1beta1Ingress> initiateWatch(Object context, String resourceVersion) throws ApiException { |
| 76 | + return Watch.createWatch(client.getApiClient(), |
| 77 | + client.callBuilder().with($ -> { |
| 78 | + $.resourceVersion = resourceVersion; |
| 79 | + $.labelSelector = LabelConstants.DOMAINUID_LABEL; // Any Ingress with a domainUID label |
| 80 | + $.timeoutSeconds = 30; |
| 81 | + $.watch = true; |
| 82 | + }).listIngressCall(ns), |
| 83 | + new TypeToken<Watch.Response<V1beta1Ingress>>() { |
| 84 | + }.getType()); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void eventCallback(Watch.Response<V1beta1Ingress> item) { |
| 89 | + processEventCallback(item); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean isStopping() { |
| 94 | + return isStopping.get(); |
| 95 | + } |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + public void processEventCallback(Watch.Response<V1beta1Ingress> item) { |
| 100 | + destination.eventCallback(item); |
| 101 | + } |
| 102 | +} |
0 commit comments