使用 Terraform 创建的 Kubernetes 服务帐户导致副本集 'doc is missing path: "/spec/volumes/0"' 错误

Kubernetes Service Account Created with Terraform causes 'doc is missing path: "/spec/volumes/0"' Error for Replica Set

我正在尝试使用关联的 ServiceAccount 创建 Kubernetes 部署,即 linked to an AWS IAM role。此 yaml 会产生所需的结果,并且相关的部署(包含在底部)会正确启动:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: service-account
  namespace: example
  annotations:
    eks.amazonaws.com/role-arn: ROLE_ARN

但是,我想改用 Terraform Kubernetes 提供程序来创建 ServiceAccount:

resource "kubernetes_service_account" "this" {
  metadata {
    name = "service-account2"
    namespace = "example"
    annotations = {
      "eks.amazonaws.com/role-arn" = "ROLE_ARN"
    }
  }
}

不幸的是,当我以这种方式创建 ServiceAccount 时,我部署的 ReplicaSet 失败并出现错误:

Error creating: Internal error occurred: Internal error occurred: jsonpatch add operation does not apply: doc is missing path: "/spec/volumes/0"

我已经确认 Deployment 是通过 Terraform 还是通过 kubectl 创建的并不重要;它不适用于 Terraform 创建的 service-account2,但适用于 kubectl 创建的 service-account。在 service-accountservice-account2 之间来回切换部署会相应地使其按您预期的那样工作或不工作。

我也确定了 eks.amazonaws.com/role-arn 是相关的; creating/assigning 不尝试 link 返回 IAM 角色的 ServiceAccounts 工作,无论它们是通过 Terraform 还是 kubectl.

创建的

使用 kubectl 来描述 Deployment、ReplicaSet、ServiceAccount 和关联的 Secret,我没有看到任何明显的差异,但我承认我并不完全确定我可能在寻找什么。

这是一个展示问题的简单部署 yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: example
  namespace: example
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: example
    spec:
      serviceAccountName: service-account # or "service-account2"
      containers:
      - name: nginx
        image: nginx:1.7.8

automountServiceAccountToken: true 添加到部署中的 pod 规范应该可以修复此错误。这通常在服务帐户上默认启用,但 Terraform 默认将其关闭。在将所需环境变量添加到 pods 的 mutating web hook 上查看此问题:https://github.com/aws/amazon-eks-pod-identity-webhook/issues/17

我有同样的问题,我在 terraform kubernetes 服务帐户资源中指定 automount_service_account_token = true 解决了它。

尝试创建以下服务帐户:

resource "kubernetes_service_account" "this" {
  metadata {
    name = "service-account2"
    namespace = "example"
    annotations = {
      "eks.amazonaws.com/role-arn" = "ROLE_ARN"
    }
  }
  automount_service_account_token = true
}