6.6. 从服务公开绑定数据

应用程序开发人员需要访问支持的服务,以构建和连接工作负载。连接工作负载以支持服务始终是一个挑战,因为每个服务提供商需要以不同的方式访问其机密并在工作负载中使用它们。

Service Binding Operator 可让应用程序开发人员将工作负载与 Operator 管理的后备服务轻松绑定,而无需任何手动步骤来配置绑定连接。要使 Service Binding Operator 作为 Operator 供应商或创建后备服务的用户提供绑定数据,您必须公开绑定数据,以便 Service Binding Operator 自动检测到绑定数据。然后,Service Binding Operator 会自动从后备服务收集绑定数据,并将其与工作负载共享,以提供一致且可预测的体验。

6.6.1. 公开绑定数据的方法

本节论述了您可以使用什么方法公开绑定数据。

确保您了解并了解您的工作负载要求和环境,以及如何与所提供的服务配合使用。

在以下情况下公开绑定数据:

  • 后备服务作为调配的服务资源提供。

    您要连接到的服务符合 Service Binding 规格。您必须创建一个带有所有所需绑定数据值的 Secret 资源,并在后备服务自定义资源 (CR) 中引用它。所有绑定数据值的检测都是自动的。

  • 后备服务不能作为调配的服务资源使用。

    您必须从后备服务公开绑定数据。根据工作负载要求和环境,您可以选择以下任一方法公开绑定数据:

    • 直接 secret 引用
    • 通过自定义资源定义(CRD)或 CR 注解声明绑定数据
    • 通过拥有的资源检测绑定数据

6.6.1.1. 置备的服务

置备的服务代表后端服务 CR,引用了放置在后备服务 CR 的 .status.binding.name 字段中的 Secret 资源。

作为 Operator 供应商或创建后备服务的用户,您可以通过创建 Secret 资源并在后备服务 CR 的 .status.binding.name 部分中引用它,使用此方法符合 Service Binding 规格。此 Secret 资源必须提供工作负载连接到后备服务所需的所有绑定数据值。

以下示例显示一个 AccountService CR,它代表一个后备服务和从 CR 引用的 Secret 资源。

示例: AccountService CR

apiVersion: example.com/v1alpha1
kind: AccountService
name: prod-account-service
spec:
# ...
status:
  binding:
    name: hippo-pguser-hippo

示例:引用的 Secret 资源

apiVersion: v1
kind: Secret
metadata:
  name: hippo-pguser-hippo
data:
  password: "<password>"
  user: "<username>"
# ...

在创建服务绑定资源时,您可以在 ServiceBinding 规格中直接提供 AccountService 资源的详情,如下所示:

示例: ServiceBinding 资源

apiVersion: binding.operators.coreos.com/v1alpha1
kind: ServiceBinding
metadata:
  name: account-service
spec:
# ...
  services:
  - group: "example.com"
    version: v1alpha1
    kind: AccountService
    name: prod-account-service
  application:
    name: spring-petclinic
    group: apps
    version: v1
    resource: deployments

示例: Specification API 中的 ServiceBinding 资源

apiVersion: servicebinding.io/v1beta1
kind: ServiceBinding
metadata:
  name: account-service
spec:
# ...
  service:
    apiVersion: example.com/v1alpha1
    kind: AccountService
    name: prod-account-service
  workload:
    apiVersion: apps/v1
    kind: Deployment
    name: spring-petclinic

此方法公开 hippo-pguser-hippo 所引用 Secret 资源中的所有键作为绑定数据,以投射到工作负载中。

6.6.1.2. 直接 secret 引用

如果所有必需的绑定数据值都位于 Service Binding 定义中可以引用的 Secret 资源中,您可以使用此方法。在这个方法中,ServiceBinding 资源直接引用 Secret 资源来连接服务。Secret 资源中的所有键都公开为绑定数据。

示例:带有 binding.operators.coreos.com API 的规格

apiVersion: binding.operators.coreos.com/v1alpha1
kind: ServiceBinding
metadata:
  name: account-service
spec:
# ...
  services:
  - group: ""
    version: v1
    kind: Secret
    name: hippo-pguser-hippo

示例:符合 servicebinding.io API 的规格

apiVersion: servicebinding.io/v1beta1
kind: ServiceBinding
metadata:
  name: account-service
