如何使用 cdktf 加载本地文件以进行 helm 发布?

Pay*_*ian 3 terraform helm3 terraform-cdk

我想在使用 cdktf 创建 helm 图表时引用本地 yaml 文件。

\n

我有以下 cdktf 配置:

\n
{\n  "language": "typescript",\n  "app": "npx ts-node main.ts",\n  "projectId": "...",\n  "terraformProviders": [\n    "hashicorp/aws@~> 3.42",\n    "hashicorp/kubernetes@ ~> 2.7.0",\n    "hashicorp/http@ ~> 2.1.0",\n    "hashicorp/tls@ ~> 3.1.0",\n    "hashicorp/helm@ ~> 2.4.1",\n    "hashicorp/random@ ~> 3.1.0",\n    "gavinbunney/kubectl@ ~> 1.14.0"\n  ],\n  "terraformModules": [\n    {\n      "name": "secrets-store-csi",\n      "source": "app.terraform.io/goldsky/secrets-store-csi/aws",\n      "version": "0.1.5"\n    }\n  ],\n  "context": {\n    "excludeStackIdFromLogicalIds": "true",\n    "allowSepCharsInLogicalIds": "true"\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

请注意npx ts-node main.ts作为应用程序。

\n

在 main.ts 中我有以下 helm 版本

\n
    new helm.Release(this, "datadog-agent", {\n      chart: "datadog",\n      name: "datadog",\n      repository: "https://helm.datadoghq.com",\n      version: "3.1.3",\n      set: [\n        {\n          name: "datadog.clusterChecks.enabled",\n          value: "true",\n        },\n        {\n          name: "clusterAgent.enabled",\n          value: "true"\n        },\n      ],\n      values: ["${file(\\"datadog-values.yaml\\")}"],\n    });\n
Run Code Online (Sandbox Code Playgroud)\n

请注意,我引用了来自 helm 提供程序的datadog-values.yaml类似于此示例的yaml 文件。

\n

datadog-values.yaml是一个姐妹文件main.ts

\n

在此输入图像描述

\n

\n

但是,当我尝试部署它时,cdktf deploy出现以下错误

\n
\xe2\x94\x82 Error: Invalid function argument\n\xe2\x94\x82\n\xe2\x94\x82   on cdk.tf.json line 1017, in resource.helm_release.datadog-agent.values:\n\xe2\x94\x82 1017:           "${file(\\"datadog-values.yaml\\")}"\n\xe2\x94\x82\n\xe2\x94\x82 Invalid value for "path" parameter: no file exists at\n\xe2\x94\x82 "datadog-values.yaml"; this function works only with files that are\n\xe2\x94\x82 distributed as part of the configuration source code, so if this file will\n\xe2\x94\x82 be created by a resource in this configuration you must instead obtain this\ngoldsky-infra-dev  \xe2\x95\xb7\n                   \xe2\x94\x82 Error: Invalid function argument\n                   \xe2\x94\x82\n                   \xe2\x94\x82   on cdk.tf.json line 1017, in resource.helm_release.datadog-agent (datadog-agent).values:\n                   \xe2\x94\x82 1017:           "${file(\\"datadog-values.yaml\\")}"\n                   \xe2\x94\x82\n                   \xe2\x94\x82 Invalid value for "path" parameter: no file exists at\n                   \xe2\x94\x82 "datadog-values.yaml"; this function works only with files that are\n                   \xe2\x94\x82 distributed as part of the configuration source code, so if this file will\n                   \xe2\x94\x82 be created by a resource in this configuration you must instead obtain this\n                   \xe2\x94\x82 result from an attribute of that resource.\n
Run Code Online (Sandbox Code Playgroud)\n

要运行部署,我执行npm run deploy:dev以下命令中的客户脚本package.json:

\n
    "build": "tsc",\n    "deploy:dev": "npm run build && npx cdktf deploy",\n
Run Code Online (Sandbox Code Playgroud)\n

如何在 helm 版本中引用我的 datadog yaml 文件,如helm 提供程序所示的示例?

\n

jav*_*lga 6

要引用 CDKTF 中的本地文件,需要使用asset。假设在项目的根级别有一个值文件夹,您可以在其中存储值 yaml 文件:

     const valuesAsset = new TerraformAsset(this, 'values-asset', {
        path: `${process.cwd()}/values/${this.chartValues}`,
        type: AssetType.FILE,
      });

      new HelmRelease(this, 'helm-release', {
        name: this.releaseName,
        chart: this.chartName,
        repository: this.chartRepository,
        values: [ Fn.file(valuesAsset.path) ]
      })
    }
Run Code Online (Sandbox Code Playgroud)

请注意,我使用了file Terraform 函数来读取文件的内容。