How do I convert a bash command for connect a docker registry to a yaml config file?

140 views Asked by At

Following this tutorial to connect a local docker registry to the KIND cluster there's the below chunk of code in the bash script. I want to use my config file, but I don't know how the below chunk fits in (there's a lot of dashes and pipes in the syntax).

cat <<EOF | kind create cluster --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]
    endpoint = ["http://${reg_name}:${reg_port}"]
EOF

My config file:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 8080
    hostPort: 80
    protocol: TCP
- role: worker
- role: worker
- role: worker
- role: worker
2

There are 2 answers

3
David Maze On BEST ANSWER

In the shell fragment you show, everything between the first and last lines, including the dashes and pipes, is a valid YAML file; the only processing the shell does is replacing ${reg_name} and ${reg_port} with the values of the corresponding environment variables.

If you want to merge this with your existing kind config file, you should be able to just combine the top-level keys:

apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
  et: cetera
containerdConfigPatches:
- |-
  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5000"]
    endpoint = ["http://kind-registry:5000"]

If you had other containerdConfigPatches, the sequence of items starting with - on each line is a YAML list (like you have in nodes:) and you could add this patch to the end of the list. (This is a little unlikely since this option isn't documented in the kind Configuration documentation.)

1
tripleee On

Here documents in YAML are problematic anyway. Try using e.g. printf instead, maybe?

printf '%s\n' \
  'kind: Cluster' \
  'apiVersion: kind.x-k8s.io/v1alpha4' \
  'containerdConfigPatches:' \
  '- |-' \
  '  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]' \
  '    endpoint = ["http://${reg_name}:${reg_port}"]' |
kind create cluster --config=-

Lucky your string doesn't contain any single quotes, so we can safely use those for wrapping. Also lucky your data doesn't contain any shell variable expansions or command substitutions, so we can use single (verbatim) quotes.

For the record, if you need to embed a literal single quote,

'you can'"'"'t get there from here'

produces the literal quoted string

you can't get there from here

(look closely; that's a single-quoted string, adjacent to a double-quoted literal single quote "'", adjacent to another single-quoted string) and if you need to expand variables or command substitutions, you will need to switch to double quotes around those strings. Example:

printf '%s\n' \
  'literal $dollar sign in single quotes, the shell won'"'"'t touch it' \
  "inside double quotes, $HOME expands to your home directory" \
  'you can combine the two, like '"$(echo '"this"')"', too!'