how to extract specific value from a configmap using oc client

5/5/2020

My config map looks like this:

apiVersion: v1
data:
  my-data.yaml |2-
   #data comes here
kind: ConfigMap

Is it possible to extract the content of my-data.yaml key via

oc get configmap

or any other oc command?

e.g.

oc get configmap myconfigmap  -o=yaml <[only my-data.yaml]>
-- Yev
kubernetes
openshift
yaml

3 Answers

5/5/2020

I'd like to demonstrate an example command, which "coderanger" mentioned before.

This example converted from yaml to json and filtered ".keyname" using "jq" command after that. You can also use "yq" command instead of python one-liner and jq combination.

oc get configmap/myconfigmap \
   -o "jsonpath={ .data['my-data\.yaml']}" | \
   python -c 'import sys, yaml, json; y=yaml.load(sys.stdin.read()); print json.dumps(y)' | \
   jq '. | .keyname'

I hope it help you.

-- Daein Park
Source: StackOverflow

5/5/2020

No. As far as kube sees, that’s just one long string. You could use the json path output mode to filter to just the one value though. And then parse it with jq or yq. Or just use jq twice :)

-- coderanger
Source: StackOverflow

5/5/2020

There are some shell workarounds to parse yaml files:

yq

You may use yq, a command line YAML processor built on top of jq.

You can download it and find documentation at http://mikefarah.github.io/yq/.

niet

Another tool is openuado/niet

Niet is like xmllint or jq but for YAML and JSON data - you can use it to slice and filter and map and transform structured data.

You can easily retrieve data by using simple expressions or using xpath advanced features to access non-trivial data.

You can easily convert YAML format into JSON format and vice versa.

bash-yaml for pure bash

For pure bash you can try:

jasperes/bash-yaml

Read a yaml file and create variables in bash

Simple 41-lines-of-bash script uses only sed and awk to parse yaml-file and create variables from it.

mrbaseman/parse_yaml

parse_yaml provides a bash function that allows parsing simple YAML files. The output is shell code that defines shell variables which contain the parsed values. bash doesn't support multidimensional arrays. Therefore a separate variable is created for each value, and the name of the variable consists of the names of all levels in the yaml file, glued together with a separator character which defaults to _

-- Yasen
Source: StackOverflow