我正在使用 Azure 应用程序配置提供程序,并希望向使用该提供程序创建的配置映射添加注释。有办法做到这一点吗?
提供程序本身不支持将注释直接添加到由 Azure 应用程序配置提供程序创建的 ConfigMap。相反,您可以通过创建一个辅助进程来实现此目的,该进程在 Azure 应用程序配置提供程序创建或更新 ConfigMap 后更新它。
确保已设置 Azure 应用程序配置存储并填充所需的配置。使用 Azure 门户、Azure CLI 或适合您偏好的其他方法。
确保您的 Kubernetes 集群可以访问 Azure 应用程序配置。通常,这涉及设置托管身份并确保将其分配给您的 Kubernetes 服务。以下是通过 Azure CLI 执行此操作的方法:
az identity create --name <identity-name> --resource-group <resource-group-name>
az aks pod-identity add --identity-resource-id <identity-resource-id> --cluster-name <cluster-name> --resource-group <resource-group-name>
使用 Azure 应用程序配置存储的连接字符串设置 Kubernetes 密钥:
kubectl create secret generic appconfig-secret --from-literal=connection-string='Endpoint=https://<your-appconfig-endpoint>.azconfig.io;Id=<id>;Secret=<secret>' --namespace default
由于 Azure 应用程序配置提供程序不支持通过其同步过程添加注释,因此部署自定义 Kubernetes 作业或控制器:
from kubernetes import client, config, watch
# Configure access to the Kubernetes cluster
config.load_kube_config()
v1 = client.CoreV1Api()
w = watch.Watch()
# Watch for changes to ConfigMaps in a specified namespace
for event in w.stream(v1.list_namespaced_config_map, namespace='default', field_selector='metadata.name=<configmap-name>'):
config_map = event['object']
if 'annotations' not in config_map.metadata:
config_map.metadata.annotations = {}
config_map.metadata.annotations.update({'example-annotation': 'added-value'})
v1.patch_namespaced_config_map(name=config_map.metadata.name, namespace='default', body=config_map)
应用它
kubectl apply -f custom-controller.yaml
验证注释
kubectl describe configmap <configmap-name>