Skip to content

本文为 SPIRE 官方文档 plugin_server_bundlepublisher_k8s_configmap 的中文译本,以英文原文为准。 🔗 穿越源码分析:Server 详解 · BundlePublisher

Server 插件:BundlePublisher "k8s_configmap"

k8s_configmap 插件将 server 当前的信任包(trust bundle)存入指定的 Kubernetes ConfigMap,并保持其持续更新。该插件支持配置多个集群。

该插件接受以下配置:

配置项说明默认值
clusters集群的映射(map),以任意 ID 作为键,插件会将当前信任包发布到这些集群。

WARNING

clusters 为空时,插件不会发布信任包。

主配置中的每个集群具有以下配置选项:

配置项说明是否必填默认值
configmap_nameConfigMap 的名称。是。
configmap_keyConfigMap 中存放证书包的键(key)。是。
namespaceConfigMap 所在的命名空间(namespace)。是。
kubeconfig_path磁盘上 kubeconfig 的路径,其中包含用于与 Kubernetes API server 交互的配置。若未设置,则使用集群内(in-cluster)凭据。否。
format信任包的存储格式,<spiffe | jwks | pem>。详见支持的信任包格式是。
refresh_hint使用 spiffe 格式时,设置信任包的刷新提示(refresh hint)。以字符串指定,例如 '10m'、'1h'。详见 time.ParseDuration否。

支持的信任包格式

支持以下信任包格式:

SPIFFE 格式

信任包表示为符合 RFC 7517 的 JWK Set,其具体参数在 SPIFFE Trust Domain and Bundle 规范中定义。其中同时包含 JWT authorities 与 X.509 authorities。

JWKS 格式

信任包编码为符合 RFC 7517 的 JWK Set,但省略了 SPIFFE 专有参数。其中同时包含 JWT authorities 与 X.509 authorities。

PEM 格式

信任包使用 PEM 编码格式。其中仅包含 X.509 authorities。

配置 Kubernetes

要使用该插件,需要为 SPIRE Server 的 Service Account 配置 Kubernetes 权限:

  • 对于集群内(in-cluster)的 SPIRE server:向运行 SPIRE 的 Service Account 授予权限。
  • 对于集群外(out-of-cluster)的 SPIRE server:向 kubeconfig 中指定的 Service Account 授予权限。

该插件使用 Kubernetes 的 Apply 操作来管理 ConfigMap。该操作会在 ConfigMap 不存在时创建它,存在时则更新它。Service Account 需要具备在指定命名空间中对 ConfigMap 使用 patch 动词的权限。

所需权限

Service Account 需要以下权限:

  • 对 ConfigMap 的 get(Apply 操作读取当前状态时所需)
  • 对 ConfigMap 的 patch(Apply 操作更新资源时所需)
  • 对 ConfigMap 的 create(当 ConfigMap 不存在时所需)

示例

在本例中,假设 Service Account 为 spire-server

yaml
kind: Role # 注意:此处使用 Role 而非 ClusterRole,以获得命名空间级别的权限
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: spire-server-role
  namespace: spire
rules:
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["create", "get", "patch"]
  resourceNames: ["spire-bundle"]  # 将 create、get 和 patch 操作限制到特定的 ConfigMap

---

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: spire-server-role-binding
  namespace: spire
subjects:
- kind: ServiceAccount
  name: spire-server
  namespace: spire
roleRef:
  kind: Role
  name: spire-server-role
  apiGroup: rbac.authorization.k8s.io

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: spire-bundle
  namespace: spire

NOTE

Apply 操作使用服务端应用(Server-Side Apply,SSA),字段管理者(field manager)名称为 spire-bundlepublisher-k8s_configmap。这确保了 SPIRE 对 ConfigMap 的更新可被追踪,并能够与可能管理同一 ConfigMap 中其他字段的其他控制器共存。

示例配置

以下配置将本地信任包在两个不同集群的 ConfigMap 中保持更新。

hcl
    BundlePublisher "k8s_configmap" {
        plugin_data {
            clusters = {
                "example-cluster-1" = {
                    configmap_name = "example.org"
                    configmap_key = "bundle"
                    namespace = "spire"
                    kubeconfig_path = "/file/path/cluster-1"
                    format = "spiffe"
                },
                "example-cluster-2" = {
                    configmap_name = "example.org"
                    configmap_key = "bundle"
                    namespace = "spire"
                    kubeconfig_path = "/file/path/cluster-2"
                    format = "pem"
                }
            }
        }
    }