operator-framework / java-operator-sdk

Java SDK for building Kubernetes Operators
https://javaoperatorsdk.io/
Apache License 2.0
805 stars 216 forks source link

Surprising closing of caller-supplied clients #2562

Closed tombentley closed 3 weeks ago

tombentley commented 3 weeks ago

Bug Report

What did you do?

I wrote a test class with 2 @Test methods using a LocallyRunOperatorExtension with an externally-given kubernetes client:

    @RegisterExtension
    LocallyRunOperatorExtension extension = LocallyRunOperatorExtension.builder()
            .withKubernetesClient(client)
            .withReconciler(ProxyReconciler.class)
            .build();

What did you expect to see?

Both test methods to be executed

What did you see instead? Under which circumstances?

The first-executed @Test method worked fine, but the second failed in AbstractOperatorExtension.beforeEach() because the previously-executed afterEach closed the kubernetes client given to the extension via Operator.stop.

Here's the stack where the client gets closed:

doClose:254, OkHttpClientImpl (io.fabric8.kubernetes.client.okhttp)
close:300, StandardHttpClient (io.fabric8.kubernetes.client.http)
close:160, BaseClient (io.fabric8.kubernetes.client.impl)
stop:181, Operator (io.javaoperatorsdk.operator)
stop:189, Operator (io.javaoperatorsdk.operator)
after:201, LocallyRunOperatorExtension (io.javaoperatorsdk.operator.junit)
afterEachImpl:178, AbstractOperatorExtension (io.javaoperatorsdk.operator.junit)
afterEach:89, AbstractOperatorExtension (io.javaoperatorsdk.operator.junit)

This is surprising to me because my default assumption (when passing a client instance which I've instantiated to some API) is that I remain responsible for closing that client. There is no javadoc to say exactly what the contract of the withKubernetesClient(client) method actually is.

Environment

N/A

Possible Solution

Either:

Additional context

csviri commented 3 weeks ago

Hi, yes, absolutely make sense what you saying, this is partially because of historical reasons, will document better the withKubernetesClient.

You can change the behavior using .withCloseClientOnStop(false).

tombentley commented 3 weeks ago

You can change the behavior using .withCloseClientOnStop(false)

Ah, thanks I didn't manage to spot that!