【问题标题】:How to edit a pulumi resource after it's been declared如何在声明后编辑 pulumi 资源
【发布时间】:2020-05-09 21:57:00
【问题描述】:

我已经声明了一个 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 中修改资源的模式是什么?如何向我的部署添加新卷?

【问题讨论】:

    标签: pulumi


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2019-08-09
      • 2019-03-08
      相关资源
      最近更新 更多