英文:
kubectl kustomize add annotation to multiple overlay yaml files
问题
我正在尝试将一个文件中的一些注释合并到多个资源中,以保持DRY并使Pod能够从vault中获取信息。
通常情况下,我可以通过在 "mylogger" 中使用 "kind: Deployment" 的方式添加以下代码(我认为这将只允许我从此文件中获取信息到 "mylogger" 资源中)。部署后,"mylogger" Pod 似乎在工作,并且可以获取vault信息。
其他信息是,该项目遵循 base/overlay 结构并使用 kubectl 和 kustomize 命令。
对于文件...
vault-values.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mylogger
spec:
template:
metadata:
annotations:
inject-vault-value1: "path-to-vault-value1"
inject-vault-value2: "path-to-vault-value2"
mylogger.yml 资源文件如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: mylogger
labels:
app: mylogger
spec:
replicas: 2
selector:
matchLabels:
app: mylogger
template:
metadata:
labels:
app: mylogger
spec:
initContainers:
.... 其余部分在这里
运行 kubectl kustomize .../overlay/dev > manifest.yml
我可以在 manifest.yml 文件中看到期望的结果
apiVersion: apps/v1
kind: Deployment
metadata:
name: mylogger
labels:
app: mylogger
spec:
replicas: 1
selector:
matchLabels:
app: mylogger
template:
metadata:
annotations:
inject-vault-value1: "path-to-vault-value1"
inject-vault-value2: "path-to-vault-value2"
labels:
app: mylogger
spec:
initContainers:
... 其余部分
规范中的部分
是否可以使用 vault-value.yml 文件并将其内容插入到例如 myjob 资源中?基本上是从 "spec" 以及之后的部分,到它的注释
myjob.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myjob
spec:
replicas: 1
template:
spec:
containers:
- name: myjob
env:
- name: random__env__variable
value: false
... 其余部分在这里
注意:我希望使用 overlay 文件夹中的文件,因为它具有特定环境的正确 vault 信息。在基本文件夹中,我没有与 vault 信息或 vault yaml 文件相关的内容。
尽管 "patchesStrategicMerge" 命令可能会有帮助,但对于 kustomize 命令来说,它似乎只适用于基本/叠加内容。
英文:
I am trying to merge some annotations in one file to multiple resources to keep it DRY and in order for pods to get information from a vault.
Generally I can add the following code to "mylogger" by using the kind: Deployment (which I presume will only allow me to get the info from this file into only the mylogger resource). After deployment the mylogger pod seems to be working, and can get the vault information.
Other information is that the project follows the base/overlay structure and uses kubectl and kustomize commands.
For the files...
vault-values.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mylogger
spec:
template:
metadata:
annotations:
inject-vault-value1: "path-to-vault-value1"
inject-vault-value2: "path-to-vault-value2"
The mylogger.yml resource file is
apiVersion: apps/v1
kind: Deployment
metadata:
name: mylogger
labels:
app: mylogger
spec:
replicas: 2
selector:
matchLabels:
app: mylogger
template:
metadata:
labels:
app: mylogger
spec:
initContainers:
.... and rest of file here
doing kubectl kustomize .../overlay/dev > manifest.yml
I can see the desired result in my manifest.yml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: mylogger
labels:
app: mylogger
spec:
replicas: 1
selector:
matchLabels:
app: mylogger
template:
metadata:
annotations:
inject-vault-value1: "path-to-vault-value1"
inject-vault-value2: "path-to-vault-value2"
labels:
app: mylogger
spec:
initContainers:
... rest if file
The part under spec > template > metadata > annotations > inject-vault-value1 is there.
Is it possible to use the vault-value.yml file and insert its contents into for example myjob resource? Basically the part from spec and down, to its annotations
myjob.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myjob
spec:
replicas: 1
template:
spec:
containers:
- name: myjob
env:
- name: random__env__variable
value: false
...rest of file here
Note: I want to use the file in the overlay folder as it has the correct vault information for that particular environment. I have nothing in base folder concerning the vault information or the vault yaml file.
Thought the command "patchesStrategicMerge" would come in handy, but for the kustomize command it seems only doable for a base/overlay contents
答案1
得分: 0
最佳实现您的目标取决于您的项目结构,但一个选项是使用 Kustomize 补丁,如下所示:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
# 这指向您加载的 `mylogger` 和 `myjob` 部署位置
resources:
- ...
patches:
- target:
kind: Deployment
patch: |
apiVersion: apps/v1
kind: Deployment
metadata:
name: this-is-ignored
spec:
template:
metadata:
annotations:
inject-vault-value1: "path-to-vault-value1"
inject-vault-value2: "path-to-vault-value2"
这将将两个自定义注释应用于由此 kustomization.yaml
文件生成的所有部署。如果您需要将其限制为特定部署,您可以使用 模式表达式或标签选择器 来匹配适当的对象。
英文:
How to best accomplish your goal depends on how your project is structured, but one option is to use a Kustomize patch, like this:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
# This points to where you're loading your `mylogger` and `myjob` deployments
resources:
- ...
patches:
- target:
kind: Deployment
patch: |
apiVersion: apps/v1
kind: Deployment
metadata:
name: this-is-ignored
spec:
template:
metadata:
annotations:
inject-vault-value1: "path-to-vault-value1"
inject-vault-value2: "path-to-vault-value2"
This will apply your two custom annotations to all deployments generated by this kustomization.yaml
file. If you need to limit it to specific deployments, you can use a pattern expression or label selector to match the appropriate objects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论