0

I have a config I need to apply from kubectl, but I don't quite understand what it is:

apiVersion: v1
kind: ConfigMap
data:
  localHosting.v1: |
    host: "localhost:1234"
    containerHost: "container-name:5555"

I can't figure out how to make this configmap using a kubectl create configmap invocation though. I thought that ConfigMaps were just key-value pairs, and the usage of the --from-literal flag seems to expect that, but this config seems to have an additional level of nesting.

I know you could typically create a YAML file or pipe in the whole YAML file using a heredoc, but that's not a very good solution in my situation. Is there a way to craft kubectl create configmap command to do this?

Stabby
  • 97
  • 1
  • 6
  • Can you write out that YAML file exactly as you've shown it, and `kubectl apply` it? [How do I break a string in YAML over multiple lines?](https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines) might help explain the specific syntax here. – David Maze Jul 14 '23 at 10:34

1 Answers1

2

I thought that ConfigMaps were just key-value pairs...

They are just key-value pairs. In your example, localHosting.v1 is the key, and the string...

host: "localhost:1234"
containerHost: "container-name:5555"

...is the value. You can create this from the command line by running something like:

kubectl create cm myconfigmap '--from-literal=localHosting.v1=host: "localhost:1234"
containerHost: "container-name:5555
'

Which results in:

$ kubectl get cm myconfigmap -o yaml

apiVersion: v1
data:
  localHosting.v1: |
    host: "localhost:1234"
    containerHost: "container-name:5555
kind: ConfigMap
metadata:
  creationTimestamp: "2023-07-14T02:28:14Z"
  name: myconfigmap
  namespace: sandbox
  resourceVersion: "57813424"
  uid: a126c38f-6836-4875-9a1b-7291910b7490

As with any other shell command, we use single quotes (') to quote a multi-line string value.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Thank you for providing this correct command! I considered this possibility, but I was unable to get the command right because I was putting quotes in the wrong places. – Stabby Jul 14 '23 at 17:27