Kustomize/Kubernetes - 嵌套 envFrom 注入

发布于 2025-01-17 03:39:52 字数 1855 浏览 3 评论 0原文

我正在尝试添加基于 envFrom 的 envs 注入

。简化的结构看起来像这样:

├── base
│   ├   ─ backend
    │   ├── backend.properties
    │   ├── app1
    │   │   ├── app1_backend.properties
        ├   ── deployment.yaml
    │   │   ├── ingress.yaml
    │   │   ├── kustomization.yaml
    ├── common.properties
    ├── frontend
    │   ├── app1
        │   ├── app1_frontend.properties
    │   │   ├── deployment.yaml
    │   │   ├── ingress.yaml
    │   │   ├── kustomization.yaml
    │   │   └── service.yaml
    │   ├── frontend.properties
    │   └── kustomization.yaml
    └── kustomization.yaml

我想在主级别(公共)、后端/前端级别和特定应用程序级别生成属性。 所以我试图在主级别上添加以下补丁并且它可以工作:

  - op: add
    path: /spec/template/spec/containers/0/envFrom
    value:
    - configMapRef:
        name: common-properties

并将以下代码添加到嵌套目录(后端/前端/特定应用程序)

- op: add
  path: "/spec/template/spec/containers/0/envFrom/-"
  value:
    configMapRef:
      name: backend-properties

但它不适用于以下错误:

add operation does not apply: doc is missing path: "/spec/template/spec/containers/0/envFrom/-": missing value

我在 GitHub 上看到了一些示例,其中使用语法:https://github.com/search?l=YAML&p=1&q=%2Fspec%2Ftemplate%2Fspec%2Fcontainers%2F0%2FenvFrom%2F-&type=Code (您有登录查看结果)而且我不确定这是否会在特定的 Kustomize 版本上停止工作(我正在使用最新版本 - 4.5.3)或者它从未工作过

我已经使用 /- 向资源编写一些 Kustomize 补丁和语法通常对于清单上已存在的资源效果很好。 可以在不同级别注入 envFrom 吗?

I'm trying to add envs injection based on envFrom

A simplified structure looks something like that:

├── base
│   ├   ─ backend
    │   ├── backend.properties
    │   ├── app1
    │   │   ├── app1_backend.properties
        ├   ── deployment.yaml
    │   │   ├── ingress.yaml
    │   │   ├── kustomization.yaml
    ├── common.properties
    ├── frontend
    │   ├── app1
        │   ├── app1_frontend.properties
    │   │   ├── deployment.yaml
    │   │   ├── ingress.yaml
    │   │   ├── kustomization.yaml
    │   │   └── service.yaml
    │   ├── frontend.properties
    │   └── kustomization.yaml
    └── kustomization.yaml

I would like to generate properties on the main level(common), backend/frontend level, and particular app level.
So I was trying to add the following patch on main level and it works:

  - op: add
    path: /spec/template/spec/containers/0/envFrom
    value:
    - configMapRef:
        name: common-properties

and following code to nested directories(backend/frontend/particular app)

- op: add
  path: "/spec/template/spec/containers/0/envFrom/-"
  value:
    configMapRef:
      name: backend-properties

But it doesn't work with the following error:

add operation does not apply: doc is missing path: "/spec/template/spec/containers/0/envFrom/-": missing value

I have seen some examples on GitHub where that syntax was used: https://github.com/search?l=YAML&p=1&q=%2Fspec%2Ftemplate%2Fspec%2Fcontainers%2F0%2FenvFrom%2F-&type=Code (you have to be logged in to see results) And I'm not sure this stopped work on specific Kustomize version(I'm using the newest version - 4.5.3) or it never worked

I have already written some Kustomize patches and syntax with /- to resources usually worked fine to resources that already exist on the manifest.
It's possible to inject that envFrom on different levels?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知所踪 2025-01-24 03:39:52

如果没有可重现的示例,很难诊断您的问题,但如果我从这个部署开始:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: example
  template:
    spec:
      containers:
        - name: example
          image: docker.io/alpine:latest
          envFrom:
            - configMapRef:
                name: example-config

并使用这个 kustomization.yaml,其中包含您的补丁,而无需
变化:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml

patches:
  - target:
      kind: Deployment
      name: example
    patch: |-
      - op: add
        path: "/spec/template/spec/containers/0/envFrom/-"
        value:
          configMapRef:
            name: backend-properties

然后一切似乎都有效,我得到了结果输出
kustomize 构建

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: example
  template:
    spec:
      containers:
      - envFrom:
        - configMapRef:
            name: example-config
        - configMapRef:
            name: backend-properties
        image: docker.io/alpine:latest
        name: example

It's hard to diagnose your problem without a reproducible example, but if I start with this Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: example
  template:
    spec:
      containers:
        - name: example
          image: docker.io/alpine:latest
          envFrom:
            - configMapRef:
                name: example-config

And use this kustomization.yaml, which includes your patch without
changes:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml

patches:
  - target:
      kind: Deployment
      name: example
    patch: |-
      - op: add
        path: "/spec/template/spec/containers/0/envFrom/-"
        value:
          configMapRef:
            name: backend-properties

Then everything seems to work and I get the resulting output from
kustomize build:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: example
  template:
    spec:
      containers:
      - envFrom:
        - configMapRef:
            name: example-config
        - configMapRef:
            name: backend-properties
        image: docker.io/alpine:latest
        name: example
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文