Skip to content
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
@@ -0,0 +1,74 @@
/*
* Copyright 2013-present 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.config.applications.test_for_1715;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.kubernetes.client.config.applications.test_for_1715.properties.APropertySourceByLabel;
import org.springframework.cloud.kubernetes.client.config.applications.test_for_1715.properties.APropertySourceByName;
import org.springframework.test.context.ActiveProfiles;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author wind57
*/
@ActiveProfiles({ "k8s" })
abstract class AbstractTests {

@Autowired
private APropertySourceByName aByName;

@Autowired
private APropertySourceByLabel aByLabel;

/**
* this one is simply read by name
*/
@Test
void testAByName() {
assertThat(aByName.aByName()).isEqualTo("aByName");
}

/**
* this one is read by name + profile
*/
@Test
void testAByNameAndProfile() {
assertThat(aByName.aByNameK8s()).isEqualTo("aByNameK8s");
}

/**
* this one is simply read by name
*/
@Test
void testAByLabel() {
assertThat(aByLabel.aByLabel()).isEqualTo("aByLabel");
}

/**
* This one is not read at all. This proves that includeProfileSpecificSources is not
* relevant for labels based searches. Notice that we do read from: 'a-by-name-k8s',
* but not from 'a-by-label-k8s'.
*/
@Test
void testAByLabelAndProfile() {
assertThat(aByLabel.aByLabelAndProfile()).isNull();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2013-present 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.config.applications.test_for_1715;

import org.springframework.boot.test.context.SpringBootTest;

/**
* Stubs for this test are in
* {@link org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.FixFor1715ConfigurationStub}
*
* @author wind57
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Fix1715App.class,
properties = { "spring.cloud.bootstrap.name=fix-1715", "fix.1715.enabled=true",
"spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true",
"spring.cloud.kubernetes.client.namespace=spring-k8s" })
public class BootstrapTests extends AbstractTests {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2013-present 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.config.applications.test_for_1715;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import io.kubernetes.client.util.ClientBuilder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.client.KubernetesClientUtils;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.mockito.Mockito.mockStatic;
import static org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.FixFor1715ConfigurationStub.stubData;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Fix1715App.class,
properties = { "spring.cloud.application.name=fix-1715", "fix.1715.enabled=true",
"spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:,classpath:./fix-1715.yaml" })
class ConfigDataTests extends AbstractTests {

private static MockedStatic<KubernetesClientUtils> clientUtilsMock;

@BeforeAll
static void wireMock() {
WireMockServer server = new WireMockServer(options().dynamicPort());
server.start();
WireMock.configureFor("localhost", server.port());
clientUtilsMock = mockStatic(KubernetesClientUtils.class);
clientUtilsMock.when(KubernetesClientUtils::kubernetesApiClient)
.thenReturn(new ClientBuilder().setBasePath(server.baseUrl()).build());
clientUtilsMock
.when(() -> KubernetesClientUtils.getApplicationNamespace(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn("spring-k8s");
stubData();
}

@AfterAll
static void teardown() {
clientUtilsMock.close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2013-present 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.config.applications.test_for_1715;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.kubernetes.client.config.applications.test_for_1715.properties.APropertySourceByLabel;
import org.springframework.cloud.kubernetes.client.config.applications.test_for_1715.properties.APropertySourceByName;

@SpringBootApplication
@EnableConfigurationProperties({ APropertySourceByName.class, APropertySourceByLabel.class })
public class Fix1715App {

public static void main(String[] args) {
SpringApplication.run(Fix1715App.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2013-present 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.config.applications.test_for_1715.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* @author wind57
*/
@ConfigurationProperties("a-by-label")
public record APropertySourceByLabel(String aByLabel, String aByLabelAndProfile) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2013-present 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.config.applications.test_for_1715.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* @author wind57
*/
@ConfigurationProperties("a-by-name")
public record APropertySourceByName(String aByName, String aByNameK8s) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2013-present 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.config.bootstrap.stubs;

import java.util.Map;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.JSON;
import io.kubernetes.client.openapi.models.V1ConfigMap;
import io.kubernetes.client.openapi.models.V1ConfigMapBuilder;
import io.kubernetes.client.openapi.models.V1ConfigMapList;
import io.kubernetes.client.openapi.models.V1ObjectMetaBuilder;
import io.kubernetes.client.util.ClientBuilder;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

@Order(0)
@Configuration
@ConditionalOnProperty("fix.1715.enabled")
public class FixFor1715ConfigurationStub {

@Bean
public WireMockServer wireMock() {
WireMockServer server = new WireMockServer(options().dynamicPort());
server.start();
WireMock.configureFor("localhost", server.port());
return server;
}

@Bean
public ApiClient apiClient(WireMockServer wireMockServer) {
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockServer.port()).build();
io.kubernetes.client.openapi.Configuration.setDefaultApiClient(apiClient);
apiClient.setDebugging(true);
stubData();
return apiClient;
}

public static void stubData() {

V1ConfigMap aByName = new V1ConfigMapBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("a-by-name").withNamespace("spring-k8s").build())
.addToData(Map.of("aByName", "aByName"))
.build();

V1ConfigMap aByNameAndProfile = new V1ConfigMapBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("a-by-name-k8s").withNamespace("spring-k8s").build())
.addToData(Map.of("aByNameK8s", "aByNameK8s"))
.build();

V1ConfigMap aByLabel = new V1ConfigMapBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("a-by-label")
.withNamespace("spring-k8s")
.withLabels(Map.of("color", "blue"))
.build())
.addToData(Map.of("aByLabel", "aByLabel"))
.build();

V1ConfigMap aByLabelAndProfile = new V1ConfigMapBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("a-by-label-k8s")
.withNamespace("spring-k8s")
.withLabels(Map.of("color", "blue"))
.build())
.addToData(Map.of("aByLabelK8s", "aByLabelK8s"))
.build();

// the actual stub for CoreV1Api calls
V1ConfigMapList configMapList = new V1ConfigMapList();
configMapList.addItemsItem(aByName);
configMapList.addItemsItem(aByNameAndProfile);
configMapList.addItemsItem(aByLabel);
configMapList.addItemsItem(aByLabelAndProfile);

WireMock.stubFor(WireMock.get("/api/v1/namespaces/spring-k8s/configmaps")
.willReturn(WireMock.aResponse().withStatus(200).withBody(JSON.serialize(configMapList))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.IncludeProfil
org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.ConfigMapNameAsPrefixConfigurationStub, \
org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.SourcesOrderConfigurationStub, \
org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.BootstrapKubernetesClientSanitizeEnvEndpointStub, \
org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.FixFor1715ConfigurationStub, \
org.springframework.cloud.kubernetes.client.config.EnableRetryBootstrapConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
spring:
application:
name: fix-1715
cloud:
kubernetes:
config:
namespace: spring-k8s
sources:

- name: a-by-name
useNameAsPrefix: true
includeProfileSpecificSources: true

- labels:
color: blue
useNameAsPrefix: true
includeProfileSpecificSources: true
Loading
Loading