Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Mészáros <[email protected]>

format

Signed-off-by: Attila Mészáros <[email protected]>

docs

Signed-off-by: Attila Mészáros <[email protected]>

feat: support to handle different cluster for InformerEventSource

Signed-off-by: Attila Mészáros <[email protected]>
  • Loading branch information
csviri committed Aug 15, 2024
1 parent e364774 commit 69bd6e6
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 3 deletions.
18 changes: 18 additions & 0 deletions docs/content/en/docs/features/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,24 @@ parts of reconciliation logic and during the execution of the controller:

For more information about MDC see this [link](https://www.baeldung.com/mdc-in-log4j-2-logback).

## InformerEventSource Multi-Cluster Support

It is possible to handle resources for remote cluster with `InformerEventSource`. To do so,
simply just set a client that connects to a remote cluster:

```java

InformerEventSourceConfiguration<Tomcat> configuration =
InformerEventSourceConfiguration.from(SecondaryResource.class, PrimaryResource.class)
.withKubernetesClient(remoteClusterClient)
.withSecondaryToPrimaryMapper(Mappers.fromDefaultAnnotations());

```

Of course, you will need to specify a `SecondaryToPrimaryMapper`, since the default that
is based on owner references naturally won't work, rather use the one that supports annotations.


## Dynamically Changing Target Namespaces

A controller can be configured to watch a specific set of namespaces in addition of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.config.DefaultResourceConfiguration;
import io.javaoperatorsdk.operator.api.config.ResourceConfiguration;
Expand Down Expand Up @@ -74,6 +75,12 @@ default String name() {
return getInformerConfig().getName();
}

/**
* Use of a specific kubernetes client, typically a client connects to a different cluster. Note
* that this is solely for multi cluster support.
*/
KubernetesClient getKubernetesClient();

@SuppressWarnings("unchecked")
@Override
default Class<R> getResourceClass() {
Expand All @@ -86,17 +93,20 @@ class DefaultInformerEventSourceConfiguration<R extends HasMetadata> extends
private final PrimaryToSecondaryMapper<?> primaryToSecondaryMapper;
private final SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
private final GroupVersionKind groupVersionKind;
private final KubernetesClient kubernetesClient;

protected DefaultInformerEventSourceConfiguration(
Class<R> resourceClass,
GroupVersionKind groupVersionKind,
PrimaryToSecondaryMapper<?> primaryToSecondaryMapper,
SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper,
InformerConfiguration<R> informerConfig) {
InformerConfiguration<R> informerConfig,
KubernetesClient kubernetesClient) {
super(resourceClass, informerConfig);
this.groupVersionKind = groupVersionKind;
this.primaryToSecondaryMapper = primaryToSecondaryMapper;
this.secondaryToPrimaryMapper = secondaryToPrimaryMapper;
this.kubernetesClient = kubernetesClient;
}

@Override
Expand All @@ -120,6 +130,11 @@ public Optional<GroupVersionKind> getGroupVersionKind() {
return Optional.ofNullable(groupVersionKind);
}

@Override
public KubernetesClient getKubernetesClient() {
return kubernetesClient;
}

public boolean inheritsNamespacesFromController() {
return InformerEventSourceConfiguration.inheritsNamespacesFromController(getNamespaces());
}
Expand All @@ -145,6 +160,7 @@ class Builder<R extends HasMetadata> {
private String name;
private PrimaryToSecondaryMapper<?> primaryToSecondaryMapper;
private SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
private KubernetesClient kubernetesClient;

private Builder(Class<R> resourceClass,
Class<? extends HasMetadata> primaryResourceClass) {
Expand Down Expand Up @@ -189,6 +205,16 @@ public Builder<R> withSecondaryToPrimaryMapper(
return this;
}

/**
* Use this is case want to create an InformerEventSource that handles resources from different
* cluster.
*/
public Builder<R> withKubernetesClient(
KubernetesClient kubernetesClient) {
this.kubernetesClient = kubernetesClient;
return this;
}

public String getName() {
return name;
}
Expand Down Expand Up @@ -229,7 +255,7 @@ public InformerEventSourceConfiguration<R> build() {
Objects.requireNonNullElse(secondaryToPrimaryMapper,
Mappers.fromOwnerReferences(HasMetadata.getApiVersion(primaryResourceClass),
HasMetadata.getKind(primaryResourceClass), false)),
config.buildForInformerEventSource());
config.buildForInformerEventSource(), kubernetesClient);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public class InformerEventSource<R extends HasMetadata, P extends HasMetadata>

public InformerEventSource(
InformerEventSourceConfiguration<R> configuration, EventSourceContext<P> context) {
this(configuration, context.getClient(),
this(configuration,
configuration.getKubernetesClient() != null ? configuration.getKubernetesClient()
: context.getClient(),
context.getControllerConfiguration().getConfigurationService()
.parseResourceVersionsForEventFilteringAndCaching());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.javaoperatorsdk.operator.baseapi.informerremotecluster;

import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.ShortNames;
import io.fabric8.kubernetes.model.annotation.Version;

@Group("sample.javaoperatorsdk")
@Version("v1")
@ShortNames("irc")
public class InformerRemoteClusterCustomResource
extends CustomResource<Void, InformerRemoteClusterStatus> implements Namespaced {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.javaoperatorsdk.operator.baseapi.informerremotecluster;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.javaoperatorsdk.jenvtest.junit.EnableKubeAPIServer;
import io.javaoperatorsdk.operator.baseapi.labelselector.LabelSelectorTestReconciler;
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;

@EnableKubeAPIServer(apiServerFlags = {"--min-request-timeout", "1"})
public class InformerRemoteClusterIT {

@RegisterExtension
LocallyRunOperatorExtension extension =
LocallyRunOperatorExtension.builder().withReconciler(new LabelSelectorTestReconciler())
.build();

@Test
void testRemoteClusterInformer() {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.javaoperatorsdk.operator.baseapi.informerremotecluster;

import java.util.List;

import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
import io.javaoperatorsdk.operator.processing.event.source.EventSource;

@ControllerConfiguration
public class InformerRemoteClusterReconciler
implements Reconciler<InformerRemoteClusterCustomResource> {


@Override
public UpdateControl<InformerRemoteClusterCustomResource> reconcile(
InformerRemoteClusterCustomResource resource,
Context<InformerRemoteClusterCustomResource> context) throws Exception {



return UpdateControl.noUpdate();
}

@Override
public List<EventSource<?, InformerRemoteClusterCustomResource>> prepareEventSources(
EventSourceContext<InformerRemoteClusterCustomResource> context) {
return Reconciler.super.prepareEventSources(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.javaoperatorsdk.operator.baseapi.informerremotecluster;

public class InformerRemoteClusterStatus {



}

0 comments on commit 69bd6e6

Please sign in to comment.