3
3
import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getUID ;
4
4
import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getVersion ;
5
5
6
+ import io .fabric8 .kubernetes .api .model .KubernetesResourceList ;
6
7
import io .fabric8 .kubernetes .client .CustomResource ;
7
8
import io .fabric8 .kubernetes .client .Watch ;
8
9
import io .fabric8 .kubernetes .client .Watcher ;
9
10
import io .fabric8 .kubernetes .client .WatcherException ;
10
11
import io .fabric8 .kubernetes .client .dsl .MixedOperation ;
12
+ import io .fabric8 .kubernetes .client .dsl .Resource ;
11
13
import io .fabric8 .kubernetes .client .dsl .internal .CustomResourceOperationsImpl ;
12
14
import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
13
15
import io .javaoperatorsdk .operator .processing .KubernetesResourceUtils ;
21
23
import org .slf4j .LoggerFactory ;
22
24
23
25
/** This is a special case since is not bound to a single custom resource */
24
- public class CustomResourceEventSource extends AbstractEventSource
25
- implements Watcher <CustomResource > {
26
+ public class CustomResourceEventSource < T extends CustomResource <?, ?>> extends AbstractEventSource
27
+ implements Watcher <T > {
26
28
27
29
private static final Logger log = LoggerFactory .getLogger (CustomResourceEventSource .class );
28
30
29
- private final MixedOperation client ;
31
+ private final CustomResourceOperationsImpl < T , KubernetesResourceList < T >> client ;
30
32
private final Set <String > targetNamespaces ;
31
33
private final boolean generationAware ;
32
34
private final String resourceFinalizer ;
@@ -35,12 +37,23 @@ public class CustomResourceEventSource extends AbstractEventSource
35
37
private final String resClass ;
36
38
37
39
public CustomResourceEventSource (
38
- MixedOperation client ,
40
+ MixedOperation <T , KubernetesResourceList <T >, Resource <T >> client ,
41
+ ControllerConfiguration <T > configuration ) {
42
+ this (
43
+ client ,
44
+ configuration .getEffectiveNamespaces (),
45
+ configuration .isGenerationAware (),
46
+ configuration .getFinalizer (),
47
+ configuration .getCustomResourceClass ());
48
+ }
49
+
50
+ CustomResourceEventSource (
51
+ MixedOperation <T , KubernetesResourceList <T >, Resource <T >> client ,
39
52
Set <String > targetNamespaces ,
40
53
boolean generationAware ,
41
54
String resourceFinalizer ,
42
- Class <? > resClass ) {
43
- this .client = client ;
55
+ Class <T > resClass ) {
56
+ this .client = ( CustomResourceOperationsImpl < T , KubernetesResourceList < T >>) client ;
44
57
this .targetNamespaces = targetNamespaces ;
45
58
this .generationAware = generationAware ;
46
59
this .resourceFinalizer = resourceFinalizer ;
@@ -50,15 +63,14 @@ public CustomResourceEventSource(
50
63
51
64
@ Override
52
65
public void start () {
53
- CustomResourceOperationsImpl crClient = (CustomResourceOperationsImpl ) client ;
54
66
if (ControllerConfiguration .allNamespacesWatched (targetNamespaces )) {
55
- var w = crClient .inAnyNamespace ().watch (this );
67
+ var w = client .inAnyNamespace ().watch (this );
56
68
watches .add (w );
57
69
log .debug ("Registered controller {} -> {} for any namespace" , resClass , w );
58
70
} else {
59
71
targetNamespaces .forEach (
60
72
ns -> {
61
- var w = crClient .inNamespace (ns ).watch (this );
73
+ var w = client .inNamespace (ns ).watch (this );
62
74
watches .add (w );
63
75
log .debug ("Registered controller {} -> {} for namespace: {}" , resClass , w , ns );
64
76
});
@@ -78,7 +90,7 @@ public void close() {
78
90
}
79
91
80
92
@ Override
81
- public void eventReceived (Watcher .Action action , CustomResource customResource ) {
93
+ public void eventReceived (Watcher .Action action , T customResource ) {
82
94
log .debug (
83
95
"Event received for action: {}, resource: {}" ,
84
96
action .name (),
@@ -104,14 +116,14 @@ public void eventReceived(Watcher.Action action, CustomResource customResource)
104
116
}
105
117
}
106
118
107
- private void markLastGenerationProcessed (CustomResource resource ) {
119
+ private void markLastGenerationProcessed (T resource ) {
108
120
if (generationAware && resource .hasFinalizer (resourceFinalizer )) {
109
121
lastGenerationProcessedSuccessfully .put (
110
122
KubernetesResourceUtils .getUID (resource ), resource .getMetadata ().getGeneration ());
111
123
}
112
124
}
113
125
114
- private boolean skipBecauseOfGeneration (CustomResource customResource ) {
126
+ private boolean skipBecauseOfGeneration (T customResource ) {
115
127
if (!generationAware ) {
116
128
return false ;
117
129
}
@@ -122,7 +134,7 @@ private boolean skipBecauseOfGeneration(CustomResource customResource) {
122
134
return !hasGenerationAlreadyBeenProcessed (customResource );
123
135
}
124
136
125
- private boolean hasGenerationAlreadyBeenProcessed (CustomResource resource ) {
137
+ private boolean hasGenerationAlreadyBeenProcessed (T resource ) {
126
138
Long lastGeneration = lastGenerationProcessedSuccessfully .get (resource .getMetadata ().getUid ());
127
139
if (lastGeneration == null ) {
128
140
return true ;
0 commit comments