Hi all! Quick question: How can I use mixin-type ...
# getting-started
c
Hi all! Quick question: How can I use mixin-type functionality with Pulumi? What I want is something along these lines: Object + feature1 + feature2 For instance, that would then become Deployment + secret1 + mountVolume (context here is Kubernetes, but it doesn't really matter). For this to work, I need to extend original objects by changing their properties. I noted that Pulumi transforms all input immediately into Output variables, meaning I can't change them any more. Is what I am looking for possible in the first place? I am coming from Tanka and was hoping to replicate its mixin behaviour in Pulumi. Thank you in advance!
e
I don't think we'd recommend mixins for this, but component resources: https://www.pulumi.com/docs/concepts/resources/components/
c
@echoing-dinner-19531 component resources are still quite unflexible, as they are supposed to instantiate everything inside of their constructor. One option is to have a fluent interface and then produce a ComponentResource only when materialising the tree
e
Maybe I'm misunderstanding what your asking for with mixins? My understanding of Tanka is it lets you compose multiple resource declarations into one with imports, and I'd think that looks a lot like a component resource, or even just having some helper functions to create those resources.
c
@echoing-dinner-19531 almost. Tanka lets you transform the JSON tree as you see fit before it materialises it and pushes it to Kubernetes. The transformation is really very flexible, let me show you how this looks like for me:
Copy code
u.applyFeatures(timescale,
            [
                u.mountConfigToFileMixin(...),
                u.injectSecretsAsEnv(...),
                u.injectConfigAsEnvMixin(n...),
                u.requestAndMountPersistentVolume(....)
            ])
The functions take the JSON, reconfigure it to add some feature and return the result - so I can chain them as I see fit. To this end, I do not need to sub class any components, I just operate "on top". This is -imho - quite lean, I was looking for this kind of transformation in Pulumi. ... the important point here is that I am transforming an existing tree
e
Stack transformations might cover some of this.
c
OK let me have a look 🙂
@echoing-dinner-19531 do you have some automatic wrappers in place that allow you to inject objects? For instance, if I want to make a secret available as an environment variable in a container, is there a direct way to do so? (this would be one application of the mixin I was describing above) So what I am looking for is:
Copy code
mainContainer = ContainerArgs(
name="timescale",                                image=...,
env_from=[                                      #EnvFromSourceArgs(secret_ref=SecretEnvSourceArgs(name=name)),                                      secrets.to_container_env()
]
...
The first line (commented with #) is the Pulumi way I currently know - quite roundabout. The second one is the shortcut I just built. .... is there a native equivalent for exactly this sort of thing? Another example of the same problem: I have a PVC which I want to mount into a container. Exactly the same story - doing that "natively" is quite verbose
(for instance, automated conversion constructors would enable exactly this kind of behaviour. Haven't seen them yet though)
e
do you have some automatic wrappers in place that allow you to inject objects?
Nothing built in, but as you've shown given this is just python code it's pretty simple to write functions to do what you want. Might be worth raising an issue at https://github.com/pulumi/pulumi-kubernetes/issues with some more details to see if it makes sense for some of these things to be built in, e.g. maybe env_from should just accept a secret object and automatically turn that into secret_ref under the hood (maybe, I'm not a k8s experts to say).