Skip to content

fix: update deprecated client usage and minor improvements #1707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 10, 2023
Merged
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 @@ -23,7 +23,8 @@ public static final <T, B> B newBuilder(Class<B> builderType, T item) {
}
}

public static final <T, B> Class<T> builderTargetType(Class<B> builderType) {
@SuppressWarnings("unchecked")
public static <T, B> Class<T> builderTargetType(Class<B> builderType) {
try {
Method method = builderType.getDeclaredMethod("build");
return (Class<T>) method.getReturnType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ResolvedControllerConfiguration<P extends HasMetadata>
private final String associatedReconcilerClassName;
private final Retry retry;
private final RateLimiter rateLimiter;
private final Optional<Duration> maxReconciliationInterval;
private final Duration maxReconciliationInterval;
private final String finalizer;
private final Map<DependentResourceSpec, Object> configurations;

Expand Down Expand Up @@ -86,7 +86,7 @@ protected ResolvedControllerConfiguration(Class<P> resourceClass, String name,
this.associatedReconcilerClassName = associatedReconcilerClassName;
this.retry = ensureRetry(retry);
this.rateLimiter = ensureRateLimiter(rateLimiter);
this.maxReconciliationInterval = Optional.ofNullable(maxReconciliationInterval);
this.maxReconciliationInterval = maxReconciliationInterval;
this.configurations = configurations != null ? configurations : Collections.emptyMap();

this.finalizer =
Expand Down Expand Up @@ -142,7 +142,7 @@ protected void setDependentResources(List<DependentResourceSpec> dependentResour

@Override
public Optional<Duration> maxReconciliationInterval() {
return maxReconciliationInterval;
return Optional.ofNullable(maxReconciliationInterval);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.javaoperatorsdk.operator.api.reconciler.dependent;

import io.javaoperatorsdk.operator.api.reconciler.Constants;
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;

import static io.javaoperatorsdk.operator.api.reconciler.Constants.NO_VALUE_SET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class EventSourceNotFoundException extends OperatorException {

private String eventSourceName;
private final String eventSourceName;

public EventSourceNotFoundException(String eventSourceName) {
this.eventSourceName = eventSourceName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@SuppressWarnings("rawtypes")
public class ControllerHealthInfo {

private EventSourceManager<?> eventSourceManager;
private final EventSourceManager<?> eventSourceManager;

public ControllerHealthInfo(EventSourceManager eventSourceManager) {
this.eventSourceManager = eventSourceManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import io.javaoperatorsdk.operator.api.reconciler.Constants;
import io.javaoperatorsdk.operator.api.reconciler.ResourceDiscriminator;
import io.javaoperatorsdk.operator.processing.event.source.filter.*;
import io.javaoperatorsdk.operator.processing.event.source.filter.GenericFilter;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnDeleteFilter;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;

import static io.javaoperatorsdk.operator.api.reconciler.Constants.NO_VALUE_SET;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ protected synchronized void handleNodeExecutionFinish(
}
}

@SuppressWarnings("unchecked")
protected <R> boolean isConditionMet(Optional<Condition<R, P>> condition,
DependentResource<R, P> dependentResource) {
if (condition.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package io.javaoperatorsdk.operator.processing.event;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
public abstract class ExternalResourceCachingEventSource<R, P extends HasMetadata>
extends AbstractResourceEventSource<R, P> implements RecentOperationCacheFiller<R> {

private static Logger log = LoggerFactory.getLogger(ExternalResourceCachingEventSource.class);
private static final Logger log =
LoggerFactory.getLogger(ExternalResourceCachingEventSource.class);

protected final CacheKeyMapper<R> cacheKeyMapper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

class DefaultPrimaryToSecondaryIndex<R extends HasMetadata> implements PrimaryToSecondaryIndex<R> {

private SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
private Map<ResourceID, Set<ResourceID>> index = new HashMap<>();
private final SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
private final Map<ResourceID, Set<ResourceID>> index = new HashMap<>();

public DefaultPrimaryToSecondaryIndex(SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper) {
this.secondaryToPrimaryMapper = secondaryToPrimaryMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class NOOPPrimaryToSecondaryIndex<R extends HasMetadata>
@SuppressWarnings("rawtypes")
private static final NOOPPrimaryToSecondaryIndex instance = new NOOPPrimaryToSecondaryIndex();

@SuppressWarnings("unchecked")
public static <T extends HasMetadata> NOOPPrimaryToSecondaryIndex<T> getInstance() {
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

public class TransformingItemStore<R extends HasMetadata> implements ItemStore<R> {

private Function<R, String> keyFunction;
private UnaryOperator<R> transformationFunction;
private ConcurrentHashMap<String, R> store = new ConcurrentHashMap<>();
private final Function<R, String> keyFunction;
private final UnaryOperator<R> transformationFunction;
private final ConcurrentHashMap<String, R> store = new ConcurrentHashMap<>();

public TransformingItemStore(UnaryOperator<R> transformationFunction) {
this(Cache::metaNamespaceKeyFunc, transformationFunction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@

class LeaderElectionManagerTest {

private ControllerManager controllerManager;
private KubernetesClient kubernetesClient;
private LeaderElectionManager leaderElectionManager;

@BeforeEach
void setUp() {
controllerManager = mock(ControllerManager.class);
ControllerManager controllerManager = mock(ControllerManager.class);
kubernetesClient = mock(KubernetesClient.class);
leaderElectionManager = new LeaderElectionManager(controllerManager);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
import io.fabric8.kubernetes.client.informers.cache.Indexer;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doAnswer;

public class MockKubernetesClient {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private static class Tomcat extends CustomResource<TomcatSpec, Void> implements

}

private class TomcatSpec {
private static class TomcatSpec {
private Integer replicas;

public Integer getReplicas() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public ConfigMap update(ConfigMap actual, ConfigMap desired, TestCustomResource
}

@Override
@SuppressWarnings("unchecked")
public Matcher.Result<ConfigMap> match(ConfigMap actualResource, TestCustomResource primary,
Context<TestCustomResource> context) {
var result = mock(Matcher.Result.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ExecutionAssert deleted(DependentResource<?, ?>... dependentResources) {
}

private List<DependentResource> getActualDependentResources() {
return actual.stream().map(rr -> rr.getDependentResource()).collect(Collectors.toList());
return actual.stream().map(ReconcileRecord::getDependentResource).collect(Collectors.toList());
}

private Optional<ReconcileRecord> getReconcileRecordFor(DependentResource dependentResource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void isCleanerIfHasDeleter() {
assertThat(managedWorkflow(spec).hasCleaner()).isTrue();
}

@SuppressWarnings("unchecked")
ManagedWorkflow managedWorkflow(DependentResourceSpec... specs) {
final var configuration = mock(ControllerConfiguration.class);
final var specList = List.of(specs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class ReconcileRecord {

private DependentResource<?, ?> dependentResource;
private final DependentResource<?, ?> dependentResource;
private final boolean deleted;

public ReconcileRecord(DependentResource<?, ?> dependentResource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void setUpSource(S source) {
setUpSource(source, true);
}

@SuppressWarnings("unchecked")
public void setUpSource(S source, boolean start) {
setUpSource(source, (T) mock(EventHandler.class), start);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void genericFilteringEvents() {
public static class TestExternalCachingEventSource
extends ExternalResourceCachingEventSource<SampleExternalResource, HasMetadata> {
public TestExternalCachingEventSource() {
super(SampleExternalResource.class, (r) -> r.getName());
super(SampleExternalResource.class, SampleExternalResource::getName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import static io.javaoperatorsdk.operator.TestUtils.markForDeletion;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

class InternalEventFiltersTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class CachingInboundEventSourceTest extends
class CachingInboundEventSourceTest extends
AbstractEventSourceTestBase<CachingInboundEventSource<SampleExternalResource, TestCustomResource>, EventHandler> {

public static final int PERIOD = 150;
private CachingInboundEventSource.ResourceFetcher<SampleExternalResource, TestCustomResource> supplier =
@SuppressWarnings("unchecked")
private final CachingInboundEventSource.ResourceFetcher<SampleExternalResource, TestCustomResource> supplier =
mock(
CachingInboundEventSource.ResourceFetcher.class);
private TestCustomResource testCustomResource = TestUtils.testCustomResource();
private CacheKeyMapper<SampleExternalResource> cacheKeyMapper =
private final TestCustomResource testCustomResource = TestUtils.testCustomResource();
private final CacheKeyMapper<SampleExternalResource> cacheKeyMapper =
r -> r.getName() + "#" + r.getValue();

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class EventRecorderTest {

public static final String RESOURCE_VERSION = "0";
public static final String RESOURCE_VERSION1 = "1";
EventRecorder<ConfigMap> eventRecorder = new EventRecorder();

EventRecorder<ConfigMap> eventRecorder = new EventRecorder<>();

ConfigMap testConfigMap = testConfigMap(RESOURCE_VERSION);
ConfigMap testConfigMap2 = testConfigMap(RESOURCE_VERSION1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

class PrimaryToSecondaryIndexTest {

private SecondaryToPrimaryMapper<ConfigMap> secondaryToPrimaryMapperMock =
@SuppressWarnings("unchecked")
private final SecondaryToPrimaryMapper<ConfigMap> secondaryToPrimaryMapperMock =
mock(SecondaryToPrimaryMapper.class);
private PrimaryToSecondaryIndex<ConfigMap> primaryToSecondaryIndex =
private final PrimaryToSecondaryIndex<ConfigMap> primaryToSecondaryIndex =
new DefaultPrimaryToSecondaryIndex<>(secondaryToPrimaryMapperMock);

private ResourceID primaryID1 = new ResourceID("id1", "default");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
class TemporaryResourceCacheTest {

public static final String RESOURCE_VERSION = "1";
private InformerEventSource<ConfigMap, ?> informerEventSource = mock(InformerEventSource.class);
private TemporaryResourceCache<ConfigMap> temporaryResourceCache =
@SuppressWarnings("unchecked")
private final InformerEventSource<ConfigMap, ?> informerEventSource =
mock(InformerEventSource.class);
private final TemporaryResourceCache<ConfigMap> temporaryResourceCache =
new TemporaryResourceCache<>(informerEventSource);

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class PerResourcePollingEventSourceTest extends
AbstractEventSourceTestBase<PerResourcePollingEventSource<SampleExternalResource, TestCustomResource>, EventHandler> {

public static final int PERIOD = 150;
private PerResourcePollingEventSource.ResourceFetcher<SampleExternalResource, TestCustomResource> supplier =
@SuppressWarnings("unchecked")
private final PerResourcePollingEventSource.ResourceFetcher<SampleExternalResource, TestCustomResource> supplier =
mock(PerResourcePollingEventSource.ResourceFetcher.class);
private Cache<TestCustomResource> resourceCache = mock(Cache.class);
private TestCustomResource testCustomResource = TestUtils.testCustomResource();
@SuppressWarnings("unchecked")
private final Cache<TestCustomResource> resourceCache = mock(Cache.class);
private final TestCustomResource testCustomResource = TestUtils.testCustomResource();

@BeforeEach
public void setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class PollingEventSourceTest
public static final int DEFAULT_WAIT_PERIOD = 100;
public static final long POLL_PERIOD = 30L;

private PollingEventSource.GenericResourceFetcher<SampleExternalResource> resourceFetcher =
@SuppressWarnings("unchecked")
private final PollingEventSource.GenericResourceFetcher<SampleExternalResource> resourceFetcher =
mock(PollingEventSource.GenericResourceFetcher.class);
private final PollingEventSource<SampleExternalResource, HasMetadata> pollingEventSource =
new PollingEventSource<>(resourceFetcher, POLL_PERIOD, SampleExternalResource.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.api.reconciler.*;
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;

@ControllerConfiguration(generationAwareEventProcessing = false)
public class TestCustomReconciler
Expand Down Expand Up @@ -78,8 +77,8 @@ public UpdateControl<TestCustomResource> reconcile(
kubernetesClient
.configMaps()
.inNamespace(resource.getMetadata().getNamespace())
.withName(existingConfigMap.getMetadata().getName())
.createOrReplace(existingConfigMap);
.resource(existingConfigMap)
.createOrReplace();
} else {
Map<String, String> labels = new HashMap<>();
labels.put("managedBy", TestCustomReconciler.class.getSimpleName());
Expand All @@ -96,7 +95,8 @@ public UpdateControl<TestCustomResource> reconcile(
kubernetesClient
.configMaps()
.inNamespace(resource.getMetadata().getNamespace())
.createOrReplace(newConfigMap);
.resource(newConfigMap)
.createOrReplace();
}
if (updateStatus) {
if (resource.getStatus() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ private ChangeNamespaceTestCustomResource createResourceInAdditionalNamespace(St
var res = customResource(name);
return client().resources(ChangeNamespaceTestCustomResource.class)
.inNamespace(ADDITIONAL_TEST_NAMESPACE)
.create(res);
.resource(res)
.create();
}

private KubernetesClient client() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void manyResourcesGetCreatedUpdatedAndDeleted() throws InterruptedException {
log.info("Creating {} new resources", NUMBER_OF_RESOURCES_CREATED);
for (int i = 0; i < NUMBER_OF_RESOURCES_CREATED; i++) {
TestCustomResource tcr = TestUtils.testCustomResourceWithPrefix(String.valueOf(i));
operator.resources(TestCustomResource.class).create(tcr);
operator.resources(TestCustomResource.class).resource(tcr).create();
}

await()
Expand All @@ -57,8 +57,8 @@ void manyResourcesGetCreatedUpdatedAndDeleted() throws InterruptedException {
operator.get(TestCustomResource.class,
TestUtils.TEST_CUSTOM_RESOURCE_PREFIX + i);
tcr.getSpec().setValue(i + UPDATED_SUFFIX);
operator.resources(TestCustomResource.class)
.createOrReplace(tcr);
operator.resources(TestCustomResource.class).resource(tcr)
.createOrReplace();
}
// sleep for a short time to make variability to the test, so some updates are not
// executed before delete
Expand All @@ -67,7 +67,7 @@ void manyResourcesGetCreatedUpdatedAndDeleted() throws InterruptedException {
log.info("Deleting {} resources", NUMBER_OF_RESOURCES_DELETED);
for (int i = 0; i < NUMBER_OF_RESOURCES_DELETED; i++) {
TestCustomResource tcr = TestUtils.testCustomResourceWithPrefix(String.valueOf(i));
operator.resources(TestCustomResource.class).delete(tcr);
operator.resources(TestCustomResource.class).resource(tcr).delete();
}

await()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ExternalStateBulkIT {
public static final int INCREASED_BULK_SIZE = 4;
public static final int DECREASED_BULK_SIZE = 2;

private ExternalIDGenServiceMock externalService = ExternalIDGenServiceMock.getInstance();
private final ExternalIDGenServiceMock externalService = ExternalIDGenServiceMock.getInstance();

@RegisterExtension
LocallyRunOperatorExtension operator =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ExternalStateIT {
public static final String INITIAL_TEST_DATA = "initialTestData";
public static final String UPDATED_DATA = "updatedData";

private ExternalIDGenServiceMock externalService = ExternalIDGenServiceMock.getInstance();
private final ExternalIDGenServiceMock externalService = ExternalIDGenServiceMock.getInstance();

@RegisterExtension
LocallyRunOperatorExtension operator =
Expand Down
Loading