|
13 | 13 | import io.fabric8.kubernetes.api.model.apps.Deployment;
|
14 | 14 | import io.fabric8.kubernetes.api.model.apps.DeploymentStatus;
|
15 | 15 | import io.fabric8.kubernetes.client.KubernetesClient;
|
16 |
| -import io.fabric8.kubernetes.client.dsl.RollableScalableResource; |
17 |
| -import io.fabric8.kubernetes.client.dsl.ServiceResource; |
18 | 16 | import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
|
19 | 17 | import io.fabric8.kubernetes.client.utils.Serialization;
|
20 |
| -import io.javaoperatorsdk.operator.api.*; |
21 | 18 | import io.javaoperatorsdk.operator.api.reconciler.*;
|
22 | 19 | import io.javaoperatorsdk.operator.processing.event.ResourceID;
|
23 | 20 | import io.javaoperatorsdk.operator.processing.event.source.EventSourceRegistry;
|
24 | 21 | import io.javaoperatorsdk.operator.processing.event.source.InformerEventSource;
|
25 | 22 |
|
| 23 | +import static io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration.NO_FINALIZER; |
26 | 24 | import static java.util.Collections.EMPTY_SET;
|
27 | 25 |
|
28 | 26 | /**
|
29 | 27 | * Runs a specified number of Tomcat app server Pods. It uses a Deployment to create the Pods. Also
|
30 | 28 | * creates a Service over which the Pods can be accessed.
|
31 | 29 | */
|
32 |
| -@ControllerConfiguration |
| 30 | +@ControllerConfiguration(finalizerName = NO_FINALIZER) |
33 | 31 | public class TomcatReconciler implements Reconciler<Tomcat>, EventSourceInitializer<Tomcat> {
|
34 | 32 |
|
35 | 33 | private final Logger log = LoggerFactory.getLogger(getClass());
|
@@ -81,13 +79,6 @@ public UpdateControl<Tomcat> reconcile(Tomcat tomcat, Context context) {
|
81 | 79 | return UpdateControl.noUpdate();
|
82 | 80 | }
|
83 | 81 |
|
84 |
| - @Override |
85 |
| - public DeleteControl cleanup(Tomcat tomcat, Context context) { |
86 |
| - deleteDeployment(tomcat); |
87 |
| - deleteService(tomcat); |
88 |
| - return DeleteControl.defaultDelete(); |
89 |
| - } |
90 |
| - |
91 | 82 | private Tomcat updateTomcatStatus(Tomcat tomcat, Deployment deployment) {
|
92 | 83 | DeploymentStatus deploymentStatus =
|
93 | 84 | Objects.requireNonNullElse(deployment.getStatus(), new DeploymentStatus());
|
@@ -156,41 +147,18 @@ private void createOrUpdateDeployment(Tomcat tomcat) {
|
156 | 147 | }
|
157 | 148 | }
|
158 | 149 |
|
159 |
| - private void deleteDeployment(Tomcat tomcat) { |
160 |
| - log.info("Deleting Deployment {}", tomcat.getMetadata().getName()); |
161 |
| - RollableScalableResource<Deployment> deployment = |
162 |
| - kubernetesClient |
163 |
| - .apps() |
164 |
| - .deployments() |
165 |
| - .inNamespace(tomcat.getMetadata().getNamespace()) |
166 |
| - .withName(tomcat.getMetadata().getName()); |
167 |
| - if (deployment.get() != null) { |
168 |
| - deployment.delete(); |
169 |
| - } |
170 |
| - } |
171 |
| - |
172 | 150 | private void createOrUpdateService(Tomcat tomcat) {
|
173 | 151 | Service service = loadYaml(Service.class, "service.yaml");
|
174 | 152 | service.getMetadata().setName(tomcat.getMetadata().getName());
|
175 | 153 | String ns = tomcat.getMetadata().getNamespace();
|
176 | 154 | service.getMetadata().setNamespace(ns);
|
| 155 | + service.getMetadata().getOwnerReferences().get(0).setName(tomcat.getMetadata().getName()); |
| 156 | + service.getMetadata().getOwnerReferences().get(0).setUid(tomcat.getMetadata().getUid()); |
177 | 157 | service.getSpec().getSelector().put("app", tomcat.getMetadata().getName());
|
178 | 158 | log.info("Creating or updating Service {} in {}", service.getMetadata().getName(), ns);
|
179 | 159 | kubernetesClient.services().inNamespace(ns).createOrReplace(service);
|
180 | 160 | }
|
181 | 161 |
|
182 |
| - private void deleteService(Tomcat tomcat) { |
183 |
| - log.info("Deleting Service {}", tomcat.getMetadata().getName()); |
184 |
| - ServiceResource<Service> service = |
185 |
| - kubernetesClient |
186 |
| - .services() |
187 |
| - .inNamespace(tomcat.getMetadata().getNamespace()) |
188 |
| - .withName(tomcat.getMetadata().getName()); |
189 |
| - if (service.get() != null) { |
190 |
| - service.delete(); |
191 |
| - } |
192 |
| - } |
193 |
| - |
194 | 162 | private <T> T loadYaml(Class<T> clazz, String yaml) {
|
195 | 163 | try (InputStream is = getClass().getResourceAsStream(yaml)) {
|
196 | 164 | return Serialization.unmarshal(is, clazz);
|
|
0 commit comments