|
| 1 | +/* |
| 2 | + * Copyright 2013-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.kubernetes.commons.config; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.apache.commons.logging.Log; |
| 25 | + |
| 26 | +import org.springframework.boot.ConfigurableBootstrapContext; |
| 27 | +import org.springframework.boot.context.config.ConfigDataLocation; |
| 28 | +import org.springframework.boot.context.config.ConfigDataLocationNotFoundException; |
| 29 | +import org.springframework.boot.context.config.ConfigDataLocationResolver; |
| 30 | +import org.springframework.boot.context.config.ConfigDataLocationResolverContext; |
| 31 | +import org.springframework.boot.context.config.ConfigDataResourceNotFoundException; |
| 32 | +import org.springframework.boot.context.config.Profiles; |
| 33 | +import org.springframework.boot.context.properties.bind.Bindable; |
| 34 | +import org.springframework.boot.context.properties.bind.Binder; |
| 35 | +import org.springframework.boot.logging.DeferredLogFactory; |
| 36 | +import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties; |
| 37 | +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; |
| 38 | +import org.springframework.cloud.kubernetes.commons.config.configdata.KubernetesConfigDataResource; |
| 39 | +import org.springframework.core.Ordered; |
| 40 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 41 | +import org.springframework.core.env.Environment; |
| 42 | +import org.springframework.core.env.MapPropertySource; |
| 43 | +import org.springframework.core.env.PropertySource; |
| 44 | +import org.springframework.core.env.StandardEnvironment; |
| 45 | + |
| 46 | +import static org.springframework.boot.cloud.CloudPlatform.KUBERNETES; |
| 47 | +import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.registerSingle; |
| 48 | +import static org.springframework.util.ClassUtils.isPresent; |
| 49 | + |
| 50 | +/** |
| 51 | + * @author Ryan Baxter |
| 52 | + */ |
| 53 | +public abstract class KubernetesConfigDataLocationResolver |
| 54 | + implements ConfigDataLocationResolver<KubernetesConfigDataResource>, Ordered { |
| 55 | + |
| 56 | + private static final Class<KubernetesClientProperties> PROPERTIES_CLASS = KubernetesClientProperties.class; |
| 57 | + |
| 58 | + private static final boolean RETRY_IS_PRESENT = isPresent("org.springframework.retry.annotation.Retryable", null); |
| 59 | + |
| 60 | + private final Log log; |
| 61 | + |
| 62 | + public KubernetesConfigDataLocationResolver(DeferredLogFactory factory) { |
| 63 | + this.log = factory.getLog(KubernetesConfigDataLocationResolver.class); |
| 64 | + } |
| 65 | + |
| 66 | + protected final String getPrefix() { |
| 67 | + return "kubernetes:"; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public final int getOrder() { |
| 72 | + return -1; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public final boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) { |
| 77 | + return location.hasPrefix(getPrefix()) |
| 78 | + && (KUBERNETES.isEnforced(context.getBinder()) || KUBERNETES.isDetected(new StandardEnvironment())); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public final List<KubernetesConfigDataResource> resolve(ConfigDataLocationResolverContext context, |
| 83 | + ConfigDataLocation location) |
| 84 | + throws ConfigDataLocationNotFoundException, ConfigDataResourceNotFoundException { |
| 85 | + return Collections.emptyList(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public final List<KubernetesConfigDataResource> resolveProfileSpecific( |
| 90 | + ConfigDataLocationResolverContext resolverContext, ConfigDataLocation location, Profiles profiles) |
| 91 | + throws ConfigDataLocationNotFoundException { |
| 92 | + PropertyHolder propertyHolder = PropertyHolder.of(resolverContext); |
| 93 | + KubernetesClientProperties clientProperties = propertyHolder.kubernetesClientProperties(); |
| 94 | + ConfigMapConfigProperties configMapProperties = propertyHolder.configMapConfigProperties(); |
| 95 | + SecretsConfigProperties secretsProperties = propertyHolder.secretsProperties(); |
| 96 | + |
| 97 | + registerProperties(resolverContext, clientProperties, configMapProperties, secretsProperties); |
| 98 | + |
| 99 | + HashMap<String, Object> kubernetesConfigData = new HashMap<>(); |
| 100 | + kubernetesConfigData.put("spring.cloud.kubernetes.client.namespace", clientProperties.namespace()); |
| 101 | + if (propertyHolder.applicationName() != null) { |
| 102 | + // If its null it means sprig.application.name was not set so don't add it to |
| 103 | + // the property source |
| 104 | + kubernetesConfigData.put("spring.application.name", propertyHolder.applicationName()); |
| 105 | + } |
| 106 | + PropertySource<Map<String, Object>> propertySource = new MapPropertySource("kubernetesConfigData", |
| 107 | + kubernetesConfigData); |
| 108 | + ConfigurableEnvironment environment = new StandardEnvironment(); |
| 109 | + environment.getPropertySources().addLast(propertySource); |
| 110 | + environment.setActiveProfiles(profiles.getAccepted().toArray(new String[0])); |
| 111 | + KubernetesNamespaceProvider namespaceProvider = kubernetesNamespaceProvider(environment); |
| 112 | + |
| 113 | + registerBeans(resolverContext, location, profiles, propertyHolder, namespaceProvider); |
| 114 | + |
| 115 | + KubernetesConfigDataResource resource = new KubernetesConfigDataResource(clientProperties, configMapProperties, |
| 116 | + secretsProperties, location.isOptional(), profiles, environment); |
| 117 | + |
| 118 | + return List.of(resource); |
| 119 | + } |
| 120 | + |
| 121 | + protected abstract void registerBeans(ConfigDataLocationResolverContext resolverContext, |
| 122 | + ConfigDataLocation location, Profiles profiles, PropertyHolder propertyHolder, |
| 123 | + KubernetesNamespaceProvider namespaceProvider); |
| 124 | + |
| 125 | + protected KubernetesNamespaceProvider kubernetesNamespaceProvider(Environment environment) { |
| 126 | + return new KubernetesNamespaceProvider(environment); |
| 127 | + } |
| 128 | + |
| 129 | + private void registerProperties(ConfigDataLocationResolverContext resolverContext, |
| 130 | + KubernetesClientProperties clientProperties, ConfigMapConfigProperties configMapProperties, |
| 131 | + SecretsConfigProperties secretsProperties) { |
| 132 | + |
| 133 | + ConfigurableBootstrapContext bootstrapContext = resolverContext.getBootstrapContext(); |
| 134 | + registerSingle(bootstrapContext, PROPERTIES_CLASS, clientProperties, "configDataKubernetesClientProperties"); |
| 135 | + |
| 136 | + if (configMapProperties != null) { |
| 137 | + registerSingle(bootstrapContext, ConfigMapConfigProperties.class, configMapProperties, |
| 138 | + "configDataConfigMapConfigProperties"); |
| 139 | + } |
| 140 | + |
| 141 | + if (secretsProperties != null) { |
| 142 | + registerSingle(bootstrapContext, SecretsConfigProperties.class, secretsProperties, |
| 143 | + "configDataSecretsConfigProperties"); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + protected record PropertyHolder(KubernetesClientProperties kubernetesClientProperties, |
| 148 | + ConfigMapConfigProperties configMapConfigProperties, SecretsConfigProperties secretsProperties, |
| 149 | + String applicationName) { |
| 150 | + |
| 151 | + private static PropertyHolder of(ConfigDataLocationResolverContext context) { |
| 152 | + Binder binder = context.getBinder(); |
| 153 | + |
| 154 | + String applicationName = binder.bind("spring.application.name", String.class).orElse(null); |
| 155 | + String namespace = binder.bind("spring.cloud.kubernetes.client.namespace", String.class) |
| 156 | + .orElse(binder.bind("kubernetes.namespace", String.class).orElse("")); |
| 157 | + |
| 158 | + KubernetesClientProperties kubernetesClientProperties = clientProperties(context, namespace); |
| 159 | + ConfigMapAndSecrets both = ConfigMapAndSecrets.of(binder); |
| 160 | + |
| 161 | + return new PropertyHolder(kubernetesClientProperties, both.configMapProperties(), |
| 162 | + both.secretsConfigProperties(), applicationName); |
| 163 | + } |
| 164 | + |
| 165 | + private static KubernetesClientProperties clientProperties(ConfigDataLocationResolverContext context, |
| 166 | + String namespace) { |
| 167 | + KubernetesClientProperties kubernetesClientProperties; |
| 168 | + ConfigurableBootstrapContext bootstrapContext = context.getBootstrapContext(); |
| 169 | + |
| 170 | + if (bootstrapContext.isRegistered(PROPERTIES_CLASS) && bootstrapContext.get(PROPERTIES_CLASS) != null) { |
| 171 | + kubernetesClientProperties = bootstrapContext.get(PROPERTIES_CLASS).withNamespace(namespace); |
| 172 | + } |
| 173 | + else { |
| 174 | + kubernetesClientProperties = context.getBinder() |
| 175 | + .bindOrCreate(KubernetesClientProperties.PREFIX, Bindable.of(PROPERTIES_CLASS)) |
| 176 | + .withNamespace(namespace); |
| 177 | + } |
| 178 | + |
| 179 | + return kubernetesClientProperties; |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * holds ConfigMapConfigProperties and SecretsConfigProperties, both can be null if |
| 187 | + * using such sources is disabled. |
| 188 | + */ |
| 189 | + private record ConfigMapAndSecrets(ConfigMapConfigProperties configMapProperties, |
| 190 | + SecretsConfigProperties secretsConfigProperties) { |
| 191 | + |
| 192 | + private static ConfigMapAndSecrets of(Binder binder) { |
| 193 | + |
| 194 | + boolean configEnabled = binder.bind("spring.cloud.kubernetes.config.enabled", boolean.class).orElse(true); |
| 195 | + boolean secretsEnabled = binder.bind("spring.cloud.kubernetes.secrets.enabled", boolean.class).orElse(true); |
| 196 | + |
| 197 | + ConfigMapConfigProperties configMapConfigProperties = null; |
| 198 | + if (configEnabled) { |
| 199 | + configMapConfigProperties = binder.bindOrCreate(ConfigMapConfigProperties.PREFIX, |
| 200 | + ConfigMapConfigProperties.class); |
| 201 | + } |
| 202 | + |
| 203 | + SecretsConfigProperties secretsProperties = null; |
| 204 | + if (secretsEnabled) { |
| 205 | + secretsProperties = binder.bindOrCreate(SecretsConfigProperties.PREFIX, SecretsConfigProperties.class); |
| 206 | + } |
| 207 | + |
| 208 | + return new ConfigMapAndSecrets(configMapConfigProperties, secretsProperties); |
| 209 | + |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | +} |
0 commit comments