Hi there, Trying to use `getResource()` on a `k8s....
# kubernetes
f
Hi there, Trying to use
getResource()
on a
k8s.helm.v2.Chart
yields a weird error:
Error: invocation of kubernetes:yaml:decode returned an error: error converting YAML to JSON: yaml: line 29: could not find expected ':'
I’m installing
mongodb-replicaset
chart and try to access the
StatefulSet
:
Copy code
this.statefulSet = this.chart.getResource(
      'apps/v1/StatefulSet',
      replicaSetName,
    )

    const uri = `mongodb://${this.statefulSet.spec.serviceName}:27017`
Following snippet example from here : https://www.pulumi.com/docs/guides/adopting/from_kubernetes/#provisioning-a-helm-chart
g
That error sounds like it may be a problem with invalid YAML in the chart. The code snippet you posted looks right to me, and I verified that the linked example is working.
f
Hi @gorgeous-egg-16927 sorry I forgot to update this one. I had a problem with input/output extrapolation elsewhere (as usual) leading to this error. I’m still stuck though, because no matter how I try to access the
statefulSet
of this chart I get with
getResource()
, its always
undefined
I can’t access its properties (
metadata
,
spec
, etc). I try to
apply()
or
interpolate
but there’s no way to get anything out of a
getResource()
at that point for me. Doing like your example yields
undefined
too.
g
This is working for me:
Copy code
const wordpress = new k8s.helm.v2.Chart("wpdev", {
    repo: "stable",
    chart: "wordpress",
    version: "9.0.3",
});

// Export the public IP for WordPress.
export const frontend = wordpress.getResource("v1/Service", "wpdev-wordpress");
I did notice that the
frontendIp
was undefined running on docker for mac because the loadbalancer status field is actually
frontend.status.loadBalancer.ingress[0].hostname
. This varies depending on which k8s you’re running.
f
I’m on aws eks, I didn’t try on any other k8s instance yet, but shouldn’t
getResource()
returns a component of the expected type with its properties set no matter what ? How can I rely on it if it does not ? I need the
StatefulSet.spec.serviceName
and it seems I can’t get it and will have to rely on a manual concatenation guess.
g
Sorry, I think my previous answer was confusing. Yes,
getResource
works as you expect. However, it returns an Output, and it looks like you’re trying to use a normal string interpolation rather than
pulumi.interpolate
to build the
uri
string. I suspect this is the actual problem.
f
As I said I tried all of these :
Copy code
const uri = `mongodb://${this.statefulSet.spec.serviceName}:27017`

const uri = pulumi.interpolate`mongodb://${this.statefulSet.spec.serviceName}:27017`

const uri = this.statefulSet.spec.apply(spec => `mongodb://${spec.serviceName}:27017`)
Always get something like
unable to get serviceName from undefined
One thing I don’t understand is that I actually get the errors during the preview, while the resource does not actually exist yet, which makes sense, but pulumi should know that, right ? I ensured the
replicasetName
variable I use in
getResource()
is the valid expected name of the resource, so we should be safe here.
g
Is
replicasetName
an
Output<string>
, or a
string
?
f
that’s a simple raw
string
If
getResource()
does not find the resource, what’s its behavior ? fail silently with an
undefined
value or something more definitive ? Again since the error is raised during preview, that’s not easy to guess what would have been the final state, services are not provisioned yet.
g
For TypeScript, it will return
undefined
Is the resource namespaced?
f
I think the chart is having its own namespace yes, let me check that
yes it is
ok so I should write something like
${namespace}/${replicasetName}
if I understand it right ?
g
Yes, that’s right
f
Thanks it finally works ! Sorry I didn’t notice this earlier 😞
g
Glad to hear it! 🙂 I opened https://github.com/pulumi/pulumi-kubernetes/issues/1173 to make this a little easier to track down in the future
f
Would definitely help, thanks ! 🙂