Skip to content

Configuration polish #926

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 11 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -12,15 +12,13 @@
public @interface KubernetesDependent {

boolean OWNED_DEFAULT = true;
boolean SKIP_UPDATE_DEFAULT = true;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We might want to put this back, it was then used in informer as recognized later, but still don't understand why was it added in first place.


boolean owned() default OWNED_DEFAULT;

boolean skipUpdateIfUnchanged() default SKIP_UPDATE_DEFAULT;

/**
* Specified which namespaces this Controller monitors for custom resources events. If no
* namespace is specified then the controller will monitor all namespaces by default.
* namespace is specified then the controller will monitor the namespaces configured for the
* controller.
*
* @return the list of namespaces this controller monitors
*/
Expand All @@ -34,4 +32,5 @@
* @return the label selector
*/
String labelSelector() default EMPTY_STRING;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,40 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
import io.javaoperatorsdk.operator.api.config.DefaultResourceConfiguration;
import io.javaoperatorsdk.operator.api.config.ResourceConfiguration;
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
import io.javaoperatorsdk.operator.processing.event.source.AssociatedSecondaryResourceIdentifier;
import io.javaoperatorsdk.operator.processing.event.source.PrimaryResourcesRetriever;

public interface KubernetesDependentResourceConfiguration<R extends HasMetadata, P extends HasMetadata>
extends InformerConfiguration<R, P>, DependentResourceConfiguration<R, P> {
extends DependentResourceConfiguration<R, P>, ResourceConfiguration<R> {

@Override
ConfigurationService getConfigurationService();

@Override
void setConfigurationService(ConfigurationService service);

class DefaultKubernetesDependentResourceConfiguration<R extends HasMetadata, P extends HasMetadata>
extends DefaultInformerConfiguration<R, P>
extends DefaultResourceConfiguration<R>
implements KubernetesDependentResourceConfiguration<R, P> {

private final boolean owned;

private final Class<DependentResource<R, P>> dependentResourceClass;
private final boolean owned;

protected DefaultKubernetesDependentResourceConfiguration(
ConfigurationService service,
ConfigurationService configurationService,
String labelSelector, Class<R> resourceClass,
PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet,
AssociatedSecondaryResourceIdentifier<P> associatedWith,
boolean skipUpdateEventPropagationIfNoChange, Set<String> namespaces, boolean owned,
Set<String> namespaces, boolean owned,
Class<DependentResource<R, P>> dependentResourceClass) {
super(service, labelSelector, resourceClass, secondaryToPrimaryResourcesIdSet, associatedWith,
skipUpdateEventPropagationIfNoChange, namespaces);
super(labelSelector, resourceClass, namespaces);
setConfigurationService(configurationService);
this.owned = owned;
this.dependentResourceClass = dependentResourceClass;
}

@Override
public boolean isOwned() {
return owned;
}
Expand All @@ -40,27 +46,24 @@ public boolean isOwned() {
public Class<? extends DependentResource<R, P>> getDependentResourceClass() {
return dependentResourceClass;
}

@Override
public Class<R> getResourceClass() {
return super.getResourceClass();
}
}

static <R extends HasMetadata, P extends HasMetadata> KubernetesDependentResourceConfiguration<R, P> from(
InformerConfiguration<R, P> cfg, boolean owned,
Class<? extends DependentResource> dependentResourceClass) {
return new DefaultKubernetesDependentResourceConfiguration<R, P>(cfg.getConfigurationService(),
cfg.getLabelSelector(), cfg.getResourceClass(), cfg.getPrimaryResourcesRetriever(),
cfg.getAssociatedResourceIdentifier(), cfg.isSkipUpdateEventPropagationIfNoChange(),
cfg.getLabelSelector(), cfg.getResourceClass(),
cfg.getNamespaces(), owned,
(Class<DependentResource<R, P>>) dependentResourceClass);
}

boolean isOwned();
Class<? extends DependentResource<R, P>> getDependentResourceClass();

@Override
default Class<R> getResourceClass() {
return InformerConfiguration.super.getResourceClass();
return null;
}

boolean isOwned();

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ class DefaultInformerConfiguration<R extends HasMetadata, P extends HasMetadata>

private final PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet;
private final AssociatedSecondaryResourceIdentifier<P> associatedWith;
private final boolean skipUpdateEventPropagationIfNoChange;

protected DefaultInformerConfiguration(ConfigurationService service, String labelSelector,
Class<R> resourceClass,
PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet,
AssociatedSecondaryResourceIdentifier<P> associatedWith,
boolean skipUpdateEventPropagationIfNoChange, Set<String> namespaces) {
Set<String> namespaces) {
super(labelSelector, resourceClass, namespaces);
setConfigurationService(service);
this.secondaryToPrimaryResourcesIdSet =
Objects.requireNonNullElse(secondaryToPrimaryResourcesIdSet,
Mappers.fromOwnerReference());
this.associatedWith =
Objects.requireNonNullElseGet(associatedWith, () -> ResourceID::fromResource);
this.skipUpdateEventPropagationIfNoChange = skipUpdateEventPropagationIfNoChange;
}

public PrimaryResourcesRetriever<R> getPrimaryResourcesRetriever() {
Expand All @@ -47,22 +45,16 @@ public AssociatedSecondaryResourceIdentifier<P> getAssociatedResourceIdentifier(
return associatedWith;
}

public boolean isSkipUpdateEventPropagationIfNoChange() {
return skipUpdateEventPropagationIfNoChange;
}
}

PrimaryResourcesRetriever<R> getPrimaryResourcesRetriever();

AssociatedSecondaryResourceIdentifier<P> getAssociatedResourceIdentifier();

boolean isSkipUpdateEventPropagationIfNoChange();

class InformerConfigurationBuilder<R extends HasMetadata, P extends HasMetadata> {

private PrimaryResourcesRetriever<R> secondaryToPrimaryResourcesIdSet;
private AssociatedSecondaryResourceIdentifier<P> associatedWith;
private boolean skipUpdateEventPropagationIfNoChange = true;
private Set<String> namespaces;
private String labelSelector;
private final Class<R> resourceClass;
Expand All @@ -86,16 +78,6 @@ public InformerConfigurationBuilder<R, P> withAssociatedSecondaryResourceIdentif
return this;
}

public InformerConfigurationBuilder<R, P> withoutSkippingEventPropagationIfUnchanged() {
this.skipUpdateEventPropagationIfNoChange = false;
return this;
}

public InformerConfigurationBuilder<R, P> skippingEventPropagationIfUnchanged(
boolean skipIfUnchanged) {
this.skipUpdateEventPropagationIfNoChange = skipIfUnchanged;
return this;
}

public InformerConfigurationBuilder<R, P> withNamespaces(String... namespaces) {
this.namespaces = namespaces != null ? Set.of(namespaces) : Collections.emptySet();
Expand All @@ -115,7 +97,7 @@ public InformerConfigurationBuilder<R, P> withLabelSelector(String labelSelector

public InformerConfiguration<R, P> build() {
return new DefaultInformerConfiguration<>(configurationService, labelSelector, resourceClass,
secondaryToPrimaryResourcesIdSet, associatedWith, skipUpdateEventPropagationIfNoChange,
secondaryToPrimaryResourcesIdSet, associatedWith,
namespaces);
}
}
Expand All @@ -136,8 +118,6 @@ static <R extends HasMetadata, P extends HasMetadata> InformerConfigurationBuild
configuration.getConfigurationService())
.withNamespaces(configuration.getNamespaces())
.withLabelSelector(configuration.getLabelSelector())
.skippingEventPropagationIfUnchanged(
configuration.isSkipUpdateEventPropagationIfNoChange())
.withAssociatedSecondaryResourceIdentifier(
configuration.getAssociatedResourceIdentifier())
.withPrimaryResourcesRetriever(configuration.getPrimaryResourcesRetriever());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.concurrent.TimeUnit;

import io.javaoperatorsdk.operator.api.config.dependent.Dependent;
import io.javaoperatorsdk.operator.processing.dependent.DependentResourceController;
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEventFilter;

@Retention(RetentionPolicy.RUNTIME)
Expand Down Expand Up @@ -65,11 +64,6 @@ ReconciliationMaxInterval reconciliationMaxInterval() default @ReconciliationMax


/**
* Optional list of classes providing {@link DependentResourceController} implementations
* encapsulating logic to handle the associated
* {@link io.javaoperatorsdk.operator.processing.Controller}'s reconciliation of dependent
* resources
*
* @return the list of {@link DependentResourceController} implementations
*/
Dependent[] dependents() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.ReconcilerUtils;
import io.javaoperatorsdk.operator.api.config.dependent.KubernetesDependentResourceConfiguration;
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
Expand All @@ -27,6 +28,11 @@ public abstract class KubernetesDependentResource<R extends HasMetadata, P exten
private boolean explicitDelete = false;
private boolean owned = true;
private InformerEventSource<R, P> informerEventSource;
private DesiredSupplier<R, P> desiredSupplier = null;
private Class<R> resourceType = null;
private AssociatedSecondaryResourceIdentifier<P> associatedSecondaryResourceIdentifier =
ResourceID::fromResource;
private PrimaryResourcesRetriever<R> primaryResourcesRetriever = Mappers.fromOwnerReference();

public KubernetesDependentResource() {
this(null);
Expand All @@ -36,6 +42,31 @@ public KubernetesDependentResource(KubernetesClient client) {
this.client = client;
}

public KubernetesDependentResource(
KubernetesClient client, Class<R> resourceType, DesiredSupplier<R, P> desiredSupplier) {
this.client = client;
this.resourceType = resourceType;
this.desiredSupplier = desiredSupplier;
}

public KubernetesDependentResource(
Class<R> resourceType, DesiredSupplier<R, P> desiredSupplier) {
this(null, resourceType, desiredSupplier);
}

// todo builder and/or factory methods
public void initWithConfiguration(KubernetesDependentResourceConfiguration<R, P> config) {
this.owned = config.isOwned();
InformerConfiguration<R, P> ic =
InformerConfiguration.from(config.getConfigurationService(), resourceType())
.withLabelSelector(config.getLabelSelector())
.withNamespaces(config.getNamespaces())
.withPrimaryResourcesRetriever(getPrimaryResourcesRetriever())
.withAssociatedSecondaryResourceIdentifier(getAssociatedSecondaryResourceIdentifier())
.build();
informerEventSource = new InformerEventSource<>(ic, client);
}

protected void beforeCreateOrUpdate(R desired, P primary) {
if (owned) {
desired.addOwnerReference(primary);
Expand Down Expand Up @@ -75,34 +106,30 @@ public Optional<EventSource> eventSource(EventSourceContext<P> context) {
if (informerEventSource != null) {
return Optional.of(informerEventSource);
}
var informerConfig = initInformerConfiguration(context);
var informerConfig = initDefaultInformerConfiguration(context);
informerEventSource = new InformerEventSource(informerConfig, context);
return Optional.of(informerEventSource);
}

@SuppressWarnings("unchecked")
private InformerConfiguration<R, P> initInformerConfiguration(EventSourceContext<P> context) {
PrimaryResourcesRetriever<R> associatedPrimaries =
(this instanceof PrimaryResourcesRetriever) ? (PrimaryResourcesRetriever<R>) this
: getDefaultPrimaryResourcesRetriever();

AssociatedSecondaryResourceIdentifier<P> associatedSecondary =
(this instanceof AssociatedSecondaryResourceIdentifier)
? (AssociatedSecondaryResourceIdentifier<P>) this
: getDefaultAssociatedSecondaryResourceIdentifier();

private InformerConfiguration<R, P> initDefaultInformerConfiguration(
EventSourceContext<P> context) {
return InformerConfiguration.from(context, resourceType())
.withPrimaryResourcesRetriever(associatedPrimaries)
.withAssociatedSecondaryResourceIdentifier(associatedSecondary)
.withPrimaryResourcesRetriever(getPrimaryResourcesRetriever())
.withAssociatedSecondaryResourceIdentifier(getAssociatedSecondaryResourceIdentifier())
.build();
}

protected AssociatedSecondaryResourceIdentifier<P> getDefaultAssociatedSecondaryResourceIdentifier() {
return ResourceID::fromResource;

protected PrimaryResourcesRetriever<R> getPrimaryResourcesRetriever() {
return (this instanceof PrimaryResourcesRetriever) ? (PrimaryResourcesRetriever<R>) this
: primaryResourcesRetriever;
}

protected PrimaryResourcesRetriever<R> getDefaultPrimaryResourcesRetriever() {
return Mappers.fromOwnerReference();
protected AssociatedSecondaryResourceIdentifier<P> getAssociatedSecondaryResourceIdentifier() {
return (this instanceof AssociatedSecondaryResourceIdentifier)
? (AssociatedSecondaryResourceIdentifier<P>) this
: associatedSecondaryResourceIdentifier;
}

public KubernetesDependentResource<R, P> setInformerEventSource(
Expand Down Expand Up @@ -147,4 +174,30 @@ public KubernetesDependentResource<R, P> setOwned(boolean owned) {
this.owned = owned;
return this;
}

@Override
public Class<R> resourceType() {
if (resourceType != null) {
return resourceType;
} else {
return super.resourceType();
}
}

@Override
protected R desired(P primary, Context context) {
return desiredSupplier.getDesired(primary, context);
}

public KubernetesDependentResource<R, P> setAssociatedSecondaryResourceIdentifier(
AssociatedSecondaryResourceIdentifier<P> associatedSecondaryResourceIdentifier) {
this.associatedSecondaryResourceIdentifier = associatedSecondaryResourceIdentifier;
return this;
}

public KubernetesDependentResource<R, P> setPrimaryResourcesRetriever(
PrimaryResourcesRetriever<R> primaryResourcesRetriever) {
this.primaryResourcesRetriever = primaryResourcesRetriever;
return this;
}
}
Loading