Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

387
Views
How to patch multi document yaml file on condition using yq?

Having YAML document something like:

---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-scraping
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: allow-webhooks

I am trying to get something like

---
apiVersion: **networking.k8s.io/v1beta1**
kind: NetworkPolicy
metadata:
  name: allow-scraping
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: allow-webhooks

So basically get document, if document has kind: NetworkPolicy then patch apiVersion: networking.k8s.io/v1beta1.

Ideally one liner, ideally with yq v4, but other solutions will be helpful too.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

With mikefarah/yq on versions beyond 4, you could do a select and update |= operation on the required document

yq e 'select(.kind == "NetworkPolicy").apiVersion |= "networking.k8s.io/v1beta1"' yaml

The above works fine on yq version 4.6.0. Use the -i flag to replace the file in-place.

over 4 years ago · Santiago Trujillo Report

0

Given that other solutions will be helpful - an alternative solution would be using kustomize:

  1. Create the kustomization.yaml file:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- network-policy.yaml
patches:
  - target:
      kind: NetworkPolicy
      group: networking.k8s.io
      version: v1
    patch: |
      - op: replace
        path: /apiVersion
        value: networking.k8s.io/v1beta1

  1. Run
kustomize build | kubectl apply -f -

or

kubectl apply -k .
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!