Merge branch '2.0.x'

This commit is contained in:
Ryan Baxter
2021-11-08 13:56:09 -05:00
8 changed files with 280 additions and 67 deletions

View File

@@ -100,9 +100,10 @@
|spring.cloud.kubernetes.secrets.labels | |
|spring.cloud.kubernetes.secrets.name | |
|spring.cloud.kubernetes.secrets.namespace | |
|spring.cloud.kubernetes.secrets.paths | |
|spring.cloud.kubernetes.secrets.paths | |
|spring.cloud.kubernetes.secrets.retry | |
|spring.cloud.kubernetes.secrets.sources | |
|spring.cloud.kubernetes.secrets.use-name-as-prefix | `false` |
|spring.cloud.kubernetes.secrets.use-name-as-prefix | `false` |
|spring.cloud.kubernetes.userAgent | `Spring-Cloud-Kubernetes-Application` | `User-Agent` header
|===
|===

View File

@@ -22,6 +22,7 @@ import io.kubernetes.client.openapi.apis.CoreV1Api;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.kubernetes.commons.ConditionalOnKubernetesEnabled;
import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties;
import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration;
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
import org.springframework.context.annotation.Bean;
@@ -40,8 +41,9 @@ public class KubernetesClientAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public ApiClient apiClient() {
public ApiClient apiClient(KubernetesClientProperties properties) {
ApiClient apiClient = kubernetesApiClient();
apiClient.setUserAgent(properties.getUserAgent());
return apiClient;
}

View File

@@ -1,61 +0,0 @@
/*
* Copyright 2013-2021 the original author or 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
*
* https://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 org.springframework.cloud.kubernetes.client;
import io.kubernetes.client.openapi.ApiClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Robert McNees
*
* This tests that the apiClient created in KubernetesClientAutoConfiguration will not set
* itself as the default apiClient. This is to avoid overwriting the user's
* defaultApiClient if they include this project.
*/
@SpringBootTest(classes = KubernetesClientDefaultApiClientTests.App.class,
properties = { "kubernetes.informer.enabled=false" })
// kubernetes.informer is disabled because
// io.kubernetes...KubernetesInformerAutoConfiguration
// creates a defaultApiClient that will be autowired instead of the ApiClient
// created in KubernetesClientAutoConfiguration
public class KubernetesClientDefaultApiClientTests {
@Autowired
private ApiClient apiClient;
@Test
public void testCreatedApiClientIsNotDefault() {
assertThat(apiClient).isNotNull();
ApiClient defaultApiClient = io.kubernetes.client.openapi.Configuration.getDefaultApiClient();
assertThat(defaultApiClient).isNotNull();
assertThat(defaultApiClient).isNotSameAs(apiClient);
}
@SpringBootApplication
static class App {
}
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright 2013-2021 the original author or 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
*
* https://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 org.springframework.cloud.kubernetes.client.default_api;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import io.kubernetes.client.openapi.ApiClient;
import okhttp3.Request;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Robert McNees
*
* This tests that the apiClient created in KubernetesClientAutoConfiguration will not set
* itself as the default apiClient. This is to avoid overwriting the user's
* defaultApiClient if they include this project.
*
* kubernetes informer is disabled because KubernetesInformerAutoConfiguration creates a
* defaultApiClient that will be autowired instead of the ApiClient created in
* KubernetesClientAutoConfiguration
*/
class KubernetesClientDefaultApiClientTests {
private static final String DISABLE_INFORMER = "kubernetes.informer.enabled=false";
private static final String USER_AGENT = "spring.cloud.kubernetes.client.userAgent=non-default";
@SpringBootTest(classes = KubernetesClientDefaultApiClientTests.App.class, properties = DISABLE_INFORMER)
@Nested
class DefaultApiClientNotSameAsApiClient {
@Autowired
private ApiClient apiClient;
@Autowired
ConfigurableApplicationContext context;
@Test
void testCreatedApiClientIsNotDefault() {
assertThat(apiClient).isNotNull();
ApiClient defaultApiClient = io.kubernetes.client.openapi.Configuration.getDefaultApiClient();
assertThat(defaultApiClient).isNotNull();
assertThat(defaultApiClient).isNotSameAs(apiClient);
}
}
@SpringBootTest(classes = KubernetesClientDefaultApiClientTests.App.class, properties = DISABLE_INFORMER)
@Nested
class ApiClientUserAgentDefaultHeader {
@Autowired
private ApiClient apiClient;
@Autowired
ConfigurableApplicationContext context;
@Test
void testApiClientUserAgentDefaultHeader() throws MalformedURLException {
assertThat(apiClient).isNotNull();
Request.Builder builder = new Request.Builder();
apiClient.processHeaderParams(Collections.emptyMap(), builder);
assertThat(builder.url(new URL("http://example.com")).build().headers().get("User-Agent"))
.isEqualTo("Spring-Cloud-Kubernetes-Application");
}
}
@SpringBootTest(classes = KubernetesClientDefaultApiClientTests.App.class,
properties = { DISABLE_INFORMER, USER_AGENT })
@Nested
class ApiClientUserAgentNonDefaultHeader {
@Autowired
private ApiClient apiClient;
@Autowired
ConfigurableApplicationContext context;
@Test
void testApiClientUserAgentDefaultHeader() throws MalformedURLException {
assertThat(apiClient).isNotNull();
Request.Builder builder = new Request.Builder();
apiClient.processHeaderParams(Collections.emptyMap(), builder);
assertThat(builder.url(new URL("http://example.com")).build().headers().get("User-Agent"))
.isEqualTo("non-default");
}
}
@SpringBootApplication
static class App {
}
}

View File

@@ -28,6 +28,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("spring.cloud.kubernetes.client")
public class KubernetesClientProperties {
/**
* Default user-agent for kubernetes client.
*/
public static final String DEFAULT_USER_AGENT = "Spring-Cloud-Kubernetes-Application";
/**
* Default path for namespace file.
*/
@@ -87,6 +92,8 @@ public class KubernetesClientProperties {
private String serviceAccountNamespacePath = SERVICE_ACCOUNT_NAMESPACE_PATH;
private String userAgent = DEFAULT_USER_AGENT;
public String getServiceAccountNamespacePath() {
return serviceAccountNamespacePath;
}
@@ -307,4 +314,12 @@ public class KubernetesClientProperties {
this.oauthToken = oauthToken;
}
public String getUserAgent() {
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
}

View File

@@ -63,7 +63,7 @@ public class Fabric8AutoConfiguration {
@ConditionalOnMissingBean(Config.class)
public Config kubernetesClientConfig(KubernetesClientProperties kubernetesClientProperties) {
Config base = Config.autoConfigure(null);
Config properties = new ConfigBuilder(base)
ConfigBuilder builder = new ConfigBuilder(base)
// Only set values that have been explicitly specified
.withMasterUrl(or(kubernetesClientProperties.getMasterUrl(), base.getMasterUrl()))
.withApiVersion(or(kubernetesClientProperties.getApiVersion(), base.getApiVersion()))
@@ -96,7 +96,14 @@ public class Fabric8AutoConfiguration {
.withHttpsProxy(or(kubernetesClientProperties.getHttpsProxy(), base.getHttpsProxy()))
.withProxyUsername(or(kubernetesClientProperties.getProxyUsername(), base.getProxyUsername()))
.withProxyPassword(or(kubernetesClientProperties.getProxyPassword(), base.getProxyPassword()))
.withNoProxy(or(kubernetesClientProperties.getNoProxy(), base.getNoProxy())).build();
.withNoProxy(or(kubernetesClientProperties.getNoProxy(), base.getNoProxy()));
String userAgent = or(base.getUserAgent(), KubernetesClientProperties.DEFAULT_USER_AGENT);
if (!kubernetesClientProperties.getUserAgent().equals(KubernetesClientProperties.DEFAULT_USER_AGENT)) {
userAgent = kubernetesClientProperties.getUserAgent();
}
Config properties = builder.withUserAgent(userAgent).build();
return properties;
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2013-2021 the original author or 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
*
* https://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 org.springframework.cloud.kubernetes.fabric8;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.example.App;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author wind57
*
* test "User-Agent" functionality via system properties
*/
@SpringBootTest(classes = App.class)
class Fabric8ClientUserAgentEnvPropertyTests {
@Autowired
private KubernetesClient client;
@BeforeAll
static void beforeAll() {
System.setProperty(Config.KUBERNETES_USER_AGENT, "spring-k8s");
}
@AfterAll
static void afterAll() {
System.clearProperty(Config.KUBERNETES_USER_AGENT);
}
@Test
void testUserAgent() {
String userAgent = client.getConfiguration().getUserAgent();
assertThat(userAgent).isEqualTo("spring-k8s");
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2013-2021 the original author or 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
*
* https://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 org.springframework.cloud.kubernetes.fabric8;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.example.App;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author wind57
*
* test "User-Agent" functionality
*/
class Fabric8ClientUserAgentTests {
private static final String USER_AGENT = "spring.cloud.kubernetes.client.userAgent=non-default";
@Nested
@SpringBootTest(classes = App.class)
class DefaultConfigurationForClient {
@Autowired
private KubernetesClient client;
@Test
void testUserAgent() {
String userAgent = client.getConfiguration().getUserAgent();
assertThat(userAgent).isEqualTo("Spring-Cloud-Kubernetes-Application");
}
}
@Nested
@SpringBootTest(classes = App.class, properties = USER_AGENT)
class PropertiesConfigurationForClient {
@Autowired
private KubernetesClient client;
@Test
void testUserAgent() {
String userAgent = client.getConfiguration().getUserAgent();
assertThat(userAgent).isEqualTo("non-default");
}
}
}