Hi all, I'd like to use a resource's own auto-gene...
# general
d
Hi all, I'd like to use a resource's own auto-generated name in its properties. In this case, it's a k8s custom resource, but I think this applies to any provider. Is this feature available somehow?
m
The downward API could help you here, and you can always access a resource's name as an output of the instantiated resource even if the name is auto-generated. I don't think it is possible to refer to other resource properties while defining a resource, e.g.,
Copy code
field_a = "name"
field_b = self.field_a.value
or something like this. The only solution to this that I know is to generate a name yourself prior to creating the resource (e.g., using a random string), store it in a variable, and then use that variable to fill in the input values.
d
This is for a k8s custom resource, not a pod, so the downward API is not applicable. Specifically, we have this:
Copy code
apiVersion: <http://grafana.integreatly.org/v1beta1|grafana.integreatly.org/v1beta1>
kind: Grafana
metadata:
  name: grafana
  namespace: monitoring
spec:
  deployment:
    spec:
      template:
        spec:
          containers:
          - image: grafana/grafana:11.5.1
            name: grafana
          volumes:
          - name: grafana-data
            persistentVolumeClaim:
              claimName: grafana-pvc  
  persistentVolumeClaim: { ... }
where the
claimName
needs to be
${metadata.name}-pvc
. Which currently isn't possible with auto-named resources. IMO this could be available in
transformations
maybe (it currently isn't)? I'll open a feature request on GitHub for this.
m
So the auto-generated name is not in the args that are passed to the transform, right?
d
no, currently if I look up
args.props.metadata.name
, it comes up as
undefined
.
Oh wait, there is a separate
name
argument 🤦‍♂️
m
I've tried to figure out the order of operations in Pulumi (name generation, transform, resource instantiation) but I couldn't find it with a quick code search on GitHub.
d
> Oh wait, there is a separate
name
argument Ah, that just contains the Pulumi-level name, not including the auto-generated suffix 😕
m
Yes, my assumption is that
transform
runs on the args that you provide in your program, and then if the resource doesn't get a name as part of these args, a name is autogenerated.
You could verify this by injecting a name as part of the transform.
But I agree that it would be useful to be able to at least read the auto-generated name during a transform. If you file a GitHub issue, let me know and I'll subscribe and vote on it.
d