Hey guys, pulumi.apply is running asynchronously w...
# general
b
Hey guys, pulumi.apply is running asynchronously while i need to make it run synchronously Scenario, i am trying to get the state of an existing service account
Copy code
let resourceId : pulumi.Input<string> = "thx/".concat(sharedSecret.name); //"namespace/shared-keystore-jks-ksa" ;
let secrets: pulumi.Output<output.core.v1.ObjectReference[]> = k8s.core.v1.ServiceAccount.get("ksa-".concat(sharedSecret.name),resourceId).secrets;
Post retrieving the secret, i am trying to find the secret name and use the name to supply for further use
Copy code
//works asynchronously
secrets.apply(v => {
            console.log("Secret Name ", v[0].name);
secretName_list.push(v[0].name);
        });
console.log(secretName);//print secret name
Complete program
Copy code
1. var serviceAccountArray = [];
2. var secretName_list=[];
3. 
4. serviceAccountArray.forEach(ksa => {
5.     let resourceId : pulumi.Input<string> = "thx/".concat(ksa.name); //"namespace/shared-keystore-jks-ksa" ;
6.     let secrets: pulumi.Output<output.core.v1.ObjectReference[]> = k8s.core.v1.ServiceAccount.get("ksa-".concat(ksa.name),resourceId).secrets;
7. //running asynchronously
8. secrets.apply(v => {
9.          console.log("Secret Name ", v[0].name);
10.         secretName_list.push(v[0].name);
11.      });
12.  console.log(secretName_list); //print secret name empty because of apply
13. });
Line no. 10 get executed after line no. 12 at the end of the program while i wanted to wait for the execution to get it complete. Any idea how this can be sort out. Thanks!
e
Try not to do mutation of variables inside an apply! It is very very unlikely to work how you want. I would expect something like the following to build that array:
Copy code
secretName_list = serviceAccountArray.map(ksa => {
    let resourceId : pulumi.Input<string> = "thx/".concat(ksa.name); //"namespace/shared-keystore-jks-ksa" ;
    let secrets: pulumi.Output<output.core.v1.ObjectReference[]> = k8s.core.v1.ServiceAccount.get("ksa-".concat(ksa.name),resourceId).secrets;
	secrets.apply(v => v[0].name);
 });
b
I ran the below code
Copy code
var secretName_list = args.applicationSecrets.map(ksa => {
    let resourceId : pulumi.Input<string> = "thx/".concat(ksa.name); //"namespace/shared-keystore-jks-ksa" ;
    let secrets: pulumi.Output<output.core.v1.ObjectReference[]> = k8s.core.v1.ServiceAccount.get("ksa-".concat(ksa.name),resourceId).secrets;
    console.log("secrent name");
	secrets.apply(v => v[0].name);
})

console.log ("arraylist ",secretName_list);
It returns the array with undefined items looks like the apply executed later and array get populated with undefined value.
how can we check the operation in apply executed synchronously
e
Oh sorry no I missed a return there. Switching languages all the time it's hard recalling the correct syntax without putting things through an IDE.
Copy code
var secretName_list = args.applicationSecrets.map(ksa => {
    let resourceId : pulumi.Input<string> = "thx/".concat(ksa.name); //"namespace/shared-keystore-jks-ksa" ;
    let secrets: pulumi.Output<output.core.v1.ObjectReference[]> = k8s.core.v1.ServiceAccount.get("ksa-".concat(ksa.name),resourceId).secrets;
    console.log("secrent name");
	return secrets.apply(v => v[0].name);
})
how can we check the operation in apply executed synchronously
You can't because they won't, and there's no way to ask Pulumi to do that. During
preview
we might not even run the code inside an
apply
b
Thanks Fraser but looks like it returns the function Output
Copy code
arraylist  [
   OutputImpl {
     __pulumiOutput: true,
     resources: [Function (anonymous)],
     allResources: [Function (anonymous)],
     isKnown: Promise { <pending> },
     isSecret: Promise { <pending> },
     promise: [Function (anonymous)],
     toString: [Function (anonymous)],
     toJSON: [Function (anonymous)]
   },
   OutputImpl {
     __pulumiOutput: true,
     resources: [Function (anonymous)],
     allResources: [Function (anonymous)],
     isKnown: Promise { <pending> },
     isSecret: Promise { <pending> },
     promise: [Function (anonymous)],
     toString: [Function (anonymous)],
     toJSON: [Function (anonymous)]
   }
 ]
how would i get the value from it
e
You can't. There is no way to go from Output<T> to T, the only valid thing you can do with Outputs is pass them in as inputs to resources or export them as stack values.
b
Thanks Fraser, your solution work for me