spec:
# ...
  service:
    apiVersion: v1
    kind: Secret
    name: hippo-pguser-hippo

6.6.1.3. 通过 CRD 或 CR 注解声明绑定数据

您可以使用此方法注解后备服务的资源,以使用特定注解公开绑定数据。在 metadata 部分下添加注解会改变后备服务的 CR 和 CRD。Service Binding Operator 会检测添加到 CR 和 CRD 的注解,然后使用基于注解提取的值创建一个 Secret 资源。

以下示例显示了在 metadata 部分添加的注解以及从资源引用的 ConfigMap 对象:

示例:从 CR 注解中定义的 Secret 对象公开绑定数据

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo
  namespace: my-petclinic
  annotations:
    service.binding: 'path={.metadata.name}-pguser-{.metadata.name},objectType=Secret'
# ...

前面的示例将 secret 名称放在 {.metadata.name}-pguser-{.metadata.name} 模板中,该模板解析为 hippo-pguser-hippo。模板可以包含多个 JSONPath 表达式。

示例:从一个资源中参考的 Secret 对象

apiVersion: v1
kind: Secret
metadata:
  name: hippo-pguser-hippo
data:
  password: "<password>"
  user: "<username>"

示例:从 CR 注解中定义的 ConfigMap 对象公开绑定数据

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo
  namespace: my-petclinic
  annotations:
    service.binding: 'path={.metadata.name}-config,objectType=ConfigMap'
# ...

前面的示例将配置映射的名称放在解析到 hippo-config{.metadata.name}-config 模板中。模板可以包含多个 JSONPath 表达式。

示例:从资源引用的 ConfigMap 对象

apiVersion: v1
kind: ConfigMap
metadata:
  name: hippo-config
data:
  db_timeout: "10s"
  user: "hippo"

6.6.1.4. 通过拥有的资源检测绑定数据

如果您的后备服务拥有一个或多个 Kubernetes 资源,如路由、服务、配置映射或 secret,您可以使用此方法检测绑定数据。在这个方法中,Service Binding Operator 会检测来自后备服务 CR 拥有的资源的绑定数据。

以下示例显示 ServiceBinding CR 中的 detectBindingResources API 选项设置为 true

Example

apiVersion: binding.operators.coreos.com/v1alpha1
kind: ServiceBinding
metadata:
  name: spring-petclinic-detect-all
  namespace: my-petclinic
spec:
  detectBindingResources: true
  services:
    - group: postgres-operator.crunchydata.com
      version: v1beta1
      kind: PostgresCluster
      name: hippo
  application:
    name: spring-petclinic
    group: apps
    version: v1
    resource: deployments

在上例中,PostgresCluster 自定义资源拥有一个或多个 Kubernetes 资源,如路由、服务、配置映射或 secret。

Service Binding Operator 会自动检测每个拥有的资源上公开的绑定数据。

6.6.2. 数据模型

注释中使用的数据模型遵循特定的惯例。

服务绑定注解必须使用以下约定:

service.binding(/<NAME>)?:
    "<VALUE>|(path=<JSONPATH_TEMPLATE>(,objectType=<OBJECT_TYPE>)?(,elementType=<ELEMENT_TYPE>)?(,sourceKey=<SOURCE_KEY>)?(,sourceValue=<SOURCE_VALUE>)?)"

其中:

<NAME>

指定要公开的绑定值的名称。只有在将 objectType 参数设置为 SecretConfigMap 时,才能将其排除。

<VALUE>

指定没有设置 path 时公开的常量值。

数据模型详细介绍了 路径elementTypeobjectTypesourceKeysourceValue 参数允许的值和语义。

表 6.4. 参数及其描述

参数描述默认值

path

包含以大括号 {} 括起的 JSONPath 表达式的 jsonpath 模板。

N/A

elementType

指定 path 参数中引用的元素值是否满足以下类型之一:

  • string
  • sliceOfStrings
  • sliceOfMaps

string

objectType

指定 path 参数中指示的元素值是否为当前命名空间中的 ConfigMapSecret 或纯文本。

Secret,如果 elementType 为非字符串。

sourceKey

指定在收集绑定数据时要添加到绑定 secret 的 ConfigMapSecret 中的键。

