我们需要使用 kubectl cli 在 azure kubernetes 中修补下面的 yaml
ConfigMap YAML:
kind: ConfigMap
apiVersion: v1
metadata:
name: group-product-spec
namespace: organization
uid: 60694109-****-****-****-*****
resourceVersion: '*******'
creationTimestamp: '2024-02-14T10:35:56Z'
managedFields:
- manager: kubectl-client-side-apply
operation: Update
apiVersion: v1
time: '2024-06-26T12:41:54Z'
fieldsType: FieldsV1
fieldsV1:
f:data:
.: {}
f:config1.xml: {}
f:config2.xml: {}
f:config3.xml: {}
f:metadata:
f:annotations:
.: {}
f:kubectl.kubernetes.io/last-applied-configuration: {}
data:
config1.xml: >-
<Sheet>
<Category name="Category1">
<Item name="item1">Cat1Item1.value</Item>
<Item name="item2">Cat1Item2.value</Item>
</Category>
<Category name="Category2">
<Item name="item1">Cat2Item1.value</Item>
<Item name="item2">Cat2Item2.value</Item>
</Category>
<Category name="Category3">
<Item name="item1">Cat3Item1.value</Item>
<Item name="item2">Cat3Item2.value</Item>
</Category>
<Category name="Groups">
<Item name="Group1" commarea="Y">G1.Value</Item>
<Item name="Group2" commarea="Y">G2.Value</Item>
<Item name="Group3" commarea="Y">G3.Value</Item>
<Group1 name="G1.Value">
<spec name="ISOS">
<specdetails name="name1">spvalue1</specdetails>
<specdetails name="name2">spvalue2</specdetails>
<specdetails name="name3">spvalue3</specdetails>
<specdetails name="name4">spvalue4</specdetails>
</spec>
</Group1>
.
.<Group2></Group2>
.
.<Group3></Group3>
.
<Category name="FinalProducts">
<Item name="Product1">Product1FinalValue</Item>
<Item name="Product2">Product2FinalValue</Item>
<Item name="Product3">Product3FinalValue</Item>
<Item name="Product4">Product4FinalValue</Item>
</Category>
</Sheet>
config2.xml: >-
<Sheet>
<Category name="Category1">
<Item name="item1">Cat1Item1.value</Item>
<Item name="item2">Cat1Item2.value</Item>
</Category>
<Category name="Category2">
<Item name="item1">Cat2Item1.value</Item>
<Item name="item2">Cat2Item2.value</Item>
</Category>
<Category name="Category3">
<Item name="item1">Cat3Item1.value</Item>
<Item name="item2">Cat3Item2.value</Item>
</Category>
<Category name="Groups">
<Item name="Group1" commarea="Y">G1.Value</Item>
<Item name="Group2" commarea="Y">G2.Value</Item>
<Item name="Group3" commarea="Y">G3.Value</Item>
<Group1 name="G1.Value">
<spec name="ISOS">
<specdetails name="name1">spvalue1</specdetails>
<specdetails name="name2">spvalue2</specdetails>
<specdetails name="name3">spvalue3</specdetails>
<specdetails name="name4">spvalue4</specdetails>
</spec>
</Group1>
.
.<Group2></Group2>
.
.<Group3></Group3>
.
<Category name="FinalProducts">
<Item name="Product1">Product1FinalValue</Item>
<Item name="Product2">Product2FinalValue</Item>
<Item name="Product3">Product3FinalValue</Item>
<Item name="Product4">Product4FinalValue</Item>
</Category>
</Sheet>
在此 YAML 中格式化 XML,以获得更好的外观
<Sheet>
<Category name="Category1">
<Item name="item1">Cat1Item1.value</Item>
<Item name="item2">Cat1Item2.value</Item>
</Category>
<Category name="Category2">
<Item name="item1">Cat2Item1.value</Item>
<Item name="item2">Cat2Item2.value</Item>
</Category>
<Category name="Category3">
<Item name="item1">Cat3Item1.value</Item>
<Item name="item2">Cat3Item2.value</Item>
</Category>
<Category name="Groups">
<Item name="Group1" commarea="Y">G1.Value</Item>
<Item name="Group2" commarea="Y">G2.Value</Item>
<Item name="Group3" commarea="Y">G3.Value</Item>
<Group1 name="G1.Value">
<spec name="ISOS">
<specdetails name="name1">spvalue1</specdetails>
<specdetails name="name2">spvalue2</specdetails>
<specdetails name="name3">spvalue3</specdetails>
<specdetails name="name4">spvalue4</specdetails>
</spec>
</Group1>
.
.
<Group2 name="G2.Value"></Group2>
.
.
<Group3 name="G3.Value"></Group3>
.
<Category name="FinalProducts">
<Item name="Product1">Product1FinalValue</Item>
<Item name="Product2">Product2FinalValue</Item>
<Item name="Product3">Product3FinalValue</Item>
<Item name="Product4">Product4FinalValue</Item>
</Category>
</Sheet>
我需要更新此 ConfigMap 中的一个节点项 例如:
将下面的 X 路径中的值从“Product3FinalValue”更新为“不可用”
数据: 配置1.xml: Sheet1 > 类别名称=“FinalProducts”> 项目名称=“Product3”> Product3FinalValue
此 ConfigMap 在 AKS 中的 organization 命名空间下保存为 group-product-spec
使用的命令:
kubectl get cm **group-product-spec** -n **organization** -o yaml
kubectl patch cm
命令,但不知道如何使用它。
kubectl
|Kubernetes 的角度来看,
data
包含
key
:
value
对的映射。在这种情况下,键是文件名,值是文件的内容。您知道
value
内容是 XML,但对于该工具来说,它只是字符串数据。您的
ConfigMap
使用YAML块标量(
>-
)来表示内容,我不确定如何使用JSON和
kubectl patch
来强制执行它提供的编码(见下文)。1.
kubectl edit
可以手动进行这些更改:
kubectl edit config/${NAME} \
--namespace=${NAMESPACE}
2. kubectl patch
kubectl patch
来
PATCH
ConfigMap
文件内容,但重申一下 - 它会丢失 转义字符(例如换行符
\n
):
NAME="..." # ConfigMap
NAMESPACE="..."
FILE="config1.xml"
# Export the XML from the ConfigMap
kubectl get configmap/${NAME} \
--namespace=${NAMESPACE} \
--output=jsonpath="{.data.config1\.xml}" \
> ${PWD}/${FILE}
# Edit it
VALUE="$(cat ${PWD}/${FILE})"
PATCH="[
{
'op': 'replace',
'path': '/data/${FILE}',
'value': '${VALUE}'
}
]"
kubectl patch configmap/${NAME} \
--namespace=${NAMESPACE} \
--type=json \
--patch="${PATCH}"
3. kubectl create
kubect create configmap ${NAME} --namespace=${NAMESPACE} --from-file=${PWD}/${FILE}
。我有兴趣看看其他人的建议。