Skip to content

fix: secondary to primary mapper supporting cluster scoped resource #1770

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 6 commits into from
Feb 14, 2023
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 @@ -14,10 +14,15 @@ public static ResourceID fromResource(HasMetadata resource) {
}

public static Optional<ResourceID> fromFirstOwnerReference(HasMetadata resource) {
return fromFirstOwnerReference(resource, false);
}

public static Optional<ResourceID> fromFirstOwnerReference(HasMetadata resource,
boolean clusterScoped) {
var ownerReferences = resource.getMetadata().getOwnerReferences();
if (!ownerReferences.isEmpty()) {
return Optional.of(new ResourceID(ownerReferences.get(0).getName(),
resource.getMetadata().getNamespace()));
clusterScoped ? null : resource.getMetadata().getNamespace()));
} else {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromLabel(
}

public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromOwnerReference() {
return resource -> ResourceID.fromFirstOwnerReference(resource).map(Set::of)
return fromOwnerReference(false);
}

/**
* @param clusterScope if the owner is a cluster scoped resource
* @return mapper
* @param <T> type of the secondary resource, where the owner reference is
*/
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromOwnerReference(
boolean clusterScope) {
return resource -> ResourceID.fromFirstOwnerReference(resource, clusterScope).map(Set::of)
.orElseGet(Collections::emptySet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,53 @@
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.*;
import io.javaoperatorsdk.operator.junit.KubernetesClientAware;
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers;

@ControllerConfiguration
public class ClusterScopedCustomResourceReconciler
implements Reconciler<ClusterScopedCustomResource>, Cleaner<ClusterScopedCustomResource>,
KubernetesClientAware {
implements Reconciler<ClusterScopedCustomResource>,
KubernetesClientAware, EventSourceInitializer<ClusterScopedCustomResource> {

public static final String DATA_KEY = "data-key";

public static final String TEST_LABEL_VALUE = "clusterscopecrtest";
public static final String TEST_LABEL_KEY = "test";

private KubernetesClient client;

@Override
public UpdateControl<ClusterScopedCustomResource> reconcile(
ClusterScopedCustomResource resource, Context<ClusterScopedCustomResource> context) {

client.configMaps().resource(desired(resource)).createOrReplace();
var optionalConfigMap = context.getSecondaryResource(ConfigMap.class);

optionalConfigMap.ifPresentOrElse(cm -> {
if (!resource.getSpec().getData().equals(cm.getData().get(DATA_KEY))) {
client.configMaps().resource(desired(resource)).replace();
}
}, () -> client.configMaps().resource(desired(resource)).create());

resource.setStatus(new ClusterScopedCustomResourceStatus());
resource.getStatus().setCreated(true);
return UpdateControl.patchStatus(resource);
}

private ConfigMap desired(ClusterScopedCustomResource resource) {
return new ConfigMapBuilder()
var cm = new ConfigMapBuilder()
.withMetadata(new ObjectMetaBuilder()
.withName(resource.getMetadata().getName())
.withNamespace(resource.getSpec().getTargetNamespace())
.withLabels(Map.of(TEST_LABEL_KEY, TEST_LABEL_VALUE))
.build())
.withData(Map.of(DATA_KEY, resource.getSpec().getData()))
.build();
cm.addOwnerReference(resource);
return cm;
}

@Override
Expand All @@ -50,9 +66,12 @@ public void setKubernetesClient(KubernetesClient kubernetesClient) {
}

@Override
public DeleteControl cleanup(ClusterScopedCustomResource resource,
Context<ClusterScopedCustomResource> context) {
client.configMaps().resource(desired(resource)).delete();
return DeleteControl.defaultDelete();
public Map<String, EventSource> prepareEventSources(
EventSourceContext<ClusterScopedCustomResource> context) {
var ies = new InformerEventSource<>(InformerConfiguration.from(ConfigMap.class, context)
.withSecondaryToPrimaryMapper(Mappers.fromOwnerReference(true))
.withLabelSelector(TEST_LABEL_KEY + "=" + TEST_LABEL_VALUE)
.build(), context);
return EventSourceInitializer.nameEventSources(ies);
}
}