备注:

  • 当与 elementType=sliceOfMaps 一同使用时,sourceKey 参数指定映射中的键,其值为在绑定 secret 中用作键的片段。
  • 使用此可选参数在引用的 SecretConfigMap 资源中公开特定条目,作为绑定数据。
  • 如果没有指定,则公开来自 SecretConfigMap 资源的所有键和值,并添加到绑定 secret 中。

N/A

sourceValue

指定映射片段中的键。

备注:

  • 此键的值用作基础,用于生成添加到绑定 secret 的键值对条目值。
  • 另外,sourceKey 的值被用作添加到绑定 secret 的 key-value 对条目的键。
  • 只有在 elementType=sliceOfMaps 时才必须。

N/A

注意

只有在 path 参数中指示的元素引用 ConfigMapSecret 资源时,sourceKeysourceValue 参数才适用。

6.6.3. 将注解映射设置为可选

您可以在注解中带有可选字段。例如,如果服务端点不需要身份验证,则凭证的路径可能不存在。在这种情况下,注解的目标路径中会出现一个字段。因此,Service Binding Operator 默认会生成一个错误。

作为服务提供商,要指明是否需要注解映射,您可以在启用服务时为注解中的 optional 标记设置值。只有在目标路径可用时,Service Binding Operator 才会提供注解映射。当目标路径不可用时,Service Binding Operator 会跳过可选映射,并继续进行现有映射的预测,而不会抛出任何错误。

流程

  • 要在注解中创建一个字段,将 optional 标志值设置为 true

    Example

    apiVersion: apps.example.org/v1beta1
    kind: Database
    metadata:
      name: my-db
      namespace: my-petclinic
      annotations:
        service.binding/username: path={.spec.name},optional=true
    # ...

注意
  • 如果将 optional 标志值设为 false,并且 Service Binding Operator 无法找到目标路径,Operator 将无法注解映射。
  • 如果 optional 标志没有设置值,Service Binding Operator 会将值视为 false,并且注解映射会失败。

6.6.4. RBAC 要求

要使用 Service Binding Operator 来公开后备服务绑定数据,您需要特定的基于角色的访问控制(RBAC)权限。在 ClusterRole 资源的 rules 字段下指定特定的操作动词,以便为后备服务资源授予 RBAC 权限。在定义这些规则时,允许 Service Binding Operator 在整个集群中读取后备服务资源的绑定数据。如果用户没有读取绑定数据或修改应用程序资源的权限,Service Binding Operator 会阻止这样的用户将服务绑定到应用程序。遵循 RBAC 要求避免用户不必要的权限,并防止访问未经授权的服务或应用程序。

Service Binding Operator 使用专用服务帐户对 Kubernetes API 执行请求。默认情况下,此帐户具有将服务绑定到工作负载的权限,它们都由以下标准 Kubernetes 或 OpenShift 对象表示:

  • 部署
  • DaemonSets
  • ReplicaSet
  • StatefulSets
  • DeploymentConfig

Operator 服务帐户绑定到一个聚合的集群角色,允许 Operator 供应商或集群管理员启用将自定义服务资源绑定到工作负载。要在 ClusterRole 中授予所需的权限,请为它标上 servicebinding.io/controller 标志,并将标志值设为 true。以下示例演示了如何允许 Service Binding Operator getwatchlist Crunchy PostgreSQL Operator 的自定义资源(CR):

示例:启用到 Crunchy PostgreSQL Operator 置备的 PostgreSQL 数据库实例的绑定

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: postgrescluster-reader
  labels:
     servicebinding.io/controller: "true"
rules:
- apiGroups:
    - postgres-operator.crunchydata.com
  resources:
    - postgresclusters
  verbs:
    - get
    - watch
    - list
  ...

此集群角色可以在安装支持服务 Operator 的过程中部署。

6.6.5. 可公开绑定数据的类别

Service Binding Operator 可让您从后备服务资源和自定义资源定义 (CRD) 中公开绑定数据值。

本节提供了示例,以演示如何使用各种可混合绑定数据类别。您必须修改这些示例,以符合您的工作环境和要求。

6.6.5.1. 从资源公开字符串

以下示例演示了如何将 PostgresCluster 自定义资源 (CR) 的 metadata.name 字段中的字符串公开为用户名:

Example

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo
  namespace: my-petclinic
  annotations:
    service.binding/username: path={.metadata.name}
# ...

6.6.5.2. 将常量值作为绑定项目公开

