https://pulumi.com logo
#kubernetes
Title
# kubernetes
s

salmon-raincoat-19475

11/02/2021, 7:02 PM
I'm trying to save my kubeconfig out to an S3 bucket so other team members have access to it...it doesn't seem like there are resources on this/perhaps discouraged? I did see https://github.com/pulumi/pulumi-kubernetes/issues/1032 and read the
Output
class in Python - which seems to suggest that it needs to stay in the
Output
type so that Pulumi objects are realized.... Is there a different approach I should be taking? Thanks!
b

billowy-army-68599

11/02/2021, 7:03 PM
you should be able to do this, but you'll need to use an
apply
I think
can you share what you have already?
s

salmon-raincoat-19475

11/02/2021, 7:05 PM
thanks, jaxx... I have tried a few things, latest, being an
apply
Copy code
kubeconfig = open('kubeconfig', 'w')
kjson = self.cluster.kubeconfig.apply(json.dumps)
kubeconfig.write(kjson)
I def. have the kubeconfig when doing
pulumi.export('kubeconfig', self.cluster.kubeconfig)
b

billowy-army-68599

11/02/2021, 7:06 PM
you should be able to use the
BucketObject
resource to write the file, you're trying to write locally at the moment?
s

salmon-raincoat-19475

11/02/2021, 7:06 PM
correct, just to validate
error:
Copy code
kubeconfig.write(kjson)
    TypeError: write() argument must be str, not Output
b

billowy-army-68599

11/02/2021, 7:10 PM
try something like this:
Copy code
def write_to_file(kubeconfig):
    f = open("kubeconfig.json", "a")
    f.write(kubeconfig)
    f.close()

json = self.cluster.kubeconfig.apply(lambda k: write_to_file(kubeconfig=k))
your
apply
needs to take a function to operate on, so simply doing
json.dumps
won't get it, and you need to actually do the write inside the apply
s

salmon-raincoat-19475

11/02/2021, 7:43 PM
awesome, thanks, that makes sense! this worked:
Copy code
bucket = pulumi_aws.s3.get_bucket("my_bucket_name")
        source = self.cluster.kubeconfig.apply(lambda s: pulumi.asset.StringAsset(json.dumps(s)))
        filename = f'{self.env_stack}-kubeconfig'
        pulumi_aws.s3.BucketObject(resource_name=filename,
            bucket=bucket.id,
            key=f'some/sub_dir/{filename}',
            source=source
        )
❤️ 1
4 Views