create a kubernetes CRD

install package  

pip3 install jinja2
pip3 install yq

Function for Jinja2 Render  

just paste the follow code to your terminal

jinji2_render(){
yf=$1
[[ "$yf" != ./* && "$yf" != /* ]] && yf="./$yf"
tmplp=${yf%/*}
tmpl=${yf##*/}
content=${2:-{\}}
cat <<EOF | python3
from jinja2 import Environment, FileSystemLoader

content=$content

file_loader = FileSystemLoader("$tmplp")
env = Environment(loader=file_loader)

template = env.get_template("$tmpl")
output=template.render(content)

print(output)
EOF
}

An Example for CRD

cat <<EOF | kubectl apply -f -
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: gnbmcses.pwek.smart.edge.org
spec:
  group: pwek.smart.edge.org
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        # openAPIV3Schema is the schema for validating custom objects.
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                ul:              # Indicates the maximum UL rank value, 2
                  type: integer
                  minimum: 0
                  maximum: 31
                  default: 9
                dl:              # Number of dlRank, 4
                  type: integer
                  minimum: 0
                  maximum: 31
                  default: 9
                rar:    # Indicates the number of uplink antenna ports, 2
                  type: integer
                  minimum: 0
                  maximum: 31
                  default: 6
                bcch:    # Indicates the number of downlink antenna ports, 4
                  type: integer
                  minimum: 0
                  maximum: 31
                  default: 9
                pcch:    # Indicates the number of downlink antenna ports, 4
                  type: integer
                  minimum: 0
                  maximum: 31
                  default: 9
  scope: Namespaced
  names:
    plural: gnbmcses
    singular: gnbmcs
    kind: GnbMCS
    shortNames:
    - mcs
EOF

An Example Jinja2 template  for CR

cat << EOF > gnb_mcs.yaml.j2
---
apiVersion: "pwek.smart.edge.org/v1"
kind: GnbMCS
metadata:
  namespace: pwek-rdc
  name: common
spec:
  ul: {{ ulMcs }}
  dl: {{ dlMcs }}
  rar: {{ rarMcs }}
  bcch: {{ bcchMcs }}
  pcch: {{ pcchMcs }}
EOF

Create an CR

prepare the data

dlMcs=9
ulMcs=10
rarMcs=11
bcchMcs=12
pcchMcs=13

Then

jinji2_render gnb_mcs.yaml.j2 "$(jq <<EOF
{
  "dlMcs": "$dlMcs",
  "ulMcs": "$ulMcs",
  "rarMcs": "$rarMcs",
  "bcchMcs": "$bcchMcs",
  "pcchMcs": "$pcchMcs"
}
EOF
)"| kubectl apply -f -

Check the CRD

kubectl api-resources --api-group=pwek.smart.edge.org

Check the CR

NS=pwek-rdc

kubectl get -n ${NS:-default} mcs

kubectl get -n ${NS:-default} mcs common -o=jsonpath="{.spec}" | jq

Update the CR

NS=pwek-rdc

value=$(jq <<EOF
{
  "spec": {
    "dl": 10
  }
}
EOF
)

kubectl patch -n ${NS:-default} mcs common --type='merge' -p "$value"

kubectl get -n ${NS:-default} mcs common -o=jsonpath="{.spec}" | jq

multi xml in one file exmaple

create a xml

cat << EOF > text.xml 
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
   <capabilities>
      <capability>urn:ietf:params:netconf:base:1.0</capability>
   </capabilities>
</hello>
]]>]]>
<?xml version="1.0" encoding="UTF-8"?>
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
      <vsData><![CDATA[
              <gnbvs xmlns="urn:rdns:com:radisys:nr:gnb">
               <gnbDuCfg>
            <id>0</id>
            <numOutboundStreams>1</numOutboundStreams>
            <maxInboundStreams>1</maxInboundStreams>
            <maxInitAttempts>5</maxInitAttempts>
            <heartBeatIntervalInMs>5000</heartBeatIntervalInMs>
            <maxPathRetx>1</maxPathRetx>
               </gnbDuCfg>
              </gnbvs>
         ]]></vsData>
</rpc>
]]>]]>
<?xml version="1.0" encoding="UTF-8"?>
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2">
   <close-session/>
</rpc>
]]>]]>
EOF

querry the xml

sed -e '1,/]]>]]>/d' -e '1,/]]>]]>/!d' -e "/^]]>]]>$/d" text.xml | \
  xq -j .rpc.vsData | \
  xq -j .gnbvs.gnbDuCfg.heartBeatIntervalInMs

 

posted @ 2022-10-12 23:32  lvmxh  阅读(41)  评论(0编辑  收藏  举报