diff --git a/docs/modules/ROOT/partials/_configprops.adoc b/docs/modules/ROOT/partials/_configprops.adoc index 587592c11c..9654b5e985 100644 --- a/docs/modules/ROOT/partials/_configprops.adoc +++ b/docs/modules/ROOT/partials/_configprops.adoc @@ -53,8 +53,7 @@ |spring.cloud.kubernetes.config.include-profile-specific-sources | `+++true+++` | |spring.cloud.kubernetes.config.labels | | |spring.cloud.kubernetes.config.name | | -|spring.cloud.kubernetes.config.namespace | | -|spring.cloud.kubernetes.config.paths | | +|spring.cloud.kubernetes.config.namespace | | |spring.cloud.kubernetes.config.retry | | |spring.cloud.kubernetes.config.sources | | |spring.cloud.kubernetes.config.use-name-as-prefix | `+++false+++` | @@ -108,10 +107,9 @@ |spring.cloud.kubernetes.secrets.include-profile-specific-sources | `+++true+++` | |spring.cloud.kubernetes.secrets.labels | | |spring.cloud.kubernetes.secrets.name | | -|spring.cloud.kubernetes.secrets.namespace | | -|spring.cloud.kubernetes.secrets.paths | | +|spring.cloud.kubernetes.secrets.namespace | | |spring.cloud.kubernetes.secrets.retry | | |spring.cloud.kubernetes.secrets.sources | | |spring.cloud.kubernetes.secrets.use-name-as-prefix | `+++false+++` | -|=== \ No newline at end of file +|=== diff --git a/docs/src/main/asciidoc/property-source-config.adoc b/docs/src/main/asciidoc/property-source-config.adoc new file mode 100644 index 0000000000..ccf3126060 --- /dev/null +++ b/docs/src/main/asciidoc/property-source-config.adoc @@ -0,0 +1,1082 @@ +== Kubernetes PropertySource implementations + +The most common approach to configuring your Spring Boot application is to create an `application.properties` or `application.yaml` or +an `application-profile.properties` or `application-profile.yaml` file that contains key-value pairs that provide customization values to your +application or Spring Boot starters. You can override these properties by specifying system properties or environment +variables. + +To enable this functionality you need to set `spring.config.import=kubernetes:` in your application's configuration properties. +Currently you can not specify a ConfigMap or Secret to load using `spring.config.import`, by default Spring Cloud Kubernetes +will load a ConfigMap and/or Secret based on the `spring.application.name` property. If `spring.application.name` is not set it will +load a ConfigMap and/or Secret with the name `application`. + +If you would like to load Kubernetes ``PropertySource``s during the bootstrap phase like it worked prior to the 3.0.x release +you can either add `spring-cloud-starter-bootstrap` to your application's classpath or set `spring.cloud.bootstrap.enabled=true` +as an environment variable. + +[[configmap-propertysource]] +=== Using a `ConfigMap` `PropertySource` + +Kubernetes provides a resource named https://kubernetes.io/docs/user-guide/configmap/[`ConfigMap`] to externalize the +parameters to pass to your application in the form of key-value pairs or embedded `application.properties` or `application.yaml` files. +The link:https://github.com/spring-cloud/spring-cloud-kubernetes/tree/master/spring-cloud-kubernetes-fabric8-config[Spring Cloud Kubernetes Config] project makes Kubernetes `ConfigMap` instances available +during application startup and triggers hot reloading of beans or Spring context when changes are detected on +observed `ConfigMap` instances. + +Everything that follows is explained mainly referring to examples using ConfigMaps, but the same stands for +Secrets, i.e.: every feature is supported for both. + +The default behavior is to create a `Fabric8ConfigMapPropertySource` (or a `KubernetesClientConfigMapPropertySource`) based on a Kubernetes `ConfigMap` that has a `metadata.name` value of either the name of +your Spring application (as defined by its `spring.application.name` property) or a custom name defined within the +`application.properties` file under the following key: `spring.cloud.kubernetes.config.name`. + +However, more advanced configuration is possible where you can use multiple `ConfigMap` instances. +The `spring.cloud.kubernetes.config.sources` list makes this possible. +For example, you could define the following `ConfigMap` instances: + +==== +[source,yaml] +---- +spring: + application: + name: cloud-k8s-app + cloud: + kubernetes: + config: + name: default-name + namespace: default-namespace + sources: + # Spring Cloud Kubernetes looks up a ConfigMap named c1 in namespace default-namespace + - name: c1 + # Spring Cloud Kubernetes looks up a ConfigMap named default-name in whatever namespace n2 + - namespace: n2 + # Spring Cloud Kubernetes looks up a ConfigMap named c3 in namespace n3 + - namespace: n3 + name: c3 +---- +==== + +In the preceding example, if `spring.cloud.kubernetes.config.namespace` had not been set, +the `ConfigMap` named `c1` would be looked up in the namespace that the application runs. +See <> to get a better understanding of how the namespace +of the application is resolved. + + +Any matching `ConfigMap` that is found is processed as follows: + +* Apply individual configuration properties. +* Apply as `yaml` (or `properties`) the content of any property that is named by the value of `spring.application.name` + (if it's not present, by `application.yaml/properties`) +* Apply as a properties file the content of the above name + each active profile. + +An example should make a lot more sense. Let's suppose that `spring.application.name=my-app` and that +we have a single active profile called `k8s`. For a configuration as below: + + +==== +[source] +---- +kind: ConfigMap +apiVersion: v1 +metadata: + name: my-app +data: + my-app.yaml: |- + ... + my-app-k8s.yaml: |- + .. + my-app-dev.yaml: |- + .. + someProp: someValue +---- +==== + +These is what we will end-up loading: + + - `my-app.yaml` treated as a file + - `my-app-k8s.yaml` treated as a file + - `my-app-dev.yaml` _ignored_, since `dev` is _not_ an active profile + - `someProp: someValue` plain property + +The single exception to the aforementioned flow is when the `ConfigMap` contains a *single* key that indicates +the file is a YAML or properties file. In that case, the name of the key does NOT have to be `application.yaml` or +`application.properties` (it can be anything) and the value of the property is treated correctly. +This features facilitates the use case where the `ConfigMap` was created by using something like the following: + +==== +[source] +---- +kubectl create configmap game-config --from-file=/path/to/app-config.yaml +---- +==== + +Assume that we have a Spring Boot application named `demo` that uses the following properties to read its thread pool +configuration. + +* `pool.size.core` +* `pool.size.maximum` + +This can be externalized to config map in `yaml` format as follows: + +==== +[source,yaml] +---- +kind: ConfigMap +apiVersion: v1 +metadata: + name: demo +data: + pool.size.core: 1 + pool.size.max: 16 +---- +==== + +Individual properties work fine for most cases. However, sometimes, embedded `yaml` is more convenient. In this case, we +use a single property named `application.yaml` to embed our `yaml`, as follows: + +==== +[source,yaml] +---- +kind: ConfigMap +apiVersion: v1 +metadata: + name: demo +data: + application.yaml: |- + pool: + size: + core: 1 + max:16 +---- +==== + +The following example also works: + +==== +[source,yaml] +---- +kind: ConfigMap +apiVersion: v1 +metadata: + name: demo +data: + custom-name.yaml: |- + pool: + size: + core: 1 + max:16 +---- +==== + +You can also define the search to happen based on labels, for example: + + +==== +[source,yaml] +---- +spring: + application: + name: labeled-configmap-with-prefix + cloud: + kubernetes: + config: + enableApi: true + useNameAsPrefix: true + namespace: spring-k8s + sources: + - labels: + letter: a +---- +==== + +This will search for every configmap in namespace `spring-k8s` that has labels `{letter : a}`. The important +thing to notice here is that unlike reading a configmap by name, this can result in _multiple_ config maps read. +As usual, the same feature is supported for secrets. + +You can also configure Spring Boot applications differently depending on active profiles that are merged together +when the `ConfigMap` is read. You can provide different property values for different profiles by using an +`application.properties` or `application.yaml` property, specifying profile-specific values, each in their own document +(indicated by the `---` sequence), as follows: + +==== +[source,yaml] +---- +kind: ConfigMap +apiVersion: v1 +metadata: + name: demo +data: + application.yml: |- + greeting: + message: Say Hello to the World + farewell: + message: Say Goodbye + --- + spring: + profiles: development + greeting: + message: Say Hello to the Developers + farewell: + message: Say Goodbye to the Developers + --- + spring: + profiles: production + greeting: + message: Say Hello to the Ops +---- +==== + +In the preceding case, the configuration loaded into your Spring Application with the `development` profile is as follows: + +==== +[source,yaml] +---- + greeting: + message: Say Hello to the Developers + farewell: + message: Say Goodbye to the Developers +---- +==== + +However, if the `production` profile is active, the configuration becomes: + +==== +[source,yaml] +---- + greeting: + message: Say Hello to the Ops + farewell: + message: Say Goodbye +---- +==== + +If both profiles are active, the property that appears last within the `ConfigMap` overwrites any preceding values. + +Another option is to create a different config map per profile and spring boot will automatically fetch it based +on active profiles + +==== +[source,yaml] +---- +kind: ConfigMap +apiVersion: v1 +metadata: + name: demo +data: + application.yml: |- + greeting: + message: Say Hello to the World + farewell: + message: Say Goodbye +---- +==== +==== +[source,yaml] +---- +kind: ConfigMap +apiVersion: v1 +metadata: + name: demo-development +data: + application.yml: |- + spring: + profiles: development + greeting: + message: Say Hello to the Developers + farewell: + message: Say Goodbye to the Developers +---- +==== +==== +[source,yaml] +---- +kind: ConfigMap +apiVersion: v1 +metadata: + name: demo-production +data: + application.yml: |- + spring: + profiles: production + greeting: + message: Say Hello to the Ops + farewell: + message: Say Goodbye +---- +==== + + +To tell Spring Boot which `profile` should be enabled see the https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.profiles[Spring Boot documentation]. +One option for activating a specific profile when deploying to Kubernetes is to launch your Spring Boot application with an environment variable that you can define in the PodSpec at the container specification. + Deployment resource file, as follows: + +==== +[source,yaml] +---- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: deployment-name + labels: + app: deployment-name +spec: + replicas: 1 + selector: + matchLabels: + app: deployment-name + template: + metadata: + labels: + app: deployment-name + spec: + containers: + - name: container-name + image: your-image + env: + - name: SPRING_PROFILES_ACTIVE + value: "development" +---- +==== + +You could run into a situation where there are multiple configs maps that have the same property names. For example: + +==== +[source,yaml] +---- +kind: ConfigMap +apiVersion: v1 +metadata: + name: config-map-one +data: + application.yml: |- + greeting: + message: Say Hello from one +---- +==== + +and + +==== +[source,yaml] +---- +kind: ConfigMap +apiVersion: v1 +metadata: + name: config-map-two +data: + application.yml: |- + greeting: + message: Say Hello from two +---- +==== + +Depending on the order in which you place these in `bootstrap.yaml|properties`, you might end up with an un-expected result (the last config map wins). For example: + +==== +[source,yaml] +---- +spring: + application: + name: cloud-k8s-app + cloud: + kubernetes: + config: + namespace: default-namespace + sources: + - name: config-map-two + - name: config-map-one +---- +==== + +will result in property `greetings.message` being `Say Hello from one`. + +There is a way to change this default configuration by specifying `useNameAsPrefix`. For example: + +==== +[source,yaml] +---- +spring: + application: + name: with-prefix + cloud: + kubernetes: + config: + useNameAsPrefix: true + namespace: default-namespace + sources: + - name: config-map-one + useNameAsPrefix: false + - name: config-map-two +---- +==== + +Such a configuration will result in two properties being generated: + + - `greetings.message` equal to `Say Hello from one`. + + - `config-map-two.greetings.message` equal to `Say Hello from two` + +Notice that `spring.cloud.kubernetes.config.useNameAsPrefix` has a _lower_ priority than `spring.cloud.kubernetes.config.sources.useNameAsPrefix`. +This allows you to set a "default" strategy for all sources, at the same time allowing to override only a few. + +If using the config map name is not an option, you can specify a different strategy, called : `explicitPrefix`. Since this is an _explicit_ prefix that +you select, it can only be supplied to the `sources` level. At the same time it has a higher priority than `useNameAsPrefix`. Let's suppose we have a third config map with these entries: + + +==== +[source,yaml] +---- +kind: ConfigMap +apiVersion: v1 +metadata: + name: config-map-three +data: + application.yml: |- + greeting: + message: Say Hello from three +---- +==== + +A configuration like the one below: + +==== +[source,yaml] +---- +spring: + application: + name: with-prefix + cloud: + kubernetes: + config: + useNameAsPrefix: true + namespace: default-namespace + sources: + - name: config-map-one + useNameAsPrefix: false + - name: config-map-two + explicitPrefix: two + - name: config-map-three +---- +==== + +will result in three properties being generated: + + - `greetings.message` equal to `Say Hello from one`. + + - `two.greetings.message` equal to `Say Hello from two`. + + - `config-map-three.greetings.message` equal to `Say Hello from three`. + +The same way you configure a prefix for configmaps, you can do it for secrets also; both for secrets that are based on name +and the ones based on labels. For example: + +==== +[source.yaml] +---- +spring: + application: + name: prefix-based-secrets + cloud: + kubernetes: + secrets: + enableApi: true + useNameAsPrefix: true + namespace: spring-k8s + sources: + - labels: + letter: a + useNameAsPrefix: false + - labels: + letter: b + explicitPrefix: two + - labels: + letter: c + - labels: + letter: d + useNameAsPrefix: true + - name: my-secret +---- +==== + +The same processing rules apply when generating property source as for config maps. The only difference is that +potentially, looking up secrets by labels can mean that we find more than one source. In such a case, prefix (if specified via `useNameAsPrefix`) +will be the names of all secrets found for those particular labels. + +One more thing to bear in mind is that we support `prefix` per _source_, not per secret. The easiest way to explain this is via an example: + +==== +[source.yaml] +---- +spring: + application: + name: prefix-based-secrets + cloud: + kubernetes: + secrets: + enableApi: true + useNameAsPrefix: true + namespace: spring-k8s + sources: + - labels: + color: blue + useNameAsPrefix: true +---- +==== + +Suppose that a query matching such a label will provide two secrets as a result: `secret-a` and `secret-b`. +Both of these secrets have the same property name: `color=sea-blue` and `color=ocean-blue`. It is undefined which +`color` will end-up as part of property sources, but the prefix for it will be `secret-a.secret-b` +(concatenated sorted naturally, names of the secrets). + +If you need more fine-grained results, adding more labels to identify the secret uniquely would be an option. + + + +By default, besides reading the config map that is specified in the `sources` configuration, Spring will also try to read +all properties from "profile aware" sources. The easiest way to explain this is via an example. Let's suppose your application +enables a profile called "dev" and you have a configuration like the one below: + +==== +[source,yaml] +---- +spring: + application: + name: spring-k8s + cloud: + kubernetes: + config: + namespace: default-namespace + sources: + - name: config-map-one +---- +==== + +Besides reading the `config-map-one`, Spring will also try to read `config-map-one-dev`; in this particular order. Each active profile +generates such a profile aware config map. + +Though your application should not be impacted by such a config map, it can be disabled if needed: + +==== +[source,yaml] +---- +spring: + application: + name: spring-k8s + cloud: + kubernetes: + config: + includeProfileSpecificSources: false + namespace: default-namespace + sources: + - name: config-map-one + includeProfileSpecificSources: false +---- +==== + +Notice that just like before, there are two levels where you can specify this property: for all config maps or +for individual ones; the latter having a higher priority. + +NOTE: You should check the security configuration section. To access config maps from inside a pod you need to have the correct +Kubernetes service accounts, roles and role bindings. + +Another option for using `ConfigMap` (or `Secrets`) instances is to mount them into the Pod and use `spring.config.import` support. For example, suppose that you have such a ConfigMap deployed: + +``` +apiVersion: v1 +kind: ConfigMap +metadata: + name: my-configmap + namespace: default +data: + from.properties: 'as-mount-initial' +``` + +You can mount it in the pod (see https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#populate-a-volume-with-data-stored-in-a-configmap): + +``` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-pod +spec: + selector: + matchLabels: + app: my-app + template: + metadata: + labels: + app: my-app + spec: + containers: + - name: .... + image: .... + volumeMounts: + - name: my-configmap + mountPath: /tmp/props + volumes: + - name: my-configmap + configMap: + name: my-configmap + items: + - key: 'from.properties' + path: key +``` + +And then in `application.properties` have a property: + +``` +spring.config.import: "configtree:/tmp/" +``` + +This will result in a Property Source being created that will hold: `props.key=as-mount-initial`. + +NOTE: Using `spring.config.import`, automatic reload +functionality will work only using configuration watcher image. Or you will need to make a `POST` request to the `/actuator/refresh` endpoint or +restart/redeploy the application manually. There is a small caveat here: kubernetes will update the value of the file once the ConfigMap changes, but that does not happen instantly. It might take up to two minutes before you see the changed values. + +To better understand the note above, consider the example: + +- there is a ConfigMap that stores `from.properties: 'as-mount-initial'` +- that config map is mounted into `/tmp` (just like the example above) +- you use `spring.config.import: configtree:/tmp` +- you have a `@ConfigurationProperty` class: + +``` +@ConfigurationProperties("props") +public class ConfigMapProperties { + // getters, setters + private String key; +} +``` + +Once you start the application, the value read by `ConfigMapProperties::getKey` will be `as-mount-initial`. Now consider the case that you change `from.properties: 'as-mount-changed'` inside the ConfigMap and trigger a restart (manually or automatic via configuration watcher). The value read by: `ConfigMapProperties::getkey` might _not_ be `as-mount-changed`. This is because it takes up to two minutes for kubernetes to update the value from the ConfigMap to the mounted volume. In the case when you are using configuration watcher, things will work as expected, because there we have: + +``` +spring.cloud.kubernetes.configuration.watcher.refreshDelay = Duration.ofMillis(120000); +``` + + +[#config-map-fail-fast] +In some cases, your application may be unable to load some of your `ConfigMaps` using the Kubernetes API. +If you want your application to fail the start-up process in such cases, you can set +`spring.cloud.kubernetes.config.fail-fast=true` to make the application start-up fail with an Exception. + +[#config-map-retry] +You can also make your application retry loading `ConfigMap` property sources on a failure. First, you need to +set `spring.cloud.kubernetes.config.fail-fast=true`. Then you need to add `spring-retry` +and `spring-boot-starter-aop` to your classpath. You can configure retry properties such as +the maximum number of attempts, backoff options like initial interval, multiplier, max interval by setting the +`spring.cloud.kubernetes.config.retry.*` properties. + +NOTE: If you already have `spring-retry` and `spring-boot-starter-aop` on the classpath for some reason +and want to enable fail-fast, but do not want retry to be enabled; you can disable retry for `ConfigMap` `PropertySources` +by setting `spring.cloud.kubernetes.config.retry.enabled=false`. + +.Properties: +[options="header,footer"] +|=== +| Name | Type | Default | Description +| `spring.cloud.kubernetes.config.enabled` | `Boolean` | `true` | Enable ConfigMaps `PropertySource` +| `spring.cloud.kubernetes.config.name` | `String` | `${spring.application.name}` | Sets the name of `ConfigMap` to look up +| `spring.cloud.kubernetes.config.namespace` | `String` | Client namespace | Sets the Kubernetes namespace where to lookup +| `spring.cloud.kubernetes.config.enableApi` | `Boolean` | `true` | Enable or disable consuming `ConfigMap` instances through APIs +| `spring.cloud.kubernetes.config.fail-fast` | `Boolean` | `false` | Enable or disable failing the application start-up when an error occurred while loading a `ConfigMap` +| `spring.cloud.kubernetes.config.retry.enabled` | `Boolean` | `true` | Enable or disable config retry. +| `spring.cloud.kubernetes.config.retry.initial-interval` | `Long` | `1000` | Initial retry interval in milliseconds. +| `spring.cloud.kubernetes.config.retry.max-attempts` | `Integer` | `6` | Maximum number of attempts. +| `spring.cloud.kubernetes.config.retry.max-interval` | `Long` | `2000` | Maximum interval for backoff. +| `spring.cloud.kubernetes.config.retry.multiplier` | `Double` | `1.1` | Multiplier for next interval. +|=== + +=== Secrets PropertySource + +Kubernetes has the notion of https://kubernetes.io/docs/concepts/configuration/secret/[Secrets] for storing +sensitive data such as passwords, OAuth tokens, and so on. This project provides integration with `Secrets` to make secrets +accessible by Spring Boot applications. You can explicitly enable or disable This feature by setting the `spring.cloud.kubernetes.secrets.enabled` property. + +When enabled, the `Fabric8SecretsPropertySource` looks up Kubernetes for `Secrets` from the following sources: + +. Reading recursively from secrets mounts +. Named after the application (as defined by `spring.application.name`) +. Matching some labels + +*Note:* + +By default, consuming Secrets through the API (points 2 and 3 above) *is not enabled* for security reasons. The permission 'list' on secrets allows clients to inspect secrets values in the specified namespace. +Further, we recommend that containers share secrets through mounted volumes. + +If you enable consuming Secrets through the API, we recommend that you limit access to Secrets by using an authorization policy, such as RBAC. +For more information about risks and best practices when consuming Secrets through the API refer to https://kubernetes.io/docs/concepts/configuration/secret/#best-practices[this doc]. + +If the secrets are found, their data is made available to the application. + +Assume that we have a spring boot application named `demo` that uses properties to read its database +configuration. We can create a Kubernetes secret by using the following command: + +==== +[source] +---- +kubectl create secret generic db-secret --from-literal=username=user --from-literal=password=p455w0rd +---- +==== + +The preceding command would create the following secret (which you can see by using `kubectl get secrets db-secret -o yaml`): + +==== +[source,yaml] +---- +apiVersion: v1 +data: + password: cDQ1NXcwcmQ= + username: dXNlcg== +kind: Secret +metadata: + creationTimestamp: 2017-07-04T09:15:57Z + name: db-secret + namespace: default + resourceVersion: "357496" + selfLink: /api/v1/namespaces/default/secrets/db-secret + uid: 63c89263-6099-11e7-b3da-76d6186905a8 +type: Opaque +---- +==== + +Note that the data contains Base64-encoded versions of the literal provided by the `create` command. + +Your application can then use this secret -- for example, by exporting the secret's value as environment variables: + +==== +[source,yaml] +---- +apiVersion: v1 +kind: Deployment +metadata: + name: ${project.artifactId} +spec: + template: + spec: + containers: + - env: + - name: DB_USERNAME + valueFrom: + secretKeyRef: + name: db-secret + key: username + - name: DB_PASSWORD + valueFrom: + secretKeyRef: + name: db-secret + key: password +---- +==== + +You can select the Secrets to consume in a number of ways: + +. By setting a named secret: ++ +==== +[source,bash] +---- +-Dspring.cloud.kubernetes.secrets.name=db-secret +---- +==== + +. By defining a list of labels: ++ +==== +[source,bash] +---- +-Dspring.cloud.kubernetes.secrets.labels.broker=activemq +-Dspring.cloud.kubernetes.secrets.labels.db=postgresql +---- +==== + +As the case with `ConfigMap`, more advanced configuration is also possible where you can use multiple `Secret` +instances. The `spring.cloud.kubernetes.secrets.sources` list makes this possible. +For example, you could define the following `Secret` instances: + +==== +[source,yaml] +---- +spring: + application: + name: cloud-k8s-app + cloud: + kubernetes: + secrets: + name: default-name + namespace: default-namespace + sources: + # Spring Cloud Kubernetes looks up a Secret named s1 in namespace default-namespace + - name: s1 + # Spring Cloud Kubernetes looks up a Secret named default-name in namespace n2 + - namespace: n2 + # Spring Cloud Kubernetes looks up a Secret named s3 in namespace n3 + - namespace: n3 + name: s3 +---- +==== + +In the preceding example, if `spring.cloud.kubernetes.secrets.namespace` had not been set, +the `Secret` named `s1` would be looked up in the namespace that the application runs. +See <> to get a better understanding of how the namespace +of the application is resolved. + +<>; if you want your application to fail to start +when it is unable to load `Secrets` property sources, you can set `spring.cloud.kubernetes.secrets.fail-fast=true`. + +It is also possible to enable retry for `Secret` property sources <>. +As with the `ConfigMap` property sources, first you need to set `spring.cloud.kubernetes.secrets.fail-fast=true`. +Then you need to add `spring-retry` and `spring-boot-starter-aop` to your classpath. +Retry behavior of the `Secret` property sources can be configured by setting the `spring.cloud.kubernetes.secrets.retry.*` +properties. + +NOTE: If you already have `spring-retry` and `spring-boot-starter-aop` on the classpath for some reason +and want to enable fail-fast, but do not want retry to be enabled; you can disable retry for `Secrets` `PropertySources` +by setting `spring.cloud.kubernetes.secrets.retry.enabled=false`. + +.Properties: +[options="header,footer"] +|=== +| Name | Type | Default | Description +| `spring.cloud.kubernetes.secrets.enabled` | `Boolean` | `true` | Enable Secrets `PropertySource` +| `spring.cloud.kubernetes.secrets.name` | `String` | `${spring.application.name}` | Sets the name of the secret to look up +| `spring.cloud.kubernetes.secrets.namespace` | `String` | Client namespace | Sets the Kubernetes namespace where to look up +| `spring.cloud.kubernetes.secrets.labels` | `Map` | `null` | Sets the labels used to lookup secrets +| `spring.cloud.kubernetes.secrets.enableApi` | `Boolean` | `false` | Enables or disables consuming secrets through APIs (examples 2 and 3) +| `spring.cloud.kubernetes.secrets.fail-fast` | `Boolean` | `false` | Enable or disable failing the application start-up when an error occurred while loading a `Secret` +| `spring.cloud.kubernetes.secrets.retry.enabled` | `Boolean` | `true` | Enable or disable secrets retry. +| `spring.cloud.kubernetes.secrets.retry.initial-interval` | `Long` | `1000` | Initial retry interval in milliseconds. +| `spring.cloud.kubernetes.secrets.retry.max-attempts` | `Integer` | `6` | Maximum number of attempts. +| `spring.cloud.kubernetes.secrets.retry.max-interval` | `Long` | `2000` | Maximum interval for backoff. +| `spring.cloud.kubernetes.secrets.retry.multiplier` | `Double` | `1.1` | Multiplier for next interval. +|=== + +Notes: + +* The `spring.cloud.kubernetes.secrets.labels` property behaves as defined by +https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Configuration-Binding#map-based-binding[Map-based binding]. +* Access to secrets through the API may be restricted for security reasons. The preferred way is to mount secrets to the Pod. + +You can find an example of an application that uses secrets (though it has not been updated to use the new `spring-cloud-kubernetes` project) at +https://github.com/fabric8-quickstarts/spring-boot-camel-config[spring-boot-camel-config] + +[[namespace-resolution]] +=== Namespace resolution +Finding an application namespace happens on a best-effort basis. There are some steps that we iterate in order +to find it. The easiest and most common one, is to specify it in the proper configuration, for example: + +==== +[source,yaml] +---- +spring: + application: + name: app + cloud: + kubernetes: + secrets: + name: secret + namespace: default + sources: + # Spring Cloud Kubernetes looks up a Secret named 'a' in namespace 'default' + - name: a + # Spring Cloud Kubernetes looks up a Secret named 'secret' in namespace 'b' + - namespace: b + # Spring Cloud Kubernetes looks up a Secret named 'd' in namespace 'c' + - namespace: c + name: d +---- +==== + +Remember that the same can be done for config maps. If such a namespace is not specified, it will be read (in this order): + +1. from property `spring.cloud.kubernetes.client.namespace` +2. from a String residing in a file denoted by `spring.cloud.kubernetes.client.serviceAccountNamespacePath` property +3. from a String residing in `/var/run/secrets/kubernetes.io/serviceaccount/namespace` file +(kubernetes default namespace path) +4. from a designated client method call (for example fabric8's : `KubernetesClient::getNamespace`), if the client provides +such a method. This, in turn, could be configured via environment properties. For example fabric8 client can be configured via +"KUBERNETES_NAMESPACE" property; consult the client documentation for exact details. + +Failure to find a namespace from the above steps will result in an Exception being raised. + +[[order_of_configMaps_and_secrets]] +=== Order of ConfigMaps and Secrets + +If, for whatever reason, you enabled both configmaps and secrets, and there is a common property between them, the value from the ConfigMap will have a higher precedence. That is: it will override whatever values are found in secrets. + +=== `PropertySource` Reload + +WARNING: This functionality has been deprecated in the 2020.0 release. Please see +the <> controller for an alternative way +to achieve the same functionality. + +Some applications may need to detect changes on external property sources and update their internal status to reflect the new configuration. +The reload feature of Spring Cloud Kubernetes is able to trigger an application reload when a related `ConfigMap` or +`Secret` changes. + +By default, this feature is disabled. You can enable it by using the `spring.cloud.kubernetes.reload.enabled=true` configuration property (for example, in the `application.properties` file). +Please notice that this will enable monitoring of configmaps only (i.e.: `spring.cloud.kubernetes.reload.monitoring-config-maps` will be set to `true`). +If you want to enable monitoring of secrets, this must be done explicitly via : `spring.cloud.kubernetes.reload.monitoring-secrets=true`. + +The following levels of reload are supported (by setting the `spring.cloud.kubernetes.reload.strategy` property): + +* `refresh` (default): Only configuration beans annotated with `@ConfigurationProperties` or `@RefreshScope` are reloaded. +This reload level leverages the refresh feature of Spring Cloud Context. + +* `restart_context`: the whole Spring `ApplicationContext` is gracefully restarted. Beans are recreated with the new configuration. +In order for the restart context functionality to work properly you must enable and expose the restart actuator endpoint +[source,yaml] +==== +---- +management: + endpoint: + restart: + enabled: true + endpoints: + web: + exposure: + include: restart +---- +==== + +* `shutdown`: the Spring `ApplicationContext` is shut down to activate a restart of the container. + When you use this level, make sure that the lifecycle of all non-daemon threads is bound to the `ApplicationContext` +and that a replication controller or replica set is configured to restart the pod. + +Assuming that the reload feature is enabled with default settings (`refresh` mode), the following bean is refreshed when the config map changes: + +==== +[java, source] +---- +@Configuration +@ConfigurationProperties(prefix = "bean") +public class MyConfig { + + private String message = "a message that can be changed live"; + + // getter and setters + +} +---- +==== + +To see that changes effectively happen, you can create another bean that prints the message periodically, as follows + +==== +[source,java] +---- +@Component +public class MyBean { + + @Autowired + private MyConfig config; + + @Scheduled(fixedDelay = 5000) + public void hello() { + System.out.println("The message is: " + config.getMessage()); + } +} +---- +==== + +You can change the message printed by the application by using a `ConfigMap`, as follows: + +==== +[source,yaml] +---- +apiVersion: v1 +kind: ConfigMap +metadata: + name: reload-example +data: + application.properties: |- + bean.message=Hello World! +---- +==== + +Any change to the property named `bean.message` in the `ConfigMap` associated with the pod is reflected in the +output. More generally speaking, changes associated to properties prefixed with the value defined by the `prefix` +field of the `@ConfigurationProperties` annotation are detected and reflected in the application. +<> is explained earlier in this chapter. + +The reload feature supports two operating modes: + +* Event (default): Watches for changes in config maps or secrets by using the Kubernetes API (web socket). +Any event produces a re-check on the configuration and, in case of changes, a reload. +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 +(by default, secrets are not monitored). +* Polling: Periodically re-creates the configuration from config maps and secrets to see if it has changed. +You can configure the polling period by using the `spring.cloud.kubernetes.reload.period` property and defaults to 15 seconds. +It requires the same role as the monitored property source. +This means, for example, that using polling on file-mounted secret sources does not require particular privileges. + +[[namespace-label-filtering]] +=== Reload namespace and label filtering +By default, a namespace chosen using the steps outlined in <> will be used to listen to changes +in configmaps and secrets. i.e.: if you do not tell reload what namespaces and configmaps/secrets to watch for, +it will watch all configmaps/secrets from the namespace that will be computed using the above algorithm. + +On the other hand, you can define a more fine-grained approach. For example, you can specify the namespaces where +changes will be monitored: + +==== +[source,yaml] +---- +spring: + application: + name: event-reload + cloud: + kubernetes: + reload: + enabled: true + strategy: shutdown + mode: event + namespaces: + - my-namespace +---- +==== + +Such a configuration will make the app watch changes only in the `my-namespace` namespace. Mind that this will +watch _all_ configmaps/secrets (depending on which one you enable). If you want an even more fine-grained approach, +you can enable "label-filtering". First we need to enable such support via : `enable-reload-filtering: true` + +==== +[source,yaml] +---- +spring: + application: + name: event-reload + cloud: + kubernetes: + reload: + enabled: true + strategy: shutdown + mode: event + namespaces: + - my-namespaces + monitoring-config-maps: true + enable-reload-filtering: true +---- +==== + +What this will do, is watch configmaps/secrets that only have the `spring.cloud.kubernetes.config.informer.enabled: true` label. + +.Properties: +[options="header,footer"] +|=== +| Name | Type | Default | Description +| `spring.cloud.kubernetes.reload.enabled` | `Boolean` | `false` | Enables monitoring of property sources and configuration reload +| `spring.cloud.kubernetes.reload.monitoring-config-maps` | `Boolean` | `true` | Allow monitoring changes in config maps +| `spring.cloud.kubernetes.reload.monitoring-secrets` | `Boolean` | `false` | Allow monitoring changes in secrets +| `spring.cloud.kubernetes.reload.strategy` | `Enum` | `refresh` | The strategy to use when firing a reload (`refresh`, `restart_context`, or `shutdown`) +| `spring.cloud.kubernetes.reload.mode` | `Enum` | `event` | Specifies how to listen for changes in property sources (`event` or `polling`) +| `spring.cloud.kubernetes.reload.period` | `Duration`| `15s` | The period for verifying changes when using the `polling` strategy +| `spring.cloud.kubernetes.reload.namespaces` | `String[]`| | namespaces where we should watch for changes +| `spring.cloud.kubernetes.reload.enable-reload-filtering` | `String` | | enabled labeled filtering for reload functionality +|=== + +Notes: + +* You should not use properties under `spring.cloud.kubernetes.reload` in config maps or secrets. Changing such properties at runtime may lead to unexpected results. +* Deleting a property or the whole config map does not restore the original state of the beans when you use the `refresh` level. diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapErrorOnReadingSourceTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapErrorOnReadingSourceTests.java index 2c79b63c80..4983828f97 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapErrorOnReadingSourceTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapErrorOnReadingSourceTests.java @@ -112,7 +112,7 @@ void namedSingleConfigMapFails() { stubFor(get(path).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), Map.of(), true, name, namespace, false, true, false, RetryProperties.DEFAULT); CoreV1Api api = new CoreV1Api(); @@ -157,7 +157,7 @@ void namedTwoConfigMapsOneFails(CapturedOutput output) { ConfigMapConfigProperties.Source sourceTwo = new ConfigMapConfigProperties.Source(configMapNameTwo, namespace, Map.of(), null, null, null); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(sourceOne, sourceTwo), Map.of(), true, null, namespace, false, true, false, RetryProperties.DEFAULT); @@ -203,7 +203,7 @@ void namedTwoConfigMapsBothFail(CapturedOutput output) { ConfigMapConfigProperties.Source sourceTwo = new ConfigMapConfigProperties.Source(configMapNameTwo, namespace, Map.of(), null, null, null); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(sourceOne, sourceTwo), Map.of(), true, null, namespace, false, true, false, RetryProperties.DEFAULT); @@ -248,7 +248,7 @@ void labeledSingleConfigMapFails(CapturedOutput output) { ConfigMapConfigProperties.Source configMapSource = new ConfigMapConfigProperties.Source(null, namespace, labels, null, null, null); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(configMapSource), labels, true, null, namespace, false, true, false, RetryProperties.DEFAULT); CoreV1Api api = new CoreV1Api(); @@ -300,7 +300,7 @@ void labeledTwoConfigMapsOneFails(CapturedOutput output) { ConfigMapConfigProperties.Source sourceTwo = new ConfigMapConfigProperties.Source(null, namespace, configMapTwoLabels, null, null, null); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(sourceOne, sourceTwo), Map.of("one", "1", "two", "2"), true, null, namespace, false, true, false, RetryProperties.DEFAULT); @@ -355,7 +355,7 @@ void labeledTwoConfigMapsBothFail(CapturedOutput output) { ConfigMapConfigProperties.Source sourceTwo = new ConfigMapConfigProperties.Source(null, namespace, configMapTwoLabels, null, null, null); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(sourceOne, sourceTwo), Map.of("one", "1", "two", "2"), true, null, namespace, false, true, false, RetryProperties.DEFAULT); diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySourceLocatorTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySourceLocatorTests.java index 1266b00f70..4676806896 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySourceLocatorTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySourceLocatorTests.java @@ -99,8 +99,8 @@ void locateWithoutSources() { CoreV1Api api = new CoreV1Api(); stubFor(get("/api/v1/namespaces/default/configmaps") .willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(PROPERTIES_CONFIGMAP_LIST)))); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), List.of(), - Map.of(), true, "bootstrap-640", null, false, false, false, RetryProperties.DEFAULT); + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), Map.of(), + true, "bootstrap-640", null, false, false, false, RetryProperties.DEFAULT); MockEnvironment mockEnvironment = new MockEnvironment(); mockEnvironment.setProperty("spring.cloud.kubernetes.client.namespace", "default"); PropertySource propertySource = new KubernetesClientConfigMapPropertySourceLocator(api, @@ -118,8 +118,8 @@ void locateWithSources() { ConfigMapConfigProperties.Source source = new ConfigMapConfigProperties.Source("bootstrap-640", "default", Collections.emptyMap(), null, null, null); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), - List.of(source), Map.of(), true, "fake-name", null, false, false, false, RetryProperties.DEFAULT); + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(source), + Map.of(), true, "fake-name", null, false, false, false, RetryProperties.DEFAULT); PropertySource propertySource = new KubernetesClientConfigMapPropertySourceLocator(api, configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment())) @@ -141,8 +141,8 @@ void testLocateWithoutNamespaceConstructor() { stubFor(get("/api/v1/namespaces/default/configmaps") .willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(PROPERTIES_CONFIGMAP_LIST)))); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), List.of(), - Map.of(), true, "bootstrap-640", null, false, false, false, RetryProperties.DEFAULT); + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), Map.of(), + true, "bootstrap-640", null, false, false, false, RetryProperties.DEFAULT); assertThatThrownBy(() -> new KubernetesClientConfigMapPropertySourceLocator(api, configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment())) @@ -161,8 +161,8 @@ void testLocateWithoutNamespace() { CoreV1Api api = new CoreV1Api(); stubFor(get("/api/v1/namespaces/default/configmaps") .willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(PROPERTIES_CONFIGMAP_LIST)))); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), List.of(), - Map.of(), true, "bootstrap-640", null, false, false, false, RetryProperties.DEFAULT); + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), Map.of(), + true, "bootstrap-640", null, false, false, false, RetryProperties.DEFAULT); assertThatThrownBy(() -> new KubernetesClientConfigMapPropertySourceLocator(api, configMapConfigProperties, new KubernetesNamespaceProvider(ENV)) .locate(ENV)).isInstanceOf(NamespaceResolutionFailedException.class); @@ -174,8 +174,8 @@ public void locateShouldThrowExceptionOnFailureWhenFailFastIsEnabled() { stubFor(get("/api/v1/namespaces/default/configmaps") .willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), List.of(), - Map.of(), true, "bootstrap-640", "default", false, false, true, RetryProperties.DEFAULT); + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), Map.of(), + true, "bootstrap-640", "default", false, false, true, RetryProperties.DEFAULT); KubernetesClientConfigMapPropertySourceLocator locator = new KubernetesClientConfigMapPropertySourceLocator(api, configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment())); @@ -190,8 +190,8 @@ public void locateShouldNotThrowExceptionOnFailureWhenFailFastIsDisabled() { stubFor(get("/api/v1/namespaces/default/configmaps") .willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), List.of(), - Map.of(), true, "bootstrap-640", "default", false, false, false, RetryProperties.DEFAULT); + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), Map.of(), + true, "bootstrap-640", "default", false, false, false, RetryProperties.DEFAULT); KubernetesClientConfigMapPropertySourceLocator locator = new KubernetesClientConfigMapPropertySourceLocator(api, configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment())); diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySourceLocatorTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySourceLocatorTests.java index 5bcf6ca005..698785d0bd 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySourceLocatorTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySourceLocatorTests.java @@ -140,7 +140,7 @@ void getLocateWithSources() { SecretsConfigProperties.Source source2 = new SecretsConfigProperties.Source("rabbit-password", "", Collections.emptyMap(), null, null, null); - SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), + SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(source1, source2), true, "app", "default", false, true, false, RetryProperties.DEFAULT); PropertySource propertySource = new KubernetesClientSecretsPropertySourceLocator(api, @@ -154,8 +154,8 @@ void getLocateWithSources() { void getLocateWithOutSources() { CoreV1Api api = new CoreV1Api(); stubFor(get(LIST_API).willReturn(aResponse().withStatus(200).withBody(LIST_BODY))); - SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), - List.of(), true, "db-secret", "default", false, true, false, RetryProperties.DEFAULT); + SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), true, + "db-secret", "default", false, true, false, RetryProperties.DEFAULT); PropertySource propertySource = new KubernetesClientSecretsPropertySourceLocator(api, new KubernetesNamespaceProvider(new MockEnvironment()), secretsConfigProperties) @@ -176,8 +176,8 @@ void testLocateWithoutNamespaceConstructor() { CoreV1Api api = new CoreV1Api(); stubFor(get(LIST_API).willReturn(aResponse().withStatus(200).withBody(LIST_BODY))); - SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), - List.of(), true, "db-secret", "", false, true, false, RetryProperties.DEFAULT); + SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), true, + "db-secret", "", false, true, false, RetryProperties.DEFAULT); assertThatThrownBy(() -> new KubernetesClientSecretsPropertySourceLocator(api, new KubernetesNamespaceProvider(new MockEnvironment()), secretsConfigProperties) @@ -189,8 +189,8 @@ void locateShouldThrowExceptionOnFailureWhenFailFastIsEnabled() { CoreV1Api api = new CoreV1Api(); stubFor(get(LIST_API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); - SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), - List.of(), true, "db-secret", "default", false, true, true, RetryProperties.DEFAULT); + SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), true, + "db-secret", "default", false, true, true, RetryProperties.DEFAULT); KubernetesClientSecretsPropertySourceLocator locator = new KubernetesClientSecretsPropertySourceLocator(api, new KubernetesNamespaceProvider(new MockEnvironment()), secretsConfigProperties); @@ -204,8 +204,8 @@ void locateShouldNotThrowExceptionOnFailureWhenFailFastIsDisabled() { CoreV1Api api = new CoreV1Api(); stubFor(get(LIST_API).willReturn(aResponse().withStatus(500).withBody("Internal Server Error"))); - SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), - List.of(), true, "db-secret", "default", false, true, false, RetryProperties.DEFAULT); + SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), true, + "db-secret", "default", false, true, false, RetryProperties.DEFAULT); KubernetesClientSecretsPropertySourceLocator locator = new KubernetesClientSecretsPropertySourceLocator(api, new KubernetesNamespaceProvider(new MockEnvironment()), secretsConfigProperties); diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadConfigMapTest.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadConfigMapTest.java index a0d458f710..4e76776d62 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadConfigMapTest.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadConfigMapTest.java @@ -201,8 +201,7 @@ AbstractEnvironment environment() { // KubernetesClientConfigMapPropertySource, // otherwise we can't properly test reload functionality ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), - List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, FAIL_FAST, - RetryProperties.DEFAULT); + Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, FAIL_FAST, RetryProperties.DEFAULT); KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(mockEnvironment); PropertySource propertySource = new KubernetesClientConfigMapPropertySourceLocator(coreV1Api, @@ -224,7 +223,7 @@ ConfigReloadProperties configReloadProperties() { @Bean @Primary ConfigMapConfigProperties configMapConfigProperties() { - return new ConfigMapConfigProperties(true, List.of(), List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, + return new ConfigMapConfigProperties(true, List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, FAIL_FAST, RetryProperties.DEFAULT); } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadSecretTest.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadSecretTest.java index cc4af32b82..85aeab732b 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadSecretTest.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/EventReloadSecretTest.java @@ -207,7 +207,7 @@ AbstractEnvironment environment() { // KubernetesClientConfigMapPropertySource, // otherwise we can't properly test reload functionality SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), - List.of(), true, SECRET_NAME, NAMESPACE, false, true, FAIL_FAST, RetryProperties.DEFAULT); + true, SECRET_NAME, NAMESPACE, false, true, FAIL_FAST, RetryProperties.DEFAULT); KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(mockEnvironment); PropertySource propertySource = new KubernetesClientSecretsPropertySourceLocator(coreV1Api, @@ -229,7 +229,7 @@ ConfigReloadProperties configReloadProperties() { @Bean @Primary SecretsConfigProperties secretsConfigProperties() { - return new SecretsConfigProperties(true, Map.of(), List.of(), List.of(), true, SECRET_NAME, NAMESPACE, + return new SecretsConfigProperties(true, Map.of(), List.of(), true, SECRET_NAME, NAMESPACE, false, true, FAIL_FAST, RetryProperties.DEFAULT); } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadConfigMapTest.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadConfigMapTest.java index 0e55494d6e..46f7473fc0 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadConfigMapTest.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadConfigMapTest.java @@ -181,7 +181,7 @@ AbstractEnvironment environment() { // KubernetesClientConfigMapPropertySource, // otherwise we can't properly test reload functionality ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), - List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, true, RetryProperties.DEFAULT); + Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, true, RetryProperties.DEFAULT); KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(mockEnvironment); PropertySource propertySource = new KubernetesClientConfigMapPropertySourceLocator(coreV1Api, @@ -203,7 +203,7 @@ ConfigReloadProperties configReloadProperties() { @Bean @Primary ConfigMapConfigProperties configMapConfigProperties() { - return new ConfigMapConfigProperties(true, List.of(), List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, + return new ConfigMapConfigProperties(true, List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, FAIL_FAST, RetryProperties.DEFAULT); } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadSecretTest.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadSecretTest.java index 4173ac8da2..c4ca7a97e3 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadSecretTest.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload_it/PollingReloadSecretTest.java @@ -187,7 +187,7 @@ AbstractEnvironment environment() { // KubernetesClientSecretPropertySource, // otherwise we can't properly test reload functionality SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), - List.of(), true, SECRET_NAME, NAMESPACE, false, true, false, RetryProperties.DEFAULT); + true, SECRET_NAME, NAMESPACE, false, true, false, RetryProperties.DEFAULT); KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(mockEnvironment); PropertySource propertySource = new KubernetesClientSecretsPropertySourceLocator(coreV1Api, @@ -209,7 +209,7 @@ ConfigReloadProperties configReloadProperties() { @Bean @Primary SecretsConfigProperties secretsConfigProperties() { - return new SecretsConfigProperties(true, Map.of(), List.of(), List.of(), true, SECRET_NAME, NAMESPACE, + return new SecretsConfigProperties(true, Map.of(), List.of(), true, SECRET_NAME, NAMESPACE, false, true, FAIL_FAST, RetryProperties.DEFAULT); } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java index 0fb4f504cc..308a7d3d7a 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java @@ -35,10 +35,9 @@ * @author Isik Erhan */ @ConfigurationProperties(ConfigMapConfigProperties.PREFIX) -public record ConfigMapConfigProperties(@DefaultValue("true") boolean enableApi, @DefaultValue List paths, - @DefaultValue List sources, @DefaultValue Map labels, - @DefaultValue("true") boolean enabled, String name, String namespace, boolean useNameAsPrefix, - @DefaultValue("true") boolean includeProfileSpecificSources, boolean failFast, +public record ConfigMapConfigProperties(@DefaultValue("true") boolean enableApi, @DefaultValue List sources, + @DefaultValue Map labels, @DefaultValue("true") boolean enabled, String name, String namespace, + boolean useNameAsPrefix, @DefaultValue("true") boolean includeProfileSpecificSources, boolean failFast, @DefaultValue RetryProperties retry) { /** diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java index f299629309..2160eea05e 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2021 the original author or authors. + * Copyright 2013-2023 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. @@ -16,16 +16,9 @@ package org.springframework.cloud.kubernetes.commons.config; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.Collection; -import java.util.HashMap; import java.util.LinkedHashSet; -import java.util.Locale; -import java.util.Map; import java.util.Set; -import java.util.function.Function; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -37,10 +30,6 @@ import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; -import static org.springframework.cloud.kubernetes.commons.config.PropertySourceUtils.KEY_VALUE_TO_PROPERTIES; -import static org.springframework.cloud.kubernetes.commons.config.PropertySourceUtils.PROPERTIES_TO_MAP; -import static org.springframework.cloud.kubernetes.commons.config.PropertySourceUtils.yamlParserGenerator; - /** * A {@link PropertySourceLocator} that uses config maps. * @@ -89,8 +78,6 @@ public PropertySource locate(Environment environment) { }); } - addPropertySourcesFromPaths(environment, composite); - cache.discardAll(); return composite; } @@ -102,56 +89,4 @@ public Collection> locateCollection(Environment environment) { return PropertySourceLocator.super.locateCollection(environment); } - private void addPropertySourcesFromPaths(Environment environment, CompositePropertySource composite) { - Set uniquePaths = new LinkedHashSet<>(properties.paths()); - if (!uniquePaths.isEmpty()) { - LOG.warn( - "path support is deprecated and will be removed in a future release. Please use spring.config.import"); - } - LOG.debug("paths property sources : " + uniquePaths); - uniquePaths.stream().map(Paths::get).filter(p -> { - boolean exists = Files.exists(p); - if (!exists) { - LOG.warn("Configured input path: " + p - + " will be ignored because it does not exist on the file system"); - } - return exists; - }).filter(p -> { - boolean regular = Files.isRegularFile(p); - if (!regular) { - LOG.warn("Configured input path: " + p + " will be ignored because it is not a regular file"); - } - return regular; - }).toList().forEach(p -> { - try { - String content = new String(Files.readAllBytes(p)).trim(); - String filename = p.toAbsolutePath().toString().toLowerCase(Locale.ROOT); - if (filename.endsWith(".properties")) { - addPropertySourceIfNeeded(c -> PROPERTIES_TO_MAP.apply(KEY_VALUE_TO_PROPERTIES.apply(c)), content, - filename, composite); - } - else if (filename.endsWith(".yml") || filename.endsWith(".yaml")) { - addPropertySourceIfNeeded(c -> PROPERTIES_TO_MAP.apply(yamlParserGenerator(environment).apply(c)), - content, filename, composite); - } - } - catch (IOException e) { - LOG.warn("Error reading input file", e); - } - }); - } - - private void addPropertySourceIfNeeded(Function> contentToMapFunction, String content, - String name, CompositePropertySource composite) { - - Map map = new HashMap<>(contentToMapFunction.apply(content)); - if (map.isEmpty()) { - LOG.warn("Property source: " + name + "will be ignored because no properties could be found"); - } - else { - LOG.debug("will add file-based property source : " + name); - composite.addFirstPropertySource(new MountConfigMapPropertySource(name, map)); - } - } - } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigProperties.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigProperties.java index c8ddda3def..3a1b54d091 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigProperties.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigProperties.java @@ -37,9 +37,8 @@ */ @ConfigurationProperties(SecretsConfigProperties.PREFIX) public record SecretsConfigProperties(boolean enableApi, @DefaultValue Map labels, - @DefaultValue List paths, @DefaultValue List sources, @DefaultValue("true") boolean enabled, - String name, String namespace, boolean useNameAsPrefix, - @DefaultValue("true") boolean includeProfileSpecificSources, boolean failFast, + @DefaultValue List sources, @DefaultValue("true") boolean enabled, String name, String namespace, + boolean useNameAsPrefix, @DefaultValue("true") boolean includeProfileSpecificSources, boolean failFast, @DefaultValue RetryProperties retry) { /** diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsPropertySourceLocator.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsPropertySourceLocator.java index f35e40e0e6..38436464fd 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsPropertySourceLocator.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsPropertySourceLocator.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2021 the original author or authors. + * Copyright 2013-2023 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. @@ -16,24 +16,10 @@ package org.springframework.cloud.kubernetes.commons.config; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.EnumSet; import java.util.HashSet; import java.util.List; -import java.util.Locale; -import java.util.Objects; import java.util.Set; -import java.util.function.BiConsumer; -import java.util.function.BinaryOperator; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Collector; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -84,8 +70,6 @@ public PropertySource locate(Environment environment) { Set uniqueSources = new HashSet<>(sources); LOG.debug("Secrets normalized sources : " + sources); CompositePropertySource composite = new CompositePropertySource("composite-secrets"); - // read for secrets mount - putPathConfig(composite); if (this.properties.enableApi()) { uniqueSources.forEach(s -> { @@ -115,83 +99,4 @@ private SecretsPropertySource getSecretsPropertySourceForSingleSecret(Configurab protected abstract SecretsPropertySource getPropertySource(ConfigurableEnvironment environment, NormalizedSource normalizedSource); - protected void putPathConfig(CompositePropertySource composite) { - - if (!properties.paths().isEmpty()) { - LOG.warn( - "path support is deprecated and will be removed in a future release. Please use spring.config.import"); - } - - this.properties.paths().stream().map(Paths::get).filter(Files::exists).flatMap(x -> { - try { - return Files.walk(x); - } - catch (IOException e) { - LOG.warn("Error walking properties files", e); - return null; - } - }) - .filter(Objects::nonNull) - .filter(Files::isRegularFile) - .collect(new SecretsPropertySourceCollector()) - .forEach(composite::addPropertySource); - } - - /** - * @author wind57 - */ - private static class SecretsPropertySourceCollector - implements Collector, List> { - - @Override - public Supplier> supplier() { - return ArrayList::new; - } - - @Override - public BiConsumer, Path> accumulator() { - return (list, filePath) -> { - SecretsPropertySource source = property(filePath); - if (source != null) { - list.add(source); - } - }; - } - - @Override - public BinaryOperator> combiner() { - return (left, right) -> { - left.addAll(right); - return left; - }; - } - - @Override - public Function, List> finisher() { - return Function.identity(); - } - - @Override - public Set characteristics() { - return EnumSet.of(Characteristics.UNORDERED, Characteristics.IDENTITY_FINISH); - } - - private SecretsPropertySource property(Path filePath) { - - String fileName = filePath.getFileName().toString(); - - try { - String content = new String(Files.readAllBytes(filePath)).trim(); - String sourceName = fileName.toLowerCase(Locale.ROOT); - SourceData sourceData = new SourceData(sourceName, Collections.singletonMap(fileName, content)); - return new SecretsPropertySource(sourceData); - } - catch (IOException e) { - LOG.warn("Error reading properties file", e); - return null; - } - } - - } - } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtil.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtil.java index 02de15f2c1..092c5bf15f 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtil.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2022 the original author or authors. + * Copyright 2013-2023 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. @@ -26,7 +26,6 @@ import org.springframework.cloud.bootstrap.config.BootstrapPropertySource; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; -import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MapPropertySource; @@ -108,24 +107,16 @@ public static > List findPropertySources(Class else if (sourceClass.isInstance(source)) { managedSources.add(sourceClass.cast(source)); } - else if (source instanceof MountConfigMapPropertySource mountConfigMapPropertySource) { - // we know that the type is correct here - managedSources.add((S) mountConfigMapPropertySource); - } else if (source instanceof BootstrapPropertySource bootstrapPropertySource) { PropertySource propertySource = bootstrapPropertySource.getDelegate(); LOG.debug(() -> "bootstrap delegate class : " + propertySource.getClass()); if (sourceClass.isInstance(propertySource)) { sources.add(propertySource); } - else if (propertySource instanceof MountConfigMapPropertySource mountConfigMapPropertySource) { - // we know that the type is correct here - managedSources.add((S) mountConfigMapPropertySource); - } } } - LOG.debug(() -> "findPropertySources : " + managedSources.stream().map(PropertySource::getName).toList()); + LOG.debug(() -> "sources from findPropertySources : " + managedSources); return managedSources; } diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/SanitizeTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/SanitizeTests.java index b28d2c7c9b..b95655232b 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/SanitizeTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/SanitizeTests.java @@ -26,11 +26,11 @@ import org.springframework.boot.actuate.endpoint.Sanitizer; import org.springframework.boot.actuate.endpoint.SanitizingFunction; import org.springframework.cloud.bootstrap.config.BootstrapPropertySource; -import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource; import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySource; import org.springframework.cloud.kubernetes.commons.config.SourceData; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.MapPropertySource; import static org.springframework.boot.actuate.endpoint.SanitizableData.SANITIZED_VALUE; @@ -82,7 +82,7 @@ void bootstrapPropertySourceSecrets() { void notSecretsPropertySource() { BootstrapPropertySource bootstrapPropertySource = new BootstrapPropertySource<>( - new MountConfigMapPropertySource("mount-source", Map.of())); + new DummyPropertySource("mount-source", Map.of())); Sanitizer sanitizer = new Sanitizer(SANITIZING_FUNCTIONS); SanitizableData sanitizableData = new SanitizableData(bootstrapPropertySource, "secret", "xyz"); @@ -107,8 +107,7 @@ void compositeOneSecretOneMount() { CompositePropertySource compositePropertySource = new CompositePropertySource("composite"); compositePropertySource.addFirstPropertySource( new SecretsPropertySource(new SourceData("secret-source", Map.of("secret", "xyz")))); - compositePropertySource - .addFirstPropertySource(new MountConfigMapPropertySource("mount-source", Map.of("mount", "abc"))); + compositePropertySource.addFirstPropertySource(new DummyPropertySource("mount-source", Map.of("mount", "abc"))); Sanitizer sanitizer = new Sanitizer(SANITIZING_FUNCTIONS); SanitizableData sanitizableDataSecret = new SanitizableData(compositePropertySource, "secret", "xyz"); @@ -118,4 +117,12 @@ void compositeOneSecretOneMount() { Assertions.assertEquals(sanitizer.sanitize(sanitizableDataMount, SHOW_UNSANITIZED), "abc"); } + static class DummyPropertySource extends MapPropertySource { + + DummyPropertySource(String name, Map source) { + super(name, source); + } + + } + } diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesBindingTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesBindingTests.java index bab1e9d20d..7ad2ecc7b5 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesBindingTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesBindingTests.java @@ -37,7 +37,6 @@ void testWithDefaults() { ConfigMapConfigProperties props = context.getBean(ConfigMapConfigProperties.class); Assertions.assertNotNull(props); Assertions.assertTrue(props.enableApi()); - Assertions.assertTrue(props.paths().isEmpty()); Assertions.assertTrue(props.sources().isEmpty()); Assertions.assertTrue(props.labels().isEmpty()); Assertions.assertTrue(props.enabled()); @@ -60,7 +59,6 @@ void testWithDefaults() { void testWithNonDefaults() { new ApplicationContextRunner().withUserConfiguration(Config.class) .withPropertyValues("spring.cloud.kubernetes.config.enableApi=false", - "spring.cloud.kubernetes.config.paths[0]=a", "spring.cloud.kubernetes.config.paths[1]=b", "spring.cloud.kubernetes.config.sources[0].name=source-a", "spring.cloud.kubernetes.config.sources[0].namespace=source-namespace-a", "spring.cloud.kubernetes.config.sources[0].labels.key=source-value", @@ -83,10 +81,6 @@ void testWithNonDefaults() { Assertions.assertNotNull(props); Assertions.assertFalse(props.enableApi()); - Assertions.assertEquals(props.paths().size(), 2); - Assertions.assertEquals(props.paths().get(0), "a"); - Assertions.assertEquals(props.paths().get(1), "b"); - Assertions.assertEquals(props.sources().size(), 1); ConfigMapConfigProperties.Source source = props.sources().get(0); Assertions.assertEquals(source.name(), "source-a"); diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java index f4b0a98cc0..10364d19bc 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java @@ -46,7 +46,7 @@ class ConfigMapConfigPropertiesTests { */ @Test void testUseNameAsPrefixUnsetEmptySources() { - ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), List.of(), Map.of(), true, + ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), Map.of(), true, "config-map-a", "spring-k8s", false, false, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); @@ -72,7 +72,7 @@ void testUseNameAsPrefixUnsetEmptySources() { */ @Test void testUseNameAsPrefixSetEmptySources() { - ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), List.of(), Map.of(), true, + ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), Map.of(), true, "config-map-a", "spring-k8s", true, false, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); @@ -104,8 +104,8 @@ void testUseNameAsPrefixUnsetNonEmptySources() { ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source("config-map-one", null, Collections.emptyMap(), null, null, null); - ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), List.of(one), Map.of(), - true, "config-map-a", "spring-k8s", true, false, false, RetryProperties.DEFAULT); + ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(one), Map.of(), true, + "config-map-a", "spring-k8s", true, false, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); Assertions.assertEquals(sources.size(), 1, "a single NormalizedSource is expected"); @@ -147,8 +147,8 @@ void testUseNameAsPrefixSetNonEmptySources() { ConfigMapConfigProperties.Source three = new ConfigMapConfigProperties.Source("config-map-three", null, Collections.emptyMap(), null, true, null); - ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), List.of(one, two, three), - Map.of(), true, "config-map-a", "spring-k8s", true, false, false, RetryProperties.DEFAULT); + ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(one, two, three), Map.of(), + true, "config-map-a", "spring-k8s", true, false, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); Assertions.assertEquals(sources.size(), 3, "3 NormalizedSources are expected"); @@ -196,9 +196,8 @@ void testMultipleCases() { ConfigMapConfigProperties.Source four = new ConfigMapConfigProperties.Source(null, "config-map-four", Collections.emptyMap(), null, false, null); - ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), - List.of(one, two, three, four), Map.of(), true, "config-map-a", "spring-k8s", true, false, false, - RetryProperties.DEFAULT); + ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(one, two, three, four), + Map.of(), true, "config-map-a", "spring-k8s", true, false, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); Assertions.assertEquals(sources.size(), 4, "4 NormalizedSources are expected"); @@ -229,7 +228,7 @@ void testMultipleCases() { @Test void testUseIncludeProfileSpecificSourcesNoChanges() { - ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), List.of(), Map.of(), true, + ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), Map.of(), true, "config-map-a", "spring-k8s", false, true, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); @@ -258,7 +257,7 @@ void testUseIncludeProfileSpecificSourcesNoChanges() { @Test void testUseIncludeProfileSpecificSourcesDefaultChanged() { - ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), List.of(), Map.of(), true, + ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), Map.of(), true, "config-map-a", "spring-k8s", false, false, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); @@ -302,8 +301,8 @@ void testUseIncludeProfileSpecificSourcesDefaultChangedSourceOverride() { ConfigMapConfigProperties.Source three = new ConfigMapConfigProperties.Source("config-map-three", null, Collections.emptyMap(), null, null, false); - ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), List.of(one, two, three), - Map.of(), true, "config-map-a", "spring-k8s", false, false, false, RetryProperties.DEFAULT); + ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(one, two, three), Map.of(), + true, "config-map-a", "spring-k8s", false, false, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); Assertions.assertEquals(sources.size(), 3); @@ -359,9 +358,8 @@ void testLabelsMultipleCases() { ConfigMapConfigProperties.Source four = new ConfigMapConfigProperties.Source(null, null, Map.of("fourth-label", "configmap-four"), null, null, null); - ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), - List.of(one, two, three, four), Map.of(), true, "config-map-a", "spring-k8s", false, false, false, - RetryProperties.DEFAULT); + ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(one, two, three, four), + Map.of(), true, "config-map-a", "spring-k8s", false, false, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); // we get 8 property sources, since "named" ones with "application" are diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/KubernetesConfigDataLocationResolverTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/KubernetesConfigDataLocationResolverTests.java index e830ac4a08..fc9a3c514b 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/KubernetesConfigDataLocationResolverTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/KubernetesConfigDataLocationResolverTests.java @@ -224,11 +224,11 @@ void testResolveProfileSpecificFour() { // 'one' and 'two' prove that we have not registered ConfigMapConfigProperties and // SecretsConfigProperties in the bootstrap context - ConfigMapConfigProperties one = new ConfigMapConfigProperties(false, List.of(), List.of(), Map.of(), false, - null, null, false, false, false, null); + ConfigMapConfigProperties one = new ConfigMapConfigProperties(false, List.of(), Map.of(), false, null, null, + false, false, false, null); - SecretsConfigProperties two = new SecretsConfigProperties(false, Map.of(), List.of(), List.of(), false, null, - null, false, false, false, null); + SecretsConfigProperties two = new SecretsConfigProperties(false, Map.of(), List.of(), false, null, null, false, + false, false, null); KubernetesClientProperties kubernetesClientProperties = RESOLVER_CONTEXT.getBootstrapContext() .get(KubernetesClientProperties.class); @@ -293,11 +293,6 @@ void testResolveProfileSpecificSix() { Assertions.assertFalse(result.get(0).getConfigMapProperties().enableApi()); // on the other hand, @Default will be picked here Assertions.assertTrue(result.get(0).getConfigMapProperties().enabled()); - - // we have @DefaultValue enabled on paths, but it is not going to be picked up - // because of the explicit property we set in environment - Assertions.assertEquals("a", result.get(0).getSecretsConfigProperties().paths().get(0)); - // on the other hand, @Default will be picked here Assertions.assertTrue(result.get(0).getSecretsConfigProperties().includeProfileSpecificSources()); } diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesBindingTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesBindingTests.java index 0e390eabde..a756310bb4 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesBindingTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesBindingTests.java @@ -37,7 +37,6 @@ void testWithDefaults() { SecretsConfigProperties props = context.getBean(SecretsConfigProperties.class); Assertions.assertNotNull(props); Assertions.assertFalse(props.enableApi()); - Assertions.assertTrue(props.paths().isEmpty()); Assertions.assertTrue(props.sources().isEmpty()); Assertions.assertTrue(props.labels().isEmpty()); Assertions.assertTrue(props.enabled()); @@ -60,7 +59,6 @@ void testWithDefaults() { void testWithNonDefaults() { new ApplicationContextRunner().withUserConfiguration(Config.class) .withPropertyValues("spring.cloud.kubernetes.secrets.enableApi=false", - "spring.cloud.kubernetes.secrets.paths[0]=a", "spring.cloud.kubernetes.secrets.paths[1]=b", "spring.cloud.kubernetes.secrets.sources[0].name=source-a", "spring.cloud.kubernetes.secrets.sources[0].namespace=source-namespace-a", "spring.cloud.kubernetes.secrets.sources[0].labels.key=source-value", @@ -83,10 +81,6 @@ void testWithNonDefaults() { Assertions.assertNotNull(props); Assertions.assertFalse(props.enableApi()); - Assertions.assertEquals(props.paths().size(), 2); - Assertions.assertEquals(props.paths().get(0), "a"); - Assertions.assertEquals(props.paths().get(1), "b"); - Assertions.assertEquals(props.sources().size(), 1); SecretsConfigProperties.Source source = props.sources().get(0); Assertions.assertEquals(source.name(), "source-a"); diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesTests.java index cb3cd6439f..6a424ab2c1 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesTests.java @@ -39,8 +39,8 @@ class SecretsConfigPropertiesTests { @Test void emptySourcesSecretName() { - SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(), List.of(), true, - null, "namespace", false, true, false, RetryProperties.DEFAULT); + SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(), true, null, + "namespace", false, true, false, RetryProperties.DEFAULT); List source = properties.determineSources(new MockEnvironment()); Assertions.assertEquals(source.size(), 1); @@ -80,8 +80,8 @@ void multipleSources() { SecretsConfigProperties.Source three = new SecretsConfigProperties.Source(null, "spring-k8s", Map.of("three", "3"), null, false, false); - SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(), - List.of(one, two, three), true, null, "namespace", false, true, false, RetryProperties.DEFAULT); + SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(one, two, three), + true, null, "namespace", false, true, false, RetryProperties.DEFAULT); List result = properties.determineSources(new MockEnvironment()); Assertions.assertEquals(result.size(), 6); @@ -125,8 +125,8 @@ void multipleSources() { @Test void testUseNameAsPrefixUnsetEmptySources() { - SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(), List.of(), true, - "secret-a", "namespace", false, true, false, RetryProperties.DEFAULT); + SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(), true, "secret-a", + "namespace", false, true, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); Assertions.assertEquals(sources.size(), 1, "empty sources must generate a List with a single NormalizedSource"); @@ -151,8 +151,8 @@ void testUseNameAsPrefixUnsetEmptySources() { @Test void testUseNameAsPrefixSetEmptySources() { - SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(), List.of(), true, - "secret-a", "namespace", true, true, false, RetryProperties.DEFAULT); + SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(), true, "secret-a", + "namespace", true, true, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); Assertions.assertEquals(sources.size(), 1, "empty sources must generate a List with a single NormalizedSource"); @@ -183,7 +183,7 @@ void testUseNameAsPrefixUnsetNonEmptySources() { SecretsConfigProperties.Source one = new SecretsConfigProperties.Source("secret-one", "spring-k8s", Map.of(), null, true, false); - SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(), List.of(one), true, + SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(one), true, "secret-one", null, false, true, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); @@ -226,8 +226,8 @@ void testUseNameAsPrefixSetNonEmptySources() { SecretsConfigProperties.Source three = new SecretsConfigProperties.Source("secret-three", "spring-k8s", Map.of(), null, true, false); - SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(), - List.of(one, two, three), true, "secret-one", null, false, true, false, RetryProperties.DEFAULT); + SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(one, two, three), + true, "secret-one", null, false, true, false, RetryProperties.DEFAULT); List sources = properties.determineSources(new MockEnvironment()); Assertions.assertEquals(sources.size(), 3, "3 NormalizedSources are expected"); @@ -275,7 +275,7 @@ void testMultipleCases() { SecretsConfigProperties.Source four = new SecretsConfigProperties.Source("secret-four", "spring-k8s", Map.of(), null, false, false); - SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(), + SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(one, two, three, four), true, "secret-one", "spring-k8s", false, false, false, RetryProperties.DEFAULT); @@ -335,7 +335,7 @@ void testLabelsMultipleCases() { SecretsConfigProperties.Source four = new SecretsConfigProperties.Source(null, "spring-k8s", Map.of("fourth-label", "secret-four"), null, false, false); - SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(), + SecretsConfigProperties properties = new SecretsConfigProperties(false, Map.of(), List.of(one, two, three, four), false, null, "spring-k8s", false, false, false, RetryProperties.DEFAULT); diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtilTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtilTests.java index df1f54b2b2..c2783df557 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtilTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtilTests.java @@ -27,7 +27,6 @@ import org.springframework.cloud.bootstrap.config.BootstrapPropertySource; import org.springframework.cloud.kubernetes.commons.config.Constants; -import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.MapPropertySource; @@ -140,15 +139,13 @@ public Object getProperty(String name) { return null; } })); - propertySources.addFirst(new MountConfigMapPropertySource("mounted", Map.of("a", "b"))); List result = ConfigReloadUtil.findPropertySources(PlainPropertySource.class, environment); - Assertions.assertEquals(4, result.size()); - Assertions.assertEquals("b", result.get(0).getProperty("a")); - Assertions.assertEquals("plain", result.get(1).getProperty("")); - Assertions.assertEquals("from-bootstrap", result.get(2).getProperty("")); - Assertions.assertEquals("from-inner-two-composite", result.get(3).getProperty("")); + Assertions.assertEquals(3, result.size()); + Assertions.assertEquals("plain", result.get(0).getProperty("")); + Assertions.assertEquals("from-bootstrap", result.get(1).getProperty("")); + Assertions.assertEquals("from-inner-two-composite", result.get(2).getProperty("")); } @Test diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapTestUtil.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapTestUtil.java index 9de16cec72..adfd0ff0c6 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapTestUtil.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/ConfigMapTestUtil.java @@ -17,9 +17,6 @@ package org.springframework.cloud.kubernetes.fabric8.config; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; import java.util.Objects; import io.fabric8.kubernetes.client.utils.IOHelpers; @@ -41,8 +38,4 @@ public static String readResourceFile(String file) { return resource; } - public static void createFileWithContent(String file, String content) throws IOException { - Files.write(Paths.get(file), content.getBytes(), StandardOpenOption.CREATE); - } - } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapErrorOnReadingSourceTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapErrorOnReadingSourceTests.java index 549cb4dd5f..23e41045b9 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapErrorOnReadingSourceTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapErrorOnReadingSourceTests.java @@ -73,7 +73,7 @@ void namedSingleConfigMapFails() { mockServer.expect().withPath(path).andReturn(500, "Internal Server Error").once(); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), Map.of(), true, name, namespace, false, true, false, RetryProperties.DEFAULT); Fabric8ConfigMapPropertySourceLocator locator = new Fabric8ConfigMapPropertySourceLocator(mockClient, @@ -113,7 +113,7 @@ void namedTwoConfigMapsOneFails() { Source sourceOne = new Source(configMapNameOne, namespace, Map.of(), null, null, null); Source sourceTwo = new Source(configMapNameTwo, namespace, Map.of(), null, null, null); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(sourceOne, sourceTwo), Map.of(), true, null, namespace, false, true, false, RetryProperties.DEFAULT); @@ -148,7 +148,7 @@ void namedTwoConfigMapsBothFail() { Source sourceOne = new Source(configMapNameOne, namespace, Map.of(), null, null, null); Source sourceTwo = new Source(configMapNameTwo, namespace, Map.of(), null, null, null); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(sourceOne, sourceTwo), Map.of(), true, null, namespace, false, true, false, RetryProperties.DEFAULT); @@ -181,7 +181,7 @@ void labeledSingleConfigMapFails(CapturedOutput output) { Source configMapSource = new Source(null, namespace, labels, null, null, null); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(configMapSource), labels, true, null, namespace, false, true, false, RetryProperties.DEFAULT); Fabric8ConfigMapPropertySourceLocator locator = new Fabric8ConfigMapPropertySourceLocator(mockClient, @@ -226,7 +226,7 @@ void labeledTwoConfigMapsOneFails(CapturedOutput output) { Source sourceOne = new Source(null, namespace, configMapOneLabels, null, null, null); Source sourceTwo = new Source(null, namespace, configMapTwoLabels, null, null, null); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(sourceOne, sourceTwo), Map.of("one", "1", "two", "2"), true, null, namespace, false, true, false, RetryProperties.DEFAULT); @@ -266,7 +266,7 @@ void labeledTwoConfigMapsBothFail(CapturedOutput output) { Source sourceOne = new Source(null, namespace, configMapOneLabels, null, null, null); Source sourceTwo = new Source(null, namespace, configMapTwoLabels, null, null, null); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(sourceOne, sourceTwo), Map.of("one", "1", "two", "2"), true, null, namespace, false, true, false, RetryProperties.DEFAULT); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocatorMockTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocatorMockTests.java index 2e9f1421a7..a15d2192c5 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocatorMockTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocatorMockTests.java @@ -46,8 +46,8 @@ class Fabric8ConfigMapPropertySourceLocatorMockTests { @Test void constructorWithoutClientNamespaceMustFail() { - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), List.of(), - Map.of(), true, "name", null, false, true, false, RetryProperties.DEFAULT); + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), Map.of(), + true, "name", null, false, true, false, RetryProperties.DEFAULT); Mockito.when(client.getNamespace()).thenReturn(null); Fabric8ConfigMapPropertySourceLocator source = new Fabric8ConfigMapPropertySourceLocator(client, diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocatorTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocatorTests.java index 68026db2d0..ae63e02b57 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocatorTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocatorTests.java @@ -56,8 +56,8 @@ void locateShouldThrowExceptionOnFailureWhenFailFastIsEnabled() { mockServer.expect().withPath(path).andReturn(500, "Internal Server Error").always(); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), List.of(), - Map.of(), true, name, namespace, false, true, true, RetryProperties.DEFAULT); + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), Map.of(), + true, name, namespace, false, true, true, RetryProperties.DEFAULT); Fabric8ConfigMapPropertySourceLocator locator = new Fabric8ConfigMapPropertySourceLocator(mockClient, configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment())); @@ -74,8 +74,8 @@ void locateShouldNotThrowExceptionOnFailureWhenFailFastIsDisabled() { mockServer.expect().withPath(path).andReturn(500, "Internal Server Error").always(); - ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), List.of(), - Map.of(), true, name, namespace, false, true, false, RetryProperties.DEFAULT); + ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), Map.of(), + true, name, namespace, false, true, false, RetryProperties.DEFAULT); Fabric8ConfigMapPropertySourceLocator locator = new Fabric8ConfigMapPropertySourceLocator(mockClient, configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment())); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretErrorOnReadingSourceTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretErrorOnReadingSourceTests.java index 4c203861f3..7bf227157d 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretErrorOnReadingSourceTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretErrorOnReadingSourceTests.java @@ -73,7 +73,7 @@ void namedSingleSecretFails(CapturedOutput output) { mockServer.expect().withPath(path).andReturn(500, "Internal Server Error").once(); - SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), + SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), true, name, namespace, false, true, false, RetryProperties.DEFAULT); Fabric8SecretsPropertySourceLocator locator = new Fabric8SecretsPropertySourceLocator(mockClient, @@ -112,7 +112,7 @@ void namedTwoSecretsOneFails() { Source sourceOne = new Source(secretNameOne, namespace, Map.of(), null, null, null); Source sourceTwo = new Source(secretNameTwo, namespace, Map.of(), null, null, null); - SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), + SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(sourceOne, sourceTwo), true, null, namespace, false, true, false, RetryProperties.DEFAULT); Fabric8SecretsPropertySourceLocator locator = new Fabric8SecretsPropertySourceLocator(mockClient, @@ -146,7 +146,7 @@ void namedTwoSecretsBothFail() { Source sourceOne = new Source(secretNameOne, namespace, Map.of(), null, null, null); Source sourceTwo = new Source(secretNameTwo, namespace, Map.of(), null, null, null); - SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), + SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(sourceOne, sourceTwo), true, null, namespace, false, true, false, RetryProperties.DEFAULT); Fabric8SecretsPropertySourceLocator locator = new Fabric8SecretsPropertySourceLocator(mockClient, @@ -178,7 +178,7 @@ void labeledSingleSecretFails(CapturedOutput output) { Source secretSource = new Source(null, namespace, labels, null, null, null); - SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, labels, List.of(), + SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, labels, List.of(secretSource), true, null, namespace, false, true, false, RetryProperties.DEFAULT); Fabric8SecretsPropertySourceLocator locator = new Fabric8SecretsPropertySourceLocator(mockClient, @@ -224,7 +224,7 @@ void labeledTwoSecretsOneFails(CapturedOutput output) { Source sourceTwo = new Source(null, namespace, secretTwoLabels, null, null, null); SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, - Map.of("one", "1", "two", "2"), List.of(), List.of(sourceOne, sourceTwo), true, null, namespace, false, + Map.of("one", "1", "two", "2"), List.of(sourceOne, sourceTwo), true, null, namespace, false, true, false, RetryProperties.DEFAULT); Fabric8SecretsPropertySourceLocator locator = new Fabric8SecretsPropertySourceLocator(mockClient, @@ -264,7 +264,7 @@ void labeledTwoConfigMapsBothFail(CapturedOutput output) { Source sourceTwo = new Source(null, namespace, secretTwoLabels, null, null, null); SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, - Map.of("one", "1", "two", "2"), List.of(), List.of(sourceOne, sourceTwo), true, null, namespace, false, + Map.of("one", "1", "two", "2"), List.of(sourceOne, sourceTwo), true, null, namespace, false, true, false, RetryProperties.DEFAULT); Fabric8SecretsPropertySourceLocator locator = new Fabric8SecretsPropertySourceLocator(mockClient, diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceLocatorTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceLocatorTests.java index 53c4d0a711..d4a67c3a1f 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceLocatorTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceLocatorTests.java @@ -56,8 +56,8 @@ void locateShouldThrowExceptionOnFailureWhenFailFastIsEnabled() { mockServer.expect().withPath(path).andReturn(500, "Internal Server Error").always(); - SecretsConfigProperties configMapConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), - List.of(), true, name, namespace, false, true, true, RetryProperties.DEFAULT); + SecretsConfigProperties configMapConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), true, + name, namespace, false, true, true, RetryProperties.DEFAULT); Fabric8SecretsPropertySourceLocator locator = new Fabric8SecretsPropertySourceLocator(mockClient, configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment())); @@ -74,8 +74,8 @@ void locateShouldNotThrowExceptionOnFailureWhenFailFastIsDisabled() { mockServer.expect().withPath(path).andReturn(500, "Internal Server Error").always(); - SecretsConfigProperties configMapConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), - List.of(), true, name, namespace, false, true, false, RetryProperties.DEFAULT); + SecretsConfigProperties configMapConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), true, + name, namespace, false, true, false, RetryProperties.DEFAULT); Fabric8SecretsPropertySourceLocator locator = new Fabric8SecretsPropertySourceLocator(mockClient, configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment())); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/config_maps_mixed/ConfigMapsMixed.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/config_maps_mixed/ConfigMapsMixed.java index 616cc602b7..90fc6aa1ae 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/config_maps_mixed/ConfigMapsMixed.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/config_maps_mixed/ConfigMapsMixed.java @@ -16,45 +16,30 @@ package org.springframework.cloud.kubernetes.fabric8.config.config_maps_mixed; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.HashMap; import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.fabric8.config.ConfigMapTestUtil; import org.springframework.cloud.kubernetes.fabric8.config.TestApplication; import org.springframework.test.web.reactive.server.WebTestClient; -import static org.assertj.core.util.Lists.newArrayList; - @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestApplication.class, - properties = { "spring.application.name=" + BootstrapConfigMapsMixedTests.APPLICATION_NAME, - "spring.cloud.kubernetes.config.enableApi=true", - "spring.cloud.kubernetes.config.paths=" + BootstrapConfigMapsMixedTests.FILE_NAME_FULL_PATH, - "spring.main.cloud-platform=KUBERNETES" }) + properties = { "spring.application.name=" + ConfigMapsMixed.APPLICATION_NAME, + "spring.cloud.kubernetes.config.enableApi=true", "spring.main.cloud-platform=KUBERNETES" }) abstract class ConfigMapsMixed { - protected static final String FILES_ROOT_PATH = "/tmp/scktests"; - - protected static final String FILE_NAME = "application-path.yaml"; - - protected static final String FILE_NAME_FULL_PATH = FILES_ROOT_PATH + "/" + FILE_NAME; - protected static final String APPLICATION_NAME = "configmap-mixed-example"; @Autowired private WebTestClient webClient; - static void setUpBeforeClass(KubernetesClient mockClient) throws IOException { + static void setUpBeforeClass(KubernetesClient mockClient) { // Configure the kubernetes master url to point to the mock server System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); @@ -64,10 +49,6 @@ static void setUpBeforeClass(KubernetesClient mockClient) throws IOException { System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test"); System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true"); - Files.createDirectories(Paths.get(FILES_ROOT_PATH)); - ConfigMapTestUtil.createFileWithContent(FILE_NAME_FULL_PATH, - ConfigMapTestUtil.readResourceFile("application-path.yaml")); - HashMap data = new HashMap<>(); data.put("bean.morning", "Buenos Dias ConfigMap, %s"); @@ -80,41 +61,6 @@ static void setUpBeforeClass(KubernetesClient mockClient) throws IOException { mockClient.configMaps().inNamespace("test").resource(configMap).create(); } - @AfterAll - static void teardownAfterClass() { - newArrayList(FILE_NAME_FULL_PATH, FILES_ROOT_PATH).forEach(fn -> { - try { - Files.delete(Paths.get(fn)); - } - catch (IOException ignored) { - } - }); - } - - @Test - void greetingInputShouldReturnPropertyFromFile() { - this.webClient.get() - .uri("/api/greeting") - .exchange() - .expectStatus() - .isOk() - .expectBody() - .jsonPath("content") - .isEqualTo("Hello ConfigMap, World from path"); - } - - @Test - void farewellInputShouldReturnPropertyFromFile() { - this.webClient.get() - .uri("/api/farewell") - .exchange() - .expectStatus() - .isOk() - .expectBody() - .jsonPath("content") - .isEqualTo("Bye ConfigMap, World from path"); - } - @Test void morningInputShouldReturnPropertyFromApi() { this.webClient.get() diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/configmaps_from_file_paths/BootstrapConfigMapsFromFilePathsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/configmaps_from_file_paths/BootstrapConfigMapsFromFilePathsTests.java deleted file mode 100644 index d7a2e0034f..0000000000 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/configmaps_from_file_paths/BootstrapConfigMapsFromFilePathsTests.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2013-2019 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.fabric8.config.configmaps_from_file_paths; - -import java.io.IOException; - -import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; - -import org.springframework.test.context.TestPropertySource; - -@TestPropertySource(properties = { "spring.cloud.bootstrap.enabled=true" }) -@EnableKubernetesMockClient(crud = true, https = false) -class BootstrapConfigMapsFromFilePathsTests extends ConfigMapsFromFilePaths { - - private static KubernetesClient mockClient; - - @BeforeAll - static void setUpBeforeClass() throws IOException { - setUpBeforeClass(mockClient); - } - -} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/configmaps_from_file_paths/ConfigDataConfigMapsFromFilePathsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/configmaps_from_file_paths/ConfigDataConfigMapsFromFilePathsTests.java deleted file mode 100644 index a071f8ddbc..0000000000 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/configmaps_from_file_paths/ConfigDataConfigMapsFromFilePathsTests.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2013-2019 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.fabric8.config.configmaps_from_file_paths; - -import java.io.IOException; - -import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.junit.jupiter.api.BeforeAll; - -import org.springframework.test.context.TestPropertySource; - -@TestPropertySource(properties = { "spring.config.import=kubernetes:" }) -@EnableKubernetesMockClient(crud = true, https = false) -class ConfigDataConfigMapsFromFilePathsTests extends ConfigMapsFromFilePaths { - - private static KubernetesClient mockClient; - - @BeforeAll - static void setUpBeforeClass() throws IOException { - setUpBeforeClass(mockClient); - } - -} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/configmaps_from_file_paths/ConfigMapsFromFilePaths.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/configmaps_from_file_paths/ConfigMapsFromFilePaths.java deleted file mode 100644 index aa00cfb705..0000000000 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/configmaps_from_file_paths/ConfigMapsFromFilePaths.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright 2013-2019 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.fabric8.config.configmaps_from_file_paths; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.List; - -import io.fabric8.kubernetes.client.Config; -import io.fabric8.kubernetes.client.KubernetesClient; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.kubernetes.commons.config.Constants; -import org.springframework.cloud.kubernetes.fabric8.config.ConfigMapTestUtil; -import org.springframework.cloud.kubernetes.fabric8.config.TestApplication; -import org.springframework.test.web.reactive.server.WebTestClient; - -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestApplication.class, - properties = { "spring.application.name=configmap-path-example", - "spring.cloud.kubernetes.config.enableApi=false", - "spring.cloud.kubernetes.config.paths=" + ConfigMapsFromFilePaths.FIRST_FILE_NAME_FULL_PATH + "," - + ConfigMapsFromFilePaths.SECOND_FILE_NAME_FULL_PATH + "," - + ConfigMapsFromFilePaths.FIRST_FILE_NAME_DUPLICATED_FULL_PATH, - "spring.main.cloud-platform=KUBERNETES" }) -abstract class ConfigMapsFromFilePaths { - - protected static final String FILES_ROOT_PATH = "/tmp/scktests"; - - protected static final String FILES_SUB_PATH = "another-directory"; - - protected static final String SECOND_FILE_NAME = "extra.properties"; - - protected static final String UNUSED_FILE_NAME = "unused.properties"; - - protected static final String FIRST_FILE_NAME_FULL_PATH = FILES_ROOT_PATH + "/" + Constants.APPLICATION_PROPERTIES; - - protected static final String SECOND_FILE_NAME_FULL_PATH = FILES_ROOT_PATH + "/" + SECOND_FILE_NAME; - - protected static final String UNUSED_FILE_NAME_FULL_PATH = FILES_ROOT_PATH + "/" + UNUSED_FILE_NAME; - - protected static final String FIRST_FILE_NAME_DUPLICATED_FULL_PATH = FILES_ROOT_PATH + "/" + FILES_SUB_PATH + "/" - + Constants.APPLICATION_PROPERTIES; - - @Autowired - private WebTestClient webClient; - - static void setUpBeforeClass(KubernetesClient mockClient) throws IOException { - - // Configure the kubernetes master url to point to the mock server - System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); - System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); - System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false"); - System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false"); - System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test"); - System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true"); - Files.createDirectories(Paths.get(FILES_ROOT_PATH + "/" + FILES_SUB_PATH)); - ConfigMapTestUtil.createFileWithContent(FIRST_FILE_NAME_FULL_PATH, "bean.greeting=Hello from path!"); - ConfigMapTestUtil.createFileWithContent(SECOND_FILE_NAME_FULL_PATH, "bean.farewell=Bye from path!"); - ConfigMapTestUtil.createFileWithContent(UNUSED_FILE_NAME_FULL_PATH, "bean.morning=Morning from path!"); - ConfigMapTestUtil.createFileWithContent(FIRST_FILE_NAME_DUPLICATED_FULL_PATH, - "bean.bonjour=Bonjour from path!"); - } - - @AfterAll - static void teardownAfterClass() { - List.of(FIRST_FILE_NAME_FULL_PATH, SECOND_FILE_NAME_FULL_PATH, SECOND_FILE_NAME_FULL_PATH, FILES_ROOT_PATH) - .forEach(fn -> { - try { - Files.delete(Paths.get(fn)); - } - catch (IOException ignored) { - } - }); - } - - @Test - void greetingInputShouldReturnPropertyFromFirstFile() { - this.webClient.get() - .uri("/api/greeting") - .exchange() - .expectStatus() - .isOk() - .expectBody() - .jsonPath("content") - .isEqualTo("Hello from path!"); - } - - @Test - void farewellInputShouldReturnPropertyFromSecondFile() { - this.webClient.get() - .uri("/api/farewell") - .exchange() - .expectStatus() - .isOk() - .expectBody() - .jsonPath("content") - .isEqualTo("Bye from path!"); - } - - @Test - void morningInputShouldReturnDefaultValue() { - this.webClient.get() - .uri("/api/morning") - .exchange() - .expectStatus() - .isOk() - .expectBody() - .jsonPath("content") - .isEqualTo("Good morning, World!"); - } - - @Test - void bonjourInputShouldReturnPropertyFromDuplicatedFile() { - this.webClient.get() - .uri("/api/bonjour") - .exchange() - .expectStatus() - .isOk() - .expectBody() - .jsonPath("content") - .isEqualTo("Bonjour from path!"); - } - -} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/fail_fast_enabled_retry_disabled/ConfigDataConfigFailFastEnabledButRetryDisabledTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/fail_fast_enabled_retry_disabled/ConfigDataConfigFailFastEnabledButRetryDisabledTests.java index 985e0cc9ff..ccc35d30a4 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/fail_fast_enabled_retry_disabled/ConfigDataConfigFailFastEnabledButRetryDisabledTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/locator_retry/fail_fast_enabled_retry_disabled/ConfigDataConfigFailFastEnabledButRetryDisabledTests.java @@ -71,7 +71,7 @@ static class LocalConfig { */ @Bean ConfigMapConfigProperties properties(Environment environment) { - return new ConfigMapConfigProperties(true, List.of(), List.of(), Map.of(), true, null, null, false, true, + return new ConfigMapConfigProperties(true, List.of(), Map.of(), true, null, null, false, true, Boolean.parseBoolean(environment.getProperty("spring.cloud.kubernetes.config.fail-fast")), RetryProperties.DEFAULT); } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/EventReloadConfigMapTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/EventReloadConfigMapTest.java index d14efbabab..a959cbc3b0 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/EventReloadConfigMapTest.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/EventReloadConfigMapTest.java @@ -162,7 +162,7 @@ AbstractEnvironment environment() { // simulate that environment already has a Fabric8ConfigMapPropertySource, // otherwise we can't properly test reload functionality ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), - List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, true, RetryProperties.DEFAULT); + Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, true, RetryProperties.DEFAULT); KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(mockEnvironment); PropertySource propertySource = new VisibleFabric8ConfigMapPropertySourceLocator(kubernetesClient, @@ -184,7 +184,7 @@ ConfigReloadProperties configReloadProperties() { @Bean @Primary ConfigMapConfigProperties configMapConfigProperties() { - return new ConfigMapConfigProperties(true, List.of(), List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, + return new ConfigMapConfigProperties(true, List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, FAIL_FAST, RetryProperties.DEFAULT); } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/EventReloadSecretTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/EventReloadSecretTest.java index 546ae964b4..080dd212a4 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/EventReloadSecretTest.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/EventReloadSecretTest.java @@ -169,7 +169,7 @@ AbstractEnvironment environment() { // Fabric8SecretsPropertySourceLocator, // otherwise we can't properly test reload functionality SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), - List.of(), true, SECRET_NAME, NAMESPACE, false, true, true, RetryProperties.DEFAULT); + true, SECRET_NAME, NAMESPACE, false, true, true, RetryProperties.DEFAULT); KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(mockEnvironment); PropertySource propertySource = new VisibleFabric8SecretsPropertySourceLocator(kubernetesClient, @@ -191,7 +191,7 @@ ConfigReloadProperties configReloadProperties() { @Bean @Primary SecretsConfigProperties secretsConfigProperties() { - return new SecretsConfigProperties(true, Map.of(), List.of(), List.of(), true, SECRET_NAME, NAMESPACE, + return new SecretsConfigProperties(true, Map.of(), List.of(), true, SECRET_NAME, NAMESPACE, false, true, FAIL_FAST, RetryProperties.DEFAULT); } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadConfigMapTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadConfigMapTest.java index 372ca8be31..62f88d3e15 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadConfigMapTest.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadConfigMapTest.java @@ -152,7 +152,7 @@ AbstractEnvironment environment() { // simulate that environment already has a Fabric8ConfigMapPropertySource, // otherwise we can't properly test reload functionality ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties(true, List.of(), - List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, true, RetryProperties.DEFAULT); + Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, true, RetryProperties.DEFAULT); KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(mockEnvironment); PropertySource propertySource = new VisibleFabric8ConfigMapPropertySourceLocator(kubernetesClient, @@ -174,7 +174,7 @@ ConfigReloadProperties configReloadProperties() { @Bean @Primary ConfigMapConfigProperties configMapConfigProperties() { - return new ConfigMapConfigProperties(true, List.of(), List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, + return new ConfigMapConfigProperties(true, List.of(), Map.of(), true, CONFIG_MAP_NAME, NAMESPACE, false, true, FAIL_FAST, RetryProperties.DEFAULT); } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadSecretTest.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadSecretTest.java index 68667637ce..d0630ae47f 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadSecretTest.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/PollingReloadSecretTest.java @@ -159,7 +159,7 @@ AbstractEnvironment environment() { // simulate that environment already has a Fabric8SecretsPropertySource, // otherwise we can't properly test reload functionality SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties(true, Map.of(), List.of(), - List.of(), true, SECRET_NAME, NAMESPACE, false, true, true, RetryProperties.DEFAULT); + true, SECRET_NAME, NAMESPACE, false, true, true, RetryProperties.DEFAULT); KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(mockEnvironment); PropertySource propertySource = new VisibleFabric8SecretsPropertySourceLocator(kubernetesClient, @@ -181,7 +181,7 @@ ConfigReloadProperties configReloadProperties() { @Bean @Primary SecretsConfigProperties secretsConfigProperties() { - return new SecretsConfigProperties(true, Map.of(), List.of(), List.of(), true, SECRET_NAME, NAMESPACE, + return new SecretsConfigProperties(true, Map.of(), List.of(), true, SECRET_NAME, NAMESPACE, false, true, FAIL_FAST, RetryProperties.DEFAULT); } diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/App.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/App.java index 83cd4057e7..e789f25152 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/App.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/App.java @@ -25,7 +25,7 @@ */ @SpringBootApplication @EnableConfigurationProperties({ LeftProperties.class, RightProperties.class, RightWithLabelsProperties.class, - ConfigMapProperties.class, SecretProperties.class }) + ConfigMapProperties.class, SecretProperties.class, ConfigMapNoMountProperties.class }) public class App { public static void main(String[] args) { diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MountConfigMapPropertySource.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapNoMountProperties.java similarity index 64% rename from spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MountConfigMapPropertySource.java rename to spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapNoMountProperties.java index 7cbffac4c0..2636073e3e 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MountConfigMapPropertySource.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapNoMountProperties.java @@ -14,19 +14,21 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.commons.config; +package org.springframework.cloud.kubernetes.fabric8.client.reload; -import java.util.Map; +import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.core.env.MapPropertySource; +@ConfigurationProperties("props") +public class ConfigMapNoMountProperties { -/** - * @author wind57 - */ -public final class MountConfigMapPropertySource extends MapPropertySource { + private String key; + + public String getKey() { + return key; + } - public MountConfigMapPropertySource(String name, Map source) { - super(name, source); + public void setKey(String key) { + this.key = key; } } diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapProperties.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapProperties.java index bb3fb709e1..e90a177fe4 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapProperties.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapProperties.java @@ -30,8 +30,8 @@ public String getKey() { return key; } - public void setKey(String key1) { - this.key = key1; + public void setKey(String key) { + this.key = key; } } diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/Controller.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/Controller.java index 12469a7fff..e7da9ddc4d 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/Controller.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/reload/Controller.java @@ -35,14 +35,17 @@ public class Controller { private final SecretProperties secretProperties; + private final ConfigMapNoMountProperties configMapNoMountProperties; + public Controller(LeftProperties leftProperties, RightProperties rightProperties, RightWithLabelsProperties rightWithLabelsProperties, ConfigMapProperties configMapProperties, - SecretProperties secretProperties) { + SecretProperties secretProperties, ConfigMapNoMountProperties configMapNoMountProperties) { this.leftProperties = leftProperties; this.rightProperties = rightProperties; this.rightWithLabelsProperties = rightWithLabelsProperties; this.configMapProperties = configMapProperties; this.secretProperties = secretProperties; + this.configMapNoMountProperties = configMapNoMountProperties; } @GetMapping("/left") @@ -65,6 +68,11 @@ public String key() { return configMapProperties.getKey(); } + @GetMapping("/key-no-mount") + public String keyNoMount() { + return configMapNoMountProperties.getKey(); + } + @GetMapping("/key-from-secret") public String keyFromSecret() { return secretProperties.getKey(); diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-configtree.yaml b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-configtree.yaml new file mode 100644 index 0000000000..7a977072cb --- /dev/null +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-configtree.yaml @@ -0,0 +1,16 @@ +spring: + application: + name: poll-reload-mount + config: + import: "kubernetes:,configtree:/tmp/" + +management: + endpoint: + refresh: + enabled: true + restart: + enabled: true + endpoints: + web: + exposure: + include: '*' diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-mount.yaml b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-mount.yaml deleted file mode 100644 index cd1765f5c8..0000000000 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-mount.yaml +++ /dev/null @@ -1,17 +0,0 @@ -spring: - application: - name: poll-reload-mount - cloud: - kubernetes: - reload: - enabled: true - monitoring-config-maps: true - strategy: shutdown - mode: polling - period: 5000 - config: - paths: - - /tmp/application.properties - config: - import: "kubernetes:" - diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-no-mount.yaml b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-no-mount.yaml index baf0f12d35..56038d0242 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-no-mount.yaml +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-no-mount.yaml @@ -13,6 +13,4 @@ spring: enabled: true monitoring-config-maps: true strategy: shutdown - mode: polling - period: 5000 diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-with-bootstrap.yaml b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-with-bootstrap.yaml index c997322d16..5b999c3ea2 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-with-bootstrap.yaml +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-with-bootstrap.yaml @@ -7,6 +7,16 @@ spring: enabled: true monitoring-config-maps: true strategy: shutdown - mode: polling - period: 5000 + +management: + endpoint: + refresh: + enabled: true + restart: + enabled: true + + endpoints: + web: + exposure: + include: '*' diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/bootstrap-with-bootstrap.yaml b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/bootstrap-with-bootstrap.yaml deleted file mode 100644 index 0aed2eb9dd..0000000000 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/bootstrap-with-bootstrap.yaml +++ /dev/null @@ -1,6 +0,0 @@ -spring: - cloud: - kubernetes: - config: - paths: - - /tmp/application.properties diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/BootstrapEnabledPollingReloadConfigMapMountDelegate.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/BootstrapEnabledPollingReloadConfigMapMountDelegate.java deleted file mode 100644 index 0b6c397967..0000000000 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/BootstrapEnabledPollingReloadConfigMapMountDelegate.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2013-2023 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.fabric8.client.reload; - -import java.io.InputStream; -import java.time.Duration; -import java.util.Map; - -import io.fabric8.kubernetes.api.model.ConfigMap; -import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.utils.Serialization; -import org.junit.jupiter.api.Assertions; -import org.testcontainers.k3s.K3sContainer; - -import org.springframework.cloud.kubernetes.commons.config.Constants; -import org.springframework.cloud.kubernetes.integration.tests.commons.Commons; -import org.springframework.cloud.kubernetes.integration.tests.commons.fabric8_client.Util; -import org.springframework.http.HttpMethod; -import org.springframework.web.reactive.function.client.WebClient; - -import static org.awaitility.Awaitility.await; - -final class BootstrapEnabledPollingReloadConfigMapMountDelegate { - - /** - *
-	 *     - we have bootstrap enabled, which means we will 'locate' property sources
-	 *       from config maps.
-	 *     - there are no explicit config maps to search for, but what we will also read,
-	 *     	 is 'spring.cloud.kubernetes.config.paths', which we have set to
-	 *     	 '/tmp/application.properties'
-	 *       in this test. That is populated by the volumeMounts (see deployment-mount.yaml)
-	 *     - we first assert that we are actually reading the path based source via (1), (2) and (3).
-	 *
-	 *     - we then change the config map content, wait for k8s to pick it up and replace them
-	 *     - our polling will then detect that change, and trigger a reload.
-	 * 
- */ - static void testPollingReloadConfigMapWithBootstrap(KubernetesClient client, Util util, K3sContainer container, - String appLabelValue) { - // (1) - Commons.waitForLogStatement("paths property sources : [/tmp/application.properties]", container, appLabelValue); - // (2) - Commons.waitForLogStatement("will add file-based property source : /tmp/application.properties", container, - appLabelValue); - // (3) - WebClient webClient = TestUtil.builder().baseUrl("http://localhost/key").build(); - String result = webClient.method(HttpMethod.GET) - .retrieve() - .bodyToMono(String.class) - .retryWhen(TestUtil.retrySpec()) - .block(); - - // we first read the initial value from the configmap - Assertions.assertEquals("as-mount-initial", result); - - // replace data in configmap and wait for k8s to pick it up - // our polling will detect that and restart the app - InputStream configMapStream = util.inputStream("configmap.yaml"); - ConfigMap configMap = Serialization.unmarshal(configMapStream, ConfigMap.class); - configMap.setData(Map.of(Constants.APPLICATION_PROPERTIES, "from.properties.key=as-mount-changed")); - client.configMaps().inNamespace("default").resource(configMap).createOrReplace(); - - await().timeout(Duration.ofSeconds(360)) - .until(() -> webClient.method(HttpMethod.GET) - .retrieve() - .bodyToMono(String.class) - .retryWhen(TestUtil.retrySpec()) - .block() - .equals("as-mount-changed")); - - } - -} diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapMountPollingReloadDelegate.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapMountPollingReloadDelegate.java index c0f5fc5d3f..3b96a1fe13 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapMountPollingReloadDelegate.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapMountPollingReloadDelegate.java @@ -18,16 +18,15 @@ import java.io.InputStream; import java.time.Duration; +import java.util.HashMap; import java.util.Map; +import java.util.Optional; import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.utils.Serialization; import org.junit.jupiter.api.Assertions; -import org.testcontainers.k3s.K3sContainer; -import org.springframework.cloud.kubernetes.commons.config.Constants; -import org.springframework.cloud.kubernetes.integration.tests.commons.Commons; import org.springframework.cloud.kubernetes.integration.tests.commons.fabric8_client.Util; import org.springframework.http.HttpMethod; import org.springframework.web.reactive.function.client.WebClient; @@ -41,28 +40,19 @@ final class ConfigMapMountPollingReloadDelegate { /** *
-	 *     - we have "spring.config.import: kubernetes", which means we will 'locate' property sources
+	 *     - we have "spring.config.import: kubernetes:,configtree:/tmp/", which means we will 'locate' property sources
 	 *       from config maps.
 	 *     - the property above means that at the moment we will be searching for config maps that only
 	 *       match the application name, in this specific test there is no such config map.
-	 *     - what we will also read, is 'spring.cloud.kubernetes.config.paths', which we have set to
-	 *     	 '/tmp/application.properties'
-	 *       in this test. That is populated by the volumeMounts (see deployment-mount.yaml)
-	 *     - we first assert that we are actually reading the path based source via (1), (2) and (3).
+	 *     - what we will also read, is /tmp directory according to configtree rules.
+	 *       As such, a property "props.key" (see TestUtil::BODY_SIX) will be in environment.
 	 *
-	 *     - we then change the config map content, wait for k8s to pick it up and replace them
-	 *     - our polling will then detect that change, and trigger a reload.
+	 *     - we then change the config map content, wait for configuration watcher to pick up the change
+	 *       and schedule a refresh event, based on http.
 	 * 
*/ - static void testConfigMapMountPollingReload(KubernetesClient client, Util util, K3sContainer container, - String appLabelValue) { - // (1) - Commons.waitForLogStatement("paths property sources : [/tmp/application.properties]", container, appLabelValue); - // (2) - Commons.waitForLogStatement("will add file-based property source : /tmp/application.properties", container, - appLabelValue); - // (3) - WebClient webClient = TestUtil.builder().baseUrl("http://localhost/key").build(); + static void testConfigMapMountPollingReload(KubernetesClient client, Util util) { + WebClient webClient = TestUtil.builder().baseUrl("http://localhost/key-no-mount").build(); String result = webClient.method(HttpMethod.GET) .retrieve() .bodyToMono(String.class) @@ -74,12 +64,24 @@ static void testConfigMapMountPollingReload(KubernetesClient client, Util util, // replace data in configmap and wait for k8s to pick it up // our polling will detect that and restart the app - InputStream configMapStream = util.inputStream("configmap.yaml"); - ConfigMap configMap = Serialization.unmarshal(configMapStream, ConfigMap.class); - configMap.setData(Map.of(Constants.APPLICATION_PROPERTIES, "from.properties.key=as-mount-changed")); - client.configMaps().inNamespace("default").resource(configMap).createOrReplace(); + InputStream configMapMountStream = util.inputStream("configmap-configtree.yaml"); + ConfigMap configMapMount = Serialization.unmarshal(configMapMountStream, ConfigMap.class); + configMapMount.setData(Map.of("from.properties", "as-mount-changed")); + // add label so that configuration-watcher picks this up + Map existingLabels = new HashMap<>( + Optional.ofNullable(configMapMount.getMetadata().getLabels()).orElse(Map.of())); + existingLabels.put("spring.cloud.kubernetes.config", "true"); + configMapMount.getMetadata().setLabels(existingLabels); - await().timeout(Duration.ofSeconds(360)) + // add annotation for which app to send the http event to + Map existingAnnotations = new HashMap<>( + Optional.ofNullable(configMapMount.getMetadata().getAnnotations()).orElse(Map.of())); + existingAnnotations.put("spring.cloud.kubernetes.configmap.apps", + "spring-cloud-kubernetes-fabric8-client-reload"); + configMapMount.getMetadata().setAnnotations(existingAnnotations); + TestUtil.replaceConfigMap(client, configMapMount, "default"); + + await().timeout(Duration.ofSeconds(180)) .until(() -> webClient.method(HttpMethod.GET) .retrieve() .bodyToMono(String.class) diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/Fabric8EventReloadIT.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/Fabric8EventReloadIT.java index 13bda6d496..486158f0c7 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/Fabric8EventReloadIT.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/Fabric8EventReloadIT.java @@ -50,6 +50,8 @@ */ class Fabric8EventReloadIT { + private static final String CONFIGURATION_WATCHER_IMAGE_NAME = "spring-cloud-kubernetes-configuration-watcher"; + private static final String IMAGE_NAME = "spring-cloud-kubernetes-fabric8-client-reload"; private static final String DOCKER_IMAGE = "docker.io/springcloud/" + IMAGE_NAME + ":" + pomVersion(); @@ -65,9 +67,13 @@ class Fabric8EventReloadIT { @BeforeAll static void beforeAll() throws Exception { K3S.start(); + Commons.validateImage(IMAGE_NAME, K3S); Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S); + Commons.validateImage(CONFIGURATION_WATCHER_IMAGE_NAME, K3S); + Commons.loadSpringCloudKubernetesImage(CONFIGURATION_WATCHER_IMAGE_NAME, K3S); + util = new Util(K3S); client = util.client(); @@ -75,6 +81,7 @@ static void beforeAll() throws Exception { util.createNamespace("right"); util.setUpClusterWide(NAMESPACE, Set.of("left", "right")); util.setUp(NAMESPACE); + util.configWatcher(Phase.CREATE); manifests(Phase.CREATE); } @@ -83,6 +90,7 @@ static void beforeAll() throws Exception { static void afterAll() { util.deleteNamespace("left"); util.deleteNamespace("right"); + util.configWatcher(Phase.DELETE); manifests(Phase.DELETE); } @@ -145,7 +153,6 @@ void testInformFromOneNamespaceEventNotTriggered() { testInformFromOneNamespaceEventTriggeredSecretsDisabled(); testDataChangesInConfigMap(); testConfigMapMountPollingReload(); - testPollingReloadConfigMapWithBootstrap(); testSecretReload(); } @@ -343,18 +350,11 @@ void testDataChangesInConfigMap() { void testConfigMapMountPollingReload() { TestUtil.reCreateSources(util, client); TestUtil.patchFive(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE); - ConfigMapMountPollingReloadDelegate.testConfigMapMountPollingReload(client, util, K3S, IMAGE_NAME); - } - - void testPollingReloadConfigMapWithBootstrap() { - TestUtil.reCreateSources(util, client); - TestUtil.patchSix(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE); - BootstrapEnabledPollingReloadConfigMapMountDelegate.testPollingReloadConfigMapWithBootstrap(client, util, K3S, - IMAGE_NAME); + ConfigMapMountPollingReloadDelegate.testConfigMapMountPollingReload(client, util); } void testSecretReload() { - TestUtil.patchSeven(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE); + TestUtil.patchSix(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE); SecretsEventsReloadDelegate.testSecretReload(client, K3S, IMAGE_NAME); } diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/TestUtil.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/TestUtil.java index 7ede854c6f..b82b883c9d 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/TestUtil.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/TestUtil.java @@ -179,7 +179,13 @@ final class TestUtil { { "configMap": { "defaultMode": 420, - "name": "poll-reload" + "name": "poll-reload-configtree", + "items": [ + { + "key": "from.properties", + "path": "key" + } + ] }, "name": "config-map-volume" } @@ -187,7 +193,7 @@ final class TestUtil { "containers": [{ "volumeMounts": [ { - "mountPath": "/tmp", + "mountPath": "/tmp/props", "name": "config-map-volume" } ], @@ -196,7 +202,7 @@ final class TestUtil { "env": [ { "name": "SPRING_PROFILES_ACTIVE", - "value": "mount" + "value": "configtree" }, { "name": "SPRING_CLOUD_BOOTSTRAP_ENABLED", @@ -223,58 +229,6 @@ final class TestUtil { """; private static final String BODY_SIX = """ - { - "spec": { - "template": { - "spec": { - "volumes": [ - { - "configMap": { - "defaultMode": 420, - "name": "poll-reload" - }, - "name": "config-map-volume" - } - ], - "containers": [{ - "volumeMounts": [ - { - "mountPath": "/tmp", - "name": "config-map-volume" - } - ], - "name": "spring-cloud-kubernetes-fabric8-client-configmap-event-reload", - "image": "image_name_here", - "env": [ - { - "name": "SPRING_PROFILES_ACTIVE", - "value": "with-bootstrap" - }, - { - "name": "SPRING_CLOUD_BOOTSTRAP_ENABLED", - "value": "TRUE" - }, - { - "name": "LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG_RELOAD", - "value": "DEBUG" - }, - { - "name": "LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG", - "value": "DEBUG" - }, - { - "name": "LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS", - "value": "DEBUG" - } - ] - }] - } - } - } - } - """; - - private static final String BODY_SEVEN = """ { "spec": { "template": { @@ -303,14 +257,17 @@ static void reCreateSources(Util util, KubernetesClient client) { InputStream leftConfigMapStream = util.inputStream("left-configmap.yaml"); InputStream rightConfigMapStream = util.inputStream("right-configmap.yaml"); InputStream configMapStream = util.inputStream("configmap.yaml"); + InputStream configMapMountStream = util.inputStream("configmap-configtree.yaml"); ConfigMap leftConfigMap = Serialization.unmarshal(leftConfigMapStream, ConfigMap.class); ConfigMap rightConfigMap = Serialization.unmarshal(rightConfigMapStream, ConfigMap.class); ConfigMap configMap = Serialization.unmarshal(configMapStream, ConfigMap.class); + ConfigMap configMapMount = Serialization.unmarshal(configMapMountStream, ConfigMap.class); replaceConfigMap(client, leftConfigMap, "left"); replaceConfigMap(client, rightConfigMap, "right"); replaceConfigMap(client, configMap, "default"); + replaceConfigMap(client, configMapMount, "default"); } static void replaceConfigMap(KubernetesClient client, ConfigMap configMap, String namespace) { @@ -341,10 +298,6 @@ static void patchSix(Util util, String dockerImage, String deploymentName, Strin util.patchWithReplace(dockerImage, deploymentName, namespace, BODY_SIX, POD_LABELS); } - static void patchSeven(Util util, String dockerImage, String deploymentName, String namespace) { - util.patchWithReplace(dockerImage, deploymentName, namespace, BODY_SEVEN, POD_LABELS); - } - static WebClient.Builder builder() { return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create())); } diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/resources/configmap-configtree.yaml b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/resources/configmap-configtree.yaml new file mode 100644 index 0000000000..810e482a7c --- /dev/null +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/resources/configmap-configtree.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: poll-reload-configtree + namespace: default +data: + from.properties: "as-mount-initial" diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/resources/ingress.yaml b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/resources/ingress.yaml index d6e9e49a9e..d34a7ebebe 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/resources/ingress.yaml +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/resources/ingress.yaml @@ -1,8 +1,7 @@ apiVersion: networking.k8s.io/v1 kind: Ingress metadata: - name: spring-cloud-kubernetes-fabric8-client-reload-ingress - namespace: default + name: spring-k8s-client-ingress-reload spec: rules: - http: diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/java/org/springframework/cloud/kubernetes/k8s/client/reload/ConfigMapProperties.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/java/org/springframework/cloud/kubernetes/k8s/client/reload/ConfigMapProperties.java index f6e65df380..6d6ccfb908 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/java/org/springframework/cloud/kubernetes/k8s/client/reload/ConfigMapProperties.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/java/org/springframework/cloud/kubernetes/k8s/client/reload/ConfigMapProperties.java @@ -21,7 +21,7 @@ /** * @author wind57 */ -@ConfigurationProperties("from.properties") +@ConfigurationProperties("props") public class ConfigMapProperties { private String key; @@ -30,8 +30,8 @@ public String getKey() { return key; } - public void setKey(String key1) { - this.key = key1; + public void setKey(String key) { + this.key = key; } } diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/java/org/springframework/cloud/kubernetes/k8s/client/reload/Controller.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/java/org/springframework/cloud/kubernetes/k8s/client/reload/Controller.java index e2eb643534..b23d7f1b12 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/java/org/springframework/cloud/kubernetes/k8s/client/reload/Controller.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/java/org/springframework/cloud/kubernetes/k8s/client/reload/Controller.java @@ -16,6 +16,10 @@ package org.springframework.cloud.kubernetes.k8s.client.reload; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @@ -56,9 +60,19 @@ public String witLabel() { return rightWithLabelsProperties.getValue(); } - @GetMapping("/mount") - public String key() { + @GetMapping("/key-no-mount") + public String keyNoMount() { return configMapProperties.getKey(); } + @GetMapping("/debug") + public String debug() { + try { + return Files.readAllLines(Paths.get("/tmp/props/key")).get(0); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + } diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/resources/application-mount.yaml b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/resources/application-mount.yaml index cd1765f5c8..7a977072cb 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/resources/application-mount.yaml +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/resources/application-mount.yaml @@ -1,17 +1,16 @@ spring: application: name: poll-reload-mount - cloud: - kubernetes: - reload: - enabled: true - monitoring-config-maps: true - strategy: shutdown - mode: polling - period: 5000 - config: - paths: - - /tmp/application.properties config: - import: "kubernetes:" + import: "kubernetes:,configtree:/tmp/" +management: + endpoint: + refresh: + enabled: true + restart: + enabled: true + endpoints: + web: + exposure: + include: '*' diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/resources/application-with-bootstrap.yaml b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/resources/application-with-bootstrap.yaml index c997322d16..92152ef039 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/resources/application-with-bootstrap.yaml +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/main/resources/application-with-bootstrap.yaml @@ -1,12 +1,21 @@ +logging: + level: + root: DEBUG + spring: application: - name: poll-reload-mount-boostrap - cloud: - kubernetes: - reload: - enabled: true - monitoring-config-maps: true - strategy: shutdown - mode: polling - period: 5000 + name: poll-reload + config: + import: "kubernetes:,configtree:/tmp/" + +management: + endpoint: + refresh: + enabled: true + restart: + enabled: true + endpoints: + web: + exposure: + include: '*' diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadIT.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadIT.java index 9814a9066b..611cb0fdb8 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadIT.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadIT.java @@ -43,20 +43,20 @@ import org.springframework.web.reactive.function.client.WebClient; import static org.awaitility.Awaitility.await; -import static org.springframework.cloud.kubernetes.k8s.client.reload.configmap.BootstrapEnabledPollingReloadConfigMapMountDelegate.testBootstrapEnabledPollingReloadConfigMapMount; import static org.springframework.cloud.kubernetes.k8s.client.reload.configmap.DataChangesInConfigMapReloadDelegate.testSimple; import static org.springframework.cloud.kubernetes.k8s.client.reload.configmap.K8sClientConfigMapReloadITUtil.builder; import static org.springframework.cloud.kubernetes.k8s.client.reload.configmap.K8sClientConfigMapReloadITUtil.patchOne; import static org.springframework.cloud.kubernetes.k8s.client.reload.configmap.K8sClientConfigMapReloadITUtil.patchThree; import static org.springframework.cloud.kubernetes.k8s.client.reload.configmap.K8sClientConfigMapReloadITUtil.patchTwo; import static org.springframework.cloud.kubernetes.k8s.client.reload.configmap.K8sClientConfigMapReloadITUtil.retrySpec; -import static org.springframework.cloud.kubernetes.k8s.client.reload.configmap.PollingReloadConfigMapMountDelegate.testPollingReloadConfigMapMount; /** * @author wind57 */ class K8sClientConfigMapReloadIT { + private static final String CONFIGURATION_WATCHER_IMAGE_NAME = "spring-cloud-kubernetes-configuration-watcher"; + private static final String IMAGE_NAME = "spring-cloud-kubernetes-k8s-client-reload"; private static final String DEPLOYMENT_NAME = "spring-k8s-client-reload"; @@ -74,13 +74,19 @@ class K8sClientConfigMapReloadIT { @BeforeAll static void beforeAll() throws Exception { K3S.start(); + Commons.validateImage(IMAGE_NAME, K3S); Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S); + + Commons.validateImage(CONFIGURATION_WATCHER_IMAGE_NAME, K3S); + Commons.loadSpringCloudKubernetesImage(CONFIGURATION_WATCHER_IMAGE_NAME, K3S); + util = new Util(K3S); util.createNamespace("left"); util.createNamespace("right"); util.setUpClusterWide(NAMESPACE, Set.of("left", "right")); util.setUp(NAMESPACE); + util.configWatcher(Phase.CREATE); api = new CoreV1Api(); } @@ -90,6 +96,7 @@ static void afterAll() { manifests(Phase.DELETE); util.deleteNamespace("left"); util.deleteNamespace("right"); + util.configWatcher(Phase.DELETE); } /** @@ -148,8 +155,7 @@ private void testAllOther() throws Exception { testInform(); testInformFromOneNamespaceEventTriggeredSecretsDisabled(); testSimple(DOCKER_IMAGE, DEPLOYMENT_NAME, K3S); - testPollingReloadConfigMapMount(DEPLOYMENT_NAME, K3S, util, DOCKER_IMAGE); - testBootstrapEnabledPollingReloadConfigMapMount(DEPLOYMENT_NAME, K3S, util, DOCKER_IMAGE); + ReloadConfigMapMountDelegate.testReloadConfigMapMount(DEPLOYMENT_NAME, util, DOCKER_IMAGE); } diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadITUtil.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadITUtil.java index 2043b40e0f..127267a86d 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadITUtil.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadITUtil.java @@ -262,7 +262,13 @@ private K8sClientConfigMapReloadITUtil() { { "configMap": { "defaultMode": 420, - "name": "poll-reload-as-mount" + "name": "poll-reload-as-mount", + "items": [ + { + "key": "from.properties", + "path": "key" + } + ] }, "name": "config-map-volume" } @@ -272,7 +278,7 @@ private K8sClientConfigMapReloadITUtil() { "image": "image_name_here", "volumeMounts": [ { - "mountPath": "/tmp", + "mountPath": "/tmp/props", "name": "config-map-volume" } ], @@ -336,7 +342,13 @@ private K8sClientConfigMapReloadITUtil() { { "configMap": { "defaultMode": 420, - "name": "poll-reload-as-mount" + "name": "poll-reload-as-mount", + "items": [ + { + "key": "from.properties", + "path": "key" + } + ] }, "name": "config-map-volume" } @@ -346,7 +358,7 @@ private K8sClientConfigMapReloadITUtil() { "image": "image_name_here", "volumeMounts": [ { - "mountPath": "/tmp", + "mountPath": "/tmp/props", "name": "config-map-volume" } ], diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/PollingReloadConfigMapMountDelegate.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/ReloadConfigMapMountDelegate.java similarity index 61% rename from spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/PollingReloadConfigMapMountDelegate.java rename to spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/ReloadConfigMapMountDelegate.java index 3092e92535..46b3c2b534 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/PollingReloadConfigMapMountDelegate.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/ReloadConfigMapMountDelegate.java @@ -17,15 +17,14 @@ package org.springframework.cloud.kubernetes.k8s.client.reload.configmap; import java.time.Duration; +import java.util.HashMap; import java.util.Map; +import java.util.Optional; import io.kubernetes.client.openapi.apis.CoreV1Api; import io.kubernetes.client.openapi.models.V1ConfigMap; import org.junit.jupiter.api.Assertions; -import org.testcontainers.k3s.K3sContainer; -import org.springframework.cloud.kubernetes.commons.config.Constants; -import org.springframework.cloud.kubernetes.integration.tests.commons.Commons; import org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util; import org.springframework.http.HttpMethod; import org.springframework.web.reactive.function.client.WebClient; @@ -35,42 +34,30 @@ /** * @author wind57 */ -final class PollingReloadConfigMapMountDelegate { +final class ReloadConfigMapMountDelegate { - private PollingReloadConfigMapMountDelegate() { + private ReloadConfigMapMountDelegate() { } /** *
-	 *     - we have "spring.config.import: kubernetes", which means we will 'locate' property sources
+	 *     - we have "spring.config.import: kubernetes:,configtree:/tmp/", which means we will 'locate' property sources
 	 *       from config maps.
 	 *     - the property above means that at the moment we will be searching for config maps that only
 	 *       match the application name, in this specific test there is no such config map.
-	 *     - what we will also read, is 'spring.cloud.kubernetes.config.paths', which we have set to
-	 *     	 '/tmp/application.properties'
-	 *       in this test. That is populated by the volumeMounts (see BODY_FIVE)
-	 *     - we first assert that we are actually reading the path based source via (1), (2) and (3).
+	 *     - what we will also read, is /tmp directory according to configtree rules.
+	 *       As such, a property "props.key" (see K8sClientConfigMapReloadITUtil::BODY_FIVE) will be in environment.
 	 *
-	 *     - we then change the config map content, wait for k8s to pick it up and replace them
-	 *     - our polling will then detect that change, and trigger a reload.
+	 *     - we then change the config map content, wait for configuration watcher to pick up the change
+	 *       and schedule a refresh event, based on http.
 	 * 
*/ - static void testPollingReloadConfigMapMount(String deploymentName, K3sContainer k3sContainer, Util util, - String imageName) throws Exception { + static void testReloadConfigMapMount(String deploymentName, Util util, String imageName) throws Exception { K8sClientConfigMapReloadITUtil.patchFive(deploymentName, "default", imageName); - // (1) - Commons.waitForLogStatement("paths property sources : [/tmp/application.properties]", k3sContainer, - deploymentName); - - // (2) - Commons.waitForLogStatement("will add file-based property source : /tmp/application.properties", k3sContainer, - deploymentName); - - // (3) - WebClient webClient = K8sClientConfigMapReloadITUtil.builder().baseUrl("http://localhost/mount").build(); + WebClient webClient = K8sClientConfigMapReloadITUtil.builder().baseUrl("http://localhost/key-no-mount").build(); String result = webClient.method(HttpMethod.GET) .retrieve() .bodyToMono(String.class) @@ -83,7 +70,18 @@ static void testPollingReloadConfigMapMount(String deploymentName, K3sContainer // replace data in configmap and wait for k8s to pick it up // our polling will detect that and restart the app V1ConfigMap configMap = (V1ConfigMap) util.yaml("configmap-mount.yaml"); - configMap.setData(Map.of(Constants.APPLICATION_PROPERTIES, "from.properties.key=as-mount-changed")); + configMap.setData(Map.of("from.properties", "as-mount-changed")); + // add label so that configuration-watcher picks this up + Map existingLabels = new HashMap<>( + Optional.ofNullable(configMap.getMetadata().getLabels()).orElse(Map.of())); + existingLabels.put("spring.cloud.kubernetes.config", "true"); + configMap.getMetadata().setLabels(existingLabels); + + // add annotation for which app to send the http event to + Map existingAnnotations = new HashMap<>( + Optional.ofNullable(configMap.getMetadata().getAnnotations()).orElse(Map.of())); + existingAnnotations.put("spring.cloud.kubernetes.configmap.apps", "spring-k8s-client-reload"); + configMap.getMetadata().setAnnotations(existingAnnotations); new CoreV1Api().replaceNamespacedConfigMap("poll-reload-as-mount", "default", configMap, null, null, null, null); diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/resources/configmap-mount.yaml b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/resources/configmap-mount.yaml index f2ea29a5c2..e0cf495c6c 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/resources/configmap-mount.yaml +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/resources/configmap-mount.yaml @@ -4,5 +4,4 @@ metadata: name: poll-reload-as-mount namespace: default data: - application.properties: | - from.properties.key=as-mount-initial + from.properties: "as-mount-initial" diff --git a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/fabric8_client/Util.java b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/fabric8_client/Util.java index 8d98a078e0..f976638239 100644 --- a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/fabric8_client/Util.java +++ b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/fabric8_client/Util.java @@ -372,6 +372,22 @@ public void wiremock(String namespace, String path, Phase phase, boolean withIng } + public void configWatcher(Phase phase) { + + InputStream deploymentStream = inputStream("config-watcher/deployment.yaml"); + InputStream serviceStream = inputStream("config-watcher/service.yaml"); + + Deployment deployment = client.apps().deployments().load(deploymentStream).item(); + Service service = client.services().load(serviceStream).item(); + + if (phase.equals(Phase.CREATE)) { + createAndWait("default", deployment.getMetadata().getName(), deployment, service, null, true); + } + else if (phase.equals(Phase.DELETE)) { + deleteAndWait("default", deployment, service, null); + } + } + private void waitForSecret(String namespace, Secret secret, Phase phase) { String secretName = secretName(secret); await().pollInterval(Duration.ofSeconds(1)).atMost(600, TimeUnit.SECONDS).until(() -> { diff --git a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java index bb24aa0176..7041d7053f 100644 --- a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java +++ b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java @@ -490,6 +490,19 @@ public void wiremock(String namespace, String path, Phase phase, boolean withIng } + public void configWatcher(Phase phase) { + + V1Deployment deployment = (V1Deployment) yaml("config-watcher/deployment.yaml"); + V1Service service = (V1Service) yaml("config-watcher/service.yaml"); + + if (phase.equals(Phase.CREATE)) { + createAndWait("default", deployment.getMetadata().getName(), deployment, service, null, true); + } + else { + deleteAndWait("default", deployment, service, null); + } + } + public static void patchWithMerge(String deploymentName, String namespace, String patchBody, Map podLabels) { try { diff --git a/spring-cloud-kubernetes-test-support/src/main/resources/config-watcher/deployment.yaml b/spring-cloud-kubernetes-test-support/src/main/resources/config-watcher/deployment.yaml new file mode 100644 index 0000000000..2cf04f3712 --- /dev/null +++ b/spring-cloud-kubernetes-test-support/src/main/resources/config-watcher/deployment.yaml @@ -0,0 +1,37 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: spring-cloud-kubernetes-configuration-watcher-deployment +spec: + selector: + matchLabels: + app: spring-cloud-kubernetes-configuration-watcher + template: + metadata: + labels: + app: spring-cloud-kubernetes-configuration-watcher + spec: + serviceAccountName: spring-cloud-kubernetes-serviceaccount + containers: + - name: spring-cloud-kubernetes-configuration-watcher + image: docker.io/springcloud/spring-cloud-kubernetes-configuration-watcher + imagePullPolicy: IfNotPresent + readinessProbe: + httpGet: + port: 8888 + path: /actuator/health/readiness + livenessProbe: + httpGet: + port: 8888 + path: /actuator/health/liveness + ports: + - containerPort: 8888 + env: + - name: LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CONFIGURATION_WATCHER + value: DEBUG + - name: LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CLIENT_CONFIG_RELOAD + value: DEBUG + - name: LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG_RELOAD + value: DEBUG +# - name: SPRING_CLOUD_KUBERNETES_CONFIGURATION_WATCHER_REFRESHDELAY +# value: "10000" diff --git a/spring-cloud-kubernetes-test-support/src/main/resources/config-watcher/service.yaml b/spring-cloud-kubernetes-test-support/src/main/resources/config-watcher/service.yaml new file mode 100644 index 0000000000..c8496317b1 --- /dev/null +++ b/spring-cloud-kubernetes-test-support/src/main/resources/config-watcher/service.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + labels: + app: spring-cloud-kubernetes-configuration-watcher + name: spring-cloud-kubernetes-configuration-watcher +spec: + ports: + - name: http + port: 8888 + targetPort: 8888 + selector: + app: spring-cloud-kubernetes-configuration-watcher + type: ClusterIP