Skip to content

refactor: add default implementation for name() #2255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ default boolean isDeletable() {
return this instanceof Deleter;
}

String name();
default String name() {
return defaultNameFor(getClass());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ public abstract class AbstractDependentResource<R, P extends HasMetadata>
private ResourceDiscriminator<R, P> resourceDiscriminator;
private final DependentResourceReconciler<R, P> dependentResourceReconciler;

protected String name = DependentResource.defaultNameFor(this.getClass());
protected String name;

@SuppressWarnings({"unchecked"})
protected AbstractDependentResource() {
this(null);
}

protected AbstractDependentResource(String name) {
creator = creatable ? (Creator<R, P>) this : null;
updater = updatable ? (Updater<R, P>) this : null;

dependentResourceReconciler = this instanceof BulkDependentResource
? new BulkDependentResourceReconciler<>((BulkDependentResource<R, P>) this)
: new SingleDependentResourceReconciler<>(this);
this.name = name == null ? DependentResource.defaultNameFor(this.getClass()) : name;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public abstract class AbstractEventSourceHolderDependentResource<R, P extends Ha
protected String eventSourceNameToUse;

protected AbstractEventSourceHolderDependentResource(Class<R> resourceType) {
this(resourceType, null);
}

protected AbstractEventSourceHolderDependentResource(Class<R> resourceType, String name) {
super(name);
this.resourceType = resourceType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public abstract class KubernetesDependentResource<R extends HasMetadata, P exten

@SuppressWarnings("unchecked")
public KubernetesDependentResource(Class<R> resourceType) {
super(resourceType);
this(resourceType, null);
}

@SuppressWarnings("unchecked")
public KubernetesDependentResource(Class<R> resourceType, String name) {
super(resourceType, name);

usingCustomResourceUpdateMatcher = this instanceof ResourceUpdaterMatcher;
updaterMatcher = usingCustomResourceUpdateMatcher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.javaoperatorsdk.operator.processing.event.ResourceID;

@SuppressWarnings("rawtypes")
public abstract class AbstractWorkflowExecutor<P extends HasMetadata> {
abstract class AbstractWorkflowExecutor<P extends HasMetadata> {

protected final Workflow<P> workflow;
protected final P primary;
Expand Down Expand Up @@ -133,19 +133,16 @@ protected <R> void registerOrDeregisterEventSourceBasedOnActivation(
boolean activationConditionMet,
DependentResourceNode<R, P> dependentResourceNode) {
if (dependentResourceNode.getActivationCondition().isPresent()) {
final var dr = dependentResourceNode.getDependentResource();
final var eventSourceRetriever = context.eventSourceRetriever();
if (activationConditionMet) {
var eventSource =
dependentResourceNode.getDependentResource().eventSource(context.eventSourceRetriever()
.eventSourceContextForDynamicRegistration());
dr.eventSource(eventSourceRetriever.eventSourceContextForDynamicRegistration());
var es = eventSource.orElseThrow();
context.eventSourceRetriever()
.dynamicallyRegisterEventSource(dependentResourceNode.getDependentResource().name(),
es);
eventSourceRetriever.dynamicallyRegisterEventSource(dr.name(), es);

} else {
context.eventSourceRetriever()
.dynamicallyDeRegisterEventSource(
dependentResourceNode.getDependentResource().name());
eventSourceRetriever.dynamicallyDeRegisterEventSource(dr.name());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ public Workflow<P> resolve(KubernetesClient client,
ControllerConfiguration<P> configuration) {
final var alreadyResolved = new HashMap<String, DependentResourceNode>(orderedSpecs.size());
for (DependentResourceSpec spec : orderedSpecs) {
final var dependentResource = resolve(spec, client, configuration);
final var node = new DependentResourceNode(
spec.getReconcileCondition(),
spec.getDeletePostCondition(),
spec.getReadyCondition(),
spec.getActivationCondition(),
resolve(spec, client, configuration));
alreadyResolved.put(node.getDependentResource().name(), node);
dependentResource);
alreadyResolved.put(dependentResource.name(), node);
spec.getDependsOn()
.forEach(depend -> node.addDependsOnRelation(alreadyResolved.get(depend)));
}
Expand All @@ -106,9 +107,9 @@ private <R> DependentResource<R, P> resolve(DependentResourceSpec<R, P> spec,
configuration.getConfigurationService().dependentResourceFactory()
.createFrom(spec, configuration);

if (spec.getName() != null && !spec.getName().equals(NO_VALUE_SET)
&& dependentResource instanceof NameSetter) {
((NameSetter) dependentResource).setName(spec.getName());
final var name = spec.getName();
if (name != null && !NO_VALUE_SET.equals(name) && dependentResource instanceof NameSetter) {
((NameSetter) dependentResource).setName(name);
}

if (dependentResource instanceof KubernetesClientAware) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @param <P> primary resource
*/
@SuppressWarnings("rawtypes")
public class DefaultWorkflow<P extends HasMetadata> implements Workflow<P> {
class DefaultWorkflow<P extends HasMetadata> implements Workflow<P> {

private final Map<String, DependentResourceNode> dependentResourceNodes;
private final Set<DependentResourceNode> topLevelResources;
Expand Down Expand Up @@ -79,7 +79,7 @@ private Map<String, DependentResourceNode> toMap(Set<DependentResourceNode> node
}
map.put(node.getDependentResource().name(), node);
}
if (topLevelResources.size() == 0) {
if (topLevelResources.isEmpty()) {
throw new IllegalStateException(
"No top-level dependent resources found. This might indicate a cyclic Set of DependentResourceNode has been provided.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;

@SuppressWarnings("rawtypes")
public class DependentResourceNode<R, P extends HasMetadata> {
class DependentResourceNode<R, P extends HasMetadata> {

private final List<DependentResourceNode> dependsOn = new LinkedList<>();
private final List<DependentResourceNode> parents = new LinkedList<>();
Expand Down Expand Up @@ -54,7 +54,6 @@ public Optional<Condition<R, P>> getReconcilePrecondition() {
return Optional.ofNullable(reconcilePrecondition);
}


public Optional<Condition<R, P>> getDeletePostcondition() {
return Optional.ofNullable(deletePostcondition);
}
Expand Down Expand Up @@ -104,12 +103,6 @@ public int hashCode() {
return this.getDependentResource().name().hashCode();
}

@SuppressWarnings("rawtypes")
static String getNameFor(DependentResource dependentResource) {
return DependentResource.defaultNameFor(dependentResource.getClass()) + "#"
+ dependentResource.hashCode();
}

@Override
public String toString() {
return "DependentResourceNode{" + getDependentResource() + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public WorkflowBuilder<P> withActivationCondition(Condition activationCondition)

DependentResourceNode getNodeByDependentResource(DependentResource<?, ?> dependentResource) {
// first check by name
final var node =
dependentResourceNodes.get(dependentResource.name());
final var node = dependentResourceNodes.get(dependentResource.name());
if (node != null) {
return node;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfigBuilder;
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

class ControllerConfigurationOverriderTest {
private final BaseConfigurationService configurationService = new BaseConfigurationService();
Expand Down Expand Up @@ -102,11 +98,6 @@ public Class<Object> resourceType() {
return Object.class;
}

@Override
public String name() {
return null;
}

@Override
public void configureWith(String config) {
this.config = config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource;

import static io.javaoperatorsdk.operator.api.config.dependent.DependentResourceConfigurationResolverTest.CustomAnnotationReconciler.DR_NAME;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

class DependentResourceConfigurationResolverTest {

Expand Down Expand Up @@ -175,11 +172,6 @@ public Class<ConfigMap> resourceType() {
return ConfigMap.class;
}

@Override
public String name() {
return null;
}

@Override
public void configureWith(CustomConfig config) {
this.config = config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public class AbstractWorkflowExecutorTest {
public class TestDependent extends KubernetesDependentResource<ConfigMap, TestCustomResource> {

public TestDependent(String name) {
super(ConfigMap.class);
setName(name);

super(ConfigMap.class, name);
}

@Override
Expand All @@ -51,7 +49,7 @@ public ReconcileResult<ConfigMap> reconcile(TestCustomResource primary,

@Override
public String toString() {
return name;
return name();
}
}

Expand Down Expand Up @@ -91,7 +89,7 @@ public void delete(TestCustomResource primary, Context<TestCustomResource> conte
}

public class TestErrorDependent implements DependentResource<String, TestCustomResource> {
private String name;
private final String name;

public TestErrorDependent(String name) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,7 @@
import io.javaoperatorsdk.operator.processing.retry.RetryExecution;
import io.javaoperatorsdk.operator.sample.readonly.ReadOnlyDependent;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

class BaseConfigurationServiceTest {

Expand Down Expand Up @@ -470,11 +464,6 @@ public Class<ConfigMap> resourceType() {
return ConfigMap.class;
}

@Override
public String name() {
return null;
}

@Override
public void configureWith(CustomConfig config) {
this.config = config;
Expand Down