Skip to content
Draft
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 @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.javaoperatorsdk.operator.baseapi.expectation;
package io.javaoperatorsdk.operator.baseapi.expectation.onallevent;

import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.CustomResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.javaoperatorsdk.operator.baseapi.expectation;
package io.javaoperatorsdk.operator.baseapi.expectation.onallevent;

public class ExpectationCustomResourceStatus {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.javaoperatorsdk.operator.baseapi.expectation;
package io.javaoperatorsdk.operator.baseapi.expectation.onallevent;

import java.time.Duration;

Expand All @@ -23,7 +23,7 @@
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;

import static io.javaoperatorsdk.operator.baseapi.expectation.ExpectationReconciler.DEPLOYMENT_READY;
import static io.javaoperatorsdk.operator.baseapi.expectation.onallevent.ExpectationReconciler.DEPLOYMENT_READY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.javaoperatorsdk.operator.baseapi.expectation;
package io.javaoperatorsdk.operator.baseapi.expectation.onallevent;

import java.time.Duration;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Java Operator SDK Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.javaoperatorsdk.operator.baseapi.expectation.periodicclean;

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("pcecr")
public class PeriodicCleanerExpectationCustomResource
extends CustomResource<Void, PeriodicCleanerExpectationCustomResourceStatus>
implements Namespaced {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Java Operator SDK Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.javaoperatorsdk.operator.baseapi.expectation.periodicclean;

public class PeriodicCleanerExpectationCustomResourceStatus {

private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright Java Operator SDK Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.javaoperatorsdk.operator.baseapi.expectation.periodicclean;

import java.time.Duration;

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

import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;

import static io.javaoperatorsdk.operator.baseapi.expectation.periodicclean.PeriodicCleanerExpectationReconciler.DEPLOYMENT_READY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

/**
* Integration test showcasing PeriodicCleanerExpectationManager usage.
*
* <p>This test demonstrates the key benefits of PeriodicCleanerExpectationManager: 1. Works without
* requiring @ControllerConfiguration(triggerReconcilerOnAllEvents = true) 2. Automatically cleans
* up stale expectations periodically 3. Maintains the same expectation API and functionality as the
* regular ExpectationManager
*/
class PeriodicCleanerExpectationIT {

public static final String TEST_1 = "test1";
public static final String TEST_2 = "test2";

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

@Test
void testPeriodicCleanerExpectationBasicFlow() {
extension
.getReconcilerOfType(PeriodicCleanerExpectationReconciler.class)
.setTimeout(Duration.ofSeconds(30));
var res = testResource();
extension.create(res);

await()
.untilAsserted(
() -> {
var actual = extension.get(PeriodicCleanerExpectationCustomResource.class, TEST_1);
assertThat(actual.getStatus()).isNotNull();
assertThat(actual.getStatus().getMessage()).isEqualTo(DEPLOYMENT_READY);
});
}

@Test
void testPeriodicCleanerExpectationTimeouts() {
extension
.getReconcilerOfType(PeriodicCleanerExpectationReconciler.class)
.setTimeout(Duration.ofMillis(300));
var res = testResource();
extension.create(res);

await()
.untilAsserted(
() -> {
var actual = extension.get(PeriodicCleanerExpectationCustomResource.class, TEST_1);
assertThat(actual.getStatus()).isNotNull();
assertThat(actual.getStatus().getMessage()).isEqualTo(DEPLOYMENT_READY);
});
}

@Test
void demonstratesNoTriggerReconcilerOnAllEventsNeeded() {
// This test demonstrates that PeriodicCleanerExpectationManager works
// without @ControllerConfiguration(triggerReconcilerOnAllEvents = true)

// The PeriodicCleanerExpectationReconciler doesn't use triggerReconcilerOnAllEvents = true
// yet expectations still work properly due to the periodic cleanup functionality

var reconciler = extension.getReconcilerOfType(PeriodicCleanerExpectationReconciler.class);
reconciler.setTimeout(Duration.ofSeconds(30));

var res = testResource("no-trigger-test");
extension.create(res);

// Verify that expectations work even without triggerReconcilerOnAllEvents = true
await()
.untilAsserted(
() -> {
var actual =
extension.get(PeriodicCleanerExpectationCustomResource.class, "no-trigger-test");
assertThat(actual.getStatus()).isNotNull();
assertThat(actual.getStatus().getMessage()).isEqualTo(DEPLOYMENT_READY);
});
}

private PeriodicCleanerExpectationCustomResource testResource() {
return testResource(TEST_1);
}

private PeriodicCleanerExpectationCustomResource testResource(String name) {
var res = new PeriodicCleanerExpectationCustomResource();
res.setMetadata(new ObjectMetaBuilder().withName(name).build());
return res;
}
}
Loading