|
| 1 | +package io.javaoperatorsdk.operator.sample; |
| 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; |
| 9 | +import io.javaoperatorsdk.operator.api.reconciler.Cleaner; |
| 10 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 11 | +import io.javaoperatorsdk.operator.api.reconciler.DeleteControl; |
| 12 | +import io.javaoperatorsdk.operator.api.reconciler.Reconciler; |
| 13 | +import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; |
| 14 | + |
| 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 | + log.info("Cleaning up resource"); |
| 52 | + try { |
| 53 | + Thread.sleep(CLEANUP_DELAY.toMillis()); |
| 54 | + return DeleteControl.defaultDelete(); |
| 55 | + } catch (InterruptedException e) { |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments