声明后如何编辑 pulumi 资源

How to edit a pulumi resource after it's been declared

我已经声明了一个 kubernetes 部署:

const ledgerDeployment = new k8s.extensions.v1beta1.Deployment("ledger", {
  spec: {
    template: {
      metadata: {
        labels: {name: "ledger"},
        name: "ledger",
        // namespace: namespace,
      },
      spec: {
        containers: [
          ...
        ],

        volumes: [

          {
            emptyDir: {},
            name: "gunicorn-socket-dir"
          }
        ]
      }
    }
  }
});

稍后在我的 index.ts 中,我想有条件地修改部署的 volumes。我认为这是 pulumi 的一个怪癖,我还没有全神贯注,但这是我目前的尝试:

if(myCondition) {
  ledgerDeployment.spec.template.spec.volumes.apply(volumes =>
    volumes.push(
    {
      name: "certificates",
      secret: {
        items: [
          {key: "tls.key", path: "proxykey"},
          {key: "tls.crt", path: "proxycert"}],
        secretName: "star.builds.qwil.co"
      }
    })
  )
)

执行此操作时出现以下错误:Property 'mode' is missing in type '{ key: string; path: string; }' but required in type 'KeyToPath'

我怀疑我使用 apply 不正确。当我尝试直接修改 ledgerDeployment.spec.template.spec.volumes.push() 时,出现错误 Property 'push' does not exist on type 'Output<Volume[]>'.

Pulumi修改资源的模式是什么?如何向我的部署添加新卷?

创建资源后无法修改资源输入。相反,您应该在调用构造函数之前放置定义输入形状的所有逻辑。

在您的示例中,这可能是:

let volumes = [
  {
    emptyDir: {},
    name: "gunicorn-socket-dir"
  }
]
if (myCondition) {
  volumes.push({...});
}
const ledgerDeployment = new k8s.extensions.v1beta1.Deployment("ledger", {
  // <-- use `volumes` here
});