Using random resource in plans with Go? #9207
-
There are very few examples to go on with applying a resource like random to use as input for random logical names in further steps in Pulumi. My use case:
My thought was to do:
This would require:
I'm having no luck so far. The syntax is pretty foreign and reminds me sort of delegate expressions in C#. Attempting to work my way through it hasn't succeeded so far. Exampleimport (
kubernetes "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes"
random "github.com/pulumi/pulumi-random/sdk/v4/go/random"
pulumi "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
config "github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"
)
for _, cluster := range clusterList {
pargs := &kubernetes.ProviderArgs{}
pargs.Namespace = pulumi.String(sharedConfig.Namespace)
pet, err := random.NewRandomPet(ctx, "random-pet"+cluster.Name, &random.RandomPetArgs{
Keepers: pulumi.Map{"cluster": pulumi.String(cluster.Name)},
Length: pulumi.Int(2),
Prefix: pulumi.String("-"),
Separator: pulumi.String("-"),
}, nil)
if err != nil {
return err
}
// I've tried combinations of this, no string conversion and others, and can't seem to get this to work reasonably.
randompet := pet.ID().ApplyT(func(result string) (string, error) {
return fmt.Sprintf("%v", result), nil
}).(pulumi.StringOutput).ToStringOutput()
ctx.Export("randompet-"+cluster.Name, randompet)
_ = ctx.Log.Info(fmt.Sprintf("petname: %+v", pet.ID()), &pulumi.LogArgs{})
// ... now pass this into functions as a string to consume to help build the logical names uniquely for each resource created in the cluster/namespace.
} I can't seem to get this to work like I need it and would really appreciate some examples of using the random petname/strings in practice for things like this multi-cluster type of deployment. Even with the operator, I'm not sure if it would solve the issue of unique URNs that is required when using state based tracking so regardless I think having more random resources in my plan will help ensure uniqueness. My Current WorkaroundUntil I get this figured out, I added a random petname to the yaml config, load into the struct and then provide it in code explicitly like: clustersuffix := "-" + cluster.Petname // petname: itchy-elephant, etc
err = ingress.NetworkingIngress(ctx, clustersuffix, renderProvider, &configData, &sharedConfig)
if err != nil {
return err
}
// ingress.go func
_, err = networkingv1.NewIngress(
ctx,
configData.ServiceDeployName(sharedConfig, "ingress")+pet, // can't join stringoutput here, must be string.
&networkingv1.IngressArgs{
},
), pulumi.Provider(prov),
)
//...
} Links |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Resource's logical names currently have to be There are three things you could do:
|
Beta Was this translation helpful? Give feedback.
Resource's logical names currently have to be
string
, notOutput<string>
(see #5234). So that limits the use of the random provider for names a lot.There are three things you could do:
preview
time because preview won't initially run the code inside apply butup
will.