mic*_*has 1 patch go webhooks kubernetes
我想编写一个变异的webhook,将默认的入口类添加到所有未显式提供的入口对象。
根据示例,我发现我需要为webhook返回提供正确的json补丁。
我首先使用kubectl尝试了补丁:
$ kubectl patch ingress mying --type='json' -p='[{"op": "add", "path": "/metadata/annotations/key", "value":"value"}]'
The "" is invalid
Run Code Online (Sandbox Code Playgroud)
当尚无注释元素时,这似乎不起作用。
$ kubectl patch ingress mying --type='json' -p='[{"op": "add", "path": "/metadata/annotations", "value":{"key":"value"}}]'
ingress.extensions/kafka-monitoring-topics-ui patched
Run Code Online (Sandbox Code Playgroud)
创建完整的注释元素可以很好地工作,但是就我而言,我需要一个kubernetes.io/ingress.class包含斜杠的键。
kubectl patch ingress mying --type='json' -p='[{"op": "add", "path": "/metadata/annotations", "value":{"kubernetes.io/ingress.class":"value"}}]'
ingress.extensions/kafka-monitoring-topics-ui patched
Run Code Online (Sandbox Code Playgroud)
创建注释对象时,此方法工作正常。但是,如果已经存在一些注释,而我只是想添加一个注释,则似乎无法添加。
简单地使用[{"op": "add", "path": "/metadata/annotations", "value":{"kubernetes.io/ingress.class":"value"}}]会删除所有现有的注释,而类似的'[{"op": "add", "path": "/metadata/annotations/kubernetes.io/ingress.class", "value": "value"}]操作由于包含斜杠而无法正常工作。
长话短说:使用适当的补丁程序简单地添加入口类的正确方法是什么?
PS:是的,我知道kubectl annotate,但是不幸的是,这对我的网络挂钩没有帮助。
Efr*_*tan 20
对我来说更容易的是annotate而不是patch:
kubectl annotate ingress mying kubernetes.io/ingress.class=value
Run Code Online (Sandbox Code Playgroud)
--dry-run -o yaml如果您想在应用更改之前对其进行测试,请添加标志。
小智 5
更换正斜杠(/中)kubernetes.io/ingress.class与~1。
您的命令应如下所示:
$ kubectl patch ingress mying --type='json' -p='[{"op": "add", "path": "/metadata/annotations/kubernetes.io~1ingress.class", "value":"nginx"}]'
Run Code Online (Sandbox Code Playgroud)
参考:RFC 6901 https://tools.ietf.org/html/rfc6901#section-3
仅使用:
[{"op": "add", "path": "/metadata/annotations/kubernetes.io~1ingress.class", "value":"nginx"}]
Run Code Online (Sandbox Code Playgroud)
当创建的 pod 没有注释时不起作用,例如来自kubectl run --generator=run-pod/v1 --attach test-deploy --image=busybox ls
要检测到这一点,您需要对带有指针注释的元数据进行单独的解析步骤,以便可以将其检测为 nil,或者在注释为空时始终发送空注释:
type Metadata struct {
Annotations *map[string]string
}
type ObjectWithMeta struct {
Metadata Metadata
}
Run Code Online (Sandbox Code Playgroud)
然后发送:
[
{"op":"add","path":"/metadata/annotations","value":{}},
{"op":"add","path":"/metadata/annotations/foo","value": "bar"}
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
552 次 |
| 最近记录: |