Brokers and Triggers 实例说明
event source: gitlabsource
基于MT通道的broker: default
trigger trigger-push - > sink event-display-push
过滤条件: dev.knative.sources.gitlab.push
trigger trigger-tag-push - > sink event-display-tag_push
过滤条件:dev.knative.sources.gitlab.tag_push
Brokers and Triggers 模型
创建基于mt-channel的broker
broker-default.yaml
apiVersion: eventing.knative.dev/v1
kind: Broker
metadata:
name: default
namespace: event-demo
创建 broker 资源
# kubectl apply -f broker-default.yaml
broker.eventing.knative.dev/default created
查看 broker 资源
# kubectl get broker -n event-demo
NAME URL AGE READY REASON
default http://broker-ingress.knative-eventing.svc.wgs.local/event-demo/default 4m19s True
创建 sink
kservice-event-display-push.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display-push
namespace: event-demo-push
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/min-scale: "1"
spec:
containers:
- image: gcr.dockerproxy.com/knative-releases/knative.dev/eventing/cmd/event_display
ports:
- containerPort: 8080
kservice-event-display-tag_push.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display-tag-push
namespace: event-demo-tag-push
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/min-scale: "1"
spec:
containers:
- image: gcr.dockerproxy.com/knative-releases/knative.dev/eventing/cmd/event_display
ports:
- containerPort: 8080
创建 event 资源
# kubectl apply -f kservice-event-display-push.yaml -f kservice-event-display-tag_push.yaml
service.serving.knative.dev/event-display-push created
service.serving.knative.dev/event-display-tag-push created
查看 event 资源
event-display-push
# kubectl get pods -n event-demo-push
NAME READY STATUS RESTARTS AGE
event-display-push-00001-deployment-69fdbb4c86-lwjnj 2/2 Running 0 54s
event-display-tag-push
# kubectl get pods -n event-demo-tag-push
NAME READY STATUS RESTARTS AGE
event-display-tag-push-00001-deployment-6df4f7f986-pkn8c 2/2 Running 0 62s
创建 trigger
trigger-filter-push-event.yaml
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: trigger-push
namespace: event-demo
spec:
broker: default
filter:
attributes:
type: dev.knative.sources.gitlab.push
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-display-push
namespace: event-demo-push
trigger-filter-tag_push-event.yaml
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: trigger-tag-push
namespace: event-demo
spec:
broker: default
filter:
attributes:
type: dev.knative.sources.gitlab.tag_push
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-display-tag-push
namespace: event-demo-tag-push
创建 trigger 资源
# kubectl apply -f trigger-filter-push-event.yaml -f trigger-filter-tag_push-event.yaml
trigger.eventing.knative.dev/trigger-push created
trigger.eventing.knative.dev/trigger-tag-push created
查看 trigger 资源
# kubectl get trigger -n event-demo
NAME BROKER SUBSCRIBER_URI AGE READY REASON
trigger-push default 91s
trigger-tag-push default 91s
创建 gitlab source
创建 GitLab Tokens
生成 secretToken
# head -c 16 /dev/urandom | base64
+FmTUzNvthZWSsVrtvAqtQ==
secret-token.yaml
apiVersion: v1
kind: Secret
metadata:
name: gitlabsecret
namespace: event-demo
type: Opaque
stringData:
accessToken: glpat-ksv2Buget9FjyKEoiTRS # GitLab上的Personal Access Token
secretToken: +FmTUzNvthZWSsVrtvAqtQ== # GitLab调用GitLabSource与Webhook Secret
创建 secretToken 资源
# kubectl apply -f secret-token.yaml
GitLabSource-to-broker.yaml
apiVersion: sources.knative.dev/v1alpha1
kind: GitLabSource
metadata:
name: gitlabsource-demo
namespace: event-demo
spec:
eventTypes: # 指定要监视的事件类型
- push_events
- issues_events
- merge_requests_events
- tag_push_events
projectUrl: http://codo.wgs.com/root/myproject # 即要监视的目标代码仓库的URL
sslverify: false # 是否进行ssl认证
accessToken: # GitLab上仓库所属的用户的personal access token
secretKeyRef:
name: gitlabsecret
key: accessToken
secretToken: # 由GitLab经Webhook调用GitLabSource时使用的Secret
secretKeyRef:
name: gitlabsecret
key: secretToken
sink: # 事件的输出目标
ref:
apiVersion: eventing.knative.dev/v1
kind: Broker
name: default
生成 GitLabSource 资源
# kubectl apply -f GitLabSource-to-broker.yaml
gitlabsource.sources.knative.dev/gitlabsource-demo created
查看 GitLabSource 资源
# kubectl get gitlabsources -n event-demo
NAME READY REASON SINK AGE
gitlabsource-broker True http://broker-ingress.knative-eventing.svc.wgs.local/event-demo/default 5m10s
查看 rt 信息
# kubectl get rt -n event-demo
NAME URL READY REASON
gitlabsource-broker-8jpbj http://gitlabsource-broker-8jpbj.event-demo.svc.wgs.local True
查看 svc
# kubectl get svc -n event-demo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default-kne-trigger-kn-channel ExternalName <none> imc-dispatcher.knative-eventing.svc.wgs.local 80/TCP 12m
gitlabsource-broker-8jpbj ExternalName <none> knative-local-gateway.istio-system.svc.wgs.local 80/TCP 107s
gitlabsource-broker-8jpbj-00001 ClusterIP 10.100.63.92 <none> 80/TCP,443/TCP 6m44s
gitlabsource-broker-8jpbj-00001-private ClusterIP 10.100.54.22 <none> 80/TCP,443/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP 6m44s
验证 gitlab webhook
gitlab source 映射外网域名
自动创建 ClusterDomainClaims
kubectl patch configmap config-network -n knative-serving -p '{"data":{"autocreate-cluster-domain-claims":"true"}}'
configmap/config-network patched
domainmapping.yaml
# 自动创建cdc
apiVersion: serving.knative.dev/v1beta1
kind: DomainMapping
metadata:
name: gitlabsource.wgs.com
namespace: event-demo
spec:
ref:
name: gitlabsource-broker-8jpbj
kind: route
apiVersion: serving.knative.dev/v1
namespace: event-demo
创建 dm
# kubectl apply -f domainmapping.yaml
domainmapping.serving.knative.dev/gitlabsource.wgs.com created
查看 dm
# kubectl get dm -n event-demo
NAME URL READY REASON
gitlabsource.wgs.com http://gitlabsource.wgs.com True
验证 trigger 规则
修改 gitlab webhook
测试 push 事件
生成 push 事件
查看 event-display-push 日志
# kubectl logs event-display-push-00001-deployment-69fdbb4c86-lwjnj -c user-container -n event-demo-push
☁️ cloudevents.Event
Context Attributes,
specversion: 1.0
type: dev.knative.sources.gitlab.push
source: http://192.168.174.108:8080/root/myproject
id: aff1c3b2-a921-4e49-ae4d-09407dde9dca
time: 2023-11-16T06:50:06.91782717Z
datacontenttype: application/json
Extensions,
comgitlabevent: Push Hook
knativearrivaltime: 2023-11-16T06:50:06.919791709Z
Data,
{
"object_kind": "push",
"before": "14081b70599203b5771e4e30d7a8df7faf4064af",
"after": "14081b70599203b5771e4e30d7a8df7faf4064af",
"ref": "refs/heads/main",
"checkout_sha": "14081b70599203b5771e4e30d7a8df7faf4064af",
"user_id": 1,
"user_name": "Administrator",
"user_username": "root",
"user_email": "",
"user_avatar": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\u0026d=identicon",
"project_id": 1,
"Project": {
"id": 1,
"name": "Myproject",
"description": "",
"web_url": "http://codo.wgs.com/root/myproject",
"avatar_url": "",
"git_ssh_url": "ssh://[email protected]:35022/root/myproject.git",
"git_http_url": "http://codo.wgs.com:8080/root/myproject.git",
"namespace": "Administrator",
"visibility_level": 20,
"path_with_namespace": "root/myproject",
"default_branch": "main",
"homepage": "http://codo.wgs.com/root/myproject",
"url": "ssh://[email protected]:35022/root/myproject.git",
"ssh_url": "ssh://[email protected]:35022/root/myproject.git",
"http_url": "http://codo.wgs.com:8080/root/myproject.git"
},
"repository": {
"name": "Myproject",
"url": "ssh://[email protected]:35022/root/myproject.git",
"description": "",
"homepage": "http://codo.wgs.com/root/myproject"
},
"commits": [
{
"id": "14081b70599203b5771e4e30d7a8df7faf4064af",
"message": "Initial commit",
"timestamp": "2023-11-13T11:04:35Z",
"url": "http://codo.wgs.com/root/myproject/-/commit/14081b70599203b5771e4e30d7a8df7faf4064af",
"author": {
"name": "Administrator",
"email": "[email protected]"
},
"added": [
"README.md"
],
"modified": [],
"removed": []
}
],
"total_commits_count": 1
}
查看 event-display-tag-push 日志
# kubectl logs event-display-tag-push-00001-deployment-6df4f7f986-pkn8c -c user-container -n event-demo-tag-push
无返回信息
测试 tag_push 事件
查看 event-display-push 日志
# kubectl logs event-display-push-00001-deployment-69fdbb4c86-lwjnj -c user-container -n event-demo-push
☁️ cloudevents.Event
Context Attributes,
specversion: 1.0
type: dev.knative.sources.gitlab.push
source: http://192.168.174.108:8080/root/myproject
id: aff1c3b2-a921-4e49-ae4d-09407dde9dca
time: 2023-11-16T06:50:06.91782717Z
datacontenttype: application/json
Extensions,
comgitlabevent: Push Hook
knativearrivaltime: 2023-11-16T06:50:06.919791709Z
Data,
{
"object_kind": "push",
"before": "14081b70599203b5771e4e30d7a8df7faf4064af",
"after": "14081b70599203b5771e4e30d7a8df7faf4064af",
"ref": "refs/heads/main",
"checkout_sha": "14081b70599203b5771e4e30d7a8df7faf4064af",
"user_id": 1,
"user_name": "Administrator",
"user_username": "root",
"user_email": "",
"user_avatar": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\u0026d=identicon",
"project_id": 1,
"Project": {
"id": 1,
"name": "Myproject",
"description": "",
"web_url": "http://codo.wgs.com/root/myproject",
"avatar_url": "",
"git_ssh_url": "ssh://[email protected]:35022/root/myproject.git",
"git_http_url": "http://codo.wgs.com:8080/root/myproject.git",
"namespace": "Administrator",
"visibility_level": 20,
"path_with_namespace": "root/myproject",
"default_branch": "main",
"homepage": "http://codo.wgs.com/root/myproject",
"url": "ssh://[email protected]:35022/root/myproject.git",
"ssh_url": "ssh://[email protected]:35022/root/myproject.git",
"http_url": "http://codo.wgs.com:8080/root/myproject.git"
},
"repository": {
"name": "Myproject",
"url": "ssh://[email protected]:35022/root/myproject.git",
"description": "",
"homepage": "http://codo.wgs.com/root/myproject"
},
"commits": [
{
"id": "14081b70599203b5771e4e30d7a8df7faf4064af",
"message": "Initial commit",
"timestamp": "2023-11-13T11:04:35Z",
"url": "http://codo.wgs.com/root/myproject/-/commit/14081b70599203b5771e4e30d7a8df7faf4064af",
"author": {
"name": "Administrator",
"email": "[email protected]"
},
"added": [
"README.md"
],
"modified": [],
"removed": []
}
],
"total_commits_count": 1
}
查看 event-display-tag-push 日志
# kubectl logs event-display-tag-push-00001-deployment-6df4f7f986-pkn8c -c user-container -n event-demo-tag-push
☁️ cloudevents.Event
Context Attributes,
specversion: 1.0
type: dev.knative.sources.gitlab.tag_push
source: http://192.168.174.108:8080/root/myproject
id: 7106a136-2afd-4d90-9bd6-55097d3f4744
time: 2023-11-16T06:58:06.306429817Z
datacontenttype: application/json
Extensions,
comgitlabevent: Tag Push Hook
knativearrivaltime: 2023-11-16T06:58:06.303249782Z
Data,
{
"object_kind": "push",
"before": "14081b70599203b5771e4e30d7a8df7faf4064af",
"after": "14081b70599203b5771e4e30d7a8df7faf4064af",
"ref": "refs/heads/main",
"checkout_sha": "14081b70599203b5771e4e30d7a8df7faf4064af",
"user_id": 1,
"user_name": "Administrator",
"user_username": "root",
"user_avatar": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\u0026d=identicon",
"project_id": 1,
"Project": {
"id": 1,
"name": "Myproject",
"description": "",
"web_url": "http://codo.wgs.com/root/myproject",
"avatar_url": "",
"git_ssh_url": "ssh://[email protected]:35022/root/myproject.git",
"git_http_url": "http://codo.wgs.com:8080/root/myproject.git",
"namespace": "Administrator",
"visibility_level": 20,
"path_with_namespace": "root/myproject",
"default_branch": "main",
"homepage": "http://codo.wgs.com/root/myproject",
"url": "ssh://[email protected]:35022/root/myproject.git",
"ssh_url": "ssh://[email protected]:35022/root/myproject.git",
"http_url": "http://codo.wgs.com:8080/root/myproject.git"
},
"repository": {
"name": "Myproject",
"url": "ssh://[email protected]:35022/root/myproject.git",
"description": "",
"homepage": "http://codo.wgs.com/root/myproject"
},
"commits": [
{
"id": "14081b70599203b5771e4e30d7a8df7faf4064af",
"message": "Initial commit",
"timestamp": "2023-11-13T11:04:35Z",
"url": "http://codo.wgs.com/root/myproject/-/commit/14081b70599203b5771e4e30d7a8df7faf4064af",
"author": {
"name": "Administrator",
"email": "[email protected]"
},
"added": [
"README.md"
],
"modified": [],
"removed": []
}
],
"total_commits_count": 1
}
标签:Triggers,root,myproject,Brokers,wgs,push,com,event,Knative
From: https://www.cnblogs.com/wangguishe/p/17835566.html