1.更改
IngressRoute
s
.spec.routes[0].services[0].name
用Kustomize
改变
入口路线
s
.spec.routes[0]。服务[0]。名称
使用Kustomize是可能的
a
NameReference
transformer (see docs here)
-幸运的是我找到了灵感
in this issue
.因此,我们需要包括
configurations
关键字在我们的
kustomize.yaml
:
nameSuffix: foobar
configurations:
# Tie target Service metadata.name to IngressRoute's spec.routes.services.name
# Once Service name is changed, the IngressRoute referrerd service name will be changed as well.
- nameReference.yml
我们还需要添加名为
nameReference.yml
:
nameReference:
- kind: Service
fieldSpecs:
- kind: IngressRoute
path: spec/routes/services/name
正如你所看到的,我们把服务的
name
到入口路线
spec/routes/services/name
.现在开始
kustomize edit set namesuffix barfoo
不仅会改变
metadata.name
部署、服务和入口路线的标签,以及
.spec.routes[0]。服务[0]。名称
入口路线,因为它现在链接到
元数据。名称
这项服务的一部分。请注意,只有当推荐人和目标都有
名称
标签
2.更改部分入口路线
.spec.routes[0].match = Host()
问题的第二部分询问如何更改入口路线的一部分
.spec.routes[0]。match=Host()
.有
an open issue in the Kustomize GitHub project
.目前Kustomize不支持此用例-只为Kustomize编写自定义生成器插件。由于这可能不是一个首选选项,所以有另一种方法是从
this blog post
。因为我们可以使用以下语法在控制台内联创建yaml文件
cat > ./myyamlfile.yml <<EOF ... EOF
我们也可以使用内联变量替换。
因此,首先将分支名称定义为变量:
RULE_HOST_BRANCHNAME=foobar
然后使用所描述的语法创建
ingressroute-patch.yml
文件内联:
cat > ./ingressroute-patch.yml <<EOF
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: microservice-api-spring-boot-ingressroute
namespace: default
spec:
entryPoints:
- web
routes:
- match: Host(\`microservice-api-spring-boot-$RULE_HOST_BRANCHNAME.tekton-argocd.de\`)
kind: Rule
services:
- name: microservice-api-spring-boot
port: 80
EOF
最后一步是使用
入口路线补丁。yml
归档为
patchesStrategicMerge
在我们的
kustomization.yaml
这样地:
patchesStrategicMerge:
- ingressroute-patch.yml
现在正在运行
kustomize build .
应为我们的设置输出正确的部署、服务和入口路线:
apiVersion: v1
kind: Service
metadata:
labels:
branch: barfoo
name: microservice-api-spring-boot-barfoo
spec:
ports:
- port: 80
targetPort: 8098
selector:
app: microservice-api-spring-boot
branch: barfoo
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
branch: barfoo
name: microservice-api-spring-boot-barfoo
spec:
replicas: 3
revisionHistoryLimit: 3
selector:
matchLabels:
app: microservice-api-spring-boot
branch: barfoo
template:
metadata:
labels:
app: microservice-api-spring-boot
branch: barfoo
spec:
containers:
- image: registry.gitlab.com/jonashackt/microservice-api-spring-boot:barfoo
name: microservice-api-spring-boot
ports:
- containerPort: 8098
imagePullSecrets:
- name: gitlab-container-registry
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
labels:
branch: barfoo
name: microservice-api-spring-boot-ingressroute-barfoo
namespace: default
spec:
entryPoints:
- web
routes:
- kind: Rule
match: Host(`microservice-api-spring-boot-barfoo.tekton-argocd.de`)
services:
- name: microservice-api-spring-boot-barfoo
port: 80