Skip to content

Commit 42acea1

Browse files
committed
Bumping versions
1 parent abbe91a commit 42acea1

File tree

18 files changed

+284
-183
lines changed

18 files changed

+284
-183
lines changed

README.adoc

Lines changed: 115 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ spring.cloud.kubernetes.discovery.enabled=false
124124
Some Spring Cloud components use the `DiscoveryClient` in order to obtain information about the local service instance. For
125125
this to work, you need to align the Kubernetes service name with the `spring.application.name` property.
126126

127+
Spring Cloud Kubernetes can also watch the Kubernetes service catalog for changes and update the
128+
`DiscoveryClient` implementation accordingly. In order to enable this functionality you need to add
129+
`@EnableScheduling` on a configuration class in your application.
130+
127131
== Kubernetes native service discovery
128132

129133
Kubernetes itself is capable of (server side) service discovery (see: https://kubernetes.io/docs/concepts/services-networking/service/#discovering-services).
@@ -138,7 +142,7 @@ Additionally, you can use Hystrix for:
138142

139143
== Kubernetes PropertySource implementations
140144

141-
The most common approach to configuring your Spring Boot application is to create an `application.properties` or `applicaiton.yaml` or
145+
The most common approach to configuring your Spring Boot application is to create an `application.properties` or `application.yaml` or
142146
an `application-profile.properties` or `application-profile.yaml` file that contains key-value pairs that provide customization values to your
143147
application or Spring Boot starters. You can override these properties by specifying system properties or environment
144148
variables.
@@ -320,26 +324,89 @@ However, if the `production` profile is active, the configuration becomes:
320324

321325
If both profiles are active, the property that appears last within the `ConfigMap` overwrites any preceding values.
322326

323-
324-
To tell Spring Boot which `profile` should be enabled at bootstrap, you can pass a system property to the Java
325-
command. To do so, you can launch your Spring Boot application with an environment variable that you can define with the OpenShift
326-
`DeploymentConfig` or Kubernetes `ReplicationConfig` resource file, as follows:
327+
Another option is to create a different config map per profile and spring boot will automatically fetch it based
328+
on active profiles
327329

328330
====
329331
[source,yaml]
330332
----
333+
kind: ConfigMap
331334
apiVersion: v1
332-
kind: DeploymentConfig
335+
metadata:
336+
name: demo
337+
data:
338+
application.yml: |-
339+
greeting:
340+
message: Say Hello to the World
341+
farewell:
342+
message: Say Goodbye
343+
----
344+
====
345+
====
346+
[source,yaml]
347+
----
348+
kind: ConfigMap
349+
apiVersion: v1
350+
metadata:
351+
name: demo-development
352+
data:
353+
application.yml: |-
354+
spring:
355+
profiles: development
356+
greeting:
357+
message: Say Hello to the Developers
358+
farewell:
359+
message: Say Goodbye to the Developers
360+
----
361+
====
362+
====
363+
[source,yaml]
364+
----
365+
kind: ConfigMap
366+
apiVersion: v1
367+
metadata:
368+
name: demo-production
369+
data:
370+
application.yml: |-
371+
spring:
372+
profiles: production
373+
greeting:
374+
message: Say Hello to the Ops
375+
farewell:
376+
message: Say Goodbye
377+
----
378+
====
379+
380+
381+
To tell Spring Boot which `profile` should be enabled at bootstrap, you can pass `SPRING_PROFILES_ACTIVE` environment variable.
382+
To do so, you can launch your Spring Boot application with an environment variable that you can define it in the PodSpec at the container specification.
383+
Deployment resource file, as follows:
384+
385+
====
386+
[source,yaml]
387+
----
388+
apiVersion: apps/v1
389+
kind: Deployment
390+
metadata:
391+
name: deployment-name
392+
labels:
393+
app: deployment-name
333394
spec:
334395
replicas: 1
335-
...
336-
spec:
337-
containers:
338-
- env:
339-
- name: JAVA_APP_DIR
340-
value: /deployments
341-
- name: JAVA_OPTIONS
342-
value: -Dspring.profiles.active=developer
396+
selector:
397+
matchLabels:
398+
app: deployment-name
399+
template:
400+
metadata:
401+
labels:
402+
app: deployment-name
403+
spec:
404+
containers:
405+
- name: container-name
406+
image: your-image
407+
env:
408+
- name: SPRING_PROFILES_ACTIVE
409+
value: "development"
343410
----
344411
====
345412

@@ -483,6 +550,36 @@ If you have all the secrets mapped to a common root, you can set them like:
483550
----
484551
====
485552

553+
As the case with `ConfigMap`, more advanced configuration is also possible where you can use multiple `Secret`
554+
instances. The `spring.cloud.kubernetes.secrets.sources` list makes this possible.
555+
For example, you could define the following `Secret` instances:
556+
557+
====
558+
[source,yaml]
559+
----
560+
spring:
561+
application:
562+
name: cloud-k8s-app
563+
cloud:
564+
kubernetes:
565+
secrets:
566+
name: default-name
567+
namespace: default-namespace
568+
sources:
569+
# Spring Cloud Kubernetes looks up a Secret named s1 in namespace default-namespace
570+
- name: s1
571+
# Spring Cloud Kubernetes looks up a Secret named default-name in whatever namespace n2
572+
- namespace: n2
573+
# Spring Cloud Kubernetes looks up a Secret named s3 in namespace n3
574+
- namespace: n3
575+
name: s3
576+
----
577+
====
578+
579+
In the preceding example, if `spring.cloud.kubernetes.secrets.namespace` had not been set,
580+
the `Secret` named `s1` would be looked up in the namespace that the application runs.
581+
582+
486583
.Properties:
487584
[options="header,footer"]
488585
|===
@@ -584,7 +681,7 @@ The reload feature supports two operating modes:
584681
Any event produces a re-check on the configuration and, in case of changes, a reload.
585682
The `view` role on the service account is required in order to listen for config map changes. A higher level role (such as `edit`) is required for secrets
586683
(by default, secrets are not monitored).
587-
* Polling: Oeriodically re-creates the configuration from config maps and secrets to see if it has changed.
684+
* Polling: Periodically re-creates the configuration from config maps and secrets to see if it has changed.
588685
You can configure the polling period by using the `spring.cloud.kubernetes.reload.period` property and defaults to 15 seconds.
589686
It requires the same role as the monitored property source.
590687
This means, for example, that using polling on file-mounted secret sources does not require particular privileges.
@@ -596,7 +693,7 @@ This means, for example, that using polling on file-mounted secret sources does
596693
| `spring.cloud.kubernetes.reload.enabled` | `Boolean` | `false` | Enables monitoring of property sources and configuration reload
597694
| `spring.cloud.kubernetes.reload.monitoring-config-maps` | `Boolean` | `true` | Allow monitoring changes in config maps
598695
| `spring.cloud.kubernetes.reload.monitoring-secrets` | `Boolean` | `false` | Allow monitoring changes in secrets
599-
| `spring.cloud.kubernetes.reload.strategy ` | `Enum` | `refresh` | The strategy to use when firing a reload (`refresh`, `restart_context`, or `shutdown`)
696+
| `spring.cloud.kubernetes.reload.strategy` | `Enum` | `refresh` | The strategy to use when firing a reload (`refresh`, `restart_context`, or `shutdown`)
600697
| `spring.cloud.kubernetes.reload.mode` | `Enum` | `event` | Specifies how to listen for changes in property sources (`event` or `polling`)
601698
| `spring.cloud.kubernetes.reload.period` | `Duration`| `15s` | The period for verifying changes when using the `polling` strategy
602699
|===
@@ -652,7 +749,6 @@ the `PortName` key. If you want to specify in which Kubernetes namespace the tar
652749
the `KubernetesNamespace` key, remembering in both instances to prefix these keys with your service name and
653750
`ribbon` prefix, as specified earlier.
654751

655-
656752
.Spring Cloud Kubernetes Ribbon Configuration
657753
|===
658754
|Property Key |Type |Default Value
@@ -679,7 +775,6 @@ the `SERVICE` mode uses load balancing of the Kubernetes service to support Isti
679775

680776
* `spring.cloud.kubernetes.ribbon.cluster-domain` Set the custom Kubernetes cluster domain suffix. The default value is: 'cluster.local'
681777

682-
683778
The following examples use this module for ribbon discovery:
684779

685780
* link:./spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example[Spring Cloud Circuitbreaker and Ribbon]
@@ -1011,6 +1106,7 @@ $ touch .springformat
10111106
==== Intellij IDEA
10121107

10131108
In order to setup Intellij you should import our coding conventions, inspection profiles and set up the checkstyle plugin.
1109+
The following files can be found in the https://github.com/spring-cloud/spring-cloud-build/tree/master/spring-cloud-build-tools[Spring Cloud Build] project.
10141110

10151111
.spring-cloud-build-tools/
10161112
----
@@ -1055,4 +1151,4 @@ Go to `File` -> `Settings` -> `Other settings` -> `Checkstyle`. There click on t
10551151
- `checkstyle.suppressions.file` - default suppressions. Please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` URL.
10561152
- `checkstyle.additional.suppressions.file` - this variable corresponds to suppressions in your local project. E.g. you're working on `spring-cloud-contract`. Then point to the `project-root/src/checkstyle/checkstyle-suppressions.xml` folder. Example for `spring-cloud-contract` would be: `/home/username/spring-cloud-contract/src/checkstyle/checkstyle-suppressions.xml`.
10571153

1058-
IMPORTANT: Remember to set the `Scan Scope` to `All sources` since we apply checkstyle rules for production and test sources.
1154+
IMPORTANT: Remember to set the `Scan Scope` to `All sources` since we apply checkstyle rules for production and test sources.

spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySource.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,18 @@ private static Map<String, String> getData(KubernetesClient client, String name,
9999
}
100100

101101
if (environment != null) {
102-
for (String activeProfile:environment.getActiveProfiles()) {
102+
for (String activeProfile : environment.getActiveProfiles()) {
103103

104104
String mapNameWithProfile = name + "-" + activeProfile;
105105

106106
ConfigMap mapWithProfile = StringUtils.isEmpty(namespace)
107-
? client.configMaps().withName(mapNameWithProfile).get()
108-
: client.configMaps().inNamespace(namespace).withName(mapNameWithProfile).get();
107+
? client.configMaps().withName(mapNameWithProfile).get()
108+
: client.configMaps().inNamespace(namespace)
109+
.withName(mapNameWithProfile).get();
109110

110111
if (mapWithProfile != null) {
111-
result.putAll(processAllEntries(mapWithProfile.getData(), environment));
112+
result.putAll(
113+
processAllEntries(mapWithProfile.getData(), environment));
112114
}
113115

114116
}
@@ -125,7 +127,6 @@ private static Map<String, String> getData(KubernetesClient client, String name,
125127
return new HashMap<>();
126128
}
127129

128-
129130
private static Map<String, String> processAllEntries(Map<String, String> input,
130131
Environment environment) {
131132

spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsConfigProperties.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,16 @@ public List<SecretsConfigProperties.NormalizedSource> determineSources() {
9595
return new ArrayList<SecretsConfigProperties.NormalizedSource>() {
9696
{
9797
add(new SecretsConfigProperties.NormalizedSource(
98-
SecretsConfigProperties.this.name,
99-
SecretsConfigProperties.this.namespace,
100-
SecretsConfigProperties.this.labels));
98+
SecretsConfigProperties.this.name,
99+
SecretsConfigProperties.this.namespace,
100+
SecretsConfigProperties.this.labels));
101101
}
102102
};
103103
}
104104

105105
return this.sources.stream()
106-
.map(s -> s.normalize(this.name, this.namespace, this.labels))
107-
.collect(Collectors.toList());
106+
.map(s -> s.normalize(this.name, this.namespace, this.labels))
107+
.collect(Collectors.toList());
108108
}
109109

110110
public static class Source {
@@ -162,16 +162,16 @@ public boolean isEmpty() {
162162
}
163163

164164
public SecretsConfigProperties.NormalizedSource normalize(String defaultName,
165-
String defaultNamespace, Map<String, String> defaultLabels) {
165+
String defaultNamespace, Map<String, String> defaultLabels) {
166166
final String normalizedName = StringUtils.isEmpty(this.name) ? defaultName
167-
: this.name;
167+
: this.name;
168168
final String normalizedNamespace = StringUtils.isEmpty(this.namespace)
169-
? defaultNamespace : this.namespace;
169+
? defaultNamespace : this.namespace;
170170
final Map<String, String> normalizedLabels = this.labels.isEmpty()
171-
? defaultLabels : this.labels;
171+
? defaultLabels : this.labels;
172172

173173
return new SecretsConfigProperties.NormalizedSource(normalizedName,
174-
normalizedNamespace, normalizedLabels);
174+
normalizedNamespace, normalizedLabels);
175175
}
176176

177177
}

spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsPropertySource.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,21 @@ public class SecretsPropertySource extends MapPropertySource {
4242
private static final String PREFIX = "secrets";
4343

4444
public SecretsPropertySource(KubernetesClient client, Environment env, String name,
45-
String namespace, Map<String, String> labels) {
45+
String namespace, Map<String, String> labels) {
4646
super(getSourceName(client, env, name, namespace),
47-
getSourceData(client, env, name, namespace, labels));
47+
getSourceData(client, env, name, namespace, labels));
4848
}
4949

5050
private static String getSourceName(KubernetesClient client, Environment env,
51-
String name, String namespace) {
51+
String name, String namespace) {
5252
return new StringBuilder().append(PREFIX)
53-
.append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR).append(name)
54-
.append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR).append(namespace)
55-
.toString();
53+
.append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR).append(name)
54+
.append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR).append(namespace)
55+
.toString();
5656
}
5757

5858
private static Map<String, Object> getSourceData(KubernetesClient client,
59-
Environment env, String name, String namespace, Map<String, String> labels) {
59+
Environment env, String name, String namespace, Map<String, String> labels) {
6060
Map<String, Object> result = new HashMap<>();
6161

6262
try {
@@ -74,18 +74,18 @@ private static Map<String, Object> getSourceData(KubernetesClient client,
7474
if (!labels.isEmpty()) {
7575
if (StringUtils.isEmpty(namespace)) {
7676
client.secrets().withLabels(labels).list().getItems()
77-
.forEach(s -> putAll(s, result));
77+
.forEach(s -> putAll(s, result));
7878
}
7979
else {
8080
client.secrets().inNamespace(namespace).withLabels(labels).list()
81-
.getItems().forEach(s -> putAll(s, result));
81+
.getItems().forEach(s -> putAll(s, result));
8282
}
8383
}
8484
}
8585
catch (Exception e) {
8686
LOG.warn("Can't read secret with name: [" + name + "] or labels [" + labels
87-
+ "] in namespace:[" + namespace + "] (cause: " + e.getMessage()
88-
+ "). Ignoring");
87+
+ "] in namespace:[" + namespace + "] (cause: " + e.getMessage()
88+
+ "). Ignoring");
8989
}
9090

9191
return result;
@@ -97,7 +97,7 @@ private static Map<String, Object> getSourceData(KubernetesClient client,
9797
private static void putAll(Secret secret, Map<String, Object> result) {
9898
if (secret != null && secret.getData() != null) {
9999
secret.getData().forEach((k, v) -> result.put(k,
100-
new String(Base64.getDecoder().decode(v)).trim()));
100+
new String(Base64.getDecoder().decode(v)).trim()));
101101
}
102102
}
103103

0 commit comments

Comments
 (0)