yq CLI Sheet: YAML Parsing, Querying

๐Ÿ’ก yq CLI Sheet: YAML Parsing, Querying โ€” Executive Summary

Technical architectural analysis, practical configuration manifests, and deployment instructions for yq CLI Sheet: YAML Parsing, Querying.

Installation
Debian
yaml
apt install golang-go
# yq4 ์ •์ƒ์„ค์น˜
go install github.com/mikefarah/yq/v4@latest
export PATH="$PATH:~/go/bin"
  • snapd
bash
# snap๋ฒ„๊ทธ์žˆ์œผ๋‹ˆ ์“ฐ์ง€๋งˆ์…ˆ
  snap install yq
  • apt
yaml
# ์ด์ƒํ•œ๋ฒ„์ „ 0.0.0 ๋‹ค์šด๋กœ๋“œ๋จ ์“ฐ์ง€๋งˆ์…ˆ
  sudo add-apt-repository ppa:rmescandon/yq
  sudo apt update
  sudo apt install yq -y
run
bash
#json to Yaml
$stdout | yq -P

#yaml to JSON
$stdout | yq -P -pyaml -ojson

#helm values.yaml to props
cat values.yaml | yq -o props
flags
bash
-p, --input-format string           [auto|a|yaml|y|json|j|props|p|csv|c|tsv|t|xml|x|base64|uri|toml|lua|l] parse format for input. (default "auto")
  -o, --output-format string          [auto|a|yaml|y|json|j|props|p|csv|c|tsv|t|xml|x|base64|uri|toml|shell|s|lua|l] output format type. (default "auto")
File write
bash
# ์ˆ˜์ •๊ฒฐ๊ณผ๋ฅผ ํ‘œ์ค€์ถœ๋ ฅ
yq [opt] $file

# ๊ธฐ์กด ํŒŒ์ผ์„ rewrite
yq -i [opt] $file
Overwrite in array
bash
yq -i '.mounts = [
	{location: "/etc/docker/certs.d/mydomain", writable: false}
]' $file

yq -i "$path = $array_items" $file
Merge items into existing array
bash
yq -i '.mounts += [
	{location: "/etc/docker/certs.d/mydomain", writable: false}
]' $file

yq -i "$path += $array_items" $file
Apply yq standard indentation
bash
myfile=template.yaml
cat $myfile | yq > /tmp/$myfile && cat /tmp/$myfile > $myfile
Change kube-manifest
bash
tag=v3.0.10.1.33.54
currentApp=Enterprise Cloud-app
project=stg-portal
harborURL=harbor.Enterprise Cloud.hanacloudia.com
templateName=stg-cmp-Enterprise Cloud-app
# Enterprise Cloud-app
yq eval -i "(select(.kind == \"Deployment\" and .metadata.name == \"admin-$currentApp\") | .spec.template.spec.containers[0].image) = \"${harborURL}/${project}/admin-${currentApp}:${tag}\"" ${templateName}-admin/template.yaml
yq eval -i "(select(.kind == \"Deployment\" and .metadata.name == \"user-$currentApp\") | .spec.template.spec.containers[0].image) = \"${harborURL}/${project}/user-${currentApp}:${tag}\"" ${templateName}-user/template.yaml

tag=v3.0.10.1.33
currentApp=Enterprise Cloud-api
project=stg-portal
harborURL=harbor.Enterprise Cloud.hanacloudia.com
templateName=stg-cmp-common-si
# SI
yq eval -i "(select(.kind == \"Deployment\" and .metadata.name == \"$currentApp\") | .spec.template.spec.containers[0].image) = \"${harborURL}/${project}/${currentApp}:${tag}\"" ${templateName}/template.yaml

tag=v3.0.5.1.25
currentApp=openstack-api
project=stg-portal
harborURL=harbor.Enterprise Cloud.hanacloudia.com
templateName=stg-cmp-common-solution
# Solution
yq eval -i "(select(.kind == \"Deployment\" and .metadata.name == \"$currentApp\") | .spec.template.spec.containers[0].image) = \"${harborURL}/${project}/${currentApp}:${tag}\"" ${templateName}/template.yaml

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *