-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Attila Mészáros <[email protected]>
- Loading branch information
Showing
7 changed files
with
217 additions
and
18 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
66 changes: 66 additions & 0 deletions
66
operator-framework/src/test/java/io/javaoperatorsdk/operator/ClusterScopedResourceIT.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,66 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
import io.javaoperatorsdk.operator.sample.clusterscopedresource.ClusterScopedCustomResource; | ||
import io.javaoperatorsdk.operator.sample.clusterscopedresource.ClusterScopedCustomResourceReconciler; | ||
import io.javaoperatorsdk.operator.sample.clusterscopedresource.ClusterScopedCustomResourceSpec; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
class ClusterScopedResourceIT { | ||
|
||
public static final String TEST_NAME = "test1"; | ||
public static final String INITIAL_DATA = "initialData"; | ||
public static final String UPDATED_DATA = "updatedData"; | ||
@RegisterExtension | ||
LocallyRunOperatorExtension operator = | ||
LocallyRunOperatorExtension.builder() | ||
.withReconciler(new ClusterScopedCustomResourceReconciler()).build(); | ||
|
||
@Test | ||
void crudOperationOnClusterScopedCustomResource() { | ||
var resource = operator.create(testResource()); | ||
|
||
await().untilAsserted(() -> { | ||
var res = operator.get(ClusterScopedCustomResource.class, TEST_NAME); | ||
assertThat(res.getStatus()).isNotNull(); | ||
assertThat(res.getStatus().getCreated()).isTrue(); | ||
var cm = operator.get(ConfigMap.class, TEST_NAME); | ||
assertThat(cm).isNotNull(); | ||
assertThat(cm.getData().get(ClusterScopedCustomResourceReconciler.DATA_KEY)) | ||
.isEqualTo(INITIAL_DATA); | ||
}); | ||
|
||
resource.getSpec().setData(UPDATED_DATA); | ||
operator.replace(resource); | ||
await().untilAsserted(() -> { | ||
var cm = operator.get(ConfigMap.class, TEST_NAME); | ||
assertThat(cm).isNotNull(); | ||
assertThat(cm.getData().get(ClusterScopedCustomResourceReconciler.DATA_KEY)) | ||
.isEqualTo(UPDATED_DATA); | ||
}); | ||
|
||
operator.delete(resource); | ||
await().untilAsserted(() -> assertThat(operator.get(ConfigMap.class, TEST_NAME)).isNull()); | ||
} | ||
|
||
|
||
ClusterScopedCustomResource testResource() { | ||
var res = new ClusterScopedCustomResource(); | ||
res.setMetadata(new ObjectMetaBuilder() | ||
.withName(TEST_NAME) | ||
.build()); | ||
res.setSpec(new ClusterScopedCustomResourceSpec()); | ||
res.getSpec().setTargetNamespace(operator.getNamespace()); | ||
res.getSpec().setData(INITIAL_DATA); | ||
|
||
return res; | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...io/javaoperatorsdk/operator/sample/clusterscopedresource/ClusterScopedCustomResource.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,15 @@ | ||
package io.javaoperatorsdk.operator.sample.clusterscopedresource; | ||
|
||
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("csc") | ||
public class ClusterScopedCustomResource | ||
extends CustomResource<ClusterScopedCustomResourceSpec, ClusterScopedCustomResourceStatus> { | ||
|
||
|
||
} |
66 changes: 66 additions & 0 deletions
66
...ratorsdk/operator/sample/clusterscopedresource/ClusterScopedCustomResourceReconciler.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,66 @@ | ||
package io.javaoperatorsdk.operator.sample.clusterscopedresource; | ||
|
||
import java.util.Map; | ||
|
||
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.fabric8.kubernetes.client.dsl.Resource; | ||
import io.javaoperatorsdk.operator.api.reconciler.*; | ||
import io.javaoperatorsdk.operator.junit.KubernetesClientAware; | ||
|
||
@ControllerConfiguration | ||
public class ClusterScopedCustomResourceReconciler | ||
implements Reconciler<ClusterScopedCustomResource>, Cleaner<ClusterScopedCustomResource>, | ||
KubernetesClientAware { | ||
|
||
public static final String DATA_KEY = "data-key"; | ||
|
||
private KubernetesClient client; | ||
|
||
@Override | ||
public UpdateControl<ClusterScopedCustomResource> reconcile( | ||
ClusterScopedCustomResource resource, Context<ClusterScopedCustomResource> context) { | ||
|
||
final var desired = desired(resource); | ||
getConfigMapResource(desired).createOrReplace(desired); | ||
|
||
resource.setStatus(new ClusterScopedCustomResourceStatus()); | ||
resource.getStatus().setCreated(true); | ||
return UpdateControl.patchStatus(resource); | ||
} | ||
|
||
private Resource<ConfigMap> getConfigMapResource(ConfigMap desired) { | ||
return client.configMaps().inNamespace(desired.getMetadata().getNamespace()) | ||
.withName(desired.getMetadata().getName()); | ||
} | ||
|
||
private ConfigMap desired(ClusterScopedCustomResource resource) { | ||
return new ConfigMapBuilder() | ||
.withMetadata(new ObjectMetaBuilder() | ||
.withName(resource.getMetadata().getName()) | ||
.withNamespace(resource.getSpec().getTargetNamespace()) | ||
.build()) | ||
.withData(Map.of(DATA_KEY, resource.getSpec().getData())) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public KubernetesClient getKubernetesClient() { | ||
return client; | ||
} | ||
|
||
@Override | ||
public void setKubernetesClient(KubernetesClient kubernetesClient) { | ||
this.client = kubernetesClient; | ||
} | ||
|
||
@Override | ||
public DeleteControl cleanup(ClusterScopedCustomResource resource, | ||
Context<ClusterScopedCustomResource> context) { | ||
final var desired = desired(resource); | ||
getConfigMapResource(desired).delete(); | ||
return DeleteControl.defaultDelete(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...avaoperatorsdk/operator/sample/clusterscopedresource/ClusterScopedCustomResourceSpec.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,25 @@ | ||
package io.javaoperatorsdk.operator.sample.clusterscopedresource; | ||
|
||
public class ClusterScopedCustomResourceSpec { | ||
|
||
private String data; | ||
private String targetNamespace; | ||
|
||
public String getData() { | ||
return data; | ||
} | ||
|
||
public ClusterScopedCustomResourceSpec setData(String data) { | ||
this.data = data; | ||
return this; | ||
} | ||
|
||
public String getTargetNamespace() { | ||
return targetNamespace; | ||
} | ||
|
||
public ClusterScopedCustomResourceSpec setTargetNamespace(String targetNamespace) { | ||
this.targetNamespace = targetNamespace; | ||
return this; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...aoperatorsdk/operator/sample/clusterscopedresource/ClusterScopedCustomResourceStatus.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,15 @@ | ||
package io.javaoperatorsdk.operator.sample.clusterscopedresource; | ||
|
||
public class ClusterScopedCustomResourceStatus { | ||
|
||
private Boolean created; | ||
|
||
public Boolean getCreated() { | ||
return created; | ||
} | ||
|
||
public ClusterScopedCustomResourceStatus setCreated(Boolean created) { | ||
this.created = created; | ||
return this; | ||
} | ||
} |