Search code examples
bashpowershellkubernetesyamlazure-aks

How to create ConfigMap yaml manifest for Azure Kubernetes using shell or PowerShell script?


I have requirement to create yaml file which can be used to create configmap in kubernetes. I have 3 xml files which are need to be imported in template yaml file.

YAML Template:

apiVersion: v1
data:
  config1.xml: |-
    <Sheet>
    .
    .
    .
    </Sheet>
  config2.xml: |-
    <Sheet>
    .
    .
    .
    
    </Sheet>
  config3.xml: |-
    <Sheet>
    .
    .
    </Sheet>
kind: ConfigMap
metadata:
  name: {{CMPlaceholder}}
  namespace: {{NSPlaceholder}}

xml file:

<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>

Similary I have 2 more xml files. I need some script to prepare the final YAML so that I can use it to create configmap in azure kubernetes service using below command

kubectl apply -f "YAMLFILEPATH"

Expecting script to take below arguments.

.\script.sh or .\script.ps1 -YamlTemplate "PathOfYAMLFile" -config1.xml "PathOfConfig1.xml" -config2.xml "PathOfConfig2.xml" -config3.xml "PathOfConfig3.xml" -configMapName "group-product-spec" -namespace "organization" 

Output of the script should be yaml created in the same directory with the name group-product-spec.yaml


Solution

  • Suppose you have three xml files, and you need to create script which can be used to create configmap in kubernetes.

    I am showing via bash so let's say I create a create-configmap.sh

    #!/bin/bash
    
    # Function to display help message
    function display_help() {
        echo "Usage: $0 -YamlTemplate <PathOfYAMLFile> -config1 <PathOfConfig1.xml> -config2 <PathOfConfig2.xml> -config3 <PathOfConfig3.xml> -configMapName <group-product-spec> -namespace <organization>"
        exit 1
    }
    
    
    while [[ $# -gt 0 ]]; do
        case "$1" in
            -YamlTemplate)
                YamlTemplate="$2"
                shift 2
                ;;
            -config1)
                Config1="$2"
                shift 2
                ;;
            -config2)
                Config2="$2"
                shift 2
                ;;
            -config3)
                Config3="$2"
                shift 2
                ;;
            -configMapName)
                ConfigMapName="$2"
                shift 2
                ;;
            -namespace)
                Namespace="$2"
                shift 2
                ;;
            *)
                display_help
                ;;
        esac
    done
    
    # Check if all required arguments are provided
    if [ -z "$YamlTemplate" ] || [ -z "$Config1" ] || [ -z "$Config2" ] || [ -z "$Config3" ] || [ -z "$ConfigMapName" ] || [ -z "$Namespace" ]; then
        display_help
    fi
    
    # Read the content of XML files
    Config1Content=$(cat "$Config1")
    Config2Content=$(cat "$Config2")
    Config3Content=$(cat "$Config3")
    
    # Create the final YAML file by replacing placeholders
    OutputYaml="${ConfigMapName}.yaml"
    cat <<EOF > "$OutputYaml"
    apiVersion: v1
    data:
      config1.xml: |-
    $(echo "$Config1Content" | sed 's/^/    /')
      config2.xml: |-
    $(echo "$Config2Content" | sed 's/^/    /')
      config3.xml: |-
    $(echo "$Config3Content" | sed 's/^/    /')
    kind: ConfigMap
    metadata:
      name: $ConfigMapName
      namespace: $Namespace
    EOF
    
    echo "ConfigMap YAML file created at $OutputYaml"
    

    Make the script executable chmod +x create-configmap.sh

    Run the script

    ./create-configmap.sh -YamlTemplate "template.yaml" -config1 "config1.xml" -config2 "config2.xml" -config3 "config3.xml" -configMapName "group-product-spec" -namespace "<whatever_is_your_namespace>"
    

    and finally check using kubectl get configmap group-product-spec

    enter image description here

    and if you describe it, you can see your xml loaded properly

    kubectl describe configmap group-product-spec -n default
    

    enter image description here