如何用Shell/PowerShell脚本生成Azure Kubernetes ConfigMap YAML清单
需求:生成Kubernetes ConfigMap YAML的脚本实现
需求说明
需要创建可用于在Azure Kubernetes Service中生成ConfigMap的YAML文件,将3个XML文件导入指定的YAML模板,并编写Shell或PowerShell脚本自动完成生成流程,最终可通过kubectl apply -f "YAMLFILEPATH"命令创建ConfigMap。
YAML模板
apiVersion: v1 data: config1.xml: |- <Sheet> . . . </Sheet> config2.xml: |- <Sheet> . . . </Sheet> config3.xml: |- <Sheet> . . </Sheet> kind: ConfigMap metadata: name: {{CMPlaceholder}} namespace: {{NSPlaceholder}}
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> </Category> </Sheet>
脚本要求
参数支持
脚本需接收以下参数:
# Shell脚本调用示例 ./script.sh -YamlTemplate "PathOfYAMLFile" -config1.xml "PathOfConfig1.xml" -config2.xml "PathOfConfig2.xml" -config3.xml "PathOfConfig3.xml" -configMapName "group-product-spec" -namespace "organization" # PowerShell脚本调用示例 .\script.ps1 -YamlTemplate "PathOfYAMLFile" -config1.xml "PathOfConfig1.xml" -config2.xml "PathOfConfig2.xml" -config3.xml "PathOfConfig3.xml" -configMapName "group-product-spec" -namespace "organization"
输出要求
生成的YAML文件需与脚本同目录,文件名为${configMapName}.yaml(示例中为group-product-spec.yaml)。
Shell脚本实现(script.sh)
#!/bin/bash # 解析参数 while [[ $# -gt 0 ]]; do case "$1" in -YamlTemplate) YAML_TEMPLATE="$2" shift 2 ;; -config1.xml) CONFIG1_PATH="$2" shift 2 ;; -config2.xml) CONFIG2_PATH="$2" shift 2 ;; -config3.xml) CONFIG3_PATH="$2" shift 2 ;; -configMapName) CM_NAME="$2" shift 2 ;; -namespace) NS_NAME="$2" shift 2 ;; *) echo "未知参数: $1" exit 1 ;; esac done # 检查必要参数是否齐全 if [[ -z "$YAML_TEMPLATE" || -z "$CONFIG1_PATH" || -z "$CONFIG2_PATH" || -z "$CONFIG3_PATH" || -z "$CM_NAME" || -z "$NS_NAME" ]]; then echo "缺少必要参数,请检查输入" exit 1 fi # 读取XML内容并添加缩进(YAML中需保持4个空格缩进) read_xml_content() { local file_path="$1" # 每行前添加4个空格,同时保留原文件换行 sed 's/^/ /' "$file_path" } CONFIG1_CONTENT=$(read_xml_content "$CONFIG1_PATH") CONFIG2_CONTENT=$(read_xml_content "$CONFIG2_PATH") CONFIG3_CONTENT=$(read_xml_content "$CONFIG3_PATH") # 替换模板中的占位符和XML内容 sed -e "s/{{CMPlaceholder}}/$CM_NAME/g" \ -e "s/{{NSPlaceholder}}/$NS_NAME/g" \ -e "/config1.xml: |-/,/<\/Sheet>/c\ config1.xml: |-\n$CONFIG1_CONTENT" \ -e "/config2.xml: |-/,/<\/Sheet>/c\ config2.xml: |-\n$CONFIG2_CONTENT" \ -e "/config3.xml: |-/,/<\/Sheet>/c\ config3.xml: |-\n$CONFIG3_CONTENT" \ "$YAML_TEMPLATE" > "./${CM_NAME}.yaml" echo "YAML文件已生成: ./${CM_NAME}.yaml"
PowerShell脚本实现(script.ps1)
param( [Parameter(Mandatory=$true)] [string]$YamlTemplate, [Parameter(Mandatory=$true)] [string]$config1xml, [Parameter(Mandatory=$true)] [string]$config2xml, [Parameter(Mandatory=$true)] [string]$config3xml, [Parameter(Mandatory=$true)] [string]$configMapName, [Parameter(Mandatory=$true)] [string]$namespace ) # 读取XML内容并添加缩进 function Get-FormattedXmlContent { param([string]$FilePath) $content = Get-Content -Path $FilePath -Raw # 每行前添加4个空格 $content -replace "`n", "`n " | ForEach-Object { " $_" } } $config1Content = Get-FormattedXmlContent -FilePath $config1xml $config2Content = Get-FormattedXmlContent -FilePath $config2xml $config3Content = Get-FormattedXmlContent -FilePath $config3xml # 读取模板内容 $templateContent = Get-Content -Path $YamlTemplate -Raw # 替换占位符 $templateContent = $templateContent -replace '{{CMPlaceholder}}', $configMapName $templateContent = $templateContent -replace '{{NSPlaceholder}}', $namespace # 替换XML内容块 $templateContent = $templateContent -replace "(?s)config1.xml: |-`n.*?`n config2.xml", "config1.xml: |-`n$config1Content`n config2.xml" $templateContent = $templateContent -replace "(?s)config2.xml: |-`n.*?`n config3.xml", "config2.xml: |-`n$config2Content`n config3.xml" $templateContent = $templateContent -replace "(?s)config3.xml: |-`n.*?`nkind:", "config3.xml: |-`n$config3Content`nkind:" # 输出到文件 $outputPath = Join-Path -Path $PSScriptRoot -ChildPath "${configMapName}.yaml" $templateContent | Out-File -Path $outputPath -Encoding UTF8 Write-Host "YAML文件已生成: $outputPath"
内容的提问来源于stack exchange,提问作者New Progrommer
相关产品推荐
相关产品推荐

