3
3
import java .util .Map ;
4
4
import java .util .concurrent .ConcurrentHashMap ;
5
5
6
+ import io .javaoperatorsdk .operator .processing .expiration .Expiration ;
7
+ import io .javaoperatorsdk .operator .processing .expiration .RetryExpiration ;
6
8
import org .slf4j .Logger ;
7
9
import org .slf4j .LoggerFactory ;
8
10
12
14
import io .javaoperatorsdk .operator .api .reconciler .Context ;
13
15
import io .javaoperatorsdk .operator .api .reconciler .dependent .DependentResource ;
14
16
import io .javaoperatorsdk .operator .processing .GroupVersionKind ;
15
- import io .javaoperatorsdk .operator .processing .event .source .informer .InformerEventSource ;
16
- import io .javaoperatorsdk .operator .processing .expiration .Expiration ;
17
- import io .javaoperatorsdk .operator .processing .expiration .RetryExpiration ;
17
+ import io .javaoperatorsdk .operator .processing .expiration .ExpirationExecution ;
18
18
import io .javaoperatorsdk .operator .processing .retry .GenericRetry ;
19
19
import io .javaoperatorsdk .operator .processing .retry .Retry ;
20
20
21
21
public class CRDPresentActivationCondition implements Condition <HasMetadata , HasMetadata > {
22
22
23
- public static Retry DEFAULT_EXPIRATION_RETRY = new GenericRetry ().setInitialInterval (2000 )
24
- .setMaxInterval (1000 * 60 * 60 )
25
- .setIntervalMultiplier (2 );
26
-
27
23
private static final Logger log = LoggerFactory .getLogger (CRDPresentActivationCondition .class );
28
24
25
+ /**
26
+ *
27
+ * */
28
+ public static Retry DEFAULT_EXPIRATION_RETRY = new GenericRetry ().setInitialInterval (2000 )
29
+ .setIntervalMultiplier (2 )
30
+ .setMaxAttempts (10 );
31
+
29
32
private final Map <GroupVersionKind , CRDCheckState > crdPresenceCache = new ConcurrentHashMap <>();
30
33
31
- private final Retry expirationRetry ;
34
+ private final Expiration expiration ;
32
35
33
36
public CRDPresentActivationCondition () {
34
- this (DEFAULT_EXPIRATION_RETRY );
37
+ this (new RetryExpiration ( DEFAULT_EXPIRATION_RETRY ) );
35
38
}
36
39
37
- public CRDPresentActivationCondition (Retry expirationRetry ) {
38
- this .expirationRetry = expirationRetry ;
40
+ public CRDPresentActivationCondition (Expiration expiration ) {
41
+ this .expiration = expiration ;
39
42
}
40
43
41
44
@ Override
@@ -47,60 +50,43 @@ public boolean isMet(DependentResource<HasMetadata, HasMetadata> dependentResour
47
50
var kind = HasMetadata .getKind (resourceClass );
48
51
var gvk = new GroupVersionKind (apiVersion , kind );
49
52
50
- InformerEventSource <CustomResourceDefinition , HasMetadata > crdInformer = null ;
51
- try {
52
- crdInformer = (InformerEventSource <CustomResourceDefinition , HasMetadata >) context
53
- .eventSourceRetriever ().getResourceEventSourceFor (CustomResourceDefinition .class );
54
- } catch (IllegalArgumentException e ) {
55
- log .trace ("Error when finding event source for CustomResourceDefinitions" , e );
56
- }
57
-
58
- if (crdInformer != null ) {
59
- return crdInformer
60
- .list (crd -> crd .getSpec ().getNames ().getKind ().equals (kind )
61
- && crd .getSpec ().getGroup ().equals (gvk .getGroup ()))
62
- .findAny ().isPresent ();
63
- } else {
64
- var crdCheckState = crdPresenceCache .computeIfAbsent (gvk ,
65
- g -> new CRDCheckState (new RetryExpiration (expirationRetry .initExecution ())));
66
- // in case of parallel execution it is only refreshed once
67
- synchronized (crdCheckState ) {
68
- if (crdCheckState .getExpiration ().isExpired ()) {
69
- refreshCache (gvk , context .getClient ());
70
- }
53
+ var crdCheckState = crdPresenceCache .computeIfAbsent (gvk ,
54
+ g -> new CRDCheckState (expiration .initExecution ()));
55
+ // in case of parallel execution it is only refreshed once
56
+ synchronized (crdCheckState ) {
57
+ if (crdCheckState .getExpiration ().isExpired ()) {
58
+ refreshCache (crdCheckState ,gvk , context .getClient ());
71
59
}
72
- return crdPresenceCache .get (gvk ).getCrdPresent ();
73
60
}
61
+ return crdPresenceCache .get (gvk ).getCrdPresent ();
74
62
}
75
63
76
- private void refreshCache (GroupVersionKind gvk , KubernetesClient client ) {
77
- var state = crdPresenceCache .computeIfAbsent (gvk ,
78
- g -> new CRDCheckState (new RetryExpiration (expirationRetry .initExecution ())));
64
+ private void refreshCache (CRDCheckState crdCheckState , GroupVersionKind gvk , KubernetesClient client ) {
79
65
80
66
boolean found = client .resources (CustomResourceDefinition .class ).list ().getItems ()
81
67
.stream ().anyMatch (crd -> crd .getSpec ().getNames ().getKind ().equals (gvk .getKind ())
82
68
&& crd .getSpec ().getGroup ().equals (gvk .getGroup ()));
83
69
84
- state .setCrdPresent (found );
85
- state .getExpiration ().refreshed ();
70
+ crdCheckState .setCrdPresent (found );
71
+ crdCheckState .getExpiration ().refreshed ();
86
72
}
87
73
88
74
static class CRDCheckState {
89
75
90
- public CRDCheckState (Expiration expiration ) {
91
- this .expiration = expiration ;
76
+ public CRDCheckState (ExpirationExecution expirationExecution ) {
77
+ this .expirationExecution = expirationExecution ;
92
78
}
93
79
94
- private Expiration expiration ;
80
+ private ExpirationExecution expirationExecution ;
95
81
96
82
private Boolean crdPresent ;
97
83
98
- public Expiration getExpiration () {
99
- return expiration ;
84
+ public ExpirationExecution getExpiration () {
85
+ return expirationExecution ;
100
86
}
101
87
102
- public void setExpiration (Expiration expiration ) {
103
- this .expiration = expiration ;
88
+ public void setExpiration (ExpirationExecution expirationExecution ) {
89
+ this .expirationExecution = expirationExecution ;
104
90
}
105
91
106
92
public Boolean getCrdPresent () {
0 commit comments