5
5
import org .slf4j .Logger ;
6
6
import org .slf4j .LoggerFactory ;
7
7
8
+ import io .fabric8 .kubernetes .api .model .GenericKubernetesResource ;
8
9
import io .fabric8 .kubernetes .api .model .HasMetadata ;
9
10
import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
10
11
import io .javaoperatorsdk .operator .api .config .Utils ;
11
12
import io .javaoperatorsdk .operator .api .config .dependent .ConfigurationConverter ;
12
13
import io .javaoperatorsdk .operator .api .config .dependent .DependentResourceSpec ;
14
+ import io .javaoperatorsdk .operator .api .config .informer .InformerConfiguration ;
13
15
import io .javaoperatorsdk .operator .api .reconciler .Constants ;
14
16
import io .javaoperatorsdk .operator .api .reconciler .dependent .DependentResource ;
15
17
import io .javaoperatorsdk .operator .processing .GroupVersionKind ;
@@ -43,7 +45,7 @@ public KubernetesDependentResourceConfig<R> configFrom(KubernetesDependent confi
43
45
useSSA = configAnnotation .useSSA ().asBoolean ();
44
46
}
45
47
46
- var informerConfiguration = createInformerConfiguration (configAnnotation ,
48
+ var informerConfiguration = createInformerConfig (configAnnotation ,
47
49
(DependentResourceSpec <R , P , KubernetesDependentResourceConfig <R >>) spec ,
48
50
controllerConfig );
49
51
@@ -52,76 +54,98 @@ public KubernetesDependentResourceConfig<R> configFrom(KubernetesDependent confi
52
54
}
53
55
54
56
@ SuppressWarnings ({"unchecked" , "rawtypes" })
55
- private InformerConfigSpec <R > createInformerConfiguration (KubernetesDependent configAnnotation ,
57
+ private InformerConfiguration <R > createInformerConfig (KubernetesDependent configAnnotation ,
56
58
DependentResourceSpec <R , P , KubernetesDependentResourceConfig <R >> spec ,
57
- ControllerConfiguration <?> controllerConfig ) {
58
- final var dependentResourceClass = (Class <? extends KubernetesDependentResource <R , P >>) spec .getDependentResourceClass ();
59
+ ControllerConfiguration <? extends HasMetadata > controllerConfig ) {
60
+ Class <? extends KubernetesDependentResource <?, ?>> dependentResourceClass =
61
+ (Class <? extends KubernetesDependentResource <?, ?>>) spec .getDependentResourceClass ();
62
+ final var resourceType = spec .getResourceClass ();
63
+
64
+ InformerConfiguration .InformerConfigurationBuilder informerConfig ;
65
+ if (configAnnotation != null && configAnnotation .informerConfig () != null &&
66
+ !Constants .NO_VALUE_SET .equals (configAnnotation .informerConfig ().groupVersionKind ())) {
67
+
68
+ if (!GenericKubernetesResource .class .isAssignableFrom (resourceType )) {
69
+ throw new IllegalStateException (
70
+ "If GroupVersionKind is set the resource type must be GenericKubernetesDependentResource for: "
71
+ + dependentResourceClass .getName ());
72
+ }
73
+
74
+ informerConfig = InformerConfiguration .from (
75
+ GroupVersionKind .fromString (configAnnotation .informerConfig ().groupVersionKind ()),
76
+ controllerConfig .getResourceClass ());
77
+ } else {
78
+ informerConfig =
79
+ InformerConfiguration .from (resourceType , controllerConfig .getResourceClass ());
80
+ }
81
+
59
82
// default name should be set even if there's no explicit informer configuration
60
- var name = DependentResource .defaultNameFor (dependentResourceClass );
83
+ informerConfig .withName (DependentResource .defaultNameFor (dependentResourceClass ));
84
+
61
85
if (configAnnotation != null && configAnnotation .informerConfig () != null ) {
62
86
// override default name if more specific one is provided
63
- final var nameFromAnnotation = configAnnotation .informerConfig ().name ();
64
- if (!Constants .NO_VALUE_SET .equals (nameFromAnnotation )) {
65
- name = nameFromAnnotation ;
66
- } else {
67
- final var nameFromDependentAnnotation = spec .getName ();
68
- if (nameFromDependentAnnotation != null
69
- && !Constants .NO_VALUE_SET .equals (nameFromDependentAnnotation )) {
70
- name = nameFromDependentAnnotation ;
71
- }
87
+ if (!Constants .NO_VALUE_SET .equals (configAnnotation .informerConfig ().name ())) {
88
+ informerConfig .withName (configAnnotation .informerConfig ().name ());
89
+ } else if (spec .getName () != null && !Constants .NO_VALUE_SET .equals (spec .getName ())) {
90
+ informerConfig .withName (spec .getName ());
72
91
}
73
92
74
93
var namespaces = Set .of (configAnnotation .informerConfig ().namespaces ());
94
+ informerConfig .withNamespaces (namespaces );
75
95
76
- final var labelFromAnnotation = configAnnotation .informerConfig ().labelSelector ();
77
- var labelSelector =
78
- Constants . NO_VALUE_SET . equals ( labelFromAnnotation ) ? null : labelFromAnnotation ;
96
+ final var fromAnnotation = configAnnotation .informerConfig ().labelSelector ();
97
+ var labelSelector = Constants . NO_VALUE_SET . equals ( fromAnnotation ) ? null : fromAnnotation ;
98
+ informerConfig . withLabelSelector ( labelSelector ) ;
79
99
80
100
final var context = Utils .contextFor (controllerConfig , dependentResourceClass ,
81
101
configAnnotation .annotationType ());
102
+
82
103
var onAddFilter = Utils .instantiate (configAnnotation .informerConfig ().onAddFilter (),
83
104
OnAddFilter .class , context );
105
+ informerConfig .withOnAddFilter ((OnAddFilter <? super R >) onAddFilter );
106
+
84
107
var onUpdateFilter =
85
108
Utils .instantiate (configAnnotation .informerConfig ().onUpdateFilter (),
86
109
OnUpdateFilter .class , context );
110
+ informerConfig .withOnUpdateFilter ((OnUpdateFilter <? super R >) onUpdateFilter );
111
+
87
112
var onDeleteFilter =
88
113
Utils .instantiate (configAnnotation .informerConfig ().onDeleteFilter (),
89
114
OnDeleteFilter .class , context );
115
+ informerConfig .withOnDeleteFilter ((OnDeleteFilter <? super R >) onDeleteFilter );
116
+
90
117
var genericFilter =
91
118
Utils .instantiate (configAnnotation .informerConfig ().genericFilter (),
92
119
GenericFilter .class ,
93
120
context );
94
121
95
- final var gvkFromAnnotation = configAnnotation . informerConfig (). groupVersionKind ( );
96
- var gvk = ! Constants . NO_VALUE_SET . equals ( gvkFromAnnotation )
97
- ? GroupVersionKind . fromString ( gvkFromAnnotation )
98
- : null ;
122
+ informerConfig . withGenericFilter (( GenericFilter <? super R >) genericFilter );
123
+
124
+ informerConfig . followControllerNamespacesOnChange (
125
+ configAnnotation . informerConfig (). followControllerNamespacesOnChange ()) ;
99
126
100
127
var primaryToSecondaryMapper =
101
128
Utils .instantiate (configAnnotation .informerConfig ().primaryToSecondaryMapper (),
102
129
PrimaryToSecondaryMapper .class , context );
130
+ informerConfig .withPrimaryToSecondaryMapper (primaryToSecondaryMapper );
103
131
104
132
var secondaryToPrimaryMapper =
105
133
Utils .instantiate (configAnnotation .informerConfig ().secondaryToPrimaryMapper (),
106
134
SecondaryToPrimaryMapper .class , context );
107
-
108
- return new InformerConfigSpec <>(name , namespaces , labelSelector ,
109
- configAnnotation .informerConfig ().followControllerNamespacesOnChange (),
110
- onAddFilter , onUpdateFilter , onDeleteFilter , genericFilter ,
111
- gvk , secondaryToPrimaryMapper , primaryToSecondaryMapper );
112
- } else {
113
- var secondaryToPrimaryMapper =
114
- (SecondaryToPrimaryMapper <R >) getSecondaryToPrimaryMapper (dependentResourceClass ,
115
- controllerConfig .getResourceClass ())
116
- .orElse (null );
117
- if (secondaryToPrimaryMapper == null ) {
118
- if (spec .getUseEventSourceWithName ().isEmpty ()) {
119
- log .warn ("No SecondaryToPrimaryMapper set for dependent resource "
120
- + dependentResourceClass .getSimpleName () +
121
- ". This might be an issue with the setup of the dependent resource" );
122
- }
135
+ if (secondaryToPrimaryMapper != null ) {
136
+ informerConfig .withSecondaryToPrimaryMapper (secondaryToPrimaryMapper );
123
137
}
124
- return new InformerConfigSpec <>(name , secondaryToPrimaryMapper );
138
+ } else {
139
+ getSecondaryToPrimaryMapper (dependentResourceClass ,
140
+ controllerConfig .getResourceClass ())
141
+ .ifPresentOrElse (informerConfig ::withSecondaryToPrimaryMapper , () -> {
142
+ if (spec .getUseEventSourceWithName ().isEmpty ()) {
143
+ log .warn ("No SecondaryToPrimaryMapper set for dependent resource "
144
+ + dependentResourceClass .getSimpleName () +
145
+ ". This might be an issue with the setup of the dependent resource" );
146
+ }
147
+ });
125
148
}
149
+ return informerConfig .build ();
126
150
}
127
151
}
0 commit comments