3
3
import static io .javaoperatorsdk .operator .EventListUtils .containsCustomResourceDeletedEvent ;
4
4
import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getUID ;
5
5
import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getVersion ;
6
- import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .markedForDeletion ;
7
6
8
7
import io .fabric8 .kubernetes .api .model .KubernetesResourceList ;
9
8
import io .fabric8 .kubernetes .client .CustomResource ;
10
9
import io .fabric8 .kubernetes .client .dsl .MixedOperation ;
11
10
import io .fabric8 .kubernetes .client .dsl .Resource ;
12
- import io .javaoperatorsdk .operator .ControllerUtils ;
13
11
import io .javaoperatorsdk .operator .api .Context ;
14
12
import io .javaoperatorsdk .operator .api .DefaultContext ;
15
13
import io .javaoperatorsdk .operator .api .DeleteControl ;
16
14
import io .javaoperatorsdk .operator .api .ResourceController ;
17
15
import io .javaoperatorsdk .operator .api .UpdateControl ;
18
16
import io .javaoperatorsdk .operator .processing .event .EventList ;
19
17
import io .javaoperatorsdk .operator .processing .event .EventSourceManager ;
20
- import java .util .ArrayList ;
21
18
import org .slf4j .Logger ;
22
19
import org .slf4j .LoggerFactory ;
23
20
@@ -65,8 +62,7 @@ private PostExecutionControl handleDispatch(ExecutionScope executionScope) {
65
62
getVersion (resource ));
66
63
return PostExecutionControl .defaultDispatch ();
67
64
}
68
- if ((markedForDeletion (resource )
69
- && !ControllerUtils .hasGivenFinalizer (resource , resourceFinalizer ))) {
65
+ if ((resource .isMarkedForDeletion () && !resource .hasFinalizer (resourceFinalizer ))) {
70
66
log .debug (
71
67
"Skipping event dispatching since its marked for deletion but has no finalizer: {}" ,
72
68
executionScope );
@@ -77,7 +73,7 @@ private PostExecutionControl handleDispatch(ExecutionScope executionScope) {
77
73
eventSourceManager ,
78
74
new EventList (executionScope .getEvents ()),
79
75
executionScope .getRetryInfo ());
80
- if (markedForDeletion ( resource )) {
76
+ if (resource . isMarkedForDeletion ( )) {
81
77
return handleDelete (resource , context );
82
78
} else {
83
79
return handleCreateOrUpdate (executionScope , resource , context );
@@ -86,8 +82,7 @@ private PostExecutionControl handleDispatch(ExecutionScope executionScope) {
86
82
87
83
private PostExecutionControl handleCreateOrUpdate (
88
84
ExecutionScope executionScope , CustomResource resource , Context context ) {
89
- if (!ControllerUtils .hasGivenFinalizer (resource , resourceFinalizer )
90
- && !markedForDeletion (resource )) {
85
+ if (!resource .hasFinalizer (resourceFinalizer ) && !resource .isMarkedForDeletion ()) {
91
86
/* We always add the finalizer if missing and not marked for deletion.
92
87
We execute the controller processing only for processing the event sent as a results
93
88
of the finalizer add. This will make sure that the resources are not created before
@@ -133,7 +128,7 @@ private PostExecutionControl handleDelete(CustomResource resource, Context conte
133
128
getUID (resource ),
134
129
getVersion (resource ));
135
130
DeleteControl deleteControl = controller .deleteResource (resource , context );
136
- boolean hasFinalizer = ControllerUtils . hasGivenFinalizer ( resource , resourceFinalizer );
131
+ boolean hasFinalizer = resource . hasFinalizer ( resourceFinalizer );
137
132
if (deleteControl == DeleteControl .DEFAULT_DELETE && hasFinalizer ) {
138
133
CustomResource customResource = removeFinalizer (resource );
139
134
return PostExecutionControl .customResourceUpdated (customResource );
@@ -151,7 +146,7 @@ private PostExecutionControl handleDelete(CustomResource resource, Context conte
151
146
private void updateCustomResourceWithFinalizer (CustomResource resource ) {
152
147
log .debug (
153
148
"Adding finalizer for resource: {} version: {}" , getUID (resource ), getVersion (resource ));
154
- addFinalizerIfNotPresent ( resource );
149
+ resource . addFinalizer ( resourceFinalizer );
155
150
replace (resource );
156
151
}
157
152
@@ -166,7 +161,7 @@ private CustomResource removeFinalizer(CustomResource resource) {
166
161
"Removing finalizer on resource: {} with version: {}" ,
167
162
getUID (resource ),
168
163
getVersion (resource ));
169
- resource .getMetadata (). getFinalizers (). remove (resourceFinalizer );
164
+ resource .removeFinalizer (resourceFinalizer );
170
165
return customResourceFacade .replaceWithLock (resource );
171
166
}
172
167
@@ -178,17 +173,6 @@ private CustomResource replace(CustomResource resource) {
178
173
return customResourceFacade .replaceWithLock (resource );
179
174
}
180
175
181
- private void addFinalizerIfNotPresent (CustomResource resource ) {
182
- if (!ControllerUtils .hasGivenFinalizer (resource , resourceFinalizer )
183
- && !markedForDeletion (resource )) {
184
- log .info ("Adding finalizer to {}" , resource .getMetadata ());
185
- if (resource .getMetadata ().getFinalizers () == null ) {
186
- resource .getMetadata ().setFinalizers (new ArrayList <>(1 ));
187
- }
188
- resource .getMetadata ().getFinalizers ().add (resourceFinalizer );
189
- }
190
- }
191
-
192
176
// created to support unit testing
193
177
public static class CustomResourceFacade <R extends CustomResource > {
194
178
0 commit comments