【发布时间】:2020-11-14 05:46:44
【问题描述】:
我正在尝试找到与 How to remove (delete) annotation in Kubernetes 等效的 API 来删除 Python 中服务类型的注释。从命令行执行此操作非常好。
最新的kubernetes-client/python 没有任何允许修补注释的 API。我总是可以删除并重新创建服务,但我想修补它。
如果有人愿意,这是一个简单的 MCVE。一个测试服务 YAML
apiVersion: v1
kind: Service
metadata:
name: my-service
annotations:
description: my test service
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
以及我正在使用的 Python 代码
from kubernetes import client, config
from kubernetes.client.rest import ApiException
config.load_kube_config()
coreV1 = client.CoreV1Api()
appsV1 = client.AppsV1Api()
try:
resp = coreV1.read_namespaced_service("my-service", "default")
del resp.metadata.annotations["description"]
patch = coreV1.patch_namespaced_service("my-service", "default", body=resp)
print(patch)
except ApiException as e:
print(str(e))
【问题讨论】:
-
我很遗憾 GitHub 太笨了,我无法链接到实际行,并且 kubernetes-client 似乎没有 GitHub Pages 或 readthedocs,但是搜索CoreV1Api.md 为
V1Service patch_namespaced_service,其中is how they patch in their example -
@mdaniel:我实际上尝试了patch_namespace_service,这似乎不适用于我删除注释的具体情况。更大的背景是我正在我的集群上运行一些集成测试。我通常做的是备份我的服务配置,做我需要的补丁(在这种情况下删除注释)并调用补丁 API。但是对于这个用例(删除注释)它不起作用
-
那么您需要在网站上包含您的代码和错误消息以解决编程问题
-
@mdaniel:我已经更新了一个 MCVE,你现在可以恢复你的关闭和否决投票
标签: kubernetes kubernetes-service kubernetes-python-client