如何在 Kubernetes yaml 文件中动态设置包含 URL(带斜杠)的值并防止:替换命令中的错误标志
How to dynamically set values containing URLs (with slashes) in Kubernetes yaml files & prevent: bad flag in substitute command
我们使用 Tekton 作为 CI/CD 解决方案,并希望替换 pipeline-run.yml
中 {{DASHBOARD_HOST}}
的值,如下所示:
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
generateName: buildpacks-test-pipeline-run-
spec:
serviceAccountName: buildpacks-service-account-gitlab # Only needed if you set up authorization
pipelineRef:
name: buildpacks-test-pipeline
workspaces:
- name: source-workspace
subPath: source
persistentVolumeClaim:
claimName: buildpacks-source-pvc
- name: cache-workspace
subPath: cache
persistentVolumeClaim:
claimName: buildpacks-source-pvc
- name: maven-repo-cache
subPath: maven-repo-cache
persistentVolumeClaim:
claimName: buildpacks-source-pvc
params:
- name: IMAGE
value: registry.gitlab.com/jonashackt/microservice-api-spring-boot # This defines the name of output image
- name: REPO_PATH_ONLY
value: jonashackt/microservice-api-spring-boot
- name: SOURCE_REVISION
value: 3c4131f8566ef157244881bacc474543ef96755d
- name: DASHBOARD_PORT
value: 8765
- name: DASHBOARD_HOST
value: {{DASHBOARD_HOST}}
我们尝试使用 sed
描述的方法 :
DASHBOARD_HOST=http://abd1c6f-123246.eu-central-1.elb.amazonaws.com
sed "s/{{DASHBOARD_HOST}}/$DASHBOARD_HOST/g" pipeline-run.yml | kubectl apply -f -
但出现以下错误:
bad flag in substitute command: '/'
知道如何使用 sed
替换 {{DASHBOARD_HOST}}
变量吗?
您要替换的变量包含斜杠 - 并且 sed "s/{{DASHBOARD_HOST}}/$DASHBOARD_HOST/g"
告诉 sed
使用 /
作为分隔符。这会产生错误。但是 as sed s
command can use any character as a delimiter,我们可以使用 s#
而不是 s/
优化解决方案,如下所示:
sed "s#{{DASHBOARD_HOST}}#$DASHBOARD_HOST#g" app-deployment.yaml | kubectl apply -f -
我们也可以省略 cat
,如 所述,因为 sed
能够自行读取文件。我们要在 app-deployment.yaml
中替换的变量可能看起来像这样:
...
params:
- name: DASHBOARD_HOST
value: {{DASHBOARD_HOST}}
...
多变量替换
使用 sed,您可以 even replace multiple variables 在您的 yaml 文件中。假设您的 app-deployment.yaml
具有以下内容:
...
params:
- name: DASHBOARD_HOST
value: {{DASHBOARD_HOST}}
- name: DASHBOARD_PORT
value: {{DASHBOARD_PORT}}
...
现在在 shell:
中设置两个变量
DASHBOARD_HOST=http://abd1c6f-123246.eu-central-1.elb.amazonaws.com
DASHBOARD_PORT=9785
然后 chain the sed s#
commands 使用 ;
像这样:
sed "s#{{DASHBOARD_HOST}}#$DASHBOARD_HOST#g;s#{{DASHBOARD_PORT}}#$DASHBOARD_PORT#g" app-deployment.yaml | kubectl apply -f -
我们使用 Tekton 作为 CI/CD 解决方案,并希望替换 pipeline-run.yml
中 {{DASHBOARD_HOST}}
的值,如下所示:
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
generateName: buildpacks-test-pipeline-run-
spec:
serviceAccountName: buildpacks-service-account-gitlab # Only needed if you set up authorization
pipelineRef:
name: buildpacks-test-pipeline
workspaces:
- name: source-workspace
subPath: source
persistentVolumeClaim:
claimName: buildpacks-source-pvc
- name: cache-workspace
subPath: cache
persistentVolumeClaim:
claimName: buildpacks-source-pvc
- name: maven-repo-cache
subPath: maven-repo-cache
persistentVolumeClaim:
claimName: buildpacks-source-pvc
params:
- name: IMAGE
value: registry.gitlab.com/jonashackt/microservice-api-spring-boot # This defines the name of output image
- name: REPO_PATH_ONLY
value: jonashackt/microservice-api-spring-boot
- name: SOURCE_REVISION
value: 3c4131f8566ef157244881bacc474543ef96755d
- name: DASHBOARD_PORT
value: 8765
- name: DASHBOARD_HOST
value: {{DASHBOARD_HOST}}
我们尝试使用 sed
描述的方法
DASHBOARD_HOST=http://abd1c6f-123246.eu-central-1.elb.amazonaws.com
sed "s/{{DASHBOARD_HOST}}/$DASHBOARD_HOST/g" pipeline-run.yml | kubectl apply -f -
但出现以下错误:
bad flag in substitute command: '/'
知道如何使用 sed
替换 {{DASHBOARD_HOST}}
变量吗?
您要替换的变量包含斜杠 - 并且 sed "s/{{DASHBOARD_HOST}}/$DASHBOARD_HOST/g"
告诉 sed
使用 /
作为分隔符。这会产生错误。但是 as sed s
command can use any character as a delimiter,我们可以使用 s#
而不是 s/
优化解决方案,如下所示:
sed "s#{{DASHBOARD_HOST}}#$DASHBOARD_HOST#g" app-deployment.yaml | kubectl apply -f -
我们也可以省略 cat
,如 sed
能够自行读取文件。我们要在 app-deployment.yaml
中替换的变量可能看起来像这样:
...
params:
- name: DASHBOARD_HOST
value: {{DASHBOARD_HOST}}
...
多变量替换
使用 sed,您可以 even replace multiple variables 在您的 yaml 文件中。假设您的 app-deployment.yaml
具有以下内容:
...
params:
- name: DASHBOARD_HOST
value: {{DASHBOARD_HOST}}
- name: DASHBOARD_PORT
value: {{DASHBOARD_PORT}}
...
现在在 shell:
中设置两个变量DASHBOARD_HOST=http://abd1c6f-123246.eu-central-1.elb.amazonaws.com
DASHBOARD_PORT=9785
然后 chain the sed s#
commands 使用 ;
像这样:
sed "s#{{DASHBOARD_HOST}}#$DASHBOARD_HOST#g;s#{{DASHBOARD_PORT}}#$DASHBOARD_PORT#g" app-deployment.yaml | kubectl apply -f -