-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support to handle different cluster for InformerEventSource (#2499
) Signed-off-by: Attila Mészáros <[email protected]> Signed-off-by: Chris Laprun <[email protected]> Co-authored-by: Chris Laprun <[email protected]>
- Loading branch information
Showing
8 changed files
with
241 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...eratorsdk/operator/baseapi/informerremotecluster/InformerRemoteClusterCustomResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
88 changes: 88 additions & 0 deletions
88
...va/io/javaoperatorsdk/operator/baseapi/informerremotecluster/InformerRemoteClusterIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.javaoperatorsdk.operator.baseapi.informerremotecluster; | ||
|
||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubeapitest.junit.EnableKubeAPIServer; | ||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers; | ||
|
||
import static io.javaoperatorsdk.operator.baseapi.informerremotecluster.InformerRemoteClusterReconciler.DATA_KEY; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
@EnableKubeAPIServer | ||
class InformerRemoteClusterIT { | ||
|
||
public static final String NAME = "test1"; | ||
public static final String CONFIG_MAP_NAME = "testcm"; | ||
public static final String INITIAL_VALUE = "initial_value"; | ||
public static final String CHANGED_VALUE = "changed_value"; | ||
public static final String CM_NAMESPACE = "default"; | ||
|
||
// injected by Kube API Test. Client for another cluster. | ||
static KubernetesClient kubernetesClient; | ||
|
||
@RegisterExtension | ||
LocallyRunOperatorExtension extension = | ||
LocallyRunOperatorExtension.builder() | ||
.withReconciler(new InformerRemoteClusterReconciler(kubernetesClient)) | ||
.build(); | ||
|
||
@Test | ||
void testRemoteClusterInformer() { | ||
var r = extension.create(testCustomResource()); | ||
|
||
var cm = kubernetesClient.configMaps() | ||
.resource(remoteConfigMap(r.getMetadata().getName(), | ||
r.getMetadata().getNamespace())) | ||
.create(); | ||
|
||
// config map does not exist on the primary resource cluster | ||
assertThat(extension.getKubernetesClient().configMaps() | ||
.inNamespace(CM_NAMESPACE) | ||
.withName(CONFIG_MAP_NAME).get()).isNull(); | ||
|
||
await().untilAsserted(() -> { | ||
var cr = extension.get(InformerRemoteClusterCustomResource.class, NAME); | ||
assertThat(cr.getStatus()).isNotNull(); | ||
assertThat(cr.getStatus().getRemoteConfigMapMessage()).isEqualTo(INITIAL_VALUE); | ||
}); | ||
|
||
cm.getData().put(DATA_KEY, CHANGED_VALUE); | ||
kubernetesClient.configMaps().resource(cm).update(); | ||
|
||
await().untilAsserted(() -> { | ||
var cr = extension.get(InformerRemoteClusterCustomResource.class, NAME); | ||
assertThat(cr.getStatus().getRemoteConfigMapMessage()).isEqualTo(CHANGED_VALUE); | ||
}); | ||
} | ||
|
||
InformerRemoteClusterCustomResource testCustomResource() { | ||
var res = new InformerRemoteClusterCustomResource(); | ||
res.setMetadata(new ObjectMetaBuilder() | ||
.withName(NAME) | ||
.build()); | ||
return res; | ||
} | ||
|
||
ConfigMap remoteConfigMap(String ownerName, String ownerNamespace) { | ||
return new ConfigMapBuilder() | ||
.withMetadata(new ObjectMetaBuilder() | ||
.withName(CONFIG_MAP_NAME) | ||
.withNamespace(CM_NAMESPACE) | ||
.withAnnotations(Map.of( | ||
Mappers.DEFAULT_ANNOTATION_FOR_NAME, ownerName, | ||
Mappers.DEFAULT_ANNOTATION_FOR_NAMESPACE, ownerNamespace)) | ||
.build()) | ||
.withData(Map.of(DATA_KEY, INITIAL_VALUE)) | ||
.build(); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
...vaoperatorsdk/operator/baseapi/informerremotecluster/InformerRemoteClusterReconciler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.javaoperatorsdk.operator.baseapi.informerremotecluster; | ||
|
||
import java.util.List; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
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.config.informer.InformerEventSourceConfiguration; | ||
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; | ||
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; | ||
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers; | ||
|
||
@ControllerConfiguration | ||
public class InformerRemoteClusterReconciler | ||
implements Reconciler<InformerRemoteClusterCustomResource> { | ||
|
||
public static final String DATA_KEY = "key"; | ||
|
||
private final KubernetesClient remoteClient; | ||
|
||
public InformerRemoteClusterReconciler(KubernetesClient remoteClient) { | ||
this.remoteClient = remoteClient; | ||
} | ||
|
||
@Override | ||
public UpdateControl<InformerRemoteClusterCustomResource> reconcile( | ||
InformerRemoteClusterCustomResource resource, | ||
Context<InformerRemoteClusterCustomResource> context) throws Exception { | ||
|
||
return context.getSecondaryResource(ConfigMap.class).map(cm -> { | ||
var r = new InformerRemoteClusterCustomResource(); | ||
r.setMetadata(new ObjectMetaBuilder() | ||
.withName(resource.getMetadata().getName()) | ||
.withNamespace(resource.getMetadata().getNamespace()) | ||
.build()); | ||
r.setStatus(new InformerRemoteClusterStatus()); | ||
r.getStatus().setRemoteConfigMapMessage(cm.getData().get(DATA_KEY)); | ||
return UpdateControl.patchStatus(r); | ||
}).orElseGet(UpdateControl::noUpdate); | ||
} | ||
|
||
@Override | ||
public List<EventSource<?, InformerRemoteClusterCustomResource>> prepareEventSources( | ||
EventSourceContext<InformerRemoteClusterCustomResource> context) { | ||
|
||
var es = new InformerEventSource<>(InformerEventSourceConfiguration | ||
.from(ConfigMap.class, InformerRemoteClusterCustomResource.class) | ||
// owner references do not work cross cluster, using | ||
// annotations here to reference primary resource | ||
.withSecondaryToPrimaryMapper(Mappers.fromDefaultAnnotations()) | ||
// setting remote client for informer | ||
.withKubernetesClient(remoteClient) | ||
.withInformerConfiguration( | ||
InformerConfiguration.Builder::withWatchAllNamespaces) | ||
.build(), context); | ||
|
||
return List.of(es); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...o/javaoperatorsdk/operator/baseapi/informerremotecluster/InformerRemoteClusterStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.javaoperatorsdk.operator.baseapi.informerremotecluster; | ||
|
||
public class InformerRemoteClusterStatus { | ||
|
||
private String remoteConfigMapMessage; | ||
|
||
public String getRemoteConfigMapMessage() { | ||
return remoteConfigMapMessage; | ||
} | ||
|
||
public void setRemoteConfigMapMessage(String remoteConfigMapMessage) { | ||
this.remoteConfigMapMessage = remoteConfigMapMessage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters