Spring Cloud Kubernetes无法重新加载秘密更改

我正在通过使用Spring Cloud Kubernetes的功能来动态加载秘密来探索其功能。但是,我仍然无法正常工作。

我有一个简单的Spring Boot应用程序,它只打印出安装在pod中的密钥的内容。这是 bootstrap.properties

中的配置
spring.cloud.kubernetes.reload.enabled=true
spring.cloud.kubernetes.reload.monitoring-secrets=true

spring.cloud.kubernetes.secrets.enabled=true
spring.cloud.kubernetes.secrets.paths=/etc/secret-volume

management.endpoint.info.enabled=true
management.endpoint.health.enabled=true
management.endpoint.restart.enabled=true

application.properties 中,我定义了属性以获取密钥的值:

mysecret.password=${MY-PWD}

在Spring Boot应用程序中,我定义了一个将存储密钥值的bean:

@Configuration
@ConfigurationProperties(prefix = "mysecret")
public class MySecret {

    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

当我在minikube上运行应用程序时,我在日志中看到Spring检测到声明的机密并激活了配置文件:

16:54:30.887 [main]信息 o.s.c.b.c.PropertySourceBootstrapConfiguration-位于属性 来源:[BootstrapPropertySource @ 1132379993 {name ='bootstrapProperties-my-pwd',properties = {MY-PWD = qwerty}}]] 16:54:30.899 [main] INFO c.c.r.ReloadSecretsApplication- 以下配置文件处于活动状态:kubernetes

过一会儿,我得到以下日志,该日志表明它已被添加为秘密监视者:

16:54:35.460 [OkHttp https://10.96.0.1 / ...]调试 i.f.k.c.d.i.WatchConnectionmanager-WebSocket已成功打开 16:54:35.460 [main] INFO o.s.c.k.c.r.EventBasedConfigurationChangeDetector-添加了新的 Kubernetes手表:秘密手表16:54:35.460 [main]信息 o.s.c.k.c.r.EventBasedConfigurationChangeDetector-Kubernetes 基于事件的配置更改检测器已激活

然后,当我更改机密时,我得到以下内容:不会触发重新加载:

11:20:15.963 [OkHttp https://10.96.0.1 / ...]警告 o.s.c.k.c.r.EventBasedConfigurationChangeDetector-当前数字 的Confimap PropertySources与从 Kubernetes-不会重新加载

有关此主题的文档非常少。 我这里有缺少的配置吗?

链接到Spring Boot应用程序:https://github.com/Azlop/spring-cloud-kubernetes-reload-secrets

iCMS 回答:Spring Cloud Kubernetes无法重新加载秘密更改

我知道这是一个没有答案的老问题。我遇到了同样的问题并经过一些研究解决了它。在这里我分享一下我对这个问题的经验,如果它可以帮助将来的人。

Spring Cloud Kubernetes 已将外部配置映射作为多个 ConfigMapPropertySource 实例存储在 CompositePropertySource 或 BootstrapPropertySource 中。当应用程序修改配置映射时,它会从 CompositePropertySource 中获取 ConfigMapPropertySource 实例,并将 ConfigMapPropertySource 实例的长度与传入的实例进行比较。如果长度不同,应用程序将返回错误消息:“当前的 Confimap PropertySource 数量与从 Kubernetes 加载的数量不匹配 - 不会发生重新加载”。

但是这里有一个潜在的问题:其他函数,在我的例子中是一个加密器,可能会改变 CompositePropertySource 的类型。后来,应用程序无法获取 ConfigMapPropertySource 实例,必须比较失败。

所以我的解决方案是创建一个自定义的 EventBasedConfigurationChangeDetector,它已经改变了比较的逻辑。原版为:

private void onEvent(ConfigMap configMap) {
    boolean changed = changed(locateMapPropertySources(this.configMapPropertySourceLocator,this.environment),findPropertySources(ConfigMapPropertySource.class));
    if (changed) {
            XXXX
    }
}

您应该重写 findPropertySources 函数以适应您的情况。在我的实例中,我解开加密的 PropertySource 以获得原始的 ConfigMapPropertySource 实例。

最后,注入您自定义的 EventBasedConfigurationChangeDetector 来替换原版即可解决问题。

本文链接:https://www.f2er.com/2004612.html

大家都在问