Skip to content

Commit

Permalink
chore: quick notes on yq (in place) edit
Browse files Browse the repository at this point in the history
  • Loading branch information
edwintye committed Apr 14, 2024
1 parent 3f8e7ef commit dc3180c
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions content/posts/2024-04-14-quick-notes-on-yq-edit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: "Quick notes on yq edit"
date: 2024-04-14
tags:
- programming
- helm
---

One of the best thing about kubernetes, and especially gitops, is that we can re-use the same definitely
across many different environment and clusters via very simple yaml manipulation. Howver, it is also true
that editing yaml en-masse is not fun and automation is highly desirable. Most people will eventually turn
to `yq`, and at the same time be slightly stuck on the syntax. So I am just out here documenting a few
of the most used commands in my normal work.

To find all the clusters that is current set to image `1.0.0` for application `app`

```shell
yq 'select(.app.image.tag=="1.0.0")' | filename *.yaml
```

which tells us which file satisfies that condition. Sometime we may want to only find the image for applications
that have been enabled.

```shell
yq 'select(.app.enabled==true and .app.image.tag=="1.0.0")' | filename *.yaml
```

Then if we want to do an in-place update of all the images to a mor up to date version on the yaml file

```shell
yq -i 'with(.app; select(. | .enabled==true and .image.tag="1.0.0") | .image.tag="2.0.0")' *.yaml
```

But there are cases where the yaml have not been properly formatted before so the minor change turns out to be way
bigger than expected. In those cases we can generate the diff on those selected changes by first formatting the
yaml - wrapped in the following shell function `yq_edit` (which I am sure you would have encountered before
in some shape or form if you live on github)

```shell
function yq_edit() {
yq eval --exit-status '.' "$2" | tee out1.yaml
yq eval --exit-status "$1" "$2" | tee out.yaml
diff -u out1.yaml out2.yaml | tee out.patch
patch "$2" < out.patch
rm out1.yaml out2.yaml out.patch "$2.orig"
}
```

which allows us to perform updates easily via

```shell
yq_edit 'with(.app; select(.image.tag="1.0.0") | .image.tag="2.0.0")' *.yaml
```

0 comments on commit dc3180c

Please sign in to comment.