Skip to content
Open
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
@@ -0,0 +1,30 @@
/*
* 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.finalizernossa;

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.Kind;
import io.fabric8.kubernetes.model.annotation.ShortNames;
import io.fabric8.kubernetes.model.annotation.Version;

@Group("sample.javaoperatorsdk")
@Version("v1")
@Kind("AddFinalizerNoSSACustomResource")
@ShortNames("afnossa")
public class AddFinalizerNoSSACustomResource extends CustomResource<Void, String>
implements Namespaced {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.finalizernossa;

import java.util.concurrent.TimeUnit;

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

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

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

class AddFinalizerNoSSAIT {

public static final String TEST_RESOURCE_NAME = "add-finalizer-no-ssa-test1";

@RegisterExtension
LocallyRunOperatorExtension operator =
LocallyRunOperatorExtension.builder()
.withConfigurationService(o -> o.withUseSSAToPatchPrimaryResource(false))
.withReconciler(new AddFinalizerNoSSAReconciler())
.build();

@Test
void addsFinalizerWithoutSSAAndRemovesItOnCleanup() {
var reconciler = operator.getReconcilerOfType(AddFinalizerNoSSAReconciler.class);

var testResource = createTestResource();
operator.create(testResource);

await("finalizer added")
.atMost(5, TimeUnit.SECONDS)
.untilAsserted(
() -> {
var actual = operator.get(AddFinalizerNoSSACustomResource.class, TEST_RESOURCE_NAME);
assertThat(actual).isNotNull();
assertThat(actual.getMetadata().getFinalizers()).hasSize(1);
Comment thread
csviri marked this conversation as resolved.
});

operator.delete(testResource);

await("resource deleted after finalizer removal")
.atMost(5, TimeUnit.SECONDS)
.until(
() -> operator.get(AddFinalizerNoSSACustomResource.class, TEST_RESOURCE_NAME) == null);

assertThat(reconciler.getNumberOfExecutions()).isEqualTo(1);
assertThat(reconciler.getNumberOfCleanupExecutions()).isEqualTo(1);
}

private AddFinalizerNoSSACustomResource createTestResource() {
AddFinalizerNoSSACustomResource cr = new AddFinalizerNoSSACustomResource();
cr.setMetadata(new ObjectMeta());
cr.getMetadata().setName(TEST_RESOURCE_NAME);
return cr;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.finalizernossa;

import java.util.concurrent.atomic.AtomicInteger;

import io.javaoperatorsdk.operator.api.reconciler.*;
import io.javaoperatorsdk.operator.support.TestExecutionInfoProvider;

@ControllerConfiguration
public class AddFinalizerNoSSAReconciler
implements Reconciler<AddFinalizerNoSSACustomResource>,
Cleaner<AddFinalizerNoSSACustomResource>,
TestExecutionInfoProvider {

private final AtomicInteger numberOfExecutions = new AtomicInteger(0);
private final AtomicInteger numberOfCleanupExecutions = new AtomicInteger(0);

@Override
public UpdateControl<AddFinalizerNoSSACustomResource> reconcile(
AddFinalizerNoSSACustomResource resource, Context<AddFinalizerNoSSACustomResource> context) {
numberOfExecutions.addAndGet(1);
return UpdateControl.noUpdate();
}

@Override
public DeleteControl cleanup(
AddFinalizerNoSSACustomResource resource, Context<AddFinalizerNoSSACustomResource> context) {
numberOfCleanupExecutions.addAndGet(1);
return DeleteControl.defaultDelete();
}

public int getNumberOfExecutions() {
return numberOfExecutions.get();
}

public int getNumberOfCleanupExecutions() {
return numberOfCleanupExecutions.get();
}
}
Loading