When using the Kubernetes provider and creating a ...
# general
a
When using the Kubernetes provider and creating a Kubernetes
CustomResource
, for example:
Copy code
...
new CustomResource("test-cluster", {
    apiVersion: "<http://acid.zalan.do/v1|acid.zalan.do/v1>",
    kind: "postgresql",
    metadata: {
        name: "test-cluster"
    },
    spec: {
      ...
    }
}
...
and that custom resource results in other new resources being created by an operator. For example a
StatefuleSet
, multiple `Secret`'s etc. If, in the same Pulumi program, after creating the
CustomResource
I want to import and use those new resources (created by the operator, when the new
CustomResource
above was created), how would I import them? Currently
pulumi up
will fail on the preview with a "resource ... does not exist" error, since the resource that would be created once run, isn't there yet. Is there an elegant/any way to handle this?
w
Depends a little on what you want to do with them. If you just want to read them in and reference state associated with them, you can use
SomeResource.get(id)
. If you want to adopt them under management of your Pulumi program to modify them, you can use
{ import: id }
. But note that you can't currently also modify the resource in the same deployment you import it - so the somewhat common thing in this case of "I want to adopt the resource and change it to be different than what it is already", doesn't have a great 1st-class answer yet. We have a few places where we've seen shelling out to
kubectl apply
work as a temporary solution. https://github.com/pulumi/pulumi-kubernetes/issues/264 is tracking this. Do any of those match what you are trying to do?
a
Thanks for the reply @white-balloon-205, apologies for the delayed response, it was after 00:00 my time 💤 For this use case I only want to read the created resource. I.e.
If you just want to read them in and reference state associated with them, you can use
SomeResource.get(id)
.
I've tried both
.get
and
import
in my example with limited success. I'm using the https://github.com/zalando/postgres-operator to spin up a Postgres cluster and as part of that, a
Secret
is created when the cluster is created containing the database credentials. The problem is whether I use
.get
or
import
:
Copy code
const databaseSecret = kx.Secret.get("<http://test.test-cluster.credentials.postgresql.acid.zalan.do|test.test-cluster.credentials.postgresql.acid.zalan.do>",
        "default/test.test-cluster.credentials.postgresql.acid.zalan.do");
this fails at preview with a "resource ... does not exist" error. If I skip preview (
--skip-preview
) the first time it still fails with same missing error. If I cancel that and run again, it succeeds (because the
Secret
is now created). I believe it comes down to a timing issue. The
Secret
hasn't been created by the operator by the time the code for
.get
or
import
tries to bring it into state. Is there a way to retry for a period of time (like some other Kubernetes resources) to allow enough time for the operator to create the cluster and the resulting
Secret
?
I can actually get it to work from scratch with
--skip-preview
by depending on the database cluster `CustomResource`:
Copy code
...
const database = new CustomResource("test-cluster", {
    apiVersion: "<http://acid.zalan.do/v1|acid.zalan.do/v1>",
    kind: "postgresql",
    metadata: {
        name: "test-cluster"
    },
    spec: {
      ...
    }
}
...

const databaseSecret = k8s.core.v1.Secret.get("<http://test.test-cluster.credentials.postgresql.acid.zalan.do|test.test-cluster.credentials.postgresql.acid.zalan.do>",
    "default/test.test-cluster.credentials.postgresql.acid.zalan.do", {
        dependsOn: database
    });
and then the dependencies work out the timing issue. However, I still have to
--skip-preview
. Is there a way to preserve the preview?