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 1 commit
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 @@ -139,13 +139,11 @@ protected <R> void registerOrDeregisterEventSourceBasedOnActivation(
.eventSourceContextForDynamicRegistration());
var es = eventSource.orElseThrow();
context.eventSourceRetriever()
.dynamicallyRegisterEventSource(dependentResourceNode.getDependentResource().name(),
es);
.dynamicallyRegisterEventSource(dependentResourceNode.getName(), es);

} else {
context.eventSourceRetriever()
.dynamicallyDeRegisterEventSource(
dependentResourceNode.getDependentResource().name());
.dynamicallyDeRegisterEventSource(dependentResourceNode.getName());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Workflow<P> resolve(KubernetesClient client,
spec.getReadyCondition(),
spec.getActivationCondition(),
resolve(spec, client, configuration));
alreadyResolved.put(node.getDependentResource().name(), node);
alreadyResolved.put(node.getName(), node);
spec.getDependsOn()
.forEach(depend -> node.addDependsOnRelation(alreadyResolved.get(depend)));
}
Expand All @@ -106,9 +106,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 @@ -77,7 +77,7 @@ private Map<String, DependentResourceNode> toMap(Set<DependentResourceNode> node
bottomLevelResource.remove(dependsOn);
}
}
map.put(node.getDependentResource().name(), node);
map.put(node.getName(), node);
}
if (topLevelResources.size() == 0) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public Optional<Condition<R, P>> getReconcilePrecondition() {
return Optional.ofNullable(reconcilePrecondition);
}

public String getName() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not very good practice in my opinion, it kinda implies that it's the name of the dependent node not the dependent resource. I would either remove this, and always access the name though DependentResource or rename this to getDependentResourceName()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DependentResourceNode is an internal class so I'd argue that it doesn't really matter and since Dependent Resources always have names now, the name of the node is the name of the associated dependent, which this method captures. This certainly could be reverted but, to me, it's pretty clear that the name of the DRN is the name of the associated DR. :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls do, when started looking it was kinda confusing now, what now we are talking about

Copy link
Collaborator

@csviri csviri Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really small thing, at least for me it is much easier to jump to a conclusion with that pattern :)

return dependentResource.name();
}

public Optional<Condition<R, P>> getDeletePostcondition() {
return Optional.ofNullable(deletePostcondition);
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