Is there a way for me to generate YAML out of Pulu...
# typescript
g
Is there a way for me to generate YAML out of Pulumi kubernetes object without actually creating that specific resource into the cluster? I know this is a very specific request but the reason I would need this is that a 3rd party app ran on our k8s cluster supports providing a job template yaml to it as a file (yes, as a file, it doesn't support ConfigMaps etc). I would like to generate that file based on other cluster data that Pulumi is controlling. Generating a valid YAML file out of Pulumi resources with
Output
abstraction is not a trivial task and I would assume Pulumi is actually under the hood generating YAML anyway from the definitions it would make sense if one could also export that data out somehow. However, I think this is not possible since as far as I can see it, Pulumi automatically registers a resource to state when you say
new <resourcetype
and you can't override this functionality, right? If the answer to my question is what I think it is then my follow up question is: is there a ready tool or library function that could take in TypeScript data (objects, arrays etc) with Pulumi
Output
and it would return a valid YAML string with
Output
values properly populated? If the answer to that one is "no" then I have one additional question: how can I reliably generate a string dynamically recursively in such a way that
Output
objects are handled correctly? I actually wrote this simple "data to YAML" converter where I tried to handle the
Output
objects correctly but unfortunately it doesn't seem to work that well...
b
g
Cool! So, how does it actually work? Doesn't look like self-explanatory to me. "to directory" - so what does it do? Render it to a yaml-file, not to a a string?
or
Output<string>
which I would prefer in this case
Maybe I'll just try it out 😁
Ah right, now I got it. I think this won't do it for me unfortunately
Guess I'll go via a route where I have a separate YAML file with some string keys which I'll replace via regexp in
.apply
for the `Output`s I need...
Not exactly pretty
s
You can construct a JavaScript object representing the structure you want, and serialise it once those outputs are known.
Strictly better than file templating.
g
@stocky-spoon-28903 yes, definitely. But how? I mean, it's trivial to generate YAML, but with
Output
objects involved it isn't anymore. Let's assume I have a longish data structure with tens of Outputs. The only way I can think of that this could be done is to list all of the Outputs in
pulumi.all
and then use that to apply the values at the same time and then return the result. To me this feels messy and hard to read. I would much prefer that I could just place the Output values directly into the data structure where they belong to and call a single function to evaluate it in such a way that all Outputs within it are realized. Then I could just serialize that to YAML without issues
I actually tried to do this. Take a data structure in, search for all Output objects, use those as pulumi.all parameters and then go replace the realized Output values in correct places in the data structure. Couldn't quite get it working and it was rather hard to debug so I kinda gave up
But that approach should work at least in theory
s
Copy code
const manifest = pulumi.output({
    key: valueThatIsOutput,
    nested: {
        key2: “prompt value”,
        key3: valueThatIsOutput
    },
}).apply(JSON.stringify)
You don’t need to reify each thing individually (it would be pretty unforgivable if you did need to do that and there wasn’t a runtime library function to do it!)
g
Hmm. I did try that obviously and that did not work
I was surprised and asked some help earlier here regarding it
Need to revisit this