Home > Net >  Pulumi kubernetes ConfigGroup takes string, I only got Outputs
Pulumi kubernetes ConfigGroup takes string, I only got Outputs

Time:12-05

I need to supply a Configgroup with yaml. The yaml needs to be populated with values from Pulumi Outputs. My problem is that the yaml field in the code below only take strings and I cannot figure a way to create this string from Outputs.

For example, imagine taking an Id of sorts from an Output and replacing foo.

Any ideas?

import * as k8s from "@pulumi/kubernetes";

const example = new k8s.yaml.ConfigGroup("example", {
    yaml: `
          apiVersion: v1
          kind: Namespace
          metadata:
             name: foo
`,
})

CodePudding user response:

Any time you're needing to use an output value inside a string, you'll need to make sure the output is resolved using an apply.

In your case, it'd look a little bit like this:

import * as k8s from "@pulumi/kubernetes";
import * as pulumi from "@pulumi/pulumi";

const nsName = pulumi.output("foo")

nsName.apply(name => {
    const example = new k8s.yaml.ConfigGroup("example", {
        yaml: `
        apiVersion: v1
        kind: Namespace
        metadata:
           name: ${name}
        `
    })
})

However, it's usually not desirable to create resources inside an apply like this, because it means previews aren't accurate. If you're just trying to create a namespace, it'd be highly recommend to use the kubernetes provider's namespace resource, instead of ConfigGroup (although I recognise you're trying to get a working example probably for something else)

You might consider using kube2pulumi to convert whatever YAML you're installing, because once you're using the standard types, you can pass outputs to fields easily, like so:

const newNs = new k8s.core.v1.Namespace("example", {
    metadata: {
        name: nsName
    }
})
  • Related