SinkBinding 无法注入 K_SINK 环境变量

问题描述

我正在尝试设置 Knative 事件管道,其中存在一个容器,该容器接受外部 gRPC 请求并将事件触发到代理中以进行进一步处理。

在我的玩具示例中,我未能使用 SinkBinding 注入 K_SINK 环境变量。这是我的配置的相关部分:

apiVersion: v1
kind: Namespace
Metadata:
  name: bora-namespace
  labels:
    eventing.knative.dev/injection: enabled

---

apiVersion: eventing.knative.dev/v1
kind: broker
Metadata:
  name: my-broker
  namespace: bora-namespace

---

apiVersion: apps/v1
kind: Deployment
Metadata:
  name: ease-pipeline-server
  namespace: bora-namespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ease-pipeline-server
  template:
    Metadata:
      labels:
        app: ease-pipeline-server
    spec:
      containers:
        - name: ease-pipeline-server
          image: docker.io/boramalper/ease-pipeline-server:latest
          imagePullPolicy: Always

---

apiVersion: sources.knative.dev/v1
kind: SinkBinding
Metadata:
  name: bind-ease-pipeline-server
  namespace: bora-namespace
spec:
  subject:
    apiVersion: apps/v1
    kind: Deployment
    selector:
      matchLabels:
        app: ease-pipeline-server
  sink:
    ref:
      apiVersion: eventing.knative.dev/v1
      kind: broker
      name: my-broker

---

kind: Service
apiVersion: v1
Metadata:
  name: ease-pipeline-server
  namespace: bora-namespace
spec:
  type: NodePort
  selector:
    app: ease-pipeline-server
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
      nodePort: 30002

由于缺少环境变量,我的容器陷入无限崩溃循环。

SinkBinding 对象似乎没有问题:

$ kubectl --namespace bora-namespace get sinkbinding
NAME                        SINK                                                                                AGE   READY   REASON
bind-ease-pipeline-server   http://broker-ingress.knative-eventing.svc.cluster.local/bora-namespace/my-broker   22m   True    

系统信息:

$ kn version
Version:      v20210526-local-0c6ef82
Build Date:   2021-05-26 06:34:50
Git Revision: 0c6ef82
Supported APIs:
* Serving
  - serving.knative.dev/v1 (knative-serving v0.23.0)
* Eventing
  - sources.knative.dev/v1 (knative-eventing v0.23.0)
  - eventing.knative.dev/v1 (knative-eventing v0.23.0)

$ kubectl version
Client Version: version.Info{Major:"1",Minor:"20",GitVersion:"v1.20.6",GitCommit:"8a62859e515889f07e3e3be6a1080413f17cf2c3",GitTreeState:"clean",BuildDate:"2021-04-15T03:28:42Z",GoVersion:"go1.15.10",Compiler:"gc",Platform:"linux/amd64"}
Server Version: version.Info{Major:"1",GitVersion:"v1.20.7",GitCommit:"132a687512d7fb058d0f5890f07d4121b3f0a2e2",BuildDate:"2021-05-12T12:32:49Z",GoVersion:"go1.15.12",Platform:"linux/amd64"}

$ lsb_release -a
No LSB modules are available.
distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:    18.04
Codename:   bionic

$ uname -a
Linux REDACTED 4.15.0-137-generic #141-Ubuntu SMP Fri Feb 19 13:46:27 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

解决方法

SinkBinding 对象具有使用标签选择器配置的主题:

subject:
  apiVersion: apps/v1
  kind: Deployment
  selector:
    matchLabels:
      app: ease-pipeline-server

但是,Deployment 对象上没有设置这样的标签:

metadata:
  name: ease-pipeline-server
  # no labels

这里的解决方案是:

  • 将相应的标签添加到 Deployment 的 metadata

    metadata:
      name: ease-pipeline-server
      labels:
        app: ease-pipeline-server
    
  • 使用与 Deployment 的 name (API documentation) 匹配的主题

    subject:
      apiVersion: apps/v1
      kind: Deployment
      name: ease-pipeline-server