This message was deleted.
# general
s
This message was deleted.
b
you can call it inside an
apply()
e
Okay. That makes sense, but what would that look like? Can you provide a code snippet to jostle my imagination? I’m struggling
I assume the apply would go in the
KubeConfig
property of the Kubernetes Provider. I just don’t know the proper was to create an
ApplyT
. Usually I am making them from a secret that already exists.
e
@billowy-army-68599 I’ve run into the same issue. Here is the error when I try to create the kubernetes provider later. The example you provided intends for the cluster and kubernetes resources to be created in different stacks. I want to do it in the same stack.
Error setting up kubeconfig
Copy code
kubeconfig, exists := ctx.GetConfig("kubeconfig")
if !exists {
	return nil, errors.New("Kubeconfig not found in context.")
}

return kubernetes.NewProvider(ctx, clusterName+"-kube-context-"+providerNameSuffix, &kubernetes.ProviderArgs{
	Cluster:    pulumi.StringPtr(clusterName),
	Kubeconfig: pulumi.StringPtr(kubeconfig),
}, opts...)
Hopefully I’m just being a dunce.
b
can you share the full code?
e
Let me know if this is comprehensive enough. I can extract it further if you need it. This won’t compile, but shows the gist of what I’m doing. I put the calling logic in
main()
and included other important functions.
b
I think i see what you're getting at - the
NewProvider
argument takes an input - is there any reason you're not passing the kubeconfig from listmanagedcredentials directly to it?
e
I tried that originally, but listimageadmincredentials errors because the cluster doesn’t exist yet. This doesn’t happen when the cluster exists. It’s a runtime exception that occurs during preview. This was my first attempt
Copy code
creds, err := containerservice.ListManagedClusterAdminCredentials(ctx, &containerservice.ListManagedClusterAdminCredentialsArgs{
	ResourceName:      clusterName,
	ResourceGroupName: pulumi.StringPtr(params.ClusterName + "-aks"),
})
if err != nil {
	return nil, errors.Wrap(err, "ListManagedClusterAdminCredentials failed to retrieve admin credentials")
}

configBytes, err := b64.StdEncoding.DecodeString(creds.Kubeconfigs[0].Value)
if err != nil {
	return nil, errors.Wrap(err, "Could not base64 decode kubeconfig file")
}

return kubernetes.NewProvider(ctx, clusterName+"-kube-context-"+providerNameSuffix, &kubernetes.ProviderArgs{
	Cluster:    pulumi.StringPtr(clusterName),
	Kubeconfig: pulumi.StringPtr(string(configBytes)),
}, opts...)
I tried implementing a check to see if the cluster exists using azure sdk for go. This allows
preview
to pass, but it fails on
up
. Looks like Pulumi isn’t running my custom code on demand, only during compilation (or whatever that terminology would be). This is a real bummer. I’m going to put in a feature request for this. If functions have
dependsOn
functionality, it should solve this problem. This is azure specific, but it could be a problem with other providers too. For example, DigitalOcean returns the kubeconfig file with the cluster response object. Maybe Pulumi would manages that situation better.
b
@gifted-vase-28337 it's a little unclear why you're doing a check to see if the cluster exists? If the cluster is in your code, Pulumi will drive towards it existing: it's declarative. You shouldn't need to check for the existence of things in this way
re: your earlier comment,
listManagedClusterAdminCrdenetials
will indeed error if the cluster doesn't exist, but if you're running it inside an
ApplyT
it will ALWAYS exists in that circumstance, because the Azure API has responded with the outputs
if you need some help understanding outputs, I wrote a blog post about it: https://www.leebriggs.co.uk/blog/2021/05/09/pulumi-apply.html
a lot of your code looks to be quite imperative, when it's not really needed
let me throw an example together
@enough-truck-34175 this does everything I think you need: https://github.com/jaxxstorm/pulumi-examples/blob/main/go/azure/aks-namespace/main.go
e
@billowy-army-68599 That worked very well for me. I swear, I tried that before, but I must have biffed something up. Thank you so much for the help!