以下示例演示了如何从 PostgresCluster 自定义资源(CR)公开一个常量值:

示例:公开一个常量值

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo
  namespace: my-petclinic
  annotations:
    "service.binding/type": "postgresql" 1

1
绑定 type 使用 postgresql 值被公开。

6.6.5.3. 公开从资源引用的整个配置映射或 secret

以下示例演示了如何通过注解公开整个 secret:

示例:通过注解公开整个 secret

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo
  namespace: my-petclinic
  annotations:
    service.binding: 'path={.metadata.name}-pguser-{.metadata.name},objectType=Secret'

示例:从后备服务资源引用的 secret

apiVersion: v1
kind: Secret
metadata:
  name: hippo-pguser-hippo
data:
  password: "<password>"
  user: "<username>"

6.6.5.4. 从一个配置映射或 secret(从一个资源指代) 中公开一个特定条目

以下示例演示了如何通过注解从配置映射中公开特定条目:

示例:通过注解从配置映射中公开条目

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo
  namespace: my-petclinic
  annotations:
    service.binding: 'path={.metadata.name}-config,objectType=ConfigMap,sourceKey=user'

示例:从后备服务资源引用的配置映射

绑定数据应具有名称为 db_timeout 的键,值为 10s

apiVersion: v1
kind: ConfigMap
metadata:
  name: hippo-config
data:
  db_timeout: "10s"
  user: "hippo"

6.6.5.5. 公开资源定义值

以下示例演示了如何通过注解公开资源定义值:

示例:通过注解公开资源定义值

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo
  namespace: my-petclinic
  annotations:
    service.binding/username: path={.metadata.name}
    ...

6.6.5.6. 使用每个条目的键和值公开集合条目

以下示例演示了如何通过注解使用每个条目的键和值公开集合条目:

示例:通过注解公开集合条目

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo
  namespace: my-petclinic
  annotations:
    "service.binding/uri": "path={.status.connections},elementType=sliceOfMaps,sourceKey=type,sourceValue=url"
spec:
# ...
status:
  connections:
    - type: primary
      url: primary.example.com
    - type: secondary
      url: secondary.example.com
    - type: '404'
      url: black-hole.example.com

以下示例演示了注解中集合的先前条目如何投射到绑定应用程序中。

示例:绑定数据文件

/bindings/<binding-name>/uri_primary => primary.example.com
/bindings/<binding-name>/uri_secondary => secondary.example.com
/bindings/<binding-name>/uri_404 => black-hole.example.com

示例:从后备服务资源配置

status:
  connections:
    - type: primary
      url: primary.example.com
    - type: secondary
      url: secondary.example.com
    - type: '404'
      url: black-hole.example.com

以上示例可帮助您使用键(如 primary, secondary)将这些值组织为项目。

6.6.5.7. 使用每个项目一个键公开集合的项目

以下示例演示了如何通过注解在各个项目中使用一个键来公开集合项目:

示例:通过注解公开集合项目

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo
  namespace: my-petclinic
  annotations:
    "service.binding/tags": "path={.spec.tags},elementType=sliceOfStrings"
spec:
    tags:
      - knowledge
      - is
      - power

以下示例演示了注解中集合的先前项目如何投射到绑定应用程序中。

示例:绑定数据文件

/bindings/<binding-name>/tags_0 => knowledge
/bindings/<binding-name>/tags_1 => is
/bindings/<binding-name>/tags_2 => power

示例:从后备服务资源配置

spec:
  tags:
  - knowledge
  - is
  - power

6.6.5.8. 每个条目使用一个键公开集合条目的值

以下示例演示了如何通过注解使用每个条目值的一个键公开集合条目的值:

示例:通过注解公开集合条目的值

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo
  namespace: my-petclinic
  annotations:
    "service.binding/url": "path={.spec.connections},elementType=sliceOfStrings,sourceValue=url"
spec:
  connections:
    - type: primary
      url: primary.example.com
    - type: secondary
      url: secondary.example.com
    - type: '404'
      url: black-hole.example.com

以下示例演示了注解中集合的先前值如何投射到绑定应用程序中。

示例:绑定数据文件

/bindings/<binding-name>/url_0 => primary.example.com
/bindings/<binding-name>/url_1 => secondary.example.com
/bindings/<binding-name>/url_2 => black-hole.example.com

6.6.6. 其他资源