Skip to content

Commit 12d340d

Browse files
committed
Remove duplicate code from watchers
1 parent 50e837a commit 12d340d

File tree

17 files changed

+292
-559
lines changed

17 files changed

+292
-559
lines changed

docs/apidocs/oracle/kubernetes/operator/watcher/Watching.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ <h3>Method Summary</h3>
173173
</tr>
174174
</table>
175175
<ul class="blockList">
176-
<li class="blockList"><a name="methods.inherited.from.class.oracle.kubernetes.operator.watcher.WatchingEventDestination">
176+
<li class="blockList"><a name="methods.inherited.from.class.oracle.kubernetes.operator.watcher.WatchListener">
177177
<!-- -->
178178
</a>
179179
<h3>Methods inherited from interface&nbsp;oracle.kubernetes.operator.watcher.<a href="../../../../oracle/kubernetes/operator/watcher/WatchingEventDestination.html" title="interface in oracle.kubernetes.operator.watcher">WatchingEventDestination</a></h3>

docs/apidocs/oracle/kubernetes/operator/watcher/class-use/WatchingEventDestination.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<html lang="en">
44
<head>
55
<!-- Generated by javadoc (9.0.4) on Wed Mar 07 11:29:56 EST 2018 -->
6-
<title>Uses of Interface oracle.kubernetes.operator.watcher.WatchingEventDestination (weblogic-kubernetes-operator 0.1.0 API)</title>
6+
<title>Uses of Interface oracle.kubernetes.operator.watcher.WatchListener (weblogic-kubernetes-operator 0.1.0 API)</title>
77
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
88
<meta name="date" content="2018-03-07">
99
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
@@ -21,13 +21,12 @@
2121
<script type="text/javascript"><!--
2222
try {
2323
if (location.href.indexOf('is-external=true') == -1) {
24-
parent.document.title="Uses of Interface oracle.kubernetes.operator.watcher.WatchingEventDestination (weblogic-kubernetes-operator 0.1.0 API)";
24+
parent.document.title="Uses of Interface oracle.kubernetes.operator.watcher.WatchListener (weblogic-kubernetes-operator 0.1.0 API)";
2525
}
2626
}
2727
catch(err) {
2828
}
29-
//-->
30-
var pathtoroot = "../../../../../";loadScripts(document, 'script');</script>
29+
//--></script>
3130
<noscript>
3231
<div>JavaScript is disabled on your browser.</div>
3332
</noscript>
@@ -95,7 +94,7 @@
9594
//-->
9695
</script>
9796
<div class="header">
98-
<h2 title="Uses of Interface oracle.kubernetes.operator.watcher.WatchingEventDestination" class="title">Uses of Interface<br>oracle.kubernetes.operator.watcher.WatchingEventDestination</h2>
97+
<h2 title="Uses of Interface oracle.kubernetes.operator.watcher.WatchListener" class="title">Uses of Interface<br>oracle.kubernetes.operator.watcher.WatchingEventDestination</h2>
9998
</div>
10099
<div class="classUseContainer">
101100
<ul class="blockList">

src/main/java/oracle/kubernetes/operator/ConfigMapWatcher.java

Lines changed: 11 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -5,103 +5,34 @@
55

66
import io.kubernetes.client.ApiException;
77
import io.kubernetes.client.models.V1ConfigMap;
8-
import io.kubernetes.client.util.Watch;
98
import oracle.kubernetes.operator.builders.WatchBuilder;
109
import oracle.kubernetes.operator.builders.WatchI;
11-
import oracle.kubernetes.operator.helpers.ClientHelper;
12-
import oracle.kubernetes.operator.helpers.ClientHolder;
13-
import oracle.kubernetes.operator.watcher.ThreadedWatcher;
14-
import oracle.kubernetes.operator.watcher.Watching;
15-
import oracle.kubernetes.operator.watcher.WatchingEventDestination;
10+
import oracle.kubernetes.operator.watcher.WatchListener;
1611

