https://pulumi.com logo
m

most-mouse-38002

06/23/2022, 4:24 PM
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

billowy-army-68599

06/23/2022, 4:35 PM
is there a reason it needs to be an env var?
m

most-mouse-38002

06/23/2022, 4:37 PM
b

billowy-army-68599

06/23/2022, 4:39 PM
are you just trying to set up a kubernetes provider to install things in a clister you've created with Pulumi?
m

most-mouse-38002

06/23/2022, 4:40 PM
Correct
b

billowy-army-68599

06/23/2022, 4:42 PM
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

most-mouse-38002

06/23/2022, 4:48 PM
Okay, that makes much more sense πŸ‘
m

most-mouse-38002

06/23/2022, 4:53 PM
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

billowy-army-68599

06/23/2022, 5:04 PM
yay! what's awesome about providers is they're at the resource level
m

most-mouse-38002

06/23/2022, 5:04 PM
from where I sit it seems like pure magic πŸ˜‚
b

billowy-army-68599

06/23/2022, 5:05 PM
glad you're enjoying it πŸ˜„
m

most-mouse-38002

06/23/2022, 5:07 PM
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

billowy-army-68599

06/23/2022, 5:07 PM
that's great to hear! We'd love to get the flux provider into the registry?
πŸ™Œ 1
m

most-mouse-38002

06/23/2022, 5:11 PM
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

billowy-army-68599

06/23/2022, 5:12 PM
makes sense. cc @broad-dog-22463
4 Views