无法授予对 GCE 实例的只读 GCR 访问权限

Cannot grant read-only GCR access to GCE instance

我有:

我似乎无法授予对该实例的只读 GCR 访问权限。所有资源都在同一个项目中。我已尝试向服务帐户授予 storage.adminstorage.objectViewer 角色,并且在每种情况下访问容器注册表都会导致 https://eu.gcr.io/v2/<project>/<image>/manifests/latest: 401 Unauthorized.

我读过的所有文档都说我需要 storage-rw 范围和 storage.objectViewer 授予该区域的 GCR 存储桶,我拥有所有这些。

我正在使用 Terraform 配置所有这些。您如何使用 Terraform 配置 GCR 对服务帐户的访问权限?

这是我的代码:

locals {
  gcr-region = "eu"
}

resource "google_service_account" "service_account" {
    account_id = "k3s-master"
    display_name = "k3s-master"
}

resource "google_project_iam_member" "project" {
  role    = "roles/logging.logWriter"
  member = "serviceAccount:${google_service_account.service_account.email}"
}

resource "google_compute_instance" "instance" {
    name = "${var.service_name}"
    machine_type = "f1-micro"

    allow_stopping_for_update = true

    service_account {
        email = "${google_service_account.service_account.email}"
        scopes = [
            "storage-rw",
      "https://www.googleapis.com/auth/cloud-platform",
      "https://www.googleapis.com/auth/compute",
        ]
    }

    boot_disk {
        initialize_params {
            image = "debian-9"
        }
    }

    network_interface {
        network = "${google_compute_network.k3s-network.self_link}"
        access_config { }
    }
}

resource "google_storage_bucket_iam_binding" "eu-binding" {
  bucket     = "eu.artifacts.${var.project}.appspot.com"
  role        = "roles/storage.objectViewer"

  members = [
    "serviceAccount:${google_service_account.service_account.email}",
  ]
}

resource "google_storage_bucket_iam_binding" "binding" {
  bucket     = "${var.project}.appspot.com"
  role        = "roles/storage.objectViewer"

  members = [
    "serviceAccount:${google_service_account.service_account.email}",
  ]
}

没错,这完全是另外一回事。

为了进一步调试,我使用了计算实例中的 curlgsutil,发现我 可以 访问底层存储桶。

我是 运行 k3s 所涉及的实例,这需要明确配置为使用服务帐户对 GCR 进行身份验证(即使计算实例 运行 在同一服务帐户下,这仍然是必需的)。

因此,kubectl create secret docker-registry ... 然后为我正在使用的 (kubernetes) 服务帐户设置 imagePullSecrets 修复了它。 More details about that here.