Skip to content

Commit

Permalink
Merge branch 'main' into major-changes-in-fabric8-discovery-implement…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
wind57 committed Feb 26, 2024
2 parents 736532c + ab680d1 commit 73f40dd
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ the `DiscoveryClient` implementation provided by that starter.

## Permissions
The Spring Cloud Discovery server uses
the Kubernetes API server to get data about Service and Endpoint resources so it needs list, watch, and
get permissions to use those endpoints. See the below sample Kubernetes deployment YAML for an
example of how to configure the Service Account on Kubernetes.
the Kubernetes API server to get data about Pod, Service and Endpoint resources, so it needs list,
watch, and get permissions to use those endpoints. See the below sample Kubernetes deployment YAML
for an example of how to configure the Service Account on Kubernetes.


## Endpoints
Expand Down Expand Up @@ -171,7 +171,7 @@ items:
name: namespace-reader
rules:
- apiGroups: ["", "extensions", "apps"]
resources: ["services", "endpoints"]
resources: ["pods", "services", "endpoints"]
verbs: ["get", "list", "watch"]
- apiVersion: apps/v1
kind: Deployment
Expand All @@ -186,10 +186,10 @@ items:
labels:
app: spring-cloud-kubernetes-discoveryserver
spec:
serviceAccount: spring-cloud-kubernetes-discoveryserver
serviceAccountName: spring-cloud-kubernetes-discoveryserver
containers:
- name: spring-cloud-kubernetes-discoveryserver
image: springcloud/spring-cloud-kubernetes-discoveryserver:3.0.0-SNAPSHOT
image: springcloud/spring-cloud-kubernetes-discoveryserver:3.1.0
imagePullPolicy: IfNotPresent
readinessProbe:
httpGet:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2024 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.
Expand All @@ -17,10 +17,10 @@
package org.springframework.cloud.kubernetes.client.loadbalancer;

