0

我有一个 bash 脚本作为变量值的 kubernetes yaml 文件。

我想将 yaml 转换为 Hocon / Typsafe .conf 格式,这样我就可以注入变量并在它们运行时转换为 yaml。

如果可能,我如何将此值转换为 Hocon?

init.sh: |-
  #!/bin/bash
  set -e
  set -x

  [ -d /var/lib/zookeeper/data ] || mkdir /var/lib/zookeeper/data
  [ -z "$ID_OFFSET" ] && ID_OFFSET=1
  export ZOOKEEPER_SERVER_ID=$((${HOSTNAME##*-} + $ID_OFFSET))
  echo "${ZOOKEEPER_SERVER_ID:-1}" | tee /var/lib/zookeeper/data/myid
  cp -Lur /etc/kafka-configmap/* /etc/kafka/
  [ ! -z "$PZOO_REPLICAS" ] && [ ! -z "$ZOO_REPLICAS" ] && {
  sed -i "s/^server\\./#server./" /etc/kafka/zookeeper.properties
  for N in $(seq $PZOO_REPLICAS); do echo "server.$N=pzoo-$(( $N - 1 )).pzoo:2888:3888:participant" >> /etc/kafka/zookeeper.properties; done
  for N in $(seq $ZOO_REPLICAS); do echo "server.$(( $PZOO_REPLICAS + $N ))=zoo-$(( $N - 1 )).zoo:2888:3888:participant" >> /etc/kafka/zookeeper.properties; done
  }
  sed -i "s/server\.$ZOOKEEPER_SERVER_ID\=[a-z0-9.-]*/server.$ZOOKEEPER_SERVER_ID=0.0.0.0/" /etc/kafka/zookeeper.properties
4

1 回答 1

0

有一种非人类可读的方式将 \n 替换为新行:

    init.sh: "#!/bin/bash\nset -e\nset -x\ncp /etc/kafka-configmap/log4j.properties /etc/kafka/\n\nKAFKA_BROKER_ID=${HOSTNAME##*-}\nSEDS=(\"s/#init#broker.id=#init#/broker.id=$KAFKA_BROKER_ID/\")\nLABELS=\"kafka-broker-id=$KAFKA_BROKER_ID\"\nANNOTATIONS=\"\"\n\nhash kubectl 2>/dev/null || {\n  SEDS+=(\"s/#init#broker.rack=#init#/#init#broker.rack=# kubectl not found in path/\")\n} && {\n  ZONE=$(kubectl get node \"$NODE_NAME\" -o=go-template='{{index .metadata.labels \"failure-domain.beta.kubernetes.io/zone\"}}')\n  if [ \"x$ZONE\" == \"x<no value>\" ]; then\n    SEDS+=(\"s/#init#broker.rack=#init#/#init#broker.rack=# zone label not found for node $NODE_NAME/\")\n  else\n    SEDS+=(\"s/#init#broker.rack=#init#/broker.rack=$ZONE/\")\n    LABELS=\"$LABELS kafka-broker-rack=$ZONE\"\n  fi\n\n  OUTSIDE_HOST=$(kubectl get node \"$NODE_NAME\" -o jsonpath='{.status.addresses[?(@.type==\"InternalIP\")].address}')\n  OUTSIDE_PORT=$((32400 + ${KAFKA_BROKER_ID}))\n  SEDS+=(\"s|#init#advertised.listeners=PLAINTEXT://#init#|advertised.listeners=PLAINTEXT://:9092,OUTSIDE://${OUTSIDE_HOST}:${OUTSIDE_PORT}|\")\n  ANNOTATIONS=\"$ANNOTATIONS kafka-listener-outside-host=$OUTSIDE_HOST kafka-listener-outside-port=$OUTSIDE_PORT\"\n\n  if [ ! -z \"$LABELS\" ]; then\n    kubectl -n $POD_NAMESPACE label pod $POD_NAME $LABELS || echo \"Failed to label $POD_NAMESPACE.$POD_NAME - RBAC issue?\"\n  fi\n  if [ ! -z \"$ANNOTATIONS\" ]; then\n    kubectl -n $POD_NAMESPACE annotate pod $POD_NAME $ANNOTATIONS || echo \"Failed to annotate $POD_NAMESPACE.$POD_NAME - RBAC issue?\"\n  fi\n}\nprintf '%s\\n' \"${SEDS[@]}\" | sed -f - /etc/kafka-configmap/server.properties > /etc/kafka/server.properties.tmp\n[ $? -eq 0 ] && mv /etc/kafka/server.properties.tmp /etc/kafka/server.properties",

我宁愿一个人类可读的解决方案

于 2019-12-16T07:21:59.143 回答