https://pulumi.com logo
#kubernetes
Title
# kubernetes
l

limited-rainbow-51650

09/08/2020, 6:36 PM
How can I implement a Pulumi ResourceTransformation callback function in a type-safe manner to modify the container spec for a
StatefulSet
? This is what I have so far:
Copy code
attachPostgreSQLConfiguration(args: pulumi.ResourceTransformationArgs): pulumi.ResourceTransformationResult | undefined {
        let props: pulumi.Input<kubernetes.types.input.apps.v1.StatefulSet> = args.props;
        let env : kubernetes.types.input.core.v1.EnvVar[] = props.spec.template.spec.containers[0].env;
        env = env.concat([
            {
                name: 'ORTHANC__POSTGRESQL__ENABLE_SSL',
                value: 'true'
            }
        ]);
        <http://pulumi.log.info|pulumi.log.info>(`In psql plugin transformation: ${util.inspect(env)}`)
        props.spec.template.spec.containers[0].env = env
        return { props: props, opts: args.opts }
    }
But I get a TypeScript error on
props.spec
saying
Object is possibly 'undefined'
g

gorgeous-egg-16927

09/08/2020, 7:15 PM
I haven’t tested it, but I’m pretty sure
pulumi.ResourceTransformationArgs
is incorrect for k8s (the general transformations API didn’t exist when k8s support was implemented). You should double-check that type
l

limited-rainbow-51650

09/08/2020, 7:17 PM
Tnx!!
@gorgeous-egg-16927 this is what I currently have:
Copy code
getTransformation(): pulumi.ResourceTransformation {
        return (args) => {
            return this.attachPostgreSQLConfiguration(args)
        }
    }

    attachPostgreSQLConfiguration(args: pulumi.ResourceTransformationArgs): pulumi.ResourceTransformationResult | undefined {
        let props = args.props;
        let env: kubernetes.types.input.core.v1.EnvVar[] = props.spec.template.spec.containers[0].env;
        env = env.concat([
            {
                name: 'ORTHANC__POSTGRESQL__HOST',
                value: this.databaseHost
            },
            {
                name: 'ORTHANC__POSTGRESQL__DATABASE',
                value: this.databaseName
            },
            {
                name: 'ORTHANC__POSTGRESQL__USERNAME',
                value: this.databaseUser
            },
            {
                name: 'ORTHANC__POSTGRESQL__PASSWORD',
                value: this.databasePassword
            },
            {
                name: 'ORTHANC__POSTGRESQL__ENABLE_SSL',
                value: 'true'
            }
        ]);
        props.spec.template.spec.containers[0].env = env
        <http://pulumi.log.info|pulumi.log.info>(`env: ${util.inspect(env)}`)
        // return { props: props, opts: args.opts }
        return undefined
    }
Even if I return
undefined
, my resource spec is transformed. I am modifying my input args instead of copying+modifying. But given what I read in the docs, the transformation should only be applied when I return a
ResourceTransformationResult
object, no?
g

gorgeous-egg-16927

09/08/2020, 7:35 PM
IIRC, the return value is ignored; i.e. the object is modified in place during the transformation
l

limited-rainbow-51650

09/08/2020, 7:35 PM
If the transformation returns undefined, this indicates that the resource will not be transformed.
https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/pulumi/#ResourceTransformation
g

gorgeous-egg-16927

09/08/2020, 7:36 PM
Yeah, that’s a separate API
l

limited-rainbow-51650

09/08/2020, 7:36 PM
What do you mean? That is the one I’m using…
g

gorgeous-egg-16927

09/08/2020, 7:37 PM
Oh, really? I’ve never actually tried to use that with k8s, and was under the impression that it didn’t work.
l

limited-rainbow-51650

09/08/2020, 7:41 PM
@gorgeous-egg-16927 still, you are confusing me. The
ResourceTransformation
type (in TypeScript) is the type definition of the callback function you can pass to the
transformations
property of in the
ResourceOptions
hash of each Pulumi resource. For me, this is the API. Can you tell me what you mean with that’s a separate API ?
g

gorgeous-egg-16927

09/08/2020, 7:43 PM
The k8s provider has several resources (Kustomize, Helm, YAML) with a
transformations
field that works differently from the general purpose
ResourceTransformation
API because it didn’t exist when those k8s resources were originally implemented. https://www.pulumi.com/docs/reference/pkg/kubernetes/helm/v3/chart/#transformations_nodejs
l

limited-rainbow-51650

09/08/2020, 7:56 PM
OK, but a transformation on a
StatefulSet
follows the regular Pulumi resource transformation model, right? Coming back to my remark, the typespec of a
ResourceTransformation
mentions that if you return
undefined
, no transformation will be applied. Still, my spec is changed because I’m modifying the input args it seems. This contradicts the docs. Is this a bug then?
g

gorgeous-egg-16927

09/08/2020, 8:06 PM
Oh, I just realized that I misunderstood your original question; sorry for the confusion. You’re transforming the StatefulSet directly rather than through a ConfigFile/Chart. I haven’t tried it, but it sounds like it could be a bug based on the docs.
l

limited-rainbow-51650

09/08/2020, 8:06 PM
Why use charts if you have Pulumi? 😂
g

gorgeous-egg-16927

09/08/2020, 8:07 PM
lol, you have no idea how much I wish that were the case
l

limited-rainbow-51650

09/08/2020, 8:15 PM
@gorgeous-egg-16927 if I don’t modify the input args, but modify a deep clone of the input props, the return value is correct with what is written in the docs (
undefined
does nothing). But when I modify the input args, changes are applied anyhow. (https://github.com/pvorb/clone)
5 Views