Skip to content

Commit 65f5109

Browse files
authored
Fabric leader clean up 8 (#1650)
1 parent 53fa555 commit 65f5109

File tree

2 files changed

+60
-59
lines changed

2 files changed

+60
-59
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2019 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,93 +17,103 @@
1717
package org.springframework.cloud.kubernetes.fabric8.leader;
1818

1919
import java.util.Map;
20+
import java.util.concurrent.locks.ReentrantLock;
2021

2122
import io.fabric8.kubernetes.api.model.ConfigMap;
2223
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
2324
import io.fabric8.kubernetes.client.KubernetesClient;
2425
import io.fabric8.kubernetes.client.KubernetesClientException;
25-
import org.slf4j.Logger;
26-
import org.slf4j.LoggerFactory;
2726

2827
import org.springframework.cloud.kubernetes.commons.leader.Leader;
2928
import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties;
3029
import org.springframework.cloud.kubernetes.commons.leader.LeadershipController;
3130
import org.springframework.cloud.kubernetes.commons.leader.PodReadinessWatcher;
31+
import org.springframework.core.log.LogAccessor;
3232
import org.springframework.integration.leader.Candidate;
3333
import org.springframework.integration.leader.event.LeaderEventPublisher;
3434

35+
import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.guarded;
36+
3537
/**
3638
* @author Gytis Trikleris
3739
*/
3840
public class Fabric8LeadershipController extends LeadershipController {
3941

40-
private static final Logger LOGGER = LoggerFactory.getLogger(Fabric8LeadershipController.class);
42+
private static final LogAccessor LOGGER = new LogAccessor(Fabric8LeadershipController.class);
4143

4244
private final KubernetesClient kubernetesClient;
4345

46+
private final ReentrantLock lock = new ReentrantLock();
47+
4448
public Fabric8LeadershipController(Candidate candidate, LeaderProperties leaderProperties,
4549
LeaderEventPublisher leaderEventPublisher, KubernetesClient kubernetesClient) {
4650
super(candidate, leaderProperties, leaderEventPublisher);
4751
this.kubernetesClient = kubernetesClient;
4852
}
4953

5054
@Override
51-
public synchronized void update() {
52-
LOGGER.debug("Checking leader state");
53-
ConfigMap configMap = getConfigMap();
54-
if (configMap == null && !leaderProperties.isCreateConfigMap()) {
55-
LOGGER.warn("ConfigMap '{}' does not exist and leaderProperties.isCreateConfigMap() "
56-
+ "is false, cannot acquire leadership", leaderProperties.getConfigMapName());
57-
notifyOnFailedToAcquire();
58-
return;
59-
}
60-
Leader leader = extractLeader(configMap);
55+
public void update() {
56+
guarded(lock, () -> {
57+
LOGGER.debug(() -> "Checking leader state");
58+
ConfigMap configMap = getConfigMap();
59+
if (configMap == null && !leaderProperties.isCreateConfigMap()) {
60+
LOGGER.warn("ConfigMap '" + leaderProperties.getConfigMapName()
61+
+ "' does not exist and leaderProperties.isCreateConfigMap() "
62+
+ "is false, cannot acquire leadership");
63+
notifyOnFailedToAcquire();
64+
return;
65+
}
66+
Leader leader = extractLeader(configMap);
6167

62-
if (leader != null && isPodReady(leader.getId())) {
63-
handleLeaderChange(leader);
64-
return;
65-
}
68+
if (leader != null && isPodReady(leader.getId())) {
69+
handleLeaderChange(leader);
70+
return;
71+
}
72+
73+
if (leader != null && leader.isCandidate(candidate)) {
74+
revoke(configMap);
75+
}
76+
else {
77+
acquire(configMap);
78+
}
79+
});
6680

67-
if (leader != null && leader.isCandidate(this.candidate)) {
68-
revoke(configMap);
69-
}
70-
else {
71-
acquire(configMap);
72-
}
7381
}
7482

75-
public synchronized void revoke() {
76-
ConfigMap configMap = getConfigMap();
77-
Leader leader = extractLeader(configMap);
83+
public void revoke() {
84+
guarded(lock, () -> {
85+
ConfigMap configMap = getConfigMap();
86+
Leader leader = extractLeader(configMap);
7887

79-
if (leader != null && leader.isCandidate(this.candidate)) {
80-
revoke(configMap);
81-
}
88+
if (leader != null && leader.isCandidate(candidate)) {
89+
revoke(configMap);
90+
}
91+
});
8292
}
8393

8494
private void revoke(ConfigMap configMap) {
85-
LOGGER.debug("Trying to revoke leadership for '{}'", this.candidate);
95+
LOGGER.debug(() -> "Trying to revoke leadership for :" + candidate);
8696

8797
try {
8898
String leaderKey = getLeaderKey();
8999
removeConfigMapEntry(configMap, leaderKey);
90100
handleLeaderChange(null);
91101
}
92102
catch (KubernetesClientException e) {
93-
LOGGER.warn("Failure when revoking leadership for '{}': {}", this.candidate, e.getMessage());
103+
LOGGER.warn("Failure when revoking leadership for : " + candidate + "because : " + e.getMessage());
94104
}
95105
}
96106

97107
private void acquire(ConfigMap configMap) {
98-
LOGGER.debug("Trying to acquire leadership for '{}'", this.candidate);
108+
LOGGER.debug(() -> "Trying to acquire leadership for :" + this.candidate);
99109

100-
if (!isPodReady(this.candidate.getId())) {
101-
LOGGER.debug("Pod of '{}' is not ready at the moment, cannot acquire leadership", this.candidate);
110+
if (!isPodReady(candidate.getId())) {
111+
LOGGER.debug("Pod : " + candidate + "is not ready at the moment, cannot acquire leadership");
102112
return;
103113
}
104114

105115
try {
106-
Map<String, String> data = getLeaderData(this.candidate);
116+
Map<String, String> data = getLeaderData(candidate);
107117

108118
if (configMap == null) {
109119
createConfigMap(data);
@@ -112,11 +122,11 @@ private void acquire(ConfigMap configMap) {
112122
updateConfigMapEntry(configMap, data);
113123
}
114124

115-
Leader newLeader = new Leader(this.candidate.getRole(), this.candidate.getId());
125+
Leader newLeader = new Leader(candidate.getRole(), candidate.getId());
116126
handleLeaderChange(newLeader);
117127
}
118128
catch (KubernetesClientException e) {
119-
LOGGER.warn("Failure when acquiring leadership for '{}': {}", this.candidate, e.getMessage());
129+
LOGGER.warn(() -> "Failure when acquiring leadership for : " + candidate + " because : " + e.getMessage());
120130
notifyOnFailedToAcquire();
121131
}
122132
}
@@ -135,47 +145,39 @@ private Leader extractLeader(ConfigMap configMap) {
135145
}
136146

137147
private boolean isPodReady(String name) {
138-
return this.kubernetesClient.pods().withName(name).isReady();
148+
return kubernetesClient.pods().withName(name).isReady();
139149
}
140150

141151
private ConfigMap getConfigMap() {
142-
return this.kubernetesClient.configMaps()
143-
.inNamespace(this.leaderProperties.getNamespace(this.kubernetesClient.getNamespace()))
144-
.withName(this.leaderProperties.getConfigMapName()).get();
152+
return kubernetesClient.configMaps().inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace()))
153+
.withName(leaderProperties.getConfigMapName()).get();
145154
}
146155

147156
private void createConfigMap(Map<String, String> data) {
148-
LOGGER.debug("Creating new config map with data: {}", data);
157+
LOGGER.debug(() -> "Creating new config map with data: " + data);
149158

150-
ConfigMap newConfigMap = new ConfigMapBuilder().withNewMetadata()
151-
.withName(this.leaderProperties.getConfigMapName()).addToLabels(PROVIDER_KEY, PROVIDER)
152-
.addToLabels(KIND_KEY, KIND).endMetadata().addToData(data).build();
159+
ConfigMap newConfigMap = new ConfigMapBuilder().withNewMetadata().withName(leaderProperties.getConfigMapName())
160+
.addToLabels(PROVIDER_KEY, PROVIDER).addToLabels(KIND_KEY, KIND).endMetadata().addToData(data).build();
153161

154-
this.kubernetesClient.configMaps()
155-
.inNamespace(this.leaderProperties.getNamespace(this.kubernetesClient.getNamespace()))
162+
kubernetesClient.configMaps().inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace()))
156163
.resource(newConfigMap).create();
157164
}
158165

159166
private void updateConfigMapEntry(ConfigMap configMap, Map<String, String> newData) {
160-
LOGGER.debug("Adding new data to config map: {}", newData);
161-
167+
LOGGER.debug(() -> "Adding new data to config map: " + newData);
162168
ConfigMap newConfigMap = new ConfigMapBuilder(configMap).addToData(newData).build();
163-
164169
updateConfigMap(configMap, newConfigMap);
165170
}
166171

167172
private void removeConfigMapEntry(ConfigMap configMap, String key) {
168-
LOGGER.debug("Removing config map entry '{}'", key);
169-
173+
LOGGER.debug(() -> "Removing config map entry: " + key);
170174
ConfigMap newConfigMap = new ConfigMapBuilder(configMap).removeFromData(key).build();
171-
172175
updateConfigMap(configMap, newConfigMap);
173176
}
174177

175178
private void updateConfigMap(ConfigMap oldConfigMap, ConfigMap newConfigMap) {
176-
this.kubernetesClient.configMaps()
177-
.inNamespace(this.leaderProperties.getNamespace(this.kubernetesClient.getNamespace()))
178-
.resource(newConfigMap).lockResourceVersion(oldConfigMap.getMetadata().getResourceVersion()).replace();
179+
kubernetesClient.configMaps().inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace()))
180+
.resource(newConfigMap).lockResourceVersion(oldConfigMap.getMetadata().getResourceVersion()).update();
179181
}
180182

181183
}

spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeadershipControllerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ void whenNonExistentConfigmapAndCreationNotAllowedStopLeadershipAcquire(Captured
9494
fabric8LeadershipController.update();
9595

9696
// then
97-
assertThat(output).contains("ConfigMap '" + testConfigmap + "' does not exist "
98-
+ "and leaderProperties.isCreateConfigMap() is false, cannot acquire leadership");
97+
assertThat(output).contains("ConfigMap 'test-configmap' does not exist and leaderProperties.isCreateConfigMap() is false, cannot acquire leadership");
9998
verify(mockLeaderEventPublisher).publishOnFailedToAcquire(any(), any(), any());
10099

101100
verify(mockKubernetesClient, never()).pods();

0 commit comments

Comments
 (0)