Skip to content

Commit

Permalink
Merge branch 'main' into major-changes-in-k8s-discovery-implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed Mar 13, 2024
2 parents f8e6f8b + bf61054 commit 4fd6c0d
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 284 deletions.
2 changes: 1 addition & 1 deletion docs/antora-playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ runtime:
format: pretty
ui:
bundle:
url: https://github.com/spring-io/antora-ui-spring/releases/download/v0.4.2/ui-bundle.zip
url: https://github.com/spring-io/antora-ui-spring/releases/download/v0.4.11/ui-bundle.zip
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 @@ -18,9 +18,9 @@

import io.kubernetes.client.openapi.apis.CoreV1Api;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.loadbalancer.ConditionalOnKubernetesLoadBalancerServiceModeEnabled;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
Expand All @@ -32,7 +32,7 @@
public class KubernetesClientLoadBalancerClientConfiguration {

@Bean
@ConditionalOnProperty(name = "spring.cloud.kubernetes.loadbalancer.mode", havingValue = "SERVICE")
@ConditionalOnKubernetesLoadBalancerServiceModeEnabled
ServiceInstanceListSupplier kubernetesServicesListSupplier(Environment environment, CoreV1Api coreV1Api,
KubernetesClientServiceInstanceMapper mapper, KubernetesDiscoveryProperties discoveryProperties,
KubernetesNamespaceProvider kubernetesNamespaceProvider, ConfigurableApplicationContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 conditional for: <code>spring.cloud.kubernetes.loadbalancer.mode</code>.
*
* @author wind57
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ConditionalOnProperty(name = "spring.cloud.kubernetes.loadbalancer.mode", havingValue = "SERVICE")
public @interface ConditionalOnKubernetesLoadBalancerServiceModeEnabled {

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<properties>
<docker.registry.organization>springcloud</docker.registry.organization>
<plexus-archiver.version>4.1.0</plexus-archiver.version>
<plexus-archiver.version>4.8.0</plexus-archiver.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<properties>
<docker.registry.organization>springcloud</docker.registry.organization>
<plexus-archiver.version>4.1.0</plexus-archiver.version>
<plexus-archiver.version>4.8.0</plexus-archiver.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<properties>
<docker.registry.organization>springcloud</docker.registry.organization>
<plexus-archiver.version>4.1.0</plexus-archiver.version>
<plexus-archiver.version>4.8.0</plexus-archiver.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,18 @@ items:
- apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: namespace-reader
namespace: default
name: namespace-reader
rules:
- apiGroups: ["", "extensions", "apps"]
resources: ["configmaps", "pods", "services", "endpoints", "secrets"]
verbs: ["get", "list", "watch"]
- apiGroups: ["", "extensions", "apps"]
resources: ["configmaps", "pods", "services", "endpoints", "secrets"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- apiVersion: apps/v1
kind: Deployment
metadata:
name: kubernetes-leader-election-example
spec:
replicas: 4
selector:
matchLabels:
app: kubernetes-leader-election-example
Expand Down Expand Up @@ -148,17 +149,10 @@ And check the leadership information again:
curl $SERVICE_URL
```

Now you should receive a message like this:
```
I am 'kubernetes-leader-election-example-1234567890-abcde' but I am not a leader of the 'world'
```

If you wouldn't do anything for a few seconds, the same instance will become a leader again because it only yielded its leadership but stayed in the cluster.
If another instance was able to acquire the leadership you should see a different instance is now the leadership. You may
have to try this a few times depending on how the service is load balanced.

Now scale the application to two instances and try all the steps again:
```
kubectl scale --replicas=2 deployment.apps/kubernetes-leader-election-example
```
`DEBUG` logging is enabled so you can view the logs of the instances in order to see which instance is acquiring leadership.

> Note: with multiple replicas in the cluster, `curl` command will access one of them depending on the Kubernetes load balancing configuration.
Thus, when trying to yield the leadership, request might go to a non-leader node first. Just execute command again until it reaches the correct node.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public LeaderController() throws UnknownHostException {
* Return a message whether this instance is a leader or not.
* @return info
*/
@GetMapping
@GetMapping("/")
public String getInfo() {
if (this.context == null) {
return String.format("I am '%s' but I am not a leader of the '%s'", this.host, this.role);
Expand All @@ -63,7 +63,7 @@ public String getInfo() {
* to give up the leadership.
* @return info about leadership
*/
@PutMapping
@PutMapping("/")
public ResponseEntity<String> revokeLeadership() {
if (this.context == null) {
String message = String.format("Cannot revoke leadership because '%s' is not a leader", this.host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
spring.cloud.kubernetes.leader.role=world
# Configmap to which leader election metadata will be saved
spring.cloud.kubernetes.leader.config-map-name=leader
logging.level.org.springframework.cloud.kubernetes.fabric8.leader=DEBUG
logging.level.org.springframework.cloud.kubernetes.commons.leader=DEBUG
Empty file.
Empty file.
Empty file.
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 @@ -18,8 +18,8 @@

import io.fabric8.kubernetes.client.KubernetesClient;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.loadbalancer.ConditionalOnKubernetesLoadBalancerServiceModeEnabled;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
Expand All @@ -33,7 +33,7 @@
public class Fabric8LoadBalancerClientConfiguration {

@Bean
@ConditionalOnProperty(name = "spring.cloud.kubernetes.loadbalancer.mode", havingValue = "SERVICE")
@ConditionalOnKubernetesLoadBalancerServiceModeEnabled
ServiceInstanceListSupplier kubernetesServicesListSupplier(Environment environment,
KubernetesClient kubernetesClient, Fabric8ServiceInstanceMapper mapper,
KubernetesDiscoveryProperties discoveryProperties, ConfigurableApplicationContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class Fabric8ServiceInstanceMapper implements KubernetesServiceInstanceMa

@Override
public KubernetesServiceInstance map(Service service) {
final ObjectMeta meta = service.getMetadata();
final List<ServicePort> ports = service.getSpec().getPorts();
ObjectMeta meta = service.getMetadata();
List<ServicePort> ports = service.getSpec().getPorts();
ServicePort port = null;
if (ports.size() == 1) {
port = ports.get(0);
Expand All @@ -67,16 +67,16 @@ else if (ports.size() > 1 && Utils.isNotNullOrEmpty(this.properties.getPortName(
if (port == null) {
return null;
}
final String host = KubernetesServiceInstanceMapper.createHost(service.getMetadata().getName(),
String host = KubernetesServiceInstanceMapper.createHost(service.getMetadata().getName(),
service.getMetadata().getNamespace(), properties.getClusterDomain());
final boolean secure = KubernetesServiceInstanceMapper.isSecure(service.getMetadata().getLabels(),
boolean secure = KubernetesServiceInstanceMapper.isSecure(service.getMetadata().getLabels(),
service.getMetadata().getAnnotations(), port.getName(), port.getPort());
return new DefaultKubernetesServiceInstance(meta.getUid(), meta.getName(), host, port.getPort(),
getServiceMetadata(service), secure);
}

private Map<String, String> getServiceMetadata(Service service) {
final Map<String, String> serviceMetadata = new HashMap<>();
Map<String, String> serviceMetadata = new HashMap<>();
KubernetesDiscoveryProperties.Metadata metadataProps = this.discoveryProperties.metadata();
if (metadataProps.addLabels()) {
Map<String, String> labelMetadata = KubernetesServiceInstanceMapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ public class Fabric8ServicesListSupplier extends KubernetesServicesListSupplier<
public Flux<List<ServiceInstance>> get() {
List<ServiceInstance> result = new ArrayList<>();
if (discoveryProperties.allNamespaces()) {
List<Service> services = this.kubernetesClient.services().inAnyNamespace()
.withField("metadata.name", this.getServiceId()).list().getItems();
List<Service> services = kubernetesClient.services().inAnyNamespace()
.withField("metadata.name", getServiceId()).list().getItems();
services.forEach(service -> result.add(mapper.map(service)));
}
else {
Service service = StringUtils.hasText(this.kubernetesClient.getNamespace())
? this.kubernetesClient.services().inNamespace(this.kubernetesClient.getNamespace())
.withName(this.getServiceId()).get()
: this.kubernetesClient.services().withName(this.getServiceId()).get();
Service service = StringUtils.hasText(kubernetesClient.getNamespace()) ? kubernetesClient.services()
.inNamespace(kubernetesClient.getNamespace()).withName(getServiceId()).get()
: kubernetesClient.services().withName(getServiceId()).get();
if (service != null) {
result.add(mapper.map(service));
}
Expand Down

This file was deleted.

Loading

0 comments on commit 4fd6c0d

Please sign in to comment.