|
| 1 | +package io.javaoperatorsdk.operator.sample.bulkdependent.external; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 10 | +import io.javaoperatorsdk.operator.processing.dependent.*; |
| 11 | +import io.javaoperatorsdk.operator.processing.dependent.external.PollingDependentResource; |
| 12 | +import io.javaoperatorsdk.operator.processing.event.ResourceID; |
| 13 | +import io.javaoperatorsdk.operator.processing.event.source.CacheKeyMapper; |
| 14 | +import io.javaoperatorsdk.operator.sample.bulkdependent.BulkDependentTestCustomResource; |
| 15 | + |
| 16 | +public class ExternalBulkDependentResource |
| 17 | + extends PollingDependentResource<ExternalResource, BulkDependentTestCustomResource> |
| 18 | + implements BulkDependentResource<ExternalResource, BulkDependentTestCustomResource>, |
| 19 | + BulkUpdater<ExternalResource, BulkDependentTestCustomResource> { |
| 20 | + |
| 21 | + public static final String EXTERNAL_RESOURCE_NAME_DELIMITER = "#"; |
| 22 | + |
| 23 | + private ExternalServiceMock externalServiceMock = ExternalServiceMock.getInstance(); |
| 24 | + |
| 25 | + public ExternalBulkDependentResource() { |
| 26 | + super(ExternalResource.class, CacheKeyMapper.singleResourceCacheKeyMapper()); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public Map<ResourceID, Set<ExternalResource>> fetchResources() { |
| 31 | + // todo |
| 32 | + Map<ResourceID, Set<ExternalResource>> result = new HashMap<>(); |
| 33 | + var resources = externalServiceMock.listResources(); |
| 34 | + |
| 35 | + return result; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void delete(BulkDependentTestCustomResource primary, |
| 40 | + Context<BulkDependentTestCustomResource> context) { |
| 41 | + deleteBulkResourcesIfRequired(0, lastKnownBulkSize(), primary, context); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public int count(BulkDependentTestCustomResource primary, |
| 46 | + Context<BulkDependentTestCustomResource> context) { |
| 47 | + return primary.getSpec().getNumberOfResources(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void deleteBulkResourceWithIndex(BulkDependentTestCustomResource primary, |
| 52 | + ExternalResource resource, int i, Context<BulkDependentTestCustomResource> context) { |
| 53 | + externalServiceMock.delete(resource.getId()); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public BulkResourceDiscriminatorFactory<ExternalResource, BulkDependentTestCustomResource> bulkResourceDiscriminatorFactory() { |
| 58 | + return index -> (resource, primary, context) -> { |
| 59 | + ExternalResource collect = context.getSecondaryResources(resource).stream() |
| 60 | + .filter(r -> r.getId().endsWith(EXTERNAL_RESOURCE_NAME_DELIMITER + index)) |
| 61 | + .collect(Collectors.collectingAndThen( |
| 62 | + Collectors.toList(), |
| 63 | + list -> { |
| 64 | + if (list.size() > 1) { |
| 65 | + throw new IllegalStateException("Found more than 1 object: " + list); |
| 66 | + } |
| 67 | + return list.get(0); |
| 68 | + })); |
| 69 | + return Optional.ofNullable(collect); |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public ExternalResource desired(BulkDependentTestCustomResource primary, int index, |
| 75 | + Context<BulkDependentTestCustomResource> context) { |
| 76 | + return new ExternalResource(toExternalResourceId(primary, index), "" + index); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public ExternalResource create(ExternalResource desired, BulkDependentTestCustomResource primary, |
| 81 | + Context<BulkDependentTestCustomResource> context) { |
| 82 | + return externalServiceMock.create(desired); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public ExternalResource update(ExternalResource actual, ExternalResource desired, |
| 87 | + BulkDependentTestCustomResource primary, Context<BulkDependentTestCustomResource> context) { |
| 88 | + return externalServiceMock.update(desired); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Matcher.Result<ExternalResource> match(ExternalResource actualResource, |
| 93 | + BulkDependentTestCustomResource primary, |
| 94 | + int index, Context<BulkDependentTestCustomResource> context) { |
| 95 | + var desired = desired(primary, index, context); |
| 96 | + return Matcher.Result.computed(desired.equals(actualResource), desired); |
| 97 | + } |
| 98 | + |
| 99 | + private static String toExternalResourceId(BulkDependentTestCustomResource primary, int i) { |
| 100 | + return primary.getMetadata().getName() + EXTERNAL_RESOURCE_NAME_DELIMITER + |
| 101 | + primary.getMetadata().getNamespace() + |
| 102 | + EXTERNAL_RESOURCE_NAME_DELIMITER + i; |
| 103 | + } |
| 104 | +} |
0 commit comments