Skip to content

Commit 9141091

Browse files
authored
Merge pull request #2229 from wind57/unify_properties
spring.cloud.kubernetes.config / spring.cloud.kubernetes.secret deprecation
2 parents 0406eaa + e0a4c4f commit 9141091

6 files changed

Lines changed: 123 additions & 36 deletions

File tree

spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigMapWatcherChangeDetector.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
import org.springframework.core.env.ConfigurableEnvironment;
3131
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
3232

33-
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.CONFIG_MAP_APPS_ANNOTATION;
34-
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.CONFIG_MAP_LABEL;
35-
3633
/**
3734
* @author Ryan Baxter
3835
* @author Kris Iyer
@@ -59,9 +56,7 @@ abstract sealed class ConfigMapWatcherChangeDetector extends KubernetesClientEve
5956

6057
@Override
6158
protected final void onEvent(KubernetesObject configMap) {
62-
// this::refreshTrigger is coming from BusEventBasedConfigMapWatcherChangeDetector
63-
WatcherUtil.onEvent(configMap, CONFIG_MAP_LABEL, CONFIG_MAP_APPS_ANNOTATION, refreshDelay, executorService,
64-
"config-map", this::triggerRefresh);
59+
WatcherUtil.onEvent(configMap, refreshDelay, executorService, this::triggerRefresh);
6560
}
6661

6762
}

spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationProperties.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,25 @@ public class ConfigurationWatcherConfigurationProperties {
4646
/**
4747
* label to enable refresh/restart when using configmaps.
4848
*/
49+
@Deprecated(forRemoval = true)
4950
public static final String CONFIG_MAP_LABEL = "spring.cloud.kubernetes.config";
5051

5152
/**
5253
* label to enable refresh/restart when using secrets.
5354
*/
55+
@Deprecated(forRemoval = true)
5456
public static final String SECRET_LABEL = "spring.cloud.kubernetes.secret";
5557

5658
/**
5759
* annotation name to enable refresh/restart for specific apps when using configmaps.
5860
*/
61+
@Deprecated(forRemoval = true)
5962
public static final String CONFIG_MAP_APPS_ANNOTATION = "spring.cloud.kubernetes.configmap.apps";
6063

6164
/**
6265
* annotation name to enable refresh/restart for specific apps when using secrets.
6366
*/
67+
@Deprecated(forRemoval = true)
6468
public static final String SECRET_APPS_ANNOTATION = "spring.cloud.kubernetes.secret.apps";
6569

6670
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2013-present 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.configuration.watcher;
18+
19+
import io.kubernetes.client.common.KubernetesObject;
20+
import io.kubernetes.client.openapi.models.V1ConfigMap;
21+
import io.kubernetes.client.openapi.models.V1Secret;
22+
23+
/**
24+
* @author wind57
25+
*/
26+
enum KubernetesSource {
27+
28+
CONFIGMAP {
29+
@Override
30+
String description() {
31+
return "configmap";
32+
}
33+
34+
@Override
35+
String label() {
36+
return "spring.cloud.kubernetes.config";
37+
}
38+
39+
@Override
40+
String annotation() {
41+
return "spring.cloud.kubernetes.configmap.apps";
42+
}
43+
},
44+
SECRET {
45+
@Override
46+
String description() {
47+
return "secret";
48+
}
49+
50+
@Override
51+
String label() {
52+
return "spring.cloud.kubernetes.secret";
53+
}
54+
55+
@Override
56+
String annotation() {
57+
return "spring.cloud.kubernetes.secret.apps";
58+
}
59+
60+
};
61+
62+
abstract String description();
63+
64+
abstract String label();
65+
66+
abstract String annotation();
67+
68+
static KubernetesSource fromK8sType(KubernetesObject kubernetesObject) {
69+
if (kubernetesObject instanceof V1ConfigMap) {
70+
return KubernetesSource.CONFIGMAP;
71+
}
72+
73+
if (kubernetesObject instanceof V1Secret) {
74+
return KubernetesSource.SECRET;
75+
}
76+
77+
throw new IllegalArgumentException("Unsupported: " + kubernetesObject.getClass());
78+
}
79+
80+
}

spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/SecretsWatcherChangeDetector.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
import org.springframework.core.env.ConfigurableEnvironment;
3131
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
3232

