https://pulumi.com logo
Title
m

many-knife-65312

03/17/2023, 8:43 PM
it looks like the
.get()
function for a k8s resource errors out when it doesn't exist. what's the right way to handle this scenario so that the resource is created if it doesn't exist?
my code looks like this:
let sa = k8s.core.v1.ServiceAccount.get(id, `${namespace}/${name}`);

if (sa === undefined)
// create the service account
the
get
function errors out and says "resource <name> does not exist"
and the script stops
it makes me sad 😢
anyone have thoughts on this? I'm beginning to think that the pattern of getting something before it exists is the wrong pattern
r

refined-engineer-9827

03/22/2023, 5:37 PM
use try/catch? I do this for a couple ops in python, mostly around bootstrapping.
# Handle bootstrapping chicken/egg problem.  Here in case something
# happens and we need to re-create the world.
initial_setup = False
try:
    core_vpc = gcp.compute.get_network(
        name = "core-vpc",
        project = "prj-core"
    )
except:
    initial_setup = True
m

many-knife-65312

03/22/2023, 5:41 PM
I tried the node equivalent and it didn't trigger the catch function. I experimented with it a bit and saw that finally() was called, but not catch.
r

refined-engineer-9827

03/22/2023, 5:42 PM
oof, I don't know node, so can't help beyond that. But yah, most of my .get() to check blocks are wrapped in try/except
m

many-knife-65312

03/22/2023, 5:44 PM
I'll try it again. I'm beginning to think that it's typescript related, in that the catch function isn't called because the type that is passed doesn't match the signature 🤷
just did a quick try and now i'm thinking it has something to do with
async
calls. I'm not a typescript expert, so i'm definitely overlooking something
try {
  staticIP = gcp.compute.GlobalAddress.get(
    `banana`,
    `banana`
  );
} catch (e) {
  console.log('here in catch')
} finally {
  console.log('here in finally');
}
in that code, I see
here in finally
, and the script exits with:
gcp:compute:GlobalAddress (banana):
    error: Preview failed: resource 'banana' does not exist