spring - 使用 spring.cloud.kubernetes.secrets.paths 属性将机密作为文件读取
问题描述
是否有一个如何使用spring.cloud.kubernetes.secrets.paths=/mnt/secrets-store
属性将机密作为文件读取的示例(作为卷安装)?
我的 Pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: nginx-mounted-secrets
namespace: default
labels:
aadpodidbinding: pod-mi
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: foo
mountPath: "/mnt/secrets-store"
readOnly: true
volumes:
- name: foo
csi:
driver: secrets-store.csi.k8s.io
readOnly: true
volumeAttributes:
secretProviderClass: spc
当我运行 Pod 时,秘密已安装
kubectl -n default exec -it nginx-mounted-secrets -- bash
root@nginx-mounted-secrets:/# ls /mnt/secrets-store
service-one-secret
service-two-secret
我尝试了以下方法,但未填充字段:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties
@Data
public class ApplicationSecrets {
private String serviceOneSecret;
private String serviceTwoSecret;
}
我在用着
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-config</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
解决方案
一般来说,spring 应用程序ConfigurationProperties
属性的构建考虑到了关注点分离的前缀(即 spring 属性与应用程序属性..)。
按照您的 yml 结构,您的属性有一些不同的关注点:元数据、规范(分为容器和卷)
因此,为了使您的实现正常工作:
@ConfigurationProperties
为每个子树定义正确的前缀并相应地使用它们
2.[OR] 加载 yml 属性并通过 @Value 查找它们,使用自定义 propertyPlaceholder,如以下答案所示:How to use YamlPropertiesFactoryBean to load YAML files using Spring Framework 4.1? 例如,它们会自动装配地图中的所有属性(此解决方案与@Value("#{${propertyname}}") private Map<String,String> propertyname;
推荐阅读
- c++ - C++:如何从另一个谓词定义一个泛型非谓词
- c# - 使用注入的记录器登录类
- javascript - 在 ASP.NET MVC 中解析莫里斯图的数据
- java - 从反应堆级别运行时的 Maven 强制执行器问题
- java - 错误:http错误:android生成签名apk中的内部错误(500)
- angular - 如何直接从 formControl 获取验证器?
- android - 如何在H264上接听电话和丢失包时修复黑屏
- vba - 过滤和计算 3 列之间的匹配项
- html - 在 Angular Material Web 应用的单个页面上显示普通 HTML 元素
- c++ - 如何在 C++ 中使用 unordered_maps 的 unordered_map?