Skip to content

Kubernetes as a Composite config source #1873

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

Merged
merged 21 commits into from
Feb 23, 2025
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
Expand Up @@ -23,12 +23,13 @@

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.config.server.config.ConfigServerAutoConfiguration;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.kubernetes.client.KubernetesClientAutoConfiguration;
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigContext;
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapPropertySource;
Expand Down Expand Up @@ -59,11 +60,21 @@ public class KubernetesConfigServerAutoConfiguration {

@Bean
@Profile("kubernetes")
public EnvironmentRepository kubernetesEnvironmentRepository(CoreV1Api coreV1Api,
@ConditionalOnMissingBean
public KubernetesEnvironmentRepository kubernetesEnvironmentRepository(CoreV1Api coreV1Api,
List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers,
KubernetesNamespaceProvider kubernetesNamespaceProvider) {
KubernetesNamespaceProvider kubernetesNamespaceProvider,
KubernetesConfigServerProperties kubernetesConfigServerProperties) {
return new KubernetesEnvironmentRepository(coreV1Api, kubernetesPropertySourceSuppliers,
kubernetesNamespaceProvider.getNamespace());
kubernetesNamespaceProvider.getNamespace(), kubernetesConfigServerProperties);
}

@Bean
@ConditionalOnBean(KubernetesEnvironmentRepository.class)
@ConditionalOnMissingBean
public KubernetesEnvironmentRepositoryFactory kubernetesEnvironmentRepositoryFactory(
KubernetesEnvironmentRepository kubernetesEnvironmentRepository) {
return new KubernetesEnvironmentRepositoryFactory(kubernetesEnvironmentRepository);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package org.springframework.cloud.kubernetes.configserver;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties;

/**
* @author Ryan Baxter
*/

@ConfigurationProperties("spring.cloud.kubernetes.configserver")
public class KubernetesConfigServerProperties {
public class KubernetesConfigServerProperties implements EnvironmentRepositoryProperties {

private int order = DEFAULT_ORDER;

private String configMapNamespaces = "";

Expand All @@ -45,4 +48,12 @@ public void setSecretsNamespaces(String secretsNamespaces) {
this.secretsNamespaces = secretsNamespaces;
}

public int getOrder() {
return this.order;
}

public void setOrder(int order) {
this.order = order;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.core.Ordered;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.StandardEnvironment;
Expand All @@ -36,7 +37,7 @@
/**
* @author Ryan Baxter
*/
public class KubernetesEnvironmentRepository implements EnvironmentRepository {
public class KubernetesEnvironmentRepository implements EnvironmentRepository, Ordered {

private static final Log LOG = LogFactory.getLog(KubernetesEnvironmentRepository.class);

Expand All @@ -46,13 +47,25 @@ public class KubernetesEnvironmentRepository implements EnvironmentRepository {

private final String namespace;

private int order = KubernetesConfigServerProperties.DEFAULT_ORDER;

@Deprecated
public KubernetesEnvironmentRepository(CoreV1Api coreApi,
List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers, String namespace) {
this.coreApi = coreApi;
this.kubernetesPropertySourceSuppliers = kubernetesPropertySourceSuppliers;
this.namespace = namespace;
}

public KubernetesEnvironmentRepository(CoreV1Api coreApi,
List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers, String namespace,
KubernetesConfigServerProperties properties) {
this.coreApi = coreApi;
this.kubernetesPropertySourceSuppliers = kubernetesPropertySourceSuppliers;
this.namespace = namespace;
this.order = properties.getOrder();
}

@Override
public Environment findOne(String application, String profile, String label) {
return findOne(application, profile, label, true);
Expand Down Expand Up @@ -118,4 +131,13 @@ private void addApplicationConfiguration(Environment environment, StandardEnviro
});
}

@Override
public int getOrder() {
return this.order;
}

public void setOrder(int order) {
this.order = order;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.configserver;

import org.springframework.cloud.config.server.environment.EnvironmentRepositoryFactory;

/**
* Factory class for creating instances of {@link KubernetesEnvironmentRepository}.
*
* @author Arjav Dongaonkar
*/
public class KubernetesEnvironmentRepositoryFactory
implements EnvironmentRepositoryFactory<KubernetesEnvironmentRepository, KubernetesConfigServerProperties> {

private final KubernetesEnvironmentRepository kubernetesEnvironmentRepository;

public KubernetesEnvironmentRepositoryFactory(KubernetesEnvironmentRepository kubernetesEnvironmentRepository) {
this.kubernetesEnvironmentRepository = kubernetesEnvironmentRepository;
}

@Override
public KubernetesEnvironmentRepository build(KubernetesConfigServerProperties environmentProperties) {
return kubernetesEnvironmentRepository;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2013-2022 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.configserver;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.config.server.environment.JGitEnvironmentRepository;
import org.springframework.context.ConfigurableApplicationContext;

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

/**
* @author Arjav Dongaonkar
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { KubernetesConfigServerApplication.class },
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes",
"spring.cloud.kubernetes.client.namespace=default", "spring.profiles.active=composite",
"spring.cloud.config.server.composite[0].type=git",
"spring.cloud.config.server.composite[0].uri=https://github.com/spring-cloud-samples/config-repo",
"spring.cloud.config.server.composite[1].type=kubernetes",
"spring.cloud.config.server.composite[1].config-map-namespace=default",
"spring.cloud.config.server.composite[1].secrets-namespace=default" })
class CompositeProfileWithGitAndKubernetesConfigSourcesTests {

@Autowired
private ConfigurableApplicationContext context;

@Test
void runTest() {
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepository.class)).hasSize(2);
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepositoryFactory.class)).hasSize(1);
assertThat(context.getBeanNamesForType(KubernetesPropertySourceSupplier.class)).isNotEmpty();
assertThat(context.getBeanNamesForType(JGitEnvironmentRepository.class)).hasSize(1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2013-2022 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.configserver;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;

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

/**
* @author Arjav Dongaonkar
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { KubernetesConfigServerApplication.class },
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes",
"spring.cloud.kubernetes.client.namespace=default", "spring.profiles.active=composite",
"spring.cloud.config.server.composite[0].type=kubernetes",
"spring.cloud.config.server.composite[0].config-map-namespace=default",
"spring.cloud.config.server.composite[0].secrets-namespace=default",
"spring.cloud.config.server.composite[1].type=kubernetes",
"spring.cloud.config.server.composite[1].config-map-namespace=another-namespace",
"spring.cloud.config.server.composite[1].secrets-namespace=another-namespace" })
class CompositeProfileWithMultipleKubernetesConfigSourcesTests {

@Autowired
private ConfigurableApplicationContext context;

@Test
void runTest() {
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepository.class)).hasSize(3);
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepositoryFactory.class)).hasSize(1);
assertThat(context.getBeanNamesForType(KubernetesPropertySourceSupplier.class)).isNotEmpty();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2013-2022 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.configserver;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;

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

/**
* @author Arjav Dongaonkar
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { KubernetesConfigServerApplication.class },
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes",
"spring.cloud.kubernetes.client.namespace=default", "spring.profiles.active=composite",
"spring.cloud.config.server.composite[0].type=kubernetes",
"spring.cloud.config.server.composite[0].config-map-namespace=default",
"spring.cloud.config.server.composite[0].secrets-namespace=default" })
class CompositeProfileWithOnlyKubernetesConfigSourceTests {

@Autowired
private ConfigurableApplicationContext context;

@Test
void runTest() {
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepository.class)).hasSize(2);
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepositoryFactory.class)).hasSize(1);
assertThat(context.getBeanNamesForType(KubernetesPropertySourceSupplier.class)).isNotEmpty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ConfigServerAutoConfigurationKubernetesDisabledTests {
@Test
void runTest() {
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepository.class)).hasSize(0);
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepositoryFactory.class)).hasSize(0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ConfigServerAutoConfigurationKubernetesEnabledProfileIncludedConfigApiDisa
void runTest() {
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepository.class)).hasSize(1);
assertThat(context.getBeanNamesForType(KubernetesPropertySourceSupplier.class)).hasSize(0);
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepositoryFactory.class)).hasSize(1);
}

}
Loading
Loading