Packer google vpc 内云认证

Packer google cloud authentication within vpc

我们正在使用 Packer 在 GCP 计算实例中构建映像。 Packer尝试根据project和image抓取镜像如下:

https://www.googleapis.com/compute/v1/projects/<project-name>/global/images/<image-name>?alt=json

然后它抛出一个错误: oauth2: cannot fetch token: Post https://accounts.google.com/o/oauth2/token: dial tcp 108.177.111.84:443: i/o timeout

基于安全原则,我们的计算实例没有外部IP地址,因此无法访问互联网。在这种情况下,accounts.google.com 不再可访问。那么我们如何验证 google apis?

我尝试启用防火墙规则并提供互联网访问路由。但是根据 here 的要求,如果实例没有外部 IP 地址,它仍然无法访问。

这意味着我们必须有一种单独的方法来验证 googleapi。 但是 Packer 支持这个吗?

这是我们拥有的打包程序生成器:

"builders": [
    {
        "type": "googlecompute",
        "project_id": "test",
        "machine_type": "n1-standard-4",
        "source_image_family": "{{user `source_family`}}",
        "source_image": "{{user `source_image`}}",
        "source_image_project_id": "{{user `source_project_id`}}",
        "region": "{{user `region`}}",
        "zone": "{{user `zone`}}",
        "network": "{{user `network`}}",
        "subnetwork": "{{user `subnetwork`}}",
        "image_name": "test-{{timestamp}}",
        "disk_size": 10,
        "disk_type": "pd-ssd",
        "state_timeout": "5m",
        "ssh_username": "build",
        "ssh_timeout": "1000s",
        "ssh_private_key_file": "./gcp-instance-key.pem",
        "service_account_email": "test-account@test-mine.iam.gserviceaccount.com",
        "omit_external_ip": true,
        "use_internal_ip": true,
        "metadata": {
            "user": "build"
        }
    }

要手动执行您想要的操作,您需要在项目内的工作计算实例上或在网络上启用了对等互连的 vpc 上打开 ssh 隧道,您要访问的计算是。

如果您随后将 CI 与 gitlab-ci 等运行器一起使用,请务必在同一 vpc 或具有对等互连的 vpc 中创建运行器。

如果您不想使用外部 ip 创建计算,您可以尝试打开到项目的 vpn 连接并通过 vpn 进行。