pulumi-eks icon indicating copy to clipboard operation
pulumi-eks copied to clipboard

Improve typing of Cluster.kubeconfig output

Open EvanBoyle opened this issue 4 years ago • 2 comments

Cluster.kubeconfig is typed as Output<any>, which seems overly permissive given the implementation:

https://github.com/pulumi/pulumi-eks/blob/master/nodejs/eks/cluster.ts#L182

Through tracing this code, it appears that we always get an object back. The any typing can lead to some easy to product errors, such as specifying the raw kubeconfig object in a property that requires a string: https://github.com/pulumi/pulumi-kubernetes/issues/1231

While that panic has been patched, it could still lead to issues elsewhere and we should consider using a more descriptive type if possible.

EvanBoyle avatar Nov 12 '20 17:11 EvanBoyle

Concrete proposal is to change the type of kubeconfig (and the underlying impl that produces it) to be a string. This is however a breaking change.

EvanBoyle avatar Aug 04 '21 00:08 EvanBoyle

Concrete proposal is to change the type of kubeconfig (and the underlying impl that produces it) to be a string. This is however a breaking change.

The alternative is to add a new API like kubeconfigJSON which returns a string, and possibly deprecate kubeconfig.

In the meantime, it is possible to convert the object to a JSON string, e.g.:

TypeScript

const kubeconfig = cluster.kubeconfig.apply(JSON.stringify);

Python

import json
kubeconfig = cluster.kubeconfig.apply(lambda k: json.dumps(k))

justinvp avatar Feb 23 '22 00:02 justinvp

Concrete proposal is to change the type of kubeconfig (and the underlying impl that produces it) to be a string. This is however a breaking change.

The alternative is to add a new API like kubeconfigJSON which returns a string, and possibly deprecate kubeconfig.

In the meantime, it is possible to convert the object to a JSON string, e.g.:

TypeScript

const kubeconfig = cluster.kubeconfig.apply(JSON.stringify);

Python

import json
kubeconfig = cluster.kubeconfig.apply(lambda k: json.dumps(k))

hi, I'm not able to convert the kubeconfig using this strategy. My code:

print(aws_eks_cluster.kubeconfig.apply(lambda k: json.dumps(k)))

which yields:

    To get the value of an Output[T] as an Output[str] consider:
    1. o.apply(lambda v: f"prefix{v}suffix")
    See https://pulumi.io/help/outputs for more details.
    This function may throw in a future version of Pulumi.

And when I call 'type()' on the value, it's <class 'pulumi.output.Output'>

Any advice?

swensons avatar Nov 11 '22 05:11 swensons

@swensons, what do you want to do with the kubeconfig?

The reason you're getting that error is because aws_eks_cluster.kubeconfig.apply(lambda k: json.dumps(k)) returns Output[str], which can't be used as a string directly, but can be passed to other resource inputs properties that accept Input[str].

If you're just looking to print out the JSON string, you could call print inside the apply like:

aws_eks_cluster.kubeconfig.apply(lambda k: print(json.dumps(k)))

justinvp avatar Nov 18 '22 19:11 justinvp

#815 added a kubeconfigJson output to Cluster. Here's an example of using it in Python:

from pulumi import ResourceOptions
from pulumi_eks import Cluster
from pulumi_kubernetes import Provider
from pulumi_kubernetes.core.v1 import Namespace

cluster = Cluster("test")

provider = Provider("eks-k8s", kubeconfig=cluster.kubeconfig_json)

namespace = Namespace("test", opts=ResourceOptions(provider=provider))

justinvp avatar Nov 21 '22 09:11 justinvp