DependencyResource { DependencyResource getOwnerResource(final String kind, final Fetcher transform) { if (stop) { return this; } if (metadata.getOwnerReferences() == null) { stop = true; return this; } V1OwnerReference ownerReference = null; for (V1OwnerReference each : metadata.getOwnerReferences()) { if (each.getKind().equals(kind)) { ownerReference = each; break; } } if (ownerReference == null) { stop = true; return this; } Optional<V1ObjectMeta> metaOptional = transform.apply(ownerReference); if (!metaOptional.isPresent()) { stop = true; return this; } return new DependencyResource(metaOptional.get()); }    }
@Test public void test() { V1ObjectMeta meta = new V1ObjectMeta(); meta.setName("pod1"); V1OwnerReference reference = new V1OwnerReference(); reference.setKind("ReplicaSet"); reference.setName("rs1"); meta.addOwnerReferencesItem(reference); DependencyResource dr = new DependencyResource(meta); DependencyResource drr = dr.getOwnerResource("ReplicaSet", ownerReference -> { assertThat(ownerReference.getName(), is("rs1")); V1ObjectMeta result = new V1ObjectMeta(); result.setName("rs1"); V1OwnerReference reference1 = new V1OwnerReference(); reference1.setKind("Deployment"); reference1.setName("deploy1"); result.addOwnerReferencesItem(reference1); return function.go(result); }).getOwnerResource("Deployment", ownerReference -> { assertThat(ownerReference.getName(), is("deploy1")); V1ObjectMeta result = new V1ObjectMeta(); result.setName("deploy1"); return result; }); assertThat(drr.getMetadata().getName(), is(resourceName)); }