使用 MutatingWebhook/sidecar 注入(inject),我尝试在创建 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|...
最佳答案
我会将此添加为评论,但我的代表太低。
https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/
显示使用 kubectl 修补部署的示例。可以在 MWH 中使用相同的 JSON 来修补部署。
Kubernetes 拥有广泛的 go 库,将所有 Kubernetes 对象封装为 go 结构。这是一个使用 MWH 注入(inject)容器的示例。
https://github.com/morvencao/kube-mutating-webhook-tutorial/blob/master/main.go#L26 <- 他们加载将从文件(由配置映射填充)修补的 YAML
https://github.com/morvencao/kube-mutating-webhook-tutorial/blob/master/deployment/configmap.yaml <- 他们正在注入(inject)的 YAML 内容(由配置映射填充)
https://github.com/morvencao/kube-mutating-webhook-tutorial/blob/master/webhook.go#L132 <- 他们循环部署中的容器并添加容器(通过 JSON 补丁)
关于json - 如何使用 MutatingWebhook 在创建时将容器注入(inject)到 Kubernetes Pod 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58531590/