import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.loadbalancer.ConditionalOnKubernetesLoadBalancerEnabled;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadBalancerProperties;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.context.annotation.Bean;
Expand All @@ -32,7 +32,7 @@
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(KubernetesLoadBalancerProperties.class)
@ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES)
@ConditionalOnProperty(value = "spring.cloud.kubernetes.loadbalancer.enabled", matchIfMissing = true)
@ConditionalOnKubernetesLoadBalancerEnabled
@LoadBalancerClients(defaultConfiguration = KubernetesClientLoadBalancerClientConfiguration.class)
public class KubernetesClientLoadBalancerAutoConfiguration {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2024 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.
Expand Down Expand Up @@ -27,7 +27,6 @@
import reactor.core.publisher.Flux;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties;
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServiceInstanceMapper;
Expand All @@ -41,23 +40,16 @@ public class KubernetesClientServicesListSupplier extends KubernetesServicesList

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

private CoreV1Api coreV1Api;
private final CoreV1Api coreV1Api;

private KubernetesClientProperties kubernetesClientProperties;

private KubernetesNamespaceProvider kubernetesNamespaceProvider;
private final String namespace;

public KubernetesClientServicesListSupplier(Environment environment,
KubernetesServiceInstanceMapper<V1Service> mapper, KubernetesDiscoveryProperties discoveryProperties,
CoreV1Api coreV1Api, KubernetesNamespaceProvider kubernetesNamespaceProvider) {
super(environment, mapper, discoveryProperties);
this.coreV1Api = coreV1Api;
this.kubernetesNamespaceProvider = kubernetesNamespaceProvider;
}

private String getNamespace() {
return kubernetesNamespaceProvider != null ? kubernetesNamespaceProvider.getNamespace()
: kubernetesClientProperties.namespace();
this.namespace = kubernetesNamespaceProvider.getNamespace();
}

@Override
Expand All @@ -71,7 +63,7 @@ public Flux<List<ServiceInstance>> get() {
null, null, null, null, null, null, null, null).getItems();
}
else {
services = coreV1Api.listNamespacedService(getNamespace(), null, null, null,
services = coreV1Api.listNamespacedService(namespace, null, null, null,
"metadata.name=" + this.getServiceId(), null, null, null, null, null, null, null).getItems();
}
services.forEach(service -> result.add(mapper.map(service)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2019-2024 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.commons.loadbalancer;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

/**
* Provides a more succinct conditional
* <code>spring.cloud.kubernetes.loadbalancer.enabled</code>.
*
* @author wind57
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ConditionalOnProperty(value = "spring.cloud.kubernetes.loadbalancer.enabled", matchIfMissing = true)
public @interface ConditionalOnKubernetesLoadBalancerEnabled {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2021 the original author or authors.
* Copyright 2013-2024 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.
Expand Down Expand Up @@ -52,8 +52,21 @@ public Flux<ServiceInstance> appInstances(@PathVariable String name) {
return reactiveDiscoveryClient.getInstances(name);
}

/**
* use the "appInstanceNonDeprecated" instead.
*/
@Deprecated(forRemoval = true)
@GetMapping("/app/{name}/{instanceId}")
public Mono<ServiceInstance> appInstance(@PathVariable String name, @PathVariable String instanceId) {
return innerAppInstance(name, instanceId);
}

@GetMapping("/apps/{name}/{instanceId}")
Mono<ServiceInstance> appInstanceNonDeprecated(@PathVariable String name, @PathVariable String instanceId) {
return innerAppInstance(name, instanceId);
}

private Mono<ServiceInstance> innerAppInstance(String name, String instanceId) {
return reactiveDiscoveryClient.getInstances(name)
.filter(serviceInstance -> serviceInstance.getInstanceId().equals(instanceId)).singleOrEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DiscoveryServerIntegrationInstanceEndpointTest {
private WebTestClient webTestClient;

@Test
void instance() {
void instanceDeprecated() {
Map<String, String> metadata = new HashMap<>();
metadata.put("spring", "true");
metadata.put("port.http", "8080");
Expand All @@ -91,6 +91,24 @@ void instance() {
.isEqualTo(kubernetesServiceInstance);
}

@Test
void instance() {
Map<String, String> metadata = new HashMap<>();
metadata.put("spring", "true");
metadata.put("port.http", "8080");
metadata.put("k8s_namespace", "namespace");
metadata.put("type", "ClusterIP");
metadata.put("k8s", "true");

DefaultKubernetesServiceInstance kubernetesServiceInstance = new DefaultKubernetesServiceInstance(
TEST_ENDPOINTS.getSubsets().get(0).getAddresses().get(0).getTargetRef().getUid(),
TEST_SERVICE.getMetadata().getName(), TEST_ENDPOINTS.getSubsets().get(0).getAddresses().get(0).getIp(),
TEST_ENDPOINTS.getSubsets().get(0).getPorts().get(0).getPort(), metadata, false,
TEST_SERVICE.getMetadata().getNamespace(), null);
webTestClient.get().uri("/apps/test-svc-3/uid2").exchange().expectBody(DefaultKubernetesServiceInstance.class)
.isEqualTo(kubernetesServiceInstance);
}

@TestConfiguration
static class TestConfig {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2024 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.
Expand All @@ -17,10 +17,10 @@
package org.springframework.cloud.kubernetes.fabric8.loadbalancer;

import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.loadbalancer.ConditionalOnKubernetesLoadBalancerEnabled;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadBalancerProperties;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.context.annotation.Bean;
Expand All @@ -34,7 +34,7 @@
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(KubernetesLoadBalancerProperties.class)
@ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES)
@ConditionalOnProperty(value = "spring.cloud.kubernetes.loadbalancer.enabled", matchIfMissing = true)
@ConditionalOnKubernetesLoadBalancerEnabled
@LoadBalancerClients(defaultConfiguration = Fabric8LoadBalancerClientConfiguration.class)
public class Fabric8LoadBalancerAutoConfiguration {

Expand Down

0 comments on commit 73f40dd

Please sign in to comment.