|
| 1 | +package io.javaoperatorsdk.operator.sample.ssalegacymatcher; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.concurrent.atomic.AtomicInteger; |
| 6 | + |
| 7 | +import io.fabric8.kubernetes.api.model.Service; |
| 8 | +import io.javaoperatorsdk.operator.SSAWithLegacyMatcherIT; |
| 9 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 10 | +import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; |
| 11 | +import io.javaoperatorsdk.operator.processing.dependent.kubernetes.GenericKubernetesResourceMatcher; |
| 12 | +import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent; |
| 13 | + |
| 14 | +import static io.javaoperatorsdk.operator.ReconcilerUtils.loadYaml; |
| 15 | + |
| 16 | +@KubernetesDependent |
| 17 | +public class ServiceDependentResource |
| 18 | + extends CRUDKubernetesDependentResource<Service, SSALegacyMatcherCustomResource> { |
| 19 | + |
| 20 | + public static AtomicInteger createUpdateCount = new AtomicInteger(0); |
| 21 | + |
| 22 | + public ServiceDependentResource() { |
| 23 | + super(Service.class); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + protected Service desired(SSALegacyMatcherCustomResource primary, |
| 28 | + Context<SSALegacyMatcherCustomResource> context) { |
| 29 | + |
| 30 | + Service service = loadYaml(Service.class, SSAWithLegacyMatcherIT.class, "service.yaml"); |
| 31 | + service.getMetadata().setName(primary.getMetadata().getName()); |
| 32 | + service.getMetadata().setNamespace(primary.getMetadata().getNamespace()); |
| 33 | + Map<String, String> labels = new HashMap<>(); |
| 34 | + labels.put("app", "deployment-name"); |
| 35 | + service.getSpec().setSelector(labels); |
| 36 | + return service; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Result<Service> match(Service actualResource, SSALegacyMatcherCustomResource primary, |
| 41 | + Context<SSALegacyMatcherCustomResource> context) { |
| 42 | + return GenericKubernetesResourceMatcher.match(this, actualResource, primary, context, |
| 43 | + true, false, false); |
| 44 | + } |
| 45 | + |
| 46 | + // override just to check the exec count |
| 47 | + @Override |
| 48 | + public Service update(Service actual, Service target, SSALegacyMatcherCustomResource primary, |
| 49 | + Context<SSALegacyMatcherCustomResource> context) { |
| 50 | + createUpdateCount.addAndGet(1); |
| 51 | + return super.update(actual, target, primary, context); |
| 52 | + } |
| 53 | + |
| 54 | + // override just to check the exec count |
| 55 | + @Override |
| 56 | + public Service create(Service target, SSALegacyMatcherCustomResource primary, |
| 57 | + Context<SSALegacyMatcherCustomResource> context) { |
| 58 | + createUpdateCount.addAndGet(1); |
| 59 | + return super.create(target, primary, context); |
| 60 | + } |
| 61 | +} |
0 commit comments