|
1 | 1 | package io.javaoperatorsdk.operator.sample;
|
2 | 2 |
|
| 3 | +import java.time.Duration; |
| 4 | + |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | + |
| 8 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
3 | 9 | import io.javaoperatorsdk.operator.api.reconciler.Cleaner;
|
4 | 10 | import io.javaoperatorsdk.operator.api.reconciler.Context;
|
5 | 11 | import io.javaoperatorsdk.operator.api.reconciler.DeleteControl;
|
6 | 12 | import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
|
7 | 13 | import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
|
8 | 14 |
|
9 |
| -import java.time.Duration; |
10 |
| - |
11 |
| -public class ControllerNamespaceDeletionReconciler implements Reconciler<ControllerNamespaceDeletionCustomResource>, |
12 |
| - Cleaner<ControllerNamespaceDeletionCustomResource> { |
13 |
| - |
14 |
| - @Override |
15 |
| - public UpdateControl<ControllerNamespaceDeletionCustomResource> reconcile(ControllerNamespaceDeletionCustomResource resource, |
16 |
| - Context<ControllerNamespaceDeletionCustomResource> context) { |
17 |
| - |
18 |
| - |
19 |
| - return null; |
20 |
| - } |
21 |
| - |
22 |
| - |
23 |
| - @Override |
24 |
| - public DeleteControl cleanup(ControllerNamespaceDeletionCustomResource resource, |
25 |
| - Context<ControllerNamespaceDeletionCustomResource> context) { |
26 |
| - try { |
27 |
| - Thread.sleep(Duration.ofSeconds(10).toMillis()); |
28 |
| - return DeleteControl.defaultDelete(); |
29 |
| - } catch (InterruptedException e) { |
30 |
| - throw new RuntimeException(e); |
31 |
| - } |
| 15 | +public class ControllerNamespaceDeletionReconciler |
| 16 | + implements Reconciler<ControllerNamespaceDeletionCustomResource>, |
| 17 | + Cleaner<ControllerNamespaceDeletionCustomResource> { |
| 18 | + |
| 19 | + private static final Logger log = |
| 20 | + LoggerFactory.getLogger(ControllerNamespaceDeletionReconciler.class); |
| 21 | + |
| 22 | + public static final Duration CLEANUP_DELAY = Duration.ofSeconds(10); |
| 23 | + |
| 24 | + @Override |
| 25 | + public UpdateControl<ControllerNamespaceDeletionCustomResource> reconcile( |
| 26 | + ControllerNamespaceDeletionCustomResource resource, |
| 27 | + Context<ControllerNamespaceDeletionCustomResource> context) { |
| 28 | + log.info("Reconciling: {} in namespace: {}", resource.getMetadata().getName(), |
| 29 | + resource.getMetadata().getNamespace()); |
| 30 | + |
| 31 | + var response = createResponseResource(resource); |
| 32 | + response.getStatus().setValue(resource.getSpec().getValue()); |
| 33 | + |
| 34 | + return UpdateControl.patchStatus(response); |
| 35 | + } |
| 36 | + |
| 37 | + private ControllerNamespaceDeletionCustomResource createResponseResource( |
| 38 | + ControllerNamespaceDeletionCustomResource resource) { |
| 39 | + var res = new ControllerNamespaceDeletionCustomResource(); |
| 40 | + res.setMetadata(new ObjectMetaBuilder() |
| 41 | + .withName(resource.getMetadata().getName()) |
| 42 | + .withNamespace(resource.getMetadata().getNamespace()) |
| 43 | + .build()); |
| 44 | + res.setStatus(new ControllerNamespaceDeletionStatus()); |
| 45 | + return res; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public DeleteControl cleanup(ControllerNamespaceDeletionCustomResource resource, |
| 50 | + Context<ControllerNamespaceDeletionCustomResource> context) { |
| 51 | + try { |
| 52 | + Thread.sleep(CLEANUP_DELAY.toMillis()); |
| 53 | + return DeleteControl.defaultDelete(); |
| 54 | + } catch (InterruptedException e) { |
| 55 | + throw new RuntimeException(e); |
32 | 56 | }
|
| 57 | + } |
33 | 58 | }
|
0 commit comments