1
- // Copyright (c) 2018, 2021 , Oracle and/or its affiliates.
1
+ // Copyright (c) 2018, 2022 , Oracle and/or its affiliates.
2
2
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3
3
4
4
package oracle .kubernetes .operator .helpers ;
17
17
import java .util .List ;
18
18
import java .util .Map ;
19
19
import java .util .Objects ;
20
+ import java .util .Optional ;
20
21
import java .util .function .Function ;
21
22
import java .util .stream .Collectors ;
22
23
35
36
import okhttp3 .internal .http2 .StreamResetException ;
36
37
import oracle .kubernetes .operator .KubernetesConstants ;
37
38
import oracle .kubernetes .operator .LabelConstants ;
39
+ import oracle .kubernetes .operator .MainDelegate ;
38
40
import oracle .kubernetes .operator .calls .CallResponse ;
39
41
import oracle .kubernetes .operator .logging .LoggingFacade ;
40
42
import oracle .kubernetes .operator .logging .LoggingFactory ;
@@ -74,7 +76,7 @@ public static void main(String[] args) throws URISyntaxException {
74
76
}
75
77
76
78
static void writeCrdFiles (String crdFileName ) throws URISyntaxException {
77
- CrdContext context = new CrdContext (null , null , null );
79
+ CrdContext context = new CrdContext (null , null );
78
80
79
81
final URI outputFile = asFileURI (crdFileName );
80
82
@@ -120,8 +122,8 @@ private static void dumpYaml(Writer writer, Object model) {
120
122
// Map = gson.fromJson(Map.class)
121
123
// yaml dump ? // ordering and format likely to change massively
122
124
123
- public static Step createDomainCrdStep (KubernetesVersion version , SemanticVersion productVersion ) {
124
- return new CrdStep (version , productVersion );
125
+ public static Step createDomainCrdStep (MainDelegate mainDelegate ) {
126
+ return new CrdStep (mainDelegate );
125
127
}
126
128
127
129
private static List <ResourceVersion > getVersions (V1CustomResourceDefinition crd ) {
@@ -144,8 +146,8 @@ boolean isOutdatedCrd(
144
146
static class CrdStep extends Step {
145
147
final CrdContext context ;
146
148
147
- CrdStep (KubernetesVersion version , SemanticVersion productVersion ) {
148
- context = new CrdContext (version , productVersion , this );
149
+ CrdStep (MainDelegate mainDelegate ) {
150
+ context = new CrdContext (mainDelegate , this );
149
151
}
150
152
151
153
@ Override
@@ -158,14 +160,12 @@ public NextAction apply(Packet packet) {
158
160
static class CrdContext {
159
161
private final Step conflictStep ;
160
162
private final V1CustomResourceDefinition model ;
161
- private final KubernetesVersion version ;
162
- private final SemanticVersion productVersion ;
163
+ private final MainDelegate mainDelegate ;
163
164
164
- CrdContext (KubernetesVersion version , SemanticVersion productVersion , Step conflictStep ) {
165
- this .version = version ;
166
- this .productVersion = productVersion ;
165
+ CrdContext (MainDelegate mainDelegate , Step conflictStep ) {
166
+ this .mainDelegate = mainDelegate ;
167
167
this .conflictStep = conflictStep ;
168
- this .model = createModel (productVersion );
168
+ this .model = createModel (Optional . ofNullable ( mainDelegate ). map ( MainDelegate :: getProductVersion ). orElse ( null ) );
169
169
}
170
170
171
171
static V1CustomResourceDefinition createModel (SemanticVersion productVersion ) {
@@ -294,7 +294,7 @@ ResponseStep<V1CustomResourceDefinition> createCreateResponseStep(Step next) {
294
294
}
295
295
296
296
private boolean isOutdatedCrd (V1CustomResourceDefinition existingCrd ) {
297
- return COMPARATOR .isOutdatedCrd (productVersion , existingCrd , this .model );
297
+ return COMPARATOR .isOutdatedCrd (mainDelegate . getProductVersion () , existingCrd , this .model );
298
298
}
299
299
300
300
private boolean existingCrdContainsVersion (V1CustomResourceDefinition existingCrd ) {
@@ -349,6 +349,8 @@ class ReadResponseStep extends DefaultResponseStep<V1CustomResourceDefinition> {
349
349
public NextAction onSuccess (
350
350
Packet packet , CallResponse <V1CustomResourceDefinition > callResponse ) {
351
351
V1CustomResourceDefinition existingCrd = callResponse .getResult ();
352
+ mainDelegate .getCrdReference ().set (existingCrd );
353
+
352
354
if (existingCrd == null ) {
353
355
return doNext (createCrd (getNext ()), packet );
354
356
} else if (isOutdatedCrd (existingCrd )) {
@@ -362,6 +364,7 @@ public NextAction onSuccess(
362
364
363
365
@ Override
364
366
protected NextAction onFailureNoRetry (Packet packet , CallResponse <V1CustomResourceDefinition > callResponse ) {
367
+ mainDelegate .getCrdReference ().set (null );
365
368
return isNotAuthorizedOrForbidden (callResponse )
366
369
? doNext (packet ) : super .onFailureNoRetry (packet , callResponse );
367
370
}
@@ -381,7 +384,10 @@ public NextAction onFailure(
381
384
@ Override
382
385
public NextAction onSuccess (
383
386
Packet packet , CallResponse <V1CustomResourceDefinition > callResponse ) {
384
- LOGGER .info (MessageKeys .CREATING_CRD , callResponse .getResult ().getMetadata ().getName ());
387
+ V1CustomResourceDefinition existingCrd = callResponse .getResult ();
388
+ mainDelegate .getCrdReference ().set (existingCrd );
389
+
390
+ LOGGER .info (MessageKeys .CREATING_CRD , existingCrd .getMetadata ().getName ());
385
391
return doNext (packet );
386
392
}
387
393
@@ -407,7 +413,9 @@ public NextAction onFailure(
407
413
@ Override
408
414
public NextAction onSuccess (
409
415
Packet packet , CallResponse <V1CustomResourceDefinition > callResponse ) {
410
- LOGGER .info (MessageKeys .CREATING_CRD , callResponse .getResult ().getMetadata ().getName ());
416
+ V1CustomResourceDefinition existingCrd = callResponse .getResult ();
417
+ mainDelegate .getCrdReference ().set (existingCrd );
418
+ LOGGER .info (MessageKeys .CREATING_CRD , existingCrd .getMetadata ().getName ());
411
419
return doNext (packet );
412
420
}
413
421
0 commit comments