1- // Copyright (c) 2018, 2021 , Oracle and/or its affiliates.
1+ // Copyright (c) 2018, 2022 , Oracle and/or its affiliates.
22// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
44package oracle .kubernetes .operator .helpers ;
1717import java .util .List ;
1818import java .util .Map ;
1919import java .util .Objects ;
20+ import java .util .Optional ;
2021import java .util .function .Function ;
2122import java .util .stream .Collectors ;
2223
3536import okhttp3 .internal .http2 .StreamResetException ;
3637import oracle .kubernetes .operator .KubernetesConstants ;
3738import oracle .kubernetes .operator .LabelConstants ;
39+ import oracle .kubernetes .operator .MainDelegate ;
3840import oracle .kubernetes .operator .calls .CallResponse ;
3941import oracle .kubernetes .operator .logging .LoggingFacade ;
4042import oracle .kubernetes .operator .logging .LoggingFactory ;
@@ -74,7 +76,7 @@ public static void main(String[] args) throws URISyntaxException {
7476 }
7577
7678 static void writeCrdFiles (String crdFileName ) throws URISyntaxException {
77- CrdContext context = new CrdContext (null , null , null );
79+ CrdContext context = new CrdContext (null , null );
7880
7981 final URI outputFile = asFileURI (crdFileName );
8082
@@ -120,8 +122,8 @@ private static void dumpYaml(Writer writer, Object model) {
120122 // Map = gson.fromJson(Map.class)
121123 // yaml dump ? // ordering and format likely to change massively
122124
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 );
125127 }
126128
127129 private static List <ResourceVersion > getVersions (V1CustomResourceDefinition crd ) {
@@ -144,8 +146,8 @@ boolean isOutdatedCrd(
144146 static class CrdStep extends Step {
145147 final CrdContext context ;
146148
147- CrdStep (KubernetesVersion version , SemanticVersion productVersion ) {
148- context = new CrdContext (version , productVersion , this );
149+ CrdStep (MainDelegate mainDelegate ) {
150+ context = new CrdContext (mainDelegate , this );
149151 }
150152
151153 @ Override
@@ -158,14 +160,12 @@ public NextAction apply(Packet packet) {
158160 static class CrdContext {
159161 private final Step conflictStep ;
160162 private final V1CustomResourceDefinition model ;
161- private final KubernetesVersion version ;
162- private final SemanticVersion productVersion ;
163+ private final MainDelegate mainDelegate ;
163164
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 ;
167167 this .conflictStep = conflictStep ;
168- this .model = createModel (productVersion );
168+ this .model = createModel (Optional . ofNullable ( mainDelegate ). map ( MainDelegate :: getProductVersion ). orElse ( null ) );
169169 }
170170
171171 static V1CustomResourceDefinition createModel (SemanticVersion productVersion ) {
@@ -294,7 +294,7 @@ ResponseStep<V1CustomResourceDefinition> createCreateResponseStep(Step next) {
294294 }
295295
296296 private boolean isOutdatedCrd (V1CustomResourceDefinition existingCrd ) {
297- return COMPARATOR .isOutdatedCrd (productVersion , existingCrd , this .model );
297+ return COMPARATOR .isOutdatedCrd (mainDelegate . getProductVersion () , existingCrd , this .model );
298298 }
299299
300300 private boolean existingCrdContainsVersion (V1CustomResourceDefinition existingCrd ) {
@@ -349,6 +349,8 @@ class ReadResponseStep extends DefaultResponseStep<V1CustomResourceDefinition> {
349349 public NextAction onSuccess (
350350 Packet packet , CallResponse <V1CustomResourceDefinition > callResponse ) {
351351 V1CustomResourceDefinition existingCrd = callResponse .getResult ();
352+ mainDelegate .getCrdReference ().set (existingCrd );
353+
352354 if (existingCrd == null ) {
353355 return doNext (createCrd (getNext ()), packet );
354356 } else if (isOutdatedCrd (existingCrd )) {
@@ -362,6 +364,7 @@ public NextAction onSuccess(
362364
363365 @ Override
364366 protected NextAction onFailureNoRetry (Packet packet , CallResponse <V1CustomResourceDefinition > callResponse ) {
367+ mainDelegate .getCrdReference ().set (null );
365368 return isNotAuthorizedOrForbidden (callResponse )
366369 ? doNext (packet ) : super .onFailureNoRetry (packet , callResponse );
367370 }
@@ -381,7 +384,10 @@ public NextAction onFailure(
381384 @ Override
382385 public NextAction onSuccess (
383386 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 ());
385391 return doNext (packet );
386392 }
387393
@@ -407,7 +413,9 @@ public NextAction onFailure(
407413 @ Override
408414 public NextAction onSuccess (
409415 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 ());
411419 return doNext (packet );
412420 }
413421
0 commit comments