pulumi

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


I've declared a kubernetes deployment like:

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"
          }
        ]
      }
    }
  }
});

Later on in my index.ts I want to conditionally modify the volumes of the deployment. I think this is a quirk of pulumi I haven't wrapped my head around but here's my current attempt:

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"
      }
    })
  )
)

When I do this I get the following error: Property 'mode' is missing in type '{ key: string; path: string; }' but required in type 'KeyToPath'

I suspect I'm using apply incorrectly. When I try to directly modify ledgerDeployment.spec.template.spec.volumes.push() I get an error Property 'push' does not exist on type 'Output<Volume[]>'.

What is the pattern for modifying resources in Pulumi? How can I add a new volume to my deployment?


Solution

  • It's not possible to modify the resource inputs after you created the resource. Instead, you should place all the logic that defines the shape of inputs before you call the constructor.

    In your example, this could be:

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