diff --git a/examples/zh/README.md b/examples/zh/README.md index 6802377ac..86d674500 100644 --- a/examples/zh/README.md +++ b/examples/zh/README.md @@ -24,7 +24,7 @@ go get sigs.k8s.io/kustomize * [remote target](../remoteBuild.md) - 通过 github URL 来构建 kustomization 。 - * [json patch](../jsonpatch.md) -在 kustomization 中应用 json patch 。 + * [json patch](jsonpatch.md) -在 kustomization 中应用 json patch 。 高级用法 diff --git a/examples/zh/jsonpatch.md b/examples/zh/jsonpatch.md new file mode 100644 index 000000000..28ee7e1be --- /dev/null +++ b/examples/zh/jsonpatch.md @@ -0,0 +1,122 @@ +# 例子: 应用 json patch(json补丁) + +kustomization文件支持诸如[JSON patches](https://tools.ietf.org/html/rfc6902)这种自定义资源. + +下面的例子将会将会使用这个特性对`Ingress`加以修改. + +首先,创建一个包含`ingress`的`kustomization`文件. + + +```bash +DEMO_HOME=$(mktemp -d) + +cat <$DEMO_HOME/kustomization.yaml +resources: +- ingress.yaml +EOF + +cat <$DEMO_HOME/ingress.yaml +apiVersion: extensions/v1beta1 +kind: Ingress +metadata: + name: my-ingress +spec: + rules: + - host: foo.bar.com + http: + paths: + - backend: + serviceName: my-api + servicePort: 80 +EOF +``` + +定义一个JSON patch文件,以更新`Ingress`对象的2个字段: + +- 把 host 从 `foo.bar.com` 改为 `foo.bar.io` +- 把 servicePort 从 `80` 改为 `8080` + + +```bash +cat <$DEMO_HOME/ingress_patch.json +[ + {"op": "replace", "path": "/spec/rules/0/host", "value": "foo.bar.io"}, + {"op": "replace", "path": "/spec/rules/0/http/paths/0/backend/servicePort", "value": 8080} +] +EOF +``` + +你也可以使用`YAML`这种文件格式创建一个补丁(patch).该例子顺便展示了“添加”操作: + + +```bash +cat <$DEMO_HOME/ingress_patch.yaml +- op: replace + path: /spec/rules/0/host + value: foo.bar.io + +- op: add + path: /spec/rules/0/http/paths/- + value: + path: '/test' + backend: + serviceName: my-test + servicePort: 8081 +EOF +``` + +在kustomization.yaml文件中增加 _patchesJson6902_ 字段,以应用该补丁 + + +```bash +cat <>$DEMO_HOME/kustomization.yaml +patchesJson6902: +- target: + group: extensions + version: v1beta1 + kind: Ingress + name: my-ingress + path: ingress_patch.json +EOF +``` + +运行 `kustomize build $DEMO_HOME`, 在输出那里可见 host 已经被正确更新. + + +```bash +test 1 == \ + $(kustomize build $DEMO_HOME | grep "host: foo.bar.io" | wc -l); \ + echo $? +``` + +运行 `kustomize build $DEMO_HOME`, 在输出那里确认 servicePort 已经被正确修改. + + + +```bash +test 1 == \ + $(kustomize build $DEMO_HOME | grep "servicePort: 8080" | wc -l); \ + echo $? +``` + +如果 patch 是YAML格式的,就能正确解析: + + +```bash +cat <>$DEMO_HOME/kustomization.yaml +patchesJson6902: +- target: + group: extensions + version: v1beta1 + kind: Ingress + name: my-ingress + path: ingress_patch.yaml +EOF +``` + + +```bash +test 1 == \ + $(kustomize build $DEMO_HOME | grep "path: /test" | wc -l); \ + echo $? +``` \ No newline at end of file