【问题标题】:How do I use a MutatingWebhook to inject a container into a Kubernetes pod on creation?如何在创建时使用 MutatingWebhook 将容器注入 Kubernetes pod?
【发布时间】:2020-02-20 05:11:57
【问题描述】:

使用 MutatingWebhook/sidecar 注入,我试图在创建容器时将容器添加到我的 Kubernetes 部署中。我知道我必须制作一个 JSON 补丁,但我不清楚添加容器所需的语法。 (我已成功为部署添加标签,FWIW。)

我知道 JSON 补丁对象看起来像这样,但我不清楚在“值”字段中放置什么。

}
  "op":    "add",
  "path":  "/spec/containers",
  "value": "containerName",
}

这是我收到的错误消息之一的示例,表明我的语法不正确。

Error creating: Internal error occurred: Internal error occurred: v1.Pod.Spec: v1.PodSpec.Containers: []v1.Container: readObjectStart: expect { or n, but found ", error found in #10 byte of ...|160000}},"{\"name\":|..., bigger context ...|OD","SETGID","SETUID"]},"runAsUser":1000160000}},"{\"name\":\"fluentd\"}"],"dnsPolicy":"ClusterFirst|...

【问题讨论】:

标签: json go kubernetes


【解决方案1】:

我会将此添加为评论,但我的代表太低了。

https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/

展示了使用 kubectl 修补部署的示例。可以在 MWH 中使用相同的 JSON 来修补部署。

Kubernetes 拥有广泛的 go 库,将所有 Kubernetes 对象封装为 go 结构。这是某人使用 MWH 注入容器的示例。

https://github.com/morvencao/kube-mutating-webhook-tutorial/blob/master/main.go#L26

https://github.com/morvencao/kube-mutating-webhook-tutorial/blob/master/deployment/configmap.yaml

https://github.com/morvencao/kube-mutating-webhook-tutorial/blob/master/webhook.go#L132

【讨论】:

    猜你喜欢
    • 2019-08-15
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多