'How to disable Spring Cloud Kubernetes in a Maven build
Running Spring Boot 2.6.6 and Spring Cloud 2021.0.1
I'm attempting to migrate an existing service to Kubernetes so I added a dependency on spring-cloud-starter-kubernetes-client-all
. By default, I have spring.cloud.kubernetes.enable=false
and use the kubernetes
profile to enable it. This is intended to allow this service to operate in both Kubernetes and the legacy environment.
My unit-tests complete successfully when building locally but fail in my Bitbucket pipeline with the following error:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.cloud.kubernetes.commons.config.NamespaceResolutionFailedException: unresolved namespace
I suspect this occurs because Bitbucket Pipelines are deployed in Kubernetes and Spring somehow detects this. I have tried the following to no avail
- Pass
--define SPRING_CLOUD_KUBERNETES_ENABLED=false
to Maven on the command line - Set this as an environment variable e.g.,
export SPRING_CLOUD_KUBERNETES_ENABLED=false
- Pass
--define spring.cloud.kubernetes.enabled=false
to Maven on the command line
I have also checked StackOverflow for similar issues and investigated the code also without avail. The class that is actually raising the issue is KubernetesClientConfigUtils
, which should be disabled.
I would appreciate any guidance you can provide.
Solution 1:[1]
After trying a few other approaches, I finally stumbled upon one that works. I added the property to the unit-test context using
@WebMvcTest(properties = { "spring.cloud.kubernetes.enabled=false" })
This should also work for any unit-test annotation that load a Spring application context such as @WebFluxTest
Solution 2:[2]
Spring Cloud checks whether the application is running in a K8s environment before loading the active spring profile configuration and adds kubernetes
to the active profiles. Previously, in Hoxton SR10, the profile was identified and bootstrap-<profile>.yml
loaded before checking for Kubernetes. spring.cloud.kubernetes.enabled
was picked up from there if set in the profile configuration or the maven pom properties.
As maven allows setting system properties on the command line, kubernetes detection can be disabled by setting it there:
mvn test -Dspring.cloud.kubernetes.enabled=false
The surefire maven plugin allows setting system properties for all tests, so it's possible to set spring.cloud.kubernetes.enabled
to be false
in the surefire plugin configuration.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<spring.cloud.kubernetes.enabled>false</spring.cloud.kubernetes.enabled>
</systemPropertyVariables>
</configuration>
</plugin>
It is also possible to set the configuration on individual test classes using @Faron's approach to explicitly set the property in any WebMvcTest
annotated unit test, e.g.:
@WebMvcTest(properties = { "spring.cloud.kubernetes.enabled=false" })
It should also work on other unit test annotation that loads a Spring application context, such as WebFluxTest
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Faron |
Solution 2 |