spring-cloud-kubernetes: Spring Cloud Kubernetes cant refresh config

JDK17 spring-boot-starter-parent 3.0.9 spring-cloud-dependencies 2022.0.4 spring-cloud-starter-kubernetes-fabric8-all spring-boot-starter-actuator

Application.yaml

spring:
  main:
    cloud-platform: kubernetes
  application:
    name: application

  config:
    import: 'kubernetes:'

  cloud:
    config:
      enabled: false
    kubernetes:
      config:
        name: master
        namespace: istio-example
        enabled: true

      client:
        master-url: "https://k8s.local:6443"
        trust-certs: true
        namespace: istio-example
        oauth-token: "token"
      discovery:
        enabled: true
        all-namespaces: false
        namespaces:
          - istio-example
      reload:
        monitoring-config-maps: true
        enabled: true
        strategy: refresh
  profiles:
    active: dev

management:
  endpoint:
    refresh:
      enabled: true 
  endpoints:
    web:
      exposure:
        include: '*' 

logging:
  level:
    org.springframework.cloud: debug

ConfigMap.yaml

kind: ConfigMap
apiVersion: v1
metadata:
  name: master
  namespace: istio-example
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: >
      {"apiVersion":"v1","data":{"master-dev.yaml":"test:\n  dev:\n    value:
      application-dev-profile","master.yaml":"test:\n  application:\n    value:
      application-profile\nspring:\n  profiles:\n    active:
      dev"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"master","namespace":"istio-example"}}
data:
  application-dev.yaml: |-
    test:
      dev:
        value: application-dev-profile2

    k8s:
      value: k8s1
  application.yaml: |-
    test:
      application:
        value: application-profile1
    spring:
      profiles:
        active: dev

Properties Class

@Data
@Component
@RefreshScope
public class RefreshProperties {

    @Value("${test.dev.value}")
    private String devValue;

    @Value("${test.application.value}")
    private String applicationValue;
}

@Autowired
private RefreshProperties pefreshProperties;

@GetMapping("/api/devValue")
    public String devValue() {
        return pefreshProperties.getDevValue();
}

POST localhost:8080/actuator/refresh

GET localhost:8080/api/devValue RESPONSE application-dev-profile2

After updating ConfigMap.yaml, call the refresh interface, and then request the devValue interface. In fact, the configuration has not been updated

I can see that the log has been printed and configuration changes have been detected

2023-10-20T10:39:30.581+08:00 DEBUG 31860 --- [pool-2-thread-3] o.s.c.k.c.c.reload.ConfigReloadUtil      : findPropertySources : [configmap.master.istio-example]
2023-10-20T10:39:30.582+08:00 DEBUG 31860 --- [pool-2-thread-3] o.s.c.k.c.c.reload.ConfigReloadUtil      : found change in : Fabric8ConfigMapPropertySource@2035899344 {name='configmap.master.istio-example', properties={test.dev.value=application-dev-profile3, k8s.value=k8s2, test.application.value=application-profile1, spring.profiles.active=dev}}
2023-10-20T10:39:30.582+08:00  INFO 31860 --- [pool-2-thread-3] o.s.c.k.c.c.reload.ConfigReloadUtil      : Detected change in config maps/secrets
2023-10-20T10:39:30.582+08:00  INFO 31860 --- [pool-2-thread-3] .s.c.k.c.c.r.ConfigurationChangeDetector : Reloading using strategy: REFRESH
2023-10-20T10:39:30.707+08:00 DEBUG 31860 --- [pool-2-thread-3] o.s.c.b.BootstrapApplicationListener$1   : Activating profiles [dev]
2023-10-20T10:39:30.721+08:00 DEBUG 31860 --- [pool-2-thread-3] o.s.c.b.BootstrapApplicationListener$1   : Activating profiles [dev]
2023-10-20T10:39:30.751+08:00  INFO 31860 --- [pool-2-thread-3] o.s.boot.SpringApplication               : The following 1 profile is active: "dev"
2023-10-20T10:39:30.756+08:00  INFO 31860 --- [pool-2-thread-3] o.s.boot.SpringApplication               : Started application in 0.173 seconds (process running for 673.806)

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Comments: 22 (10 by maintainers)

Most upvoted comments

May be caused by kubersphere enterprise space permissions,The default token does not have enterprise space authorization, so when I start it, I will prompt 401unauthorized and 403 forbidden,I modified the enterprise space permissions, and then it worked.

I still can’t reproduce it. I’ve create a PR for your project, see here

Here is what I do:

    1. create a local cluster (I am using kind), this is needed to be able to curl on the NodePort
kind create cluster --config k8s/kind.yaml
    1. kubectl proxy --port=8092 and in a different terminal: curl http://localhost:8092/api. This will show something like this:
{
  "kind": "APIVersions",
  "versions": [
    "v1"
  ],
  "serverAddressByClientCIDRs": [
    {
      "clientCIDR": "0.0.0.0/0",
      "serverAddress": "172.18.0.3:6443"
    }
  ]
}

all I need is the serverAddress. In my case: 172.18.0.3:6443. Take this value and place it in application.yaml:

      client:
        master-url: "https://172.18.0.3:6443"
    1. build the docker image (I am not using the Dockerfile), but instead (notice in my PR that I have changed the pom.xml):
mvn clean install -Dspring-boot.build-image.builder=dashaun/builder:tiny 

I only need -Dspring-boot.build-image.builder=dashaun/builder:tiny because I am on ARM Cpu, if you are not, you might not need that.

    1. check that the image has been created:
wind57@wind57s-MacBook-Pro /p/t/istio-master (try-to-reproduce-on-my-own-branch)> docker images | grep master
springcloud/master                                                            0.0.1-SNAPSHOT   0a9d4e410416   43 years ago    228MB

Please note that I have also changed:

      containers:
        - name: master
          image: docker.io/springcloud/master:0.0.1-SNAPSHOT
          imagePullPolicy: IfNotPresent
    1. load the image into kind:
kind load docker-image springcloud/master:0.0.1-SNAPSHOT
    1. create the namespace:
kubectl create ns istio-example
    1. apply manifests
kubectl apply -f k8s/privileges.yaml
kubectl apply -f k8s/config-map.yaml
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
    1. see the current value:
wind57@wind57s-MacBook-Pro /p/t/istio-master (try-to-reproduce-on-my-own-branch) [56]> curl  localhost:30950/api/devValue
application-dev-profile11
    1. Edit config-map:
kubectl edit configmap master -n istio-example
    1. curl again:
wind57@wind57s-MacBook-Pro /p/t/istio-master (try-to-reproduce-on-my-own-branch)> curl  localhost:30950/api/devValue
application-dev-profile12

If you can tell me how I ca re-produce this one against my PR that I made, that would be lovely. Thank you

I had to go through quite a few hoops to get this running: you have not defined RBAC for the service account, not said how you build the docker image, your app the way it is would fail start-up with some dependency miss-configuration… Next time, pretty please, make sure you provide a complete and minimal example, otherwise I might miss what set-up you have exactly.

What I had to do with your app (besides creating the RBAC rules for the service account) is to remove bootstrap.yaml and the spring-cloud-starter-bootstrap dependency. Otherwise, you have both config-data and bootstrap, so there would be a certain bean created two times and Spring context would not start.

After I removed the above mentioned, created the image (using the spring-boot plugin), I deployed the config-map and deployment and then did :

kubectl get pods -o wide

to find out the IP of the pod (since you have not provided a service and ingress either). Then:

curl 10.244.0.15:8080/api/devValue

and got the application-dev-profile11 as a response. So far, you get the same result.

Then, I did a kubectl edit configmap master (notice that I have simplified your initial example by not including the namespace you had, its only to make my life easier 😃 )

and then did another curl, and I got the new value as a response, the updated one.

Notice that I did not have to do any POST localhost:8080/actuator/refresh, because you already have:

....
      reload:
        monitoring-config-maps: true
        enabled: true
        strategy: refresh

and the refresh will happen automatically.


Unless you have an example where your problem is really re-producible, there is nothing I can do more.

@ryanjbaxter can you please add the proper label in here? We need to get proper feedback from OP, otherwise it should be closed. thank you

oh sorry, idea setting repository is private ,im changed. you can access