Skip to content

fix: remove CRD check #1056

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -65,8 +65,4 @@ protected boolean createIfNeeded() {
return true;
}

@Override
public boolean checkCRDAndValidateLocalModel() {
return Utils.shouldCheckCRDAndValidateLocalModel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,6 @@ default Config getClientConfiguration() {
*/
Version getVersion();

/**
* Whether the operator should query the CRD to make sure it's deployed and validate
* {@link CustomResource} implementations before attempting to register the associated
* reconcilers.
*
* <p>
* Note that this might require elevating the privileges associated with the operator to gain read
* access on the CRD resources.
*
* @return {@code true} if CRDs should be checked (default), {@code false} otherwise
*/
default boolean checkCRDAndValidateLocalModel() {
return true;
}

int DEFAULT_RECONCILIATION_THREADS_NUMBER = 5;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class ConfigurationServiceOverrider {
private final ConfigurationService original;
private Metrics metrics;
private Config clientConfig;
private boolean checkCR;
private int threadNumber;
private Cloner cloner;
private int timeoutSeconds;
Expand All @@ -22,7 +21,6 @@ public class ConfigurationServiceOverrider {
ConfigurationServiceOverrider(ConfigurationService original) {
this.original = original;
this.clientConfig = original.getClientConfiguration();
this.checkCR = original.checkCRDAndValidateLocalModel();
this.threadNumber = original.concurrentReconciliationThreads();
this.cloner = original.getResourceCloner();
this.timeoutSeconds = original.getTerminationTimeoutSeconds();
Expand All @@ -36,11 +34,6 @@ public ConfigurationServiceOverrider withClientConfiguration(Config configuratio
return this;
}

public ConfigurationServiceOverrider checkingCRDAndValidateLocalModel(boolean check) {
this.checkCR = check;
return this;
}

public ConfigurationServiceOverrider withConcurrentReconciliationThreads(int threadNumber) {
this.threadNumber = threadNumber;
return this;
Expand Down Expand Up @@ -83,11 +76,6 @@ public Config getClientConfiguration() {
return clientConfig;
}

@Override
public boolean checkCRDAndValidateLocalModel() {
return checkCR;
}

@Override
public int concurrentReconciliationThreads() {
return threadNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.javaoperatorsdk.operator.CustomResourceUtils;
import io.javaoperatorsdk.operator.MissingCRDException;
import io.javaoperatorsdk.operator.OperatorException;
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider;
Expand Down Expand Up @@ -261,27 +258,10 @@ public void start() throws OperatorException {
failOnMissingCurrentNS();

try {
// check that the custom resource is known by the cluster if configured that way
final CustomResourceDefinition crd; // todo: check proper CRD spec version based on config
if (ConfigurationServiceProvider.instance().checkCRDAndValidateLocalModel()
&& CustomResource.class.isAssignableFrom(resClass)) {
crd = kubernetesClient.apiextensions().v1().customResourceDefinitions().withName(crdName)
.get();
if (crd == null) {
throwMissingCRDException(crdName, specVersion, controllerName);
}

// Apply validations that are not handled by fabric8
CustomResourceUtils.assertCustomResource(resClass, crd);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this adds more validation than just checking if the CRD exists so this removes more than just this check.

Copy link
Collaborator Author

@csviri csviri Mar 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, but without the CRD, this is something that we actually cannot check either, or?

Copy link
Collaborator Author

@csviri csviri Mar 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also fine to have this functionality, just target defatult feature switch to false. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe we should keep the feature after all, though I have to admit that if it's off by default, I'm not sure people will ever use it… 🤔
That said, it could be activated by default in the extensions for some context (e.g. Quarkus' dev mode).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KK will issue an another PR, with default false.

}

final var context = new EventSourceContext<>(
eventSourceManager.getControllerResourceEventSource(), configuration, kubernetesClient);

prepareEventSources(context).forEach(eventSourceManager::registerEventSource);

eventSourceManager.start();

log.info("'{}' controller started, pending event sources initialization", controllerName);
} catch (MissingCRDException e) {
throwMissingCRDException(crdName, specVersion, controllerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static io.javaoperatorsdk.operator.api.config.ConfigurationService.DEFAULT_RECONCILIATION_THREADS_NUMBER;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;

class ConfigurationServiceProviderTest {
Expand Down Expand Up @@ -35,16 +37,17 @@ void shouldProvideTheSetInstanceIfProvided() {
@Test
void shouldBePossibleToOverrideConfigOnce() {
final var config = new AbstractConfigurationService(null);
assertTrue(config.checkCRDAndValidateLocalModel());
assertThat(config.concurrentReconciliationThreads())
.isEqualTo(DEFAULT_RECONCILIATION_THREADS_NUMBER);

ConfigurationServiceProvider.set(config);
var instance = ConfigurationServiceProvider.instance();
assertEquals(config, instance);

ConfigurationServiceProvider.overrideCurrent(o -> o.checkingCRDAndValidateLocalModel(false));
ConfigurationServiceProvider.overrideCurrent(o -> o.withConcurrentReconciliationThreads(10));
instance = ConfigurationServiceProvider.instance();
assertNotEquals(config, instance);
assertFalse(instance.checkCRDAndValidateLocalModel());
assertThat(instance.concurrentReconciliationThreads()).isEqualTo(10);

assertThrows(IllegalStateException.class,
() -> ConfigurationServiceProvider.overrideCurrent(o -> o.withCloseClientOnStop(false)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,19 @@
import org.junit.jupiter.api.Test;

import io.fabric8.kubernetes.api.model.Secret;
import io.javaoperatorsdk.operator.MissingCRDException;
import io.javaoperatorsdk.operator.MockKubernetesClient;
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider;
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.Cleaner;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

@SuppressWarnings("unchecked")
class ControllerTest {

final ControllerConfiguration configuration = mock(ControllerConfiguration.class);
final Reconciler reconciler = mock(Reconciler.class);

@Test
void crdShouldNotBeCheckedForNativeResources() {
final var client = MockKubernetesClient.client(Secret.class);

when(configuration.getResourceClass()).thenReturn(Secret.class);

final var controller = new Controller<Secret>(reconciler, configuration, client);
controller.start();
verify(client, never()).apiextensions();
}

@Test
void crdShouldNotBeCheckedForCustomResourcesIfDisabled() {
final var client = MockKubernetesClient.client(TestCustomResource.class);
when(configuration.getResourceClass()).thenReturn(TestCustomResource.class);

try {
ConfigurationServiceProvider.overrideCurrent(o -> o.checkingCRDAndValidateLocalModel(false));
final var controller = new Controller<TestCustomResource>(reconciler, configuration, client);
controller.start();
verify(client, never()).apiextensions();
} finally {
ConfigurationServiceProvider.reset();
}
}

@Test
void crdShouldBeCheckedForCustomResourcesByDefault() {
ConfigurationServiceProvider.reset();
final var client = MockKubernetesClient.client(TestCustomResource.class);
when(configuration.getResourceClass()).thenReturn(TestCustomResource.class);

final var controller = new Controller<TestCustomResource>(reconciler, configuration, client);
// since we're not really connected to a cluster and the CRD wouldn't be deployed anyway, we
// expect a MissingCRDException to be thrown
assertThrows(MissingCRDException.class, controller::start);
verify(client, times(1)).apiextensions();
}

@Test
void usesFinalizerIfThereIfReconcilerImplementsCleaner() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ static void classSetup() {
* implemented on TestCustomResourceSpec or TestCustomResourceStatus
*/
ConfigurationServiceProvider.overrideCurrent(overrider -> {
overrider.checkingCRDAndValidateLocalModel(false)
.withResourceCloner(new Cloner() {
@Override
public <R extends HasMetadata> R clone(R object) {
return object;
}
});
overrider.withResourceCloner(new Cloner() {
@Override
public <R extends HasMetadata> R clone(R object) {
return object;
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ void setUpResources() {
ConfigurationServiceProvider.reset();

configurationService = spy(ConfigurationService.class);
when(configurationService.checkCRDAndValidateLocalModel()).thenReturn(false);
when(configurationService.getVersion()).thenReturn(new Version("1", "1", new Date()));
when(configurationService.getConfigurationFor(any(MyController.class)))
.thenReturn(new MyConfiguration());
Expand Down