it looks like the `.get()` function for a k8s reso...
# google-cloud
m
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:
Copy code
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
use try/catch? I do this for a couple ops in python, mostly around bootstrapping.
Copy code
# 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
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
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
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
Copy code
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:
Copy code
gcp:compute:GlobalAddress (banana):
    error: Preview failed: resource 'banana' does not exist