how to extract specific value from a configmap using oc client
Asked Answered
R

4

5

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]>
Retread answered 5/5, 2020 at 9:6 Comment(0)
K
4

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.

Kamacite answered 5/5, 2020 at 10:23 Comment(1)
this is what I need, I think I don't even need the python partRetread
G
1

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 :)

Gamez answered 5/5, 2020 at 9:12 Comment(1)
please elaborate json path partRetread
P
1

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 _

Perverse answered 5/5, 2020 at 10:6 Comment(0)
C
0

(with OpenShift/OKD oc tool) you can extract data from a ConfigMaps or Secrets as files:

oc extract configmap/<myconfigmap> --key=<my-data.file1>,<my-data.fileN> -n my-namespace

oc extract secrete/<myconfigmap> --key=<my-data.file1>,<my-data.fileN> -n my-namespace

with the --key parameter it will extract the selected <my-data.file1>,<my-data.fileN> in your local directory

without the --key parameter it will extract all the files.

Collection answered 20/12, 2022 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.