首页 > 其他分享 >Knative event Brokers and Triggers 事件传递模式实例

Knative event Brokers and Triggers 事件传递模式实例

时间:2023-11-16 15:11:35浏览次数:26  
标签:Triggers root myproject Brokers wgs push com event Knative

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

相关文章

  • Knative Event gitlab source
    服务说明本地gitlab信息ip地址:192.168.174.108httpport:8080域名:codo.wgs.comkservice-event-display信息istio-ingressgateway对外地址:192.168.174.249kservice-event-display对外域名:gitlabsource.wgs.com域名解析:gitlabsource.wgs.com-->192.168.174.249......
  • Knative 基础
    Knative项目简介读音为“kay-nay-tiv”,由Google于2018年7月正式发布Kubernetes平台的原生扩展组件,让其能够轻松地部署、运行和管理Serverless类型的云原生应用由RedHat、Google和IBM等公司,以及各种初创公司组成的开源社区共同维护目标在于Serverless技术标准化Knative是什么基......
  • wpf Interaction Triggers 绑定任意方法、任意Command
     framework版本引入命名空间通过在代码中引入System.Windows.Interactivity.dll,引入了这个dll后我们就能够使用这个里面的方法来将事件映射到ViewModel层了,我们来看看具体的使用步骤,第一步就是引入命名控件xmlns:i="clr-namespace:System.Windows.Interactivity;assembl......
  • WPF触发器(Triggers):介绍与用法实例
    引言WindowsPresentationFoundation(WPF)提供了一个丰富和灵活的图形渲染框架,触发器(Triggers)是其中一个重要的功能。触发器能够用来控制或改变UI元素的属性、样式、甚至行为。在这篇博客文章中,我们将详细介绍WPF中触发器的种类、用法,并通过一些实际例子进行讲解。1.触发器的种......
  • knative serving 流量管理
    创建客户端#kubectlrunclient--image=ikubernetes/admin-box-it--rm--restart=Never--command-nknative-demo--/bin/bashroot@client/#创建应用hello-world-v1.yamlapiVersion:serving.knative.dev/v1kind:Servicemetadata:name:helloworld-gonames......
  • Serverless平台knative第十章如何应用pod频繁抖动
    负载变动频繁时,Knative可能会因为响应负载变动而导致频繁创建或销毁Pod实例为避免服务规模“抖动”,AutoScaler支持两种扩缩容模式Stable稳定模式在稳定模式中,KPA会在默认的稳定窗口期(默认为60秒)内计算Pod的平均并发数。根据这个平均并发数,KPA会调整Pod的数量,以保持稳定的负载水......
  • Serverless平台knative第九章配置文件介绍
    knative配置文件[root@ip-172-17-11-227~]#kubectlgetcm-nknative-servingNAMEDATAAGEconfig-autoscaler12d2hconfig-defaults12d2hconfig-deployment22d2hconfig-domain22d2h......
  • knative serving 域名映射
    创建应用hello-world.yamlapiVersion:serving.knative.dev/v1kind:Servicemetadata:name:helloworld-gonamespace:knative-demospec:template:spec:containers:-image:ghcr.dockerproxy.com/knative/helloworld-go:latestenv......
  • Serverless平台knative第八章流量管理
    准备环境创建一个服务两个版本[root@ip-172-17-11-227~]#catblue.yamlapiVersion:serving.knative.dev/v1kind:Servicemetadata:name:demoappspec:template:metadata:name:demoapp-01spec:containers:#-image:gcr.io/knative......
  • Serverless平台knative第六章配置最大并发数及更新操作讲解
    并发数配置apiVersion:serving.knative.dev/v1kind:Servicemetadata:name:hellospec:template:metadata:name:hello-world-002spec:containerConcurrency:10#单个pod允许的最大并发数,超过将扩容containers:#-image:gcr......