1

按照本教程将本地 docker 注册表连接到 KIND 集群,bash 脚本中有以下代码块。我想使用我的配置文件,但我不知道下面的块如何适应(语法中有很多破折号和管道)。

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

我的配置文件:

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
4

2 回答 2

2

在您显示的 shell 片段中,第一行和最后一行之间的所有内容,包括破折号和管道,都是有效的 YAML 文件;shell 所做的唯一处理是将${reg_name}和替换${reg_port}为相应环境变量的值。

如果您想将其与现有的种类配置文件合并,您应该能够只组合顶级键:

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"]

如果您有 other containerdConfigPatches,则以每行开头的项目序列-是一个 YAML 列表(就像您在 中一样nodes:),您可以将此补丁添加到列表的末尾。(这有点不太可能,因为该选项没有记录在那种配置文档中。)

于 2020-09-30T14:58:27.373 回答
1

无论如何,这里的 YAML 文档都是有问题的。尝试使用 egprintf代替,也许?

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=-

幸运的是,您的字符串不包含任何单引号,因此我们可以安全地使用它们进行换行。同样幸运的是,您的数据不包含任何 shell 变量扩展或命令替换,因此我们可以使用单(逐字)引号。

作为记录,如果您需要嵌入文字单引号,

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

产生文字引用的字符串

you can't get there from here

(仔细看;这是一个单引号字符串,与双引号文字单引号"'"相邻,与另一个单引号字符串相邻)如果您需要扩展变量或命令替换,则需要在这些周围切换到双引号字符串。例子:

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!'
于 2020-09-30T06:20:56.417 回答