|
| 1 | +/* |
| 2 | + * Copyright 2019-2023 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.client.discovery; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import io.kubernetes.client.informer.SharedIndexInformer; |
| 23 | +import io.kubernetes.client.informer.SharedInformerFactory; |
| 24 | +import io.kubernetes.client.informer.cache.Lister; |
| 25 | +import io.kubernetes.client.openapi.ApiClient; |
| 26 | +import io.kubernetes.client.openapi.models.V1Endpoints; |
| 27 | +import io.kubernetes.client.openapi.models.V1EndpointsList; |
| 28 | +import io.kubernetes.client.openapi.models.V1Service; |
| 29 | +import io.kubernetes.client.openapi.models.V1ServiceList; |
| 30 | +import io.kubernetes.client.util.Namespaces; |
| 31 | +import io.kubernetes.client.util.generic.GenericKubernetesApi; |
| 32 | +import org.apache.commons.logging.Log; |
| 33 | + |
| 34 | +import org.springframework.boot.BootstrapContext; |
| 35 | +import org.springframework.boot.BootstrapRegistry; |
| 36 | +import org.springframework.boot.context.properties.bind.BindHandler; |
| 37 | +import org.springframework.boot.context.properties.bind.Bindable; |
| 38 | +import org.springframework.boot.context.properties.bind.Binder; |
| 39 | +import org.springframework.cloud.client.ServiceInstance; |
| 40 | +import org.springframework.cloud.config.client.ConfigServerInstanceProvider; |
| 41 | +import org.springframework.cloud.kubernetes.client.KubernetesClientAutoConfiguration; |
| 42 | +import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties; |
| 43 | +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; |
| 44 | +import org.springframework.cloud.kubernetes.commons.config.KubernetesConfigServerBootstrapper; |
| 45 | +import org.springframework.cloud.kubernetes.commons.config.KubernetesConfigServerInstanceProvider; |
| 46 | +import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; |
| 47 | +import org.springframework.core.env.AbstractEnvironment; |
| 48 | +import org.springframework.core.env.Environment; |
| 49 | +import org.springframework.util.ClassUtils; |
| 50 | + |
| 51 | +/** |
| 52 | + * @author Ryan Baxter |
| 53 | + */ |
| 54 | +class KubernetesClientConfigServerBootstrapper extends KubernetesConfigServerBootstrapper { |
| 55 | + |
| 56 | + @Override |
| 57 | + public void initialize(BootstrapRegistry registry) { |
| 58 | + if (!ClassUtils.isPresent("org.springframework.cloud.config.client.ConfigServerInstanceProvider", null)) { |
| 59 | + return; |
| 60 | + } |
| 61 | + // We need to pass a lambda here rather than create a new instance of |
| 62 | + // ConfigServerInstanceProvider.Function |
| 63 | + // or else we will get ClassNotFoundExceptions if Spring Cloud Config is not on |
| 64 | + // the classpath |
| 65 | + registry.registerIfAbsent(ConfigServerInstanceProvider.Function.class, KubernetesFunction::create); |
| 66 | + } |
| 67 | + |
| 68 | + final static class KubernetesFunction implements ConfigServerInstanceProvider.Function { |
| 69 | + |
| 70 | + private final BootstrapContext context; |
| 71 | + |
| 72 | + private KubernetesFunction(BootstrapContext context) { |
| 73 | + this.context = context; |
| 74 | + } |
| 75 | + |
| 76 | + static KubernetesFunction create(BootstrapContext context) { |
| 77 | + return new KubernetesFunction(context); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public List<ServiceInstance> apply(String serviceId, Binder binder, BindHandler bindHandler, Log log) { |
| 82 | + if (binder == null || bindHandler == null || !getDiscoveryEnabled(binder, bindHandler)) { |
| 83 | + // If we don't have the Binder or BinderHandler from the |
| 84 | + // ConfigDataLocationResolverContext |
| 85 | + // we won't be able to create the necessary configuration |
| 86 | + // properties to configure the |
| 87 | + // Kubernetes DiscoveryClient |
| 88 | + return Collections.emptyList(); |
| 89 | + } |
| 90 | + KubernetesDiscoveryProperties discoveryProperties = createKubernetesDiscoveryProperties(binder, |
| 91 | + bindHandler); |
| 92 | + KubernetesClientProperties clientProperties = createKubernetesClientProperties(binder, bindHandler); |
| 93 | + return getInstanceProvider(discoveryProperties, clientProperties, context, binder, bindHandler, log) |
| 94 | + .getInstances(serviceId); |
| 95 | + } |
| 96 | + |
| 97 | + protected KubernetesConfigServerInstanceProvider getInstanceProvider( |
| 98 | + KubernetesDiscoveryProperties discoveryProperties, KubernetesClientProperties clientProperties, |
| 99 | + BootstrapContext context, Binder binder, BindHandler bindHandler, Log log) { |
| 100 | + if (context.isRegistered(KubernetesInformerDiscoveryClient.class)) { |
| 101 | + KubernetesInformerDiscoveryClient client = context.get(KubernetesInformerDiscoveryClient.class); |
| 102 | + return client::getInstances; |
| 103 | + } |
| 104 | + else { |
| 105 | + KubernetesClientAutoConfiguration clientAutoConfiguration = new KubernetesClientAutoConfiguration(); |
| 106 | + ApiClient apiClient = context.getOrElseSupply(ApiClient.class, |
| 107 | + () -> clientAutoConfiguration.apiClient(clientProperties)); |
| 108 | + |
| 109 | + KubernetesNamespaceProvider kubernetesNamespaceProvider = clientAutoConfiguration |
| 110 | + .kubernetesNamespaceProvider(getNamespaceEnvironment(binder, bindHandler)); |
| 111 | + |
| 112 | + String namespace = getInformerNamespace(kubernetesNamespaceProvider, discoveryProperties); |
| 113 | + SharedInformerFactory sharedInformerFactory = new SharedInformerFactory(apiClient); |
| 114 | + SpringCloudKubernetesInformerFactoryProcessor informerFactoryProcessor = new SpringCloudKubernetesInformerFactoryProcessor( |
| 115 | + kubernetesNamespaceProvider, apiClient, sharedInformerFactory, |
| 116 | + discoveryProperties.isAllNamespaces()); |
| 117 | + final GenericKubernetesApi<V1Service, V1ServiceList> servicesApi = new GenericKubernetesApi<>( |
| 118 | + V1Service.class, V1ServiceList.class, "", "v1", "services", apiClient); |
| 119 | + SharedIndexInformer<V1Service> serviceSharedIndexInformer = sharedInformerFactory |
| 120 | + .sharedIndexInformerFor(servicesApi, V1Service.class, 0L, namespace); |
| 121 | + Lister<V1Service> serviceLister = new Lister<>(serviceSharedIndexInformer.getIndexer()); |
| 122 | + final GenericKubernetesApi<V1Endpoints, V1EndpointsList> endpointsApi = new GenericKubernetesApi<>( |
| 123 | + V1Endpoints.class, V1EndpointsList.class, "", "v1", "endpoints", apiClient); |
| 124 | + SharedIndexInformer<V1Endpoints> endpointsSharedIndexInformer = sharedInformerFactory |
| 125 | + .sharedIndexInformerFor(endpointsApi, V1Endpoints.class, 0L, namespace); |
| 126 | + Lister<V1Endpoints> endpointsLister = new Lister<>(endpointsSharedIndexInformer.getIndexer()); |
| 127 | + KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient( |
| 128 | + kubernetesNamespaceProvider.getNamespace(), sharedInformerFactory, serviceLister, |
| 129 | + endpointsLister, serviceSharedIndexInformer, endpointsSharedIndexInformer, discoveryProperties); |
| 130 | + try { |
| 131 | + discoveryClient.afterPropertiesSet(); |
| 132 | + return discoveryClient::getInstances; |
| 133 | + } |
| 134 | + catch (Exception e) { |
| 135 | + if (log != null) { |
| 136 | + log.warn("Error initiating informer discovery client", e); |
| 137 | + } |
| 138 | + return (serviceId) -> Collections.emptyList(); |
| 139 | + } |
| 140 | + finally { |
| 141 | + sharedInformerFactory.stopAllRegisteredInformers(); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private String getInformerNamespace(KubernetesNamespaceProvider kubernetesNamespaceProvider, |
| 147 | + KubernetesDiscoveryProperties discoveryProperties) { |
| 148 | + return discoveryProperties.isAllNamespaces() ? Namespaces.NAMESPACE_ALL |
| 149 | + : kubernetesNamespaceProvider.getNamespace() == null ? Namespaces.NAMESPACE_DEFAULT |
| 150 | + : kubernetesNamespaceProvider.getNamespace(); |
| 151 | + } |
| 152 | + |
| 153 | + private Environment getNamespaceEnvironment(Binder binder, BindHandler bindHandler) { |
| 154 | + return new AbstractEnvironment() { |
| 155 | + @Override |
| 156 | + public String getProperty(String key) { |
| 157 | + return binder.bind(key, Bindable.of(String.class), bindHandler).orElse(super.getProperty(key)); |
| 158 | + } |
| 159 | + }; |
| 160 | + } |
| 161 | + |
| 162 | + // This method should never be called, but is there for backward |
| 163 | + // compatibility purposes |
| 164 | + @Override |
| 165 | + public List<ServiceInstance> apply(String serviceId) { |
| 166 | + return apply(serviceId, null, null, null); |
| 167 | + } |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments