SS-eth0
SS-eth0

Reputation: 45

How to read & modify Kube Manifest values with yq?

I have a Kube manifest that need be applied to a couple of kubernetes clusters with different resource settings. For that I need to change resource section of this file on the fly. Here's its contents:

apiVersion: v1
kind: Service
metadata:
  name: abc-api
  labels:
    app: abc-api
spec:
  ports:
  - name: http
    port: 80
    targetPort: 3000
  - name: https
    port: 3000
    targetPort: 3000
  selector:
    app: abc-api
    tier: frontend
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: abc-api
  labels:
    app: abc-api
spec:
  selector:
    matchLabels:
      app: abc-api
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: abc-api
        tier: frontend
    spec:
      containers:
      - image: ABC_IMAGE
        resources:
          requests:
            memory: "128Mi"
            cpu: .30
          limits:
            memory: "512Mi"
            cpu: .99

I searched and found that yq is a better tool for this. However when I read values from this file, it only shows it till the line with '3 dashes': no values past that.

# yq r worker/deployment.yaml 
apiVersion: v1
kind: Service
metadata:
  name: hometales-api
  labels:
    app: hometales-api
spec:
  ports:
  - name: http
    port: 80
    targetPort: 3000
  - name: https
    port: 3000
    targetPort: 3000
  selector:
    app: hometales-api
    tier: frontend

I want to read the Deployment section, as well as edit the resource values.

Section to read:

---
apiVersion: apps/v1
kind: Deployment
metadata:

....

Section to edit:

resources:
  requests:
    memory: "128Mi"
    cpu: .20
  limits:
    memory: "512Mi"
    cpu: .99

So 1st part of Q: how to read after 2nd instance of 3-dashes? 2nd part of Q: how to edit resource values on the fly?

I'm able to run this command and read this section, but can't read memory or cpu value further:

# yq r -d1 deployment.yaml "spec.template.spec.containers[0].resources.requests"
memory: "128Mi"
cpu: .20

Upvotes: 1

Views: 1748

Answers (1)

coderanger
coderanger

Reputation: 54267

Use the -d CLI option. See https://mikefarah.gitbook.io/yq/commands/write-update#multiple-documents for more details.

Also Kubernetes has its own thing for in kubectl patch.

Upvotes: 2

Related Questions