|
9 | 9 | import io.fabric8.kubernetes.api.model.HasMetadata;
|
10 | 10 | import io.fabric8.kubernetes.client.KubernetesClient;
|
11 | 11 | import io.javaoperatorsdk.operator.api.config.ConfigurationService;
|
12 |
| -import io.javaoperatorsdk.operator.api.config.Utils; |
13 | 12 | import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
|
14 | 13 | import io.javaoperatorsdk.operator.api.reconciler.Context;
|
15 | 14 | import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
|
16 |
| -import io.javaoperatorsdk.operator.api.reconciler.dependent.AbstractDependentResource; |
17 |
| -import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResourceConfigurator; |
18 |
| -import io.javaoperatorsdk.operator.api.reconciler.dependent.EventSourceProvider; |
| 15 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.GenericDependentResource; |
19 | 16 | import io.javaoperatorsdk.operator.api.reconciler.dependent.KubernetesClientAware;
|
20 | 17 | import io.javaoperatorsdk.operator.processing.event.ResourceID;
|
21 | 18 | import io.javaoperatorsdk.operator.processing.event.source.AssociatedSecondaryResourceIdentifier;
|
|
25 | 22 | import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers;
|
26 | 23 |
|
27 | 24 | public abstract class KubernetesDependentResource<R extends HasMetadata, P extends HasMetadata>
|
28 |
| -extends AbstractDependentResource<R, P, KubernetesDependentResourceConfig> |
29 |
| -implements KubernetesClientAware, EventSourceProvider<P>, |
30 |
| -DependentResourceConfigurator<KubernetesDependentResourceConfig> { |
| 25 | +extends KubernetesDependentResourceBase<R, P, KubernetesDependentResourceConfig> |
| 26 | +implements GenericDependentResource<R, P>, KubernetesClientAware { |
31 | 27 |
|
32 | 28 | private static final Logger log = LoggerFactory.getLogger(KubernetesDependentResource.class);
|
33 | 29 |
|
| 30 | +protected ResourceMatcher resourceMatcher; |
34 | 31 | protected KubernetesClient client;
|
35 |
| -private InformerEventSource<R, P> informerEventSource; |
36 | 32 | private boolean addOwnerReference;
|
37 |
| -protected ResourceMatcher resourceMatcher; |
| 33 | +private boolean editOnly = false; |
| 34 | + |
38 | 35 |
|
39 | 36 | @Override
|
40 | 37 | public void configureWith(KubernetesDependentResourceConfig config) {
|
| 38 | +super.configureWith(config); |
41 | 39 | configureWith(config.getConfigurationService(), config.labelSelector(),
|
42 |
| -Set.of(config.namespaces()), config.addOwnerReference()); |
| 40 | +Set.of(config.namespaces()), config.addOwnerReference(), config.isEditOnly()); |
43 | 41 | }
|
44 | 42 |
|
45 | 43 | @SuppressWarnings("unchecked")
|
46 | 44 | private void configureWith(ConfigurationService configService, String labelSelector,
|
47 |
| -Set<String> namespaces, boolean addOwnerReference) { |
| 45 | +Set<String> namespaces, boolean addOwnerReference, boolean editOnly) { |
48 | 46 | final var primaryResourcesRetriever =
|
49 | 47 | (this instanceof PrimaryResourcesRetriever) ? (PrimaryResourcesRetriever<R>) this
|
50 | 48 | : Mappers.fromOwnerReference();
|
@@ -59,7 +57,8 @@ private void configureWith(ConfigurationService configService, String labelSelec
|
59 | 57 | .withPrimaryResourcesRetriever(primaryResourcesRetriever)
|
60 | 58 | .withAssociatedSecondaryResourceIdentifier(secondaryResourceIdentifier)
|
61 | 59 | .build();
|
62 |
| -configureWith(configService, new InformerEventSource<>(ic, client), addOwnerReference); |
| 60 | +configureWith(configService, new InformerEventSource<>(ic, client), addOwnerReference, |
| 61 | +editOnly); |
63 | 62 | }
|
64 | 63 |
|
65 | 64 | /**
|
@@ -71,9 +70,10 @@ private void configureWith(ConfigurationService configService, String labelSelec
|
71 | 70 | */
|
72 | 71 | public void configureWith(ConfigurationService configurationService,
|
73 | 72 | InformerEventSource<R, P> informerEventSource,
|
74 |
| -boolean addOwnerReference) { |
| 73 | +boolean addOwnerReference, boolean editOnly) { |
75 | 74 | this.informerEventSource = informerEventSource;
|
76 | 75 | this.addOwnerReference = addOwnerReference;
|
| 76 | +this.editOnly = editOnly; |
77 | 77 | initResourceMatcherIfNotSet(configurationService);
|
78 | 78 | }
|
79 | 79 |
|
@@ -84,42 +84,39 @@ protected void beforeCreateOrUpdate(R desired, P primary) {
|
84 | 84 | }
|
85 | 85 |
|
86 | 86 | @Override
|
87 |
| -protected boolean match(R actualResource, R desiredResource, Context context) { |
| 87 | +public boolean match(R actualResource, R desiredResource, Context context) { |
88 | 88 | return resourceMatcher.match(actualResource, desiredResource, context);
|
89 | 89 | }
|
90 | 90 |
|
91 | 91 | @SuppressWarnings("unchecked")
|
92 | 92 | @Override
|
93 |
| -protected R create(R target, P primary, Context context) { |
| 93 | +public void create(R target, P primary, Context context) { |
| 94 | +if (editOnly) { |
| 95 | +return; |
| 96 | +} |
94 | 97 | log.debug("Creating target resource with type: " +
|
95 | 98 | "{}, with id: {}", target.getClass(), ResourceID.fromResource(target));
|
96 | 99 | beforeCreateOrUpdate(target, primary);
|
97 | 100 | Class<R> targetClass = (Class<R>) target.getClass();
|
98 |
| -return client.resources(targetClass).inNamespace(target.getMetadata().getNamespace()) |
| 101 | +client.resources(targetClass).inNamespace(target.getMetadata().getNamespace()) |
99 | 102 | .create(target);
|
100 | 103 | }
|
101 | 104 |
|
102 | 105 | @SuppressWarnings("unchecked")
|
103 | 106 | @Override
|
104 |
| -protected R update(R actual, R target, P primary, Context context) { |
| 107 | +public void update(R actual, R target, P primary, Context context) { |
105 | 108 | log.debug("Updating target resource with type: {}, with id: {}", target.getClass(),
|
106 | 109 | ResourceID.fromResource(target));
|
107 | 110 | beforeCreateOrUpdate(target, primary);
|
108 | 111 | Class<R> targetClass = (Class<R>) target.getClass();
|
109 |
| -return client.resources(targetClass).inNamespace(target.getMetadata().getNamespace()) |
| 112 | +client.resources(targetClass).inNamespace(target.getMetadata().getNamespace()) |
110 | 113 | .replace(target);
|
111 | 114 | }
|
112 | 115 |
|
113 | 116 | @Override
|
114 | 117 | public EventSource eventSource(EventSourceContext<P> context) {
|
115 | 118 | initResourceMatcherIfNotSet(context.getConfigurationService());
|
116 |
| -if (informerEventSource == null) { |
117 |
| -configureWith(context.getConfigurationService(), null, null, |
118 |
| -KubernetesDependent.ADD_OWNER_REFERENCE_DEFAULT); |
119 |
| -log.warn("Using default configuration for " + resourceType().getSimpleName() |
120 |
| -+ " KubernetesDependentResource, call configureWith to provide configuration"); |
121 |
| -} |
122 |
| -return informerEventSource; |
| 119 | +return super.eventSource(context); |
123 | 120 | }
|
124 | 121 |
|
125 | 122 | public KubernetesDependentResource<R, P> setInformerEventSource(
|
@@ -137,10 +134,6 @@ public void delete(P primary, Context context) {
|
137 | 134 | }
|
138 | 135 |
|
139 | 136 | @SuppressWarnings("unchecked")
|
140 |
| -protected Class<R> resourceType() { |
141 |
| -return (Class<R>) Utils.getFirstTypeArgumentFromExtendedClass(getClass()); |
142 |
| -} |
143 |
| - |
144 | 137 | @Override
|
145 | 138 | public Optional<R> getResource(P primaryResource) {
|
146 | 139 | return informerEventSource.getAssociated(primaryResource);
|
|
0 commit comments