Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

Programatically apply a single resource from a multi resource Kubernetes YAML file

I have a file with three configmaps in it, like the one below.

apiVersion: v1
data:
  TEST: "one"
kind: ConfigMap
metadata:
  name: test-config-one
---
apiVersion: v1
data:
  TEST: "two"
kind: ConfigMap
metadata:
  name: test-config-two
---
apiVersion: v1
data:
  TEST: "three"
kind: ConfigMap
metadata:
  name: test-config-three

I'm trying to apply only test-config-three to the cluster. I know I can break that out into its own file and run kubectl apply -f test-config-three.yaml, but is there a way to do that without having to create a new file?

I was hoping to be able to do something like:

cat file.yml | yq <get only test-config-three> | kubectl apply -f -

But yq doesn't seem to support finding a single resource in a file. I also looked at tools like kubesplit but they tend to output all resources to separate files.

Is there a way to isolate and output a single resource from a yaml file containing multiple resources without creating a new file?

Update

Thanks to @Inian's answer below, I was able to get this full command working.

cat file.yml | yq e 'select(.data.TEST == "three")' - | kubectl apply -f -
question from:https://stackoverflow.com/questions/65902130/programatically-apply-a-single-resource-from-a-multi-resource-kubernetes-yaml-fi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are two versions of yq implemented, one in Python and one in Go as I've highlighted in my answer at How can I parse a YAML file from a Linux shell script?

Using the Python version - kislyuk/yq

yq -y 'select(.data.TEST == "three")' yaml

Go version - mikefarah/yq

yq e 'select(.data.TEST == "three")' yaml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...