Search code examples
yamlyq

Update Configmap with a given xml File


I have a configmap which looks something like this -

apiVersion: v1
kind: ConfigMap
metadata:
  name: master-configmap
  labels:
data:
  broker-address: |
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
      <core xmlns="urn:activemq:core" xsi:schemaLocation="urn:activemq:core ">
        <addresses>
          <address name="tenant1.app.Q1">
            <anycast>
               <queue name="tenant1.app.Q1"/>
            </anycast>
         </address>
         <address name="tenant2.app.Q2">
            <anycast>
               <queue name="tenant2.app.Q2"/>
            </anycast>
         </address>
         <address name="tenant.global.Q">
            <anycast>
               <queue name="tenant.global.Q"/>
            </anycast>
         </address>
         <address name="tenant.global.Q1">
            <anycast>
               <queue name="tenant.global.Q1"/>
            </anycast>
         </address>
         <address name="tenant.global.Q2">
            <anycast>
               <queue name="tenant.global.Q2"/>
            </anycast>
         </address>
         <address name="tenant.global.Q3">
            <anycast>
               <queue name="tenant.global.Q3"/>
            </anycast>
         </address>
         <address name="tenant.global.Q4">
            <anycast>
               <queue name="tenant.global.Q4"/>
            </anycast>
         </address>
         
        </addresses>
      </core>
    </configuration>

  broker-security: |
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
        <core xmlns="urn:activemq:core" xsi:schemaLocation="urn:activemq:core ">
        <security-settings>
            <!-- only amq role can consume, guest role can send  -->

            <security-setting match="#">
                <permission type="createNonDurableQueue" roles="amq"/>
                <permission type="deleteNonDurableQueue" roles="amq"/>
                <permission type="createDurableQueue" roles="amq"/>
                <permission type="deleteDurableQueue" roles="amq"/>
                <permission type="createAddress" roles="amq"/>
                <permission type="deleteAddress" roles="amq"/>
                <permission type="consume" roles="amq"/>
                <permission type="browse" roles="amq"/>
                <permission type="send" roles="amq"/>
                <!-- we need this otherwise ./artemis data imp wouldn't work -->
                <permission type="manage" roles="amq"/>
            </security-setting>

            <security-setting match="Info">
                <permission type="send" roles="guest" />
                <permission type="consume" roles="guest"/>
            </security-setting>

            <security-setting match="tenant1.app.Q1">
                <permission type="send" roles="tenant1" />
                <permission type="consume" roles="tenant1"/>
            </security-setting>

            <security-setting match="tenant2.app.Q2">
                <permission type="send" roles="tenant2" />
                <permission type="consume" roles="tenant2"/>
            </security-setting>
          </security-settings> 
        </core>
      </configuration> 

  broker-connector: |
     <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
        <core xmlns="urn:activemq:core" xsi:schemaLocation="urn:activemq:core ">
            <connectors>
              <connector name="netty-connector">tcp://localhost:61616</connector>
            </connectors>
        </core>
      </configuration>   

  broker-ha-live: |
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
      <core xmlns="urn:activemq:core" xsi:schemaLocation="urn:activemq:core ">
          <ha-policy xmlns="urn:activemq:core">
            <replication>
              <master>
                 <check-for-live-server>true</check-for-live-server>
              </master>
            </replication>
          </ha-policy>
      </core>
    </configuration>   

  broker-ha-backup: |
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
      <core xmlns="urn:activemq:core" xsi:schemaLocation="urn:activemq:core ">
          <ha-policy xmlns="urn:activemq:core">
            <replication>
              <slave>
                 <allow-failback>true</allow-failback>
              </slave>
            </replication>
          </ha-policy>
      </core>
    </configuration>    


  broker-cluster: |
      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
        <configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
          <core xmlns="urn:activemq:core" xsi:schemaLocation="urn:activemq:core ">
            <broadcast-groups>
              <broadcast-group name="my-broadcast-group">
                <group-address>231.7.7.7</group-address>
                <group-port>9876</group-port>
                <broadcast-period>1000</broadcast-period>
                <connector-ref>netty-connector</connector-ref>
              </broadcast-group>
            </broadcast-groups>

            <discovery-groups>
              <discovery-group name="my-discovery-group">
                <group-address>${udp-address:231.7.7.7}</group-address>
                <group-port>9876</group-port>
                <refresh-timeout>5000</refresh-timeout>
              </discovery-group>
            </discovery-groups>

            <cluster-connections>
              <cluster-connection name="my-cluster">
                <connector-ref>netty-connector</connector-ref>
                <retry-interval>500</retry-interval>
                <discovery-group-ref discovery-group-name="my-discovery-group"/>
              </cluster-connection>
            </cluster-connections>
          </core>
        </configuration>

For each of the keys in the above file, I have a separate XML file. Now, I want to change the XML in config map from the XML file in bash.

broker-address.xml -

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
      <core xmlns="urn:activemq:core" xsi:schemaLocation="urn:activemq:core ">
        <addresses>
          <address name="tenant1.app.Q1">
            <anycast>
               <queue name="tenant1.app.Q1"/>
            </anycast>
         </address>
         <address name="tenant2.app.Q2">
            <anycast>
               <queue name="tenant2.app.Q2"/>
            </anycast>
         </address>
         <address name="tenant.global.Q">
            <anycast>
               <queue name="tenant.global.Q"/>
            </anycast>
         </address>
         <address name="tenant.global.Q1">
            <anycast>
               <queue name="tenant.global.Q1"/>
            </anycast>
         </address>
         <address name="tenant.global.Q2">
            <anycast>
               <queue name="tenant.global.Q2"/>
            </anycast>
         </address>
         <address name="tenant.global.Q3">
            <anycast>
               <queue name="tenant.global.Q3"/>
            </anycast>
         </address>
         <address name="tenant.global.Q4">
            <anycast>
               <queue name="tenant.global.Q4"/>
            </anycast>
         </address>
         
        </addresses>
      </core>
    </configuration>

I tried to do it with yq command, but don't able to do it. Or are there any more efficient methods?


Solution

  • I want to change the XML in config map from the XML file in bash.

    I cannot spot any "changes" between the .data.broker-address item and the content of the broker-address.xml file, but if all you want is to load each file's contents into the yaml file (overwriting the previous values), consider the load function provided by mikefarah/yq using the keys with added suffix ".xml" as file names:

    # add the -i flag to modify the file
    yq '.data[] |= load(key + ".xml")' configmap.yaml