33-
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.SECRET_APPS_ANNOTATION;
34-
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.SECRET_LABEL;
35-
3633
/**
3734
* @author Ryan Baxter
3835
* @author Kris Iyer
@@ -58,9 +55,7 @@ abstract sealed class SecretsWatcherChangeDetector extends KubernetesClientEvent
5855

5956
@Override
6057
protected final void onEvent(KubernetesObject secret) {
61-
// this::refreshTrigger is coming from BusEventBasedSecretsWatcherChangeDetector
62-
WatcherUtil.onEvent(secret, SECRET_LABEL, SECRET_APPS_ANNOTATION, refreshDelay, executorService, "secret",
63-
this::triggerRefresh);
58+
WatcherUtil.onEvent(secret, refreshDelay, executorService, this::triggerRefresh);
6459
}
6560

6661
}

spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/WatcherUtil.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
import org.springframework.core.log.LogAccessor;
3535

36+
import static org.springframework.cloud.kubernetes.configuration.watcher.KubernetesSource.fromK8sType;
37+
3638
/**
3739
* A common place where 'onEvent' code delegates to.
3840
*
@@ -45,31 +47,44 @@ final class WatcherUtil {
4547
private WatcherUtil() {
4648
}
4749

48-
static void onEvent(KubernetesObject kubernetesObject, String label, String annotationName, long refreshDelay,
49-
ScheduledExecutorService executorService, String type,
50+
static void onEvent(KubernetesObject kubernetesObject, long refreshDelay, ScheduledExecutorService executorService,
5051
BiFunction<KubernetesObject, String, Mono<Void>> triggerRefresh) {
5152

53+
KubernetesSource source = fromK8sType(kubernetesObject);
54+
5255
String name = kubernetesObject.getMetadata().getName();
53-
boolean isSpringCloudKubernetes = isSpringCloudKubernetes(kubernetesObject, label);
56+
boolean isSpringCloudKubernetes = isSpringCloudKubernetes(kubernetesObject, source.label());
5457

5558
if (isSpringCloudKubernetes) {
5659

57-
Set<String> apps = apps(kubernetesObject, annotationName);
60+
Set<String> apps = apps(kubernetesObject, source.annotation());
5861

5962
if (apps.isEmpty()) {
6063
apps.add(name);
6164
}
6265

6366
LOG.info(() -> "will schedule remote refresh based on apps : " + apps);
64-
apps.forEach(appName -> schedule(type, appName, refreshDelay, executorService, triggerRefresh,
65-
kubernetesObject));
67+
apps.forEach(appName -> schedule(source.description(), appName, refreshDelay, executorService,
68+
triggerRefresh, kubernetesObject));
6669

6770
}
6871
else {
69-
LOG.debug(() -> "Not publishing event." + type + ": " + name + " does not contain the label " + label);
72+
LOG.debug(() -> "Not publishing event : " + source.description() + ": " + name
73+
+ " does not contain the label " + source.label());
7074
}
7175
}
7276

77+
/**
78+
* @deprecated for removal in the next major release, in favor of informer-side
79+
* filtering via {@code spring.cloud.kubernetes.reload.config-maps-labels} and
80+
* {@code spring.cloud.kubernetes.reload.secrets-labels}.
81+
* <p>
82+
* Today the configuration watcher receives all ConfigMap/Secret events from the
83+
* informer and filters them locally by legacy labels. With informer label selectors
84+
* configured, only matching sources are delivered, so this extra local check is no
85+
* longer needed.
86+
*/
87+
@Deprecated(forRemoval = true)
7388
static boolean isSpringCloudKubernetes(KubernetesObject kubernetesObject, String label) {
7489
if (kubernetesObject.getMetadata() == null) {
7590
return false;

spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/test/java/org/springframework/cloud/kubernetes/configuration/watcher/WatcherUtilTests.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,37 @@
2727
import org.assertj.core.api.Assertions;
2828
import org.junit.jupiter.api.Test;
2929

30-
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.CONFIG_MAP_LABEL;
31-
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.SECRET_APPS_ANNOTATION;
32-
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.SECRET_LABEL;
33-
3430
class WatcherUtilTests {
3531

3632
@Test
3733
void isSpringCloudKubernetesConfigFalse() {
3834
V1ConfigMap configMap = new V1ConfigMapBuilder().withMetadata(new V1ObjectMeta().labels(Map.of())).build();
39-
boolean present = WatcherUtil.isSpringCloudKubernetes(configMap, CONFIG_MAP_LABEL);
35+
boolean present = WatcherUtil.isSpringCloudKubernetes(configMap, KubernetesSource.CONFIGMAP.label());
4036
Assertions.assertThat(present).isFalse();
4137
}
4238

4339
@Test
4440
void isSpringCloudKubernetesConfigTrue() {
4541
V1ConfigMap configMap = new V1ConfigMapBuilder()
46-
.withMetadata(new V1ObjectMeta().labels(Map.of(CONFIG_MAP_LABEL, "true")))
42+
.withMetadata(new V1ObjectMeta().labels(Map.of(KubernetesSource.CONFIGMAP.label(), "true")))
4743
.build();
48-
boolean present = WatcherUtil.isSpringCloudKubernetes(configMap, CONFIG_MAP_LABEL);
44+
boolean present = WatcherUtil.isSpringCloudKubernetes(configMap, KubernetesSource.CONFIGMAP.label());
4945
Assertions.assertThat(present).isTrue();
5046
}
5147

5248
@Test
5349
void isSpringCloudKubernetesSecretFalse() {
5450
V1Secret secret = new V1SecretBuilder().withMetadata(new V1ObjectMeta().labels(Map.of())).build();
55-
boolean present = WatcherUtil.isSpringCloudKubernetes(secret, SECRET_LABEL);
51+
boolean present = WatcherUtil.isSpringCloudKubernetes(secret, KubernetesSource.SECRET.label());
5652
Assertions.assertThat(present).isFalse();
5753
}
5854

5955
@Test
6056
void isSpringCloudKubernetesSecretTrue() {
61-
V1Secret secret = new V1SecretBuilder().withMetadata(new V1ObjectMeta().labels(Map.of(SECRET_LABEL, "true")))
57+
V1Secret secret = new V1SecretBuilder()
58+
.withMetadata(new V1ObjectMeta().labels(Map.of(KubernetesSource.SECRET.label(), "true")))
6259
.build();
63-
boolean present = WatcherUtil.isSpringCloudKubernetes(secret, SECRET_LABEL);
60+
boolean present = WatcherUtil.isSpringCloudKubernetes(secret, KubernetesSource.SECRET.label());
6461
Assertions.assertThat(present).isTrue();
6562
}
6663

@@ -81,39 +78,40 @@ void labelsPresent() {
8178
@Test
8279
void appsNoMetadata() {
8380
V1Secret secret = new V1SecretBuilder().build();
84-
Set<String> apps = WatcherUtil.apps(secret, SECRET_APPS_ANNOTATION);
81+
Set<String> apps = WatcherUtil.apps(secret, "spring.cloud.kubernetes.secret.apps");
8582
Assertions.assertThat(apps).isEmpty();
8683
}
8784

8885
@Test
8986
void appsNoAnnotations() {
9087
V1Secret secret = new V1SecretBuilder().withMetadata(new V1ObjectMeta().annotations(Map.of())).build();
91-
Set<String> apps = WatcherUtil.apps(secret, SECRET_APPS_ANNOTATION);
88+
Set<String> apps = WatcherUtil.apps(secret, "spring.cloud.kubernetes.secret.apps");
9289
Assertions.assertThat(apps).isEmpty();
9390
}
9491

9592
@Test
9693
void appsAnnotationNotFound() {
9794
V1Secret secret = new V1SecretBuilder().withMetadata(new V1ObjectMeta().annotations(Map.of("a", "b"))).build();
98-
Set<String> apps = WatcherUtil.apps(secret, SECRET_APPS_ANNOTATION);
95+
Set<String> apps = WatcherUtil.apps(secret, "spring.cloud.kubernetes.secret.apps");
9996
Assertions.assertThat(apps).isEmpty();
10097
}
10198

10299
@Test
103100
void appsSingleResult() {
104101
V1Secret secret = new V1SecretBuilder()
105-
.withMetadata(new V1ObjectMeta().annotations(Map.of(SECRET_APPS_ANNOTATION, "one-app")))
102+
.withMetadata(new V1ObjectMeta().annotations(Map.of("spring.cloud.kubernetes.secret.apps", "one-app")))
106103
.build();
107-
Set<String> apps = WatcherUtil.apps(secret, SECRET_APPS_ANNOTATION);
104+
Set<String> apps = WatcherUtil.apps(secret, "spring.cloud.kubernetes.secret.apps");
108105
Assertions.assertThat(apps).containsExactlyInAnyOrder("one-app");
109106
}
110107

111108
@Test
112109
void appsMultipleResults() {
113110
V1Secret secret = new V1SecretBuilder()
114-
.withMetadata(new V1ObjectMeta().annotations(Map.of(SECRET_APPS_ANNOTATION, "one, two, three ")))
111+
.withMetadata(
112+
new V1ObjectMeta().annotations(Map.of("spring.cloud.kubernetes.secret.apps", "one, two, three ")))
115113
.build();
116-
Set<String> apps = WatcherUtil.apps(secret, SECRET_APPS_ANNOTATION);
114+
Set<String> apps = WatcherUtil.apps(secret, "spring.cloud.kubernetes.secret.apps");
117115
Assertions.assertThat(apps).containsExactlyInAnyOrder("one", "two", "three");
118116
}
119117

0 commit comments

Comments
 (0)