I want to access my KUBE_CONFIG while running (AKS...
# general
m
I want to access my KUBE_CONFIG while running (AKS created before this method is called). Will this achieve what I want? I need to make sure the env variable is set before I get to the next step where the Kubernetes provider is attempting to talk to the cluster. (Code in comment to avoid spamming).
Copy code
func SetEnvKubeConfig(ctx *pulumi.Context, config *configuration.Configuration, cluster *containerservice.ManagedCluster) error {
	credentials := containerservice.ListManagedClusterAdminCredentialsOutput(ctx, containerservice.ListManagedClusterAdminCredentialsOutputArgs{
		ResourceGroupName: config.ResourceGroup.Name,
		ResourceName:      cluster.Name,
	})

	kubeConfig := credentials.Kubeconfigs().Index(<http://pulumi.Int|pulumi.Int>(0)).Value().ApplyT(func(encode string) string {
		kubeConfig, err := base64.StdEncoding.DecodeString(encode)
		if err != nil {
			return ""
		}
		return string(kubeConfig)
	})

	ctx.Export("KUBE_CONFIG", kubeConfig)
	
	outputKubeConfig, exists := ctx.GetConfig("KUBE_CONFIG")
	if !exists {
		return errors.New("unknown context key KUBE_CONFIG")
	}
	
	_ = os.Setenv("KUBE_CONFIG", outputKubeConfig)

	return nil
}
b
is there a reason it needs to be an env var?
m
b
are you just trying to set up a kubernetes provider to install things in a clister you've created with Pulumi?
m
Correct
b
you don't need to export the kubeconfig variable in that case, use a Kubernetes provider: https://www.pulumi.com/registry/packages/kubernetes/api-docs/provider/
you can pass that to each resource you create in your cluster and it'll always be installed in the right place
you can use your
ctx.Export
to access it from
kubectl
etc but this is the recommended way
m
Okay, that makes much more sense πŸ‘
m
Oh, I was going to try this, but I will look at your example
Copy code
func GetKubeConfig(ctx *pulumi.Context, config *configuration.Configuration, cluster *containerservice.ManagedCluster) *pulumi.StringOutput {
	credentials := containerservice.ListManagedClusterAdminCredentialsOutput(ctx, containerservice.ListManagedClusterAdminCredentialsOutputArgs{
		ResourceGroupName: config.ResourceGroup.Name,
		ResourceName:      cluster.Name,
	})

	kubeConfig := credentials.Kubeconfigs().Index(<http://pulumi.Int|pulumi.Int>(0)).Value().ApplyT(func(encode string) string {
		kubeConfig, err := base64.StdEncoding.DecodeString(encode)
		if err != nil {
			return ""
		}
		return string(kubeConfig)
	}).(pulumi.StringOutput)

	return &kubeConfig
}
Awesome, thanks. Now I understand providers fully as input params to functions as well! πŸ˜„
b
yay! what's awesome about providers is they're at the resource level
m
from where I sit it seems like pure magic πŸ˜‚
b
glad you're enjoying it πŸ˜„
m
i really like it. so much i created the bridge for the flux terraform provider today, and it seems to be working as well. we will probably be going all in on pulumi for our IaC past summer πŸ‘
b
that's great to hear! We'd love to get the flux provider into the registry?
πŸ™Œ 1
m
Right, I would like that as well, but I need to understand more of the underlying model of Pulumi, write tests, do chores and stuff before I think that’s an option? https://github.com/scav/pulumi-flux
b
makes sense. cc @broad-dog-22463