2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kustomize build の出力をフィルタするワンライナー

Last updated at Posted at 2019-11-11

kustomzize buildの出力は巨大なmultiple documentなYAMLになりがちで、
毎回 less で表示するのはつらい。

そこでmultiple documents形式のYAMLを
kindmetadata.nameでフィルタするワンライナーを書いてみました。
jqとyqを使っています。
なんで両方使うのかというと、yqがselectに対応していないっぽいからです。
ref: https://github.com/mikefarah/yq/issues/173

$ kustomize build . | yq r - -d "*" -j | jq '.[] | select(.kind | match("service";"i")) | select(.metadata.name=="prometheus")' | jq . -s | yq r -
- apiVersion: v1
  kind: ServiceAccount
  metadata:
    name: prometheus
- apiVersion: v1
  kind: Service
  metadata:
    name: prometheus
  spec:
    ports:
    - port: 9090
      targetPort: 9090
    selector:
      app.kubernetes.io/name: prometheus
    type: ClusterIP

流石に長いのでシェルスクリプトの関数にしてみました。

function kfilter() {
    cat - | yq r - -d "*" -j | \
        jq ".[] | select(.kind | match(\"$1\";\"i\")) | select(.metadata.name==\"$2\")" \
        | jq . -s | yq r -
}

おわり。

$ kustomize build . | kfilter service alertmanager
- apiVersion: v1
  kind: Service
  metadata:
    name: alertmanager
  spec:
    ports:
    - port: 9093
      targetPort: 9093
    selector:
      app.kubernetes.io/name: alertmanager
    type: ClusterIP

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?