服务说明
本地gitlab信息
ip地址:192.168.174.108
http port: 8080
域名: codo.wgs.com
kservice-event-display 信息
istio-ingressgateway 对外地址:192.168.174.249
kservice-event-display 对外域名: gitlabsource.wgs.com
域名解析: gitlabsource.wgs.com --> 192.168.174.249
Gitlab source 介绍
将GitLab仓库上的事件转换为CloudEvents;
GitLabSource为指定的事件类型创建一个Webhook,监听传入的事件,并将其传递给消费者;
Gitlab source 事件类型
◼ 推送事件:push_events
◆对应的CloudEvents的类型为“dev.knative.sources.gitlab.push”
◼ tag推送事件:tag_push_events
◼ 议题事件:issues_events
◼ 合并请求事件:merge_requests_events
◼ 私密议题事件:confidential_issues_events
◼ 私密评论:confidential_note_events
◼ 部署事件:deployment_events
◼ 作业事件:job_events
◼ 评论:note_events
◼ 流水线事件:pipeline_events
◼ Wiki页面事件:wiki_page_events
Knative Event 事件传递模式
Sources to Sink
1. 单一Sink模式,事件接收过程中不存在排队和过滤等操作;
2. Event Source的职责仅是传递消息,且无需等待Sink响应;
3. fire and forget;
配置 GitLab
出站请求
创建访问令牌
glpat-ksv2Buget9FjyKEoiTRS
创建仓库
部署 GitLabSource
下载 gitlab event source
# wget https://github.com/knative-extensions/eventing-gitlab/releases/download/knative-v1.12.0/gitlab.yaml
替换 gcr.io
# sed -i '[email protected]@gcr.dockerproxy.com@g' gitlab.yaml
部署 gitlab event source
# kubectl apply -f gitlab.yaml
namespace/knative-sources created
serviceaccount/gitlab-controller-manager created
serviceaccount/gitlab-webhook created
clusterrole.rbac.authorization.k8s.io/gitlabsource-manager-role created
clusterrole.rbac.authorization.k8s.io/eventing-contrib-gitlab-source-observer created
clusterrolebinding.rbac.authorization.k8s.io/gitlabsource-manager-rolebinding created
clusterrolebinding.rbac.authorization.k8s.io/eventing-sources-gitlab-addressable-resolver created
clusterrolebinding.rbac.authorization.k8s.io/eventing-sources-gitlab-webhook created
clusterrole.rbac.authorization.k8s.io/gitlab-webhook created
customresourcedefinition.apiextensions.k8s.io/gitlabbindings.bindings.knative.dev created
customresourcedefinition.apiextensions.k8s.io/gitlabsources.sources.knative.dev created
service/gitlab-controller-manager-service created
deployment.apps/gitlab-controller-manager created
mutatingwebhookconfiguration.admissionregistration.k8s.io/defaulting.webhook.gitlab.sources.knative.dev created
validatingwebhookconfiguration.admissionregistration.k8s.io/validation.webhook.gitlab.sources.knative.dev created
mutatingwebhookconfiguration.admissionregistration.k8s.io/gitlabbindings.webhook.gitlab.sources.knative.dev created
secret/gitlab-webhook-certs created
service/gitlab-webhook created
deployment.apps/gitlab-webhook created
查看 gitlabsource api
# kubectl api-resources |grep -i sources
apiserversources sources.knative.dev/v1 true ApiServerSource
containersources sources.knative.dev/v1 true ContainerSource
gitlabsources sources.knative.dev/v1alpha1 true GitLabSource
kafkasources sources.knative.dev/v1beta1 true KafkaSource
pingsources sources.knative.dev/v1 true PingSource
sinkbindings sources.knative.dev/v1 true SinkBinding
查看 gitlabsource 资源
# kubectl get pods -n knative-sources
NAME READY STATUS RESTARTS AGE
gitlab-controller-manager-5c445948d9-fbtv9 1/1 Running 0 8m12s
gitlab-webhook-f9d6f68b4-crg5b 1/1 Running 0 8m12s
使用 GitLab Event Source
创建 Knative Service
kservice-event-display.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: gitlab-event-display
namespace: event-demo
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
生成 Knative Service 资源
# kubectl apply -f kservice-event-display.yaml
创建 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
创建 GitLab Events Source
GitLabSource-to-knative-service.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: serving.knative.dev/v1
kind: Service
name: gitlab-event-display
生成 GitLabSource 资源
# kubectl apply -f GitLabSource-to-knative-service.yaml
查看 GitLabSource 资源
# kubectl get gitlabsources -n event-demo
NAME READY REASON SINK AGE
gitlabsource-demo True http://gitlab-event-display.event-demo.svc.wgs.local 4m39s
查看 svc 资源
# kubectl get svc -n event-demo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
gitlab-event-display ExternalName <none> knative-local-gateway.istio-system.svc.wgs.local 80/TCP 4m49s
gitlab-event-display-00001 ClusterIP 10.100.198.48 <none> 80/TCP,443/TCP 4m53s
gitlab-event-display-00001-private ClusterIP 10.100.36.4 <none> 80/TCP,443/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP 4m53s
gitlabsource-demo-8gwpw ExternalName <none> knative-local-gateway.istio-system.svc.wgs.local 80/TCP 4m22s
gitlabsource-demo-8gwpw-00001 ClusterIP 10.100.3.41 <none> 80/TCP,443/TCP 4m25s
gitlabsource-demo-8gwpw-00001-private ClusterIP 10.100.137.94 <none> 80/TCP,443/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP 4m25s
查看 route 资源
# kubectl get route -n event-demo
NAME URL READY REASON
gitlab-event-display http://gitlab-event-display.event-demo.svc.wgs.local True
gitlabsource-demo-8gwpw http://gitlabsource-demo-8gwpw.event-demo.svc.wgs.local True
域名映射
自动创建 ClusterDomainClaims
kubectl patch configmap config-network -n knative-serving -p '{"data":{"autocreate-cluster-domain-claims":"true"}}'
configmap/config-network patched
dm.yaml
# 自动创建cdc
apiVersion: serving.knative.dev/v1beta1
kind: DomainMapping
metadata:
name: gitlabsource.wgs.com
namespace: event-demo
spec:
ref:
name: gitlabsource-demo-8gwpw
kind: route
apiVersion: serving.knative.dev/v1
namespace: event-demo
创建 dm
# kubectl apply -f dm.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
查看 vs
# kubectl get vs -n event-demo
NAME GATEWAYS HOSTS AGE
gitlab-event-display-ingress ["knative-serving/knative-local-gateway"] ["gitlab-event-display.event-demo","gitlab-event-display.event-demo.svc","gitlab-event-display.event-demo.svc.wgs.local"] 7m27s
gitlab-event-display-mesh ["mesh"] ["gitlab-event-display.event-demo","gitlab-event-display.event-demo.svc","gitlab-event-display.event-demo.svc.wgs.local"] 7m27s
gitlabsource-demo-8gwpw-ingress ["knative-serving/knative-local-gateway"] ["gitlabsource-demo-8gwpw.event-demo","gitlabsource-demo-8gwpw.event-demo.svc","gitlabsource-demo-8gwpw.event-demo.svc.wgs.local"] 7m
gitlabsource-demo-8gwpw-mesh ["mesh"] ["gitlabsource-demo-8gwpw.event-demo","gitlabsource-demo-8gwpw.event-demo.svc","gitlabsource-demo-8gwpw.event-demo.svc.wgs.local"] 7m
gitlabsource.wgs.com-ingress ["knative-serving/knative-ingress-gateway"] ["gitlabsource.wgs.com"] 5m40s
gitlab 验证webhook
修改webhook 地址
测试 gitlab source event
测试事件
查看 event pod 信息
kubectl get pods -n event-demo
NAME READY STATUS RESTARTS AGE
gitlab-event-display-00001-deployment-58974dbbd8-fngcg 3/3 Running 0 12m
查看 event 日志信息
# kubectl logs gitlab-event-display-00001-deployment-58974dbbd8-fngcg -c user-container -n event-demo
☁️ cloudevents.Event
Context Attributes,
specversion: 1.0
type: dev.knative.sources.gitlab.push
source: http://192.168.174.108:8080/root/myproject
id: 999abc37-56d6-4dbc-bd03-a49bfb7d3d5d
time: 2023-11-15T06:43:55.877829725Z
datacontenttype: application/json
Extensions,
comgitlabevent: Push Hook
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://gitlabsource.wgs.com/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://gitlabsource.wgs.com/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
}
参考文档
https://github.com/knative/docs/tree/main/code-samples/eventing/gitlab-source
标签:demo,gitlabsource,gitlab,source,Knative,wgs,event,knative From: https://www.cnblogs.com/wangguishe/p/17798081.html