-
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.
feat: showcase sample for matcher with ssa in DR (#1964)
* feat: shocase sample for matcher with ssa in DR * docs update
- Loading branch information
Showing
7 changed files
with
175 additions
and
2 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
53 changes: 53 additions & 0 deletions
53
operator-framework/src/test/java/io/javaoperatorsdk/operator/SSAWithLegacyMatcherIT.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,53 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.api.model.Service; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
import io.javaoperatorsdk.operator.sample.ssalegacymatcher.SSALegacyMatcherCustomResource; | ||
import io.javaoperatorsdk.operator.sample.ssalegacymatcher.SSALegacyMatcherReconciler; | ||
import io.javaoperatorsdk.operator.sample.ssalegacymatcher.SSALegacyMatcherSpec; | ||
import io.javaoperatorsdk.operator.sample.ssalegacymatcher.ServiceDependentResource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class SSAWithLegacyMatcherIT { | ||
|
||
public static final String TEST_RESOURCE_NAME = "test1"; | ||
|
||
@RegisterExtension | ||
LocallyRunOperatorExtension extension = | ||
LocallyRunOperatorExtension.builder().withReconciler(new SSALegacyMatcherReconciler()) | ||
.build(); | ||
|
||
@Test | ||
void matchesDependentWithLegacyMatcher() { | ||
var resource = extension.create(testResource()); | ||
|
||
await().untilAsserted(() -> { | ||
var service = extension.get(Service.class, TEST_RESOURCE_NAME); | ||
assertThat(service).isNotNull(); | ||
assertThat(ServiceDependentResource.createUpdateCount.get()).isEqualTo(1); | ||
}); | ||
|
||
resource.getSpec().setValue("other_value"); | ||
|
||
await().untilAsserted(() -> { | ||
assertThat(ServiceDependentResource.createUpdateCount.get()).isEqualTo(1); | ||
}); | ||
} | ||
|
||
SSALegacyMatcherCustomResource testResource() { | ||
SSALegacyMatcherCustomResource res = new SSALegacyMatcherCustomResource(); | ||
res.setMetadata(new ObjectMetaBuilder() | ||
.withName(TEST_RESOURCE_NAME) | ||
.build()); | ||
res.setSpec(new SSALegacyMatcherSpec()); | ||
res.getSpec().setValue("initial-value"); | ||
return res; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...a/io/javaoperatorsdk/operator/sample/ssalegacymatcher/SSALegacyMatcherCustomResource.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,16 @@ | ||
package io.javaoperatorsdk.operator.sample.ssalegacymatcher; | ||
|
||
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("slm") | ||
public class SSALegacyMatcherCustomResource | ||
extends CustomResource<SSALegacyMatcherSpec, Void> | ||
implements Namespaced { | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
.../java/io/javaoperatorsdk/operator/sample/ssalegacymatcher/SSALegacyMatcherReconciler.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,28 @@ | ||
package io.javaoperatorsdk.operator.sample.ssalegacymatcher; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; | ||
import io.javaoperatorsdk.operator.api.reconciler.Reconciler; | ||
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent; | ||
|
||
@ControllerConfiguration(dependents = {@Dependent(type = ServiceDependentResource.class)}) | ||
public class SSALegacyMatcherReconciler | ||
implements Reconciler<SSALegacyMatcherCustomResource> { | ||
|
||
private final AtomicInteger numberOfExecutions = new AtomicInteger(0); | ||
|
||
@Override | ||
public UpdateControl<SSALegacyMatcherCustomResource> reconcile( | ||
SSALegacyMatcherCustomResource resource, | ||
Context<SSALegacyMatcherCustomResource> context) { | ||
numberOfExecutions.addAndGet(1); | ||
return UpdateControl.noUpdate(); | ||
} | ||
|
||
public int getNumberOfExecutions() { | ||
return numberOfExecutions.get(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...c/test/java/io/javaoperatorsdk/operator/sample/ssalegacymatcher/SSALegacyMatcherSpec.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.ssalegacymatcher; | ||
|
||
public class SSALegacyMatcherSpec { | ||
|
||
private String value; | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public SSALegacyMatcherSpec setValue(String value) { | ||
this.value = value; | ||
return this; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...st/java/io/javaoperatorsdk/operator/sample/ssalegacymatcher/ServiceDependentResource.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,61 @@ | ||
package io.javaoperatorsdk.operator.sample.ssalegacymatcher; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.fabric8.kubernetes.api.model.Service; | ||
import io.javaoperatorsdk.operator.SSAWithLegacyMatcherIT; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.GenericKubernetesResourceMatcher; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent; | ||
|
||
import static io.javaoperatorsdk.operator.ReconcilerUtils.loadYaml; | ||
|
||
@KubernetesDependent | ||
public class ServiceDependentResource | ||
extends CRUDKubernetesDependentResource<Service, SSALegacyMatcherCustomResource> { | ||
|
||
public static AtomicInteger createUpdateCount = new AtomicInteger(0); | ||
|
||
public ServiceDependentResource() { | ||
super(Service.class); | ||
} | ||
|
||
@Override | ||
protected Service desired(SSALegacyMatcherCustomResource primary, | ||
Context<SSALegacyMatcherCustomResource> context) { | ||
|
||
Service service = loadYaml(Service.class, SSAWithLegacyMatcherIT.class, "service.yaml"); | ||
service.getMetadata().setName(primary.getMetadata().getName()); | ||
service.getMetadata().setNamespace(primary.getMetadata().getNamespace()); | ||
Map<String, String> labels = new HashMap<>(); | ||
labels.put("app", "deployment-name"); | ||
service.getSpec().setSelector(labels); | ||
return service; | ||
} | ||
|
||
@Override | ||
public Result<Service> match(Service actualResource, SSALegacyMatcherCustomResource primary, | ||
Context<SSALegacyMatcherCustomResource> context) { | ||
return GenericKubernetesResourceMatcher.match(this, actualResource, primary, context, | ||
true, false, false); | ||
} | ||
|
||
// override just to check the exec count | ||
@Override | ||
public Service update(Service actual, Service target, SSALegacyMatcherCustomResource primary, | ||
Context<SSALegacyMatcherCustomResource> context) { | ||
createUpdateCount.addAndGet(1); | ||
return super.update(actual, target, primary, context); | ||
} | ||
|
||
// override just to check the exec count | ||
@Override | ||
public Service create(Service target, SSALegacyMatcherCustomResource primary, | ||
Context<SSALegacyMatcherCustomResource> context) { | ||
createUpdateCount.addAndGet(1); | ||
return super.create(target, primary, context); | ||
} | ||
} |