首页 > 其他分享 >Knative Event gitlab source

Knative Event gitlab source

时间:2023-11-15 16:24:31浏览次数:32  
标签:demo gitlabsource gitlab source Knative wgs event knative

服务说明

本地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

相关文章

  • GitLab 不允许将代码推送到该项目上受保护的分支
    不允许将代码推送到该项目上受保护的分支这意味着还没有要保护的master分支,因为空存储库没有分支。要"启用/禁用分支保护",您需要是GitLab项目的主管理员或所有者。该分支是master是受保护分支,无论是master还是开发者都无权限push,只有owner可以操作。1.gitpush:"错误:无法将某......
  • cent os 搭建 gitlab 仓库
    IP:116.204.28.108ftp://116.204.28.108账号:root密码:@@@520lpgitlab初始账号密码rooteKwI7vMGhJnoQLwrTtDlGOYlrvGnNeoVT+sVASC0U1g=linux常用命令进入指定目录:cd地址前目录下的所有文件和目录:ls创建文件:echo'文件内容'>文件名创建文件夹:mkdirname编辑文件内容:vim文件名删......
  • gitlab搭建
    部署参考网址:https://about.gitlab.com/install/ 服务器系统选型,不同服务器操作系统及不同版本部署过程不同,本次以CentOS6为例,部署步骤如下:1.在CentOS6(和RedHat/Oracle/ScientificLinux6)上,以下命令还将在系统防火墙中打开HTTP和SSH访问:sudoyuminstall-ycurlpol......
  • 宝塔安装gitlab的坑
    宝塔安装gitlab12.8.1,修改了一下存储目录位置修改之后重载配置,结果gitlab内置nginx怎么也启动不了gitlab-ctlreconfigure 查看gitlab日志gitlab-ctltail 发现如下报错:chpst:fatal:unabletorun:/opt/gitlab/embedded/sbin/nginx:filedoesnotexis 搜索查到......
  • eventSource(SSE)的实践
     1.概述目前JS实时通信方式主要由:websocket、socket.io、eventSource(SSE)这三种方式。对于这三种方式,不同客户端场景有着其各自的优势。EventSource是单向通信,而websocket是双向通信。在新闻推送、股票行情这种只需要服务器发送消息给客户端场景,使用SSE更加合适,另外SSE是使用H......
  • git、github、gitee、gitlab的区别
    git是一种版本控制系统,是一个命令,是一种工具。github是一个基于git实现在线代码托管的仓库,向互联网开放,企业版要收钱。gitlab类似github,一般用于在企业内搭建git私服,要自己搭环境。gitee即码云,是oschina免费给企业用的,不用自己搭建环境。git-ce是社区版,gitlab-ee是企......
  • 开源项目SourceBrowser 功能实现中问题修复
    前段时间看到在线原源码浏览网站SourceBrowser,就好奇怎么读的代码展示的,就拔下源码看了下,然后自己打算简单实现下,不想每个工作日弄个把小时弄了两周,才解决报错问题,可以读取到文档,也简单学习了下Roslyc.原项目中时net472跑,我先直接copy拿段读取代码,新建一个控......
  • 如何用Angular or Vue 来 实现Dynamics 365 WebResource 开发
    第一步:构建Angular项目,可以使用VisualStudio的项目模版创建(含.netCore相关)或者使用Angularcli创建,我习惯使用angularcli 执行以下命令:ngnew项目名称,回车可以选择含路由,style是CSSorLESS根据所需选取,稍等几分钟(取决于网络,会download......
  • vue-cli-service vue.config.js配置 productionSourceMap与webpack中的devtool 关联详
    https://webpack.js.org/configuration/devtool/https://cli.vuejs.org/zh/config/#productionsourcemap https://github.com/vuejs/vue-cli/blob/f0f254e4bc81ed322eeb9f7de346e987e845068e/packages/%40vue/cli-service/lib/config/prod.js#L7 可以在源码中看到if(pro......
  • IDEA中配置配置国际化时,properties文件没有Resource Bundle如何解决
    如果IDEA中配置国际化时,.properties文件没有可视化界面,则需要安装一个ResourceBundleEditor的插件 ......