如何将用于连接docker注册表的bash命令转换为yaml配置文件



根据本教程将本地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

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

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

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

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

YAML中的文档无论如何都有问题。尝试使用例如printf,也许?

printf '%sn' 
'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 '%sn' 
'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!'

最新更新