Hi, I am evaluating pulumi for my company and pote...
# general
b
Hi, I am evaluating pulumi for my company and potentially go to it from Terraform. But first hurdle 🙂 What I want to test is using the k8s provider, create ServiceAccount, reference the associated Secret that was created to retrieve the token and then use the token to do other things. My code is as follows (very simple)
Copy code
import * as k8s from "@pulumi/kubernetes";
import * as kx from "@pulumi/kubernetesx";

const serviceAccount = new k8s.core.v1.ServiceAccount("test-service-account");

serviceAccount.secrets.apply(secretName => {
    console.log(secretName[0].name);
    const token = k8s.core.v1.Secret.get(secretName[0].name,`${serviceAccount.metadata.namespace}/${secretName[0].name}`).data.apply(v => {
        console.log(v['token']);
    });
});
however, i am met with
Copy code
error: preview failed

  kubernetes:core/v1:Secret (test-service-account-gd83fdyj-token-f5wqw):
    error: Preview failed: failed to read resource because of a failure to parse resource name from request ID: Calling [toString] on an [Output<T>] is not supported.

    To get the value of an Output<T> as an Output<string> consider either:
    1: o.apply(v => `prefix${v}suffix`)
    2: pulumi.interpolate `prefix${v}suffix`
any suggestions?
l
In this style code, you would need to
apply
on
sectionName[0].name
, rather than
secretName
.
I don't know if that will work though, since I don't know what
secretName
is 🙂
s
you need to use
interpolate
to concat `Output<string>`s, but you don't need to use this if you use
secretName.namespace
. And you can use array pattern matching to assign the first element to the variable
secretName
Copy code
import * as k8s from "@pulumi/kubernetes";
import * as kx from "@pulumi/kubernetesx";

const serviceAccount = new k8s.core.v1.ServiceAccount("test-service-account");

serviceAccount.secrets.apply(([secretName]) => {
    console.log(secretName.name);
    const token = k8s.core.v1.Secret.get(secretName.name, `${secretName.namespace}/${secretName.name}`).data.apply(v => {
        console.log(v['token']);
    });
});
b
thanks for the info