1
1
package io .javaoperatorsdk .operator .processing .dependent .workflow ;
2
2
3
+ import java .util .HashMap ;
3
4
import java .util .HashSet ;
4
5
import java .util .List ;
5
6
import java .util .Set ;
6
- import java .util .function .Function ;
7
7
import java .util .stream .Collectors ;
8
8
9
9
import io .fabric8 .kubernetes .api .model .HasMetadata ;
10
10
import io .fabric8 .kubernetes .client .KubernetesClient ;
11
+ import io .javaoperatorsdk .operator .api .config .ConfigurationServiceProvider ;
11
12
import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
12
13
import io .javaoperatorsdk .operator .api .config .ExecutorServiceManager ;
14
+ import io .javaoperatorsdk .operator .api .config .dependent .DependentResourceSpec ;
15
+ import io .javaoperatorsdk .operator .api .reconciler .dependent .DependentResource ;
16
+ import io .javaoperatorsdk .operator .api .reconciler .dependent .EventSourceReferencer ;
17
+ import io .javaoperatorsdk .operator .api .reconciler .dependent .managed .KubernetesClientAware ;
13
18
14
19
import static io .javaoperatorsdk .operator .processing .dependent .workflow .Workflow .THROW_EXCEPTION_AUTOMATICALLY_DEFAULT ;
15
20
@@ -18,24 +23,23 @@ public class DefaultManagedWorkflow<P extends HasMetadata> implements ManagedWor
18
23
19
24
private final Set <String > topLevelResources ;
20
25
private final Set <String > bottomLevelResources ;
21
- private final Set < SpecDependentResourceNode > nodes ;
26
+ private final List < DependentResourceSpec <?, ?>> specs ;
22
27
private final boolean hasCleaner ;
23
28
24
- @ SuppressWarnings ("unchecked" )
25
- DefaultManagedWorkflow (Set <SpecDependentResourceNode > dependentResourceSpecs ,
29
+ DefaultManagedWorkflow (List <DependentResourceSpec <?, ?>> dependentResourceSpecs ,
26
30
boolean hasCleaner ) {
27
31
this .hasCleaner = hasCleaner ;
28
32
topLevelResources = new HashSet <>(dependentResourceSpecs .size ());
29
- bottomLevelResources =
30
- dependentResourceSpecs . stream (). map (SpecDependentResourceNode ::getName ). collect (
31
- Collectors .toSet ());
32
- nodes = dependentResourceSpecs ;
33
- dependentResourceSpecs .forEach (drn -> {
33
+ bottomLevelResources = dependentResourceSpecs . stream ()
34
+ . map (DependentResourceSpec ::getName )
35
+ . collect ( Collectors .toSet ());
36
+ specs = dependentResourceSpecs ;
37
+ dependentResourceSpecs .forEach (spec -> {
34
38
// add cycle detection?
35
- if (drn .getDependsOn ().isEmpty ()) {
36
- topLevelResources .add (drn .getName ());
39
+ if (spec .getDependsOn ().isEmpty ()) {
40
+ topLevelResources .add (spec .getName ());
37
41
} else {
38
- for (String dependsOn : ( List < String >) drn .getDependsOn ()) {
42
+ for (String dependsOn : spec .getDependsOn ()) {
39
43
bottomLevelResources .remove (dependsOn );
40
44
}
41
45
}
@@ -50,8 +54,8 @@ Set<String> getBottomLevelResources() {
50
54
return bottomLevelResources ;
51
55
}
52
56
53
- Set < SpecDependentResourceNode > nodes () {
54
- return nodes ;
57
+ List < String > nodeNames () {
58
+ return specs . stream (). map ( DependentResourceSpec :: getName ). collect ( Collectors . toList ()) ;
55
59
}
56
60
57
61
@ Override
@@ -61,26 +65,54 @@ public boolean hasCleaner() {
61
65
62
66
@ Override
63
67
public boolean isEmpty () {
64
- return nodes .isEmpty ();
68
+ return specs .isEmpty ();
65
69
}
66
70
67
71
@ Override
72
+ @ SuppressWarnings ("unchecked" )
68
73
public Workflow <P > resolve (KubernetesClient client ,
69
74
ControllerConfiguration <P > configuration ) {
70
- final var map = nodes .stream ()
71
- .map (spec -> createDRN (client , configuration , spec ))
72
- .collect (Collectors .toMap (DependentResourceNode ::getName , Function .identity ()));
73
- final var bottom = bottomLevelResources .stream ().map (map ::get ).collect (Collectors .toSet ());
74
- final var top = topLevelResources .stream ().map (map ::get ).collect (Collectors .toSet ());
75
- return new Workflow <>(map , bottom , top ,
75
+ final var alreadyResolved = new HashMap <String , DependentResourceNode >(specs .size ());
76
+ for (DependentResourceSpec spec : specs ) {
77
+ final var node = new DependentResourceNode (resolve (spec , client , configuration ));
78
+ alreadyResolved .put (node .getName (), node );
79
+ spec .getDependsOn ()
80
+ .forEach (depend -> node .addDependsOnRelation (alreadyResolved .get ((String ) depend )));
81
+ }
82
+
83
+ final var bottom =
84
+ bottomLevelResources .stream ().map (alreadyResolved ::get ).collect (Collectors .toSet ());
85
+ final var top =
86
+ topLevelResources .stream ().map (alreadyResolved ::get ).collect (Collectors .toSet ());
87
+ return new Workflow <>(alreadyResolved , bottom , top ,
76
88
ExecutorServiceManager .instance ().workflowExecutorService (),
77
89
THROW_EXCEPTION_AUTOMATICALLY_DEFAULT , hasCleaner );
78
90
}
79
91
80
- @ SuppressWarnings ("unchecked" )
81
- private DependentResourceNode createDRN (
82
- KubernetesClient client , ControllerConfiguration <P > configuration ,
83
- SpecDependentResourceNode spec ) {
84
- return new DependentResourceNode (spec .resolve (client , configuration ));
92
+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
93
+ private <R > DependentResource <R , P > resolve (DependentResourceSpec <R , P > spec ,
94
+ KubernetesClient client ,
95
+ ControllerConfiguration <P > configuration ) {
96
+ final DependentResource <R , P > dependentResource =
97
+ ConfigurationServiceProvider .instance ().dependentResourceFactory ()
98
+ .createFrom (spec , configuration );
99
+
100
+ if (dependentResource instanceof KubernetesClientAware ) {
101
+ ((KubernetesClientAware ) dependentResource ).setKubernetesClient (client );
102
+ }
103
+
104
+ spec .getUseEventSourceWithName ()
105
+ .ifPresent (esName -> {
106
+ if (dependentResource instanceof EventSourceReferencer ) {
107
+ ((EventSourceReferencer ) dependentResource ).useEventSourceWithName (esName );
108
+ } else {
109
+ throw new IllegalStateException (
110
+ "DependentResource " + spec + " wants to use EventSource named " + esName
111
+ + " but doesn't implement support for this feature by implementing "
112
+ + EventSourceReferencer .class .getSimpleName ());
113
+ }
114
+ });
115
+
116
+ return dependentResource ;
85
117
}
86
118
}
0 commit comments