1712
import java.util.concurrent.atomic.AtomicBoolean;
1813

1914
/**
2015
* This class handles ConfigMap watching. It receives config map change events and sends
2116
* them into the operator for processing.
2217
*/
23-
public class ConfigMapWatcher implements Runnable, ThreadedWatcher {
18+
public class ConfigMapWatcher extends Watcher<V1ConfigMap> {
2419
private final String ns;
25-
private final String initialResourceVersion;
26-
private final WatchingEventDestination<V1ConfigMap> destination;
27-
private final AtomicBoolean isStopping;
28-
private Thread thread;
2920

30-
public static ConfigMapWatcher create(String ns, String initialResourceVersion, WatchingEventDestination<V1ConfigMap> destination, AtomicBoolean isStopping) {
31-
ConfigMapWatcher dlw = new ConfigMapWatcher(ns, initialResourceVersion, destination, isStopping);
32-
Thread thread = new Thread(dlw);
33-
thread.setName("Thread-ConfigMapWatcher-" + ns);
34-
thread.setDaemon(true);
35-
thread.start();
36-
dlw.thread = thread;
37-
return dlw;
21+
public static ConfigMapWatcher create(String ns, String initialResourceVersion, WatchListener<V1ConfigMap> listener, AtomicBoolean isStopping) {
22+
ConfigMapWatcher watcher = new ConfigMapWatcher(ns, initialResourceVersion, listener, isStopping);
23+
watcher.start("Thread-ConfigMapWatcher-" + ns);
24+
return watcher;
3825
}
3926

40-
private ConfigMapWatcher(String ns, String initialResourceVersion, WatchingEventDestination<V1ConfigMap> destination, AtomicBoolean isStopping) {
27+
private ConfigMapWatcher(String ns, String initialResourceVersion, WatchListener<V1ConfigMap> listener, AtomicBoolean isStopping) {
28+
super(initialResourceVersion, isStopping, listener);
4129
this.ns = ns;
42-
this.initialResourceVersion = initialResourceVersion;
43-
this.destination = destination;
44-
this.isStopping = isStopping;
4530
}
4631

4732
@Override
48-
public Thread getThread() {
49-
return thread;
33+
public WatchI<V1ConfigMap> initiateWatch(WatchBuilder watchBuilder) throws ApiException {
34+
return watchBuilder.withLabelSelector(LabelConstants.CREATEDBYOPERATOR_LABEL)
35+
.createConfigMapWatch(ns);
5036
}
5137

52-
/**
53-
* Polling loop. Get the next ConfigMap object event and process it.
54-
*/
55-
@Override
56-
public void run() {
57-
ClientHelper helper = ClientHelper.getInstance();
58-
ClientHolder client = helper.take();
59-
try {
60-
Watching<V1ConfigMap> w = createWatching(client);
61-
Watcher<V1ConfigMap> watcher = new Watcher<>(w, initialResourceVersion);
62-
63-
// invoke watch on current Thread. Won't return until watch stops
64-
watcher.doWatch();
65-
66-
} finally {
67-
helper.recycle(client);
68-
}
69-
}
70-
71-
protected Watching<V1ConfigMap> createWatching(ClientHolder client) {
72-
return new Watching<V1ConfigMap>() {
73-
74-
/**
75-
* Watcher callback to issue the list ConfigMap changes. It is driven by the
76-
* Watcher wrapper to issue repeated watch requests.
77-
* @param resourceVersion resource version to omit older events
78-
* @return Watch object or null if the operation should end
79-
* @throws ApiException if there is an API error.
80-
*/
81-
@Override
82-
public WatchI<V1ConfigMap> initiateWatch(String resourceVersion) throws ApiException {
83-
return initiateWatch(new WatchBuilder(client).withResourceVersion(resourceVersion));
84-
}
85-
86-
@Override
87-
public WatchI<V1ConfigMap> initiateWatch(WatchBuilder watchBuilder) throws ApiException {
88-
return watchBuilder.withLabelSelector(LabelConstants.CREATEDBYOPERATOR_LABEL)
89-
.createConfigMapWatch(ns);
90-
}
91-
92-
@Override
93-
public void eventCallback(Watch.Response<V1ConfigMap> item) {
94-
processEventCallback(item);
95-
}
96-
97-
@Override
98-
public boolean isStopping() {
99-
return isStopping.get();
100-
}
101-
};
102-
}
103-
104-
public void processEventCallback(Watch.Response<V1ConfigMap> item) {
105-
destination.eventCallback(item);
106-
}
10738
}

src/main/java/oracle/kubernetes/operator/DomainWatcher.java

Lines changed: 11 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,101 +4,34 @@
44
package oracle.kubernetes.operator;
55

66
import io.kubernetes.client.ApiException;
7-
import io.kubernetes.client.util.Watch;
87
import oracle.kubernetes.operator.builders.WatchBuilder;
98
import oracle.kubernetes.operator.builders.WatchI;
9+
import oracle.kubernetes.operator.watcher.WatchListener;
1010
import oracle.kubernetes.weblogic.domain.v1.Domain;
11-
import oracle.kubernetes.operator.helpers.ClientHelper;
12-
import oracle.kubernetes.operator.helpers.ClientHolder;
13-
import oracle.kubernetes.operator.watcher.Watching;
14-
import oracle.kubernetes.operator.watcher.WatchingEventDestination;
1511

1612
import java.util.concurrent.atomic.AtomicBoolean;
1713

1814
/**
1915
* This class handles Domain watching. It receives domain events and sends
2016
* them into the operator for processing.
2117
*/
22-
public class DomainWatcher implements Runnable, oracle.kubernetes.operator.watcher.ThreadedWatcher {
18+
public class DomainWatcher extends Watcher<Domain> {
2319
private final String ns;
24-
private final String initialResourceVersion;
25-
private final WatchingEventDestination<Domain> destination;
26-
private final AtomicBoolean isStopping;
27-
private Thread thread;
28-
29-
public static DomainWatcher create(String ns, String initialResourceVersion, WatchingEventDestination<Domain> destination, AtomicBoolean isStopping) {
30-
DomainWatcher dlw = new DomainWatcher(ns, initialResourceVersion, destination, isStopping);
31-
Thread thread = new Thread(dlw);
32-
thread.setName("Thread-DomainWatcher-" + ns);
33-
thread.setDaemon(true);
34-
thread.start();
35-
dlw.thread = thread;
36-
return dlw;
37-
}
3820

39-
private DomainWatcher(String ns, String initialResourceVersion, WatchingEventDestination<Domain> destination, AtomicBoolean isStopping) {
40-
this.ns = ns;
41-
this.initialResourceVersion = initialResourceVersion;
42-
this.destination = destination;
43-
this.isStopping = isStopping;
21+
public static DomainWatcher create(String ns, String initialResourceVersion, WatchListener<Domain> listener, AtomicBoolean isStopping) {
22+
DomainWatcher watcher = new DomainWatcher(ns, initialResourceVersion, listener, isStopping);
23+
watcher.start("Thread-DomainWatcher-" + ns);
24+
return watcher;
4425
}
4526

46-
@Override
47-
public Thread getThread() {
48-
return thread;
27+
private DomainWatcher(String ns, String initialResourceVersion, WatchListener<Domain> listener, AtomicBoolean isStopping) {
28+
super(initialResourceVersion, isStopping, listener);
29+
this.ns = ns;
4930
}
5031

51-
/**
52-
* Polling loop. Get the next Domain object event and process it.
53-
*/
5432
@Override
55-
public void run() {
56-
ClientHelper helper = ClientHelper.getInstance();
57-
ClientHolder client = helper.take();
58-
try {
59-
Watching<Domain> w = createWatching(client);
60-
Watcher<Domain> watcher = new Watcher<>(w, initialResourceVersion);
61-
62-
// invoke watch on current Thread. Won't return until watch stops
63-
watcher.doWatch();
64-
65-
} finally {
66-
helper.recycle(client);
67-
}
33+
public WatchI<Domain> initiateWatch(WatchBuilder watchBuilder) throws ApiException {
34+
return watchBuilder.createDomainWatch(ns);
6835
}
69-
70-
protected Watching<Domain> createWatching(ClientHolder client) {
71-
return new Watching<Domain>() {
72-
73-
/**
74-
* Watcher callback to issue the list Domain changes. It is driven by the
75-
* Watcher wrapper to issue repeated watch requests.
76-
* @param resourceVersion resource version to omit older events
77-
* @return Watch object or null if the operation should end
78-
* @throws ApiException if there is an API error.
79-
*/
80-
@Override
81-
public WatchI<Domain> initiateWatch(String resourceVersion) throws ApiException {
82-
return initiateWatch(new WatchBuilder(client).withResourceVersion(resourceVersion));
83-
}
8436

85-
public WatchI<Domain> initiateWatch(WatchBuilder watchBuilder) throws ApiException {
86-
return watchBuilder.createDomainWatch(ns);
87-
}
88-
89-
@Override
90-
public void eventCallback(Watch.Response<Domain> item) {
91-
processEventCallback(item);
92-
}
93-
94-
@Override
95-
public boolean isStopping() {
96-
return isStopping.get();
97-
}
98-
};
99-
}
100-
101-
public void processEventCallback(Watch.Response<Domain> item) {
102-
destination.eventCallback(item);
103-
}
10437
}

src/main/java/oracle/kubernetes/operator/IngressWatcher.java

Lines changed: 14 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@
66
import io.kubernetes.client.ApiException;
77
import io.kubernetes.client.models.V1ObjectMeta;
88
import io.kubernetes.client.models.V1beta1Ingress;
9-
import io.kubernetes.client.util.Watch;
10-
import oracle.kubernetes.operator.builders.WatchI;
11-
import oracle.kubernetes.operator.helpers.ClientHelper;
12-
import oracle.kubernetes.operator.helpers.ClientHolder;
139
import oracle.kubernetes.operator.builders.WatchBuilder;
14-
import oracle.kubernetes.operator.watcher.ThreadedWatcher;
15-
import oracle.kubernetes.operator.watcher.Watching;
16-
import oracle.kubernetes.operator.watcher.WatchingEventDestination;
10+
import oracle.kubernetes.operator.builders.WatchI;
11+
import oracle.kubernetes.operator.watcher.WatchListener;
1712

1813
import java.util.Map;
1914
import java.util.concurrent.atomic.AtomicBoolean;
@@ -22,86 +17,27 @@
2217
* This class handles Ingress watching. It receives Ingress change events and sends
2318
* them into the operator for processing.
2419
*/
25-
public class IngressWatcher implements Runnable, ThreadedWatcher {
20+
public class IngressWatcher extends Watcher<V1beta1Ingress> {
2621
private final String ns;
27-
private final String initialResourceVersion;
28-
private final WatchingEventDestination<V1beta1Ingress> destination;
29-
private final AtomicBoolean isStopping;
30-
private Thread thread;
3122

32-
public static IngressWatcher create(String ns, String initialResourceVersion, WatchingEventDestination<V1beta1Ingress> destination, AtomicBoolean isStopping) {
33-
IngressWatcher dlw = new IngressWatcher(ns, initialResourceVersion, destination, isStopping);
34-
Thread thread = new Thread(dlw);
35-
thread.setName("Thread-IngressWatcher-" + ns);
36-
thread.setDaemon(true);
37-
thread.start();
38-
dlw.thread = thread;
39-
return dlw;
23+
public static IngressWatcher create(String ns, String initialResourceVersion, WatchListener<V1beta1Ingress> listener, AtomicBoolean isStopping) {
24+
IngressWatcher watcher = new IngressWatcher(ns, initialResourceVersion, listener, isStopping);
25+
watcher.start("Thread-IngressWatcher-" + ns);
26+
return watcher;
4027
}
4128

42-
private IngressWatcher(String ns, String initialResourceVersion, WatchingEventDestination<V1beta1Ingress> destination, AtomicBoolean isStopping) {
29+
private IngressWatcher(String ns, String initialResourceVersion, WatchListener<V1beta1Ingress> listener, AtomicBoolean isStopping) {
30+
super(initialResourceVersion, isStopping, listener);
4331
this.ns = ns;
44-
this.initialResourceVersion = initialResourceVersion;
45-
this.destination = destination;
46-
this.isStopping = isStopping;
47-
}
48-
49-
public Thread getThread() {
50-
return thread;
5132
}
5233

53-
/**
54-
* Polling loop. Get the next Ingress object event and process it.
55-
*/
5634
@Override
57-
public void run() {
58-
ClientHelper helper = ClientHelper.getInstance();
59-
ClientHolder client = helper.take();
60-
try {
61-
Watching<V1beta1Ingress> w = createWatching(client);
62-
Watcher<V1beta1Ingress> watcher = new Watcher<>(w, initialResourceVersion);
63-
64-
// invoke watch on current Thread. Won't return until watch stops
65-
watcher.doWatch();
66-
67-
} finally {
68-
helper.recycle(client);
69-
}
35+
public WatchI<V1beta1Ingress> initiateWatch(WatchBuilder watchBuilder) throws ApiException {
36+
return watchBuilder
37+
.withLabelSelectors(LabelConstants.DOMAINUID_LABEL, LabelConstants.CREATEDBYOPERATOR_LABEL)
38+
.createIngressWatch(ns);
7039
}
71-
72-
private Watching<V1beta1Ingress> createWatching(ClientHolder client) {
73-
return new Watching<V1beta1Ingress>() {
74-
75-
/**
76-
* Watcher callback to issue the list Ingress changes. It is driven by the
77-
* Watcher wrapper to issue repeated watch requests.
78-
* @param resourceVersion resource version to omit older events
79-
* @return Watch object or null if the operation should end
80-
* @throws ApiException if there is an API error.
81-
*/
82-
@Override
83-
public WatchI<V1beta1Ingress> initiateWatch(String resourceVersion) throws ApiException {
84-
return initiateWatch(new WatchBuilder(client).withResourceVersion(resourceVersion));
85-
}
8640

87-
public WatchI<V1beta1Ingress> initiateWatch(WatchBuilder watchBuilder) throws ApiException {
88-
return watchBuilder
89-
.withLabelSelectors(LabelConstants.DOMAINUID_LABEL, LabelConstants.CREATEDBYOPERATOR_LABEL)
90-
.createIngressWatch(ns);
91-
}
92-
93-
@Override
94-
public void eventCallback(Watch.Response<V1beta1Ingress> item) {
95-
processEventCallback(item);
96-
}
97-
98-
@Override
99-
public boolean isStopping() {
100-
return isStopping.get();
101-
}
102-
};
103-
}
104-
10541
static String getIngressDomainUID(V1beta1Ingress ingress) {
10642
V1ObjectMeta meta = ingress.getMetadata();
10743
Map<String, String> labels = meta.getLabels();
@@ -119,8 +55,5 @@ static String getIngressClusterName(V1beta1Ingress ingress) {
11955
}
12056
return null;
12157
}
122-
123-
public void processEventCallback(Watch.Response<V1beta1Ingress> item) {
124-
destination.eventCallback(item);
125-
}
58+
12659
}

0 commit comments

Comments
 (0)