pulumi import of k8s service is failing with error "error: Preview failed: resource does not exist

3/1/2022

I created a k8s deployment and service with yaml file and tried to import it with pulumi import Following are the Yaml files

#Deployment
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cluster1-pgbouncer
  namespace: demo
  labels:
    app: cluster1-pgbouncer
spec:
  replicas: 2
  selector:
    matchLabels:
      app: cluster1-pgbouncer
  template:
    metadata:
      labels:
        app: cluster1-pgbouncer
    spec:
      containers:
      - name: my-nginx
        image: nginx:alpine
#Service
apiVersion: v1
kind: Service
metadata:
  name: cluster1-pgbouncer
  namespace: demo
  labels:
    app: cluster1-pgbouncer
spec:
  ports:
  - port: 5432
    protocol: TCP
    targetPort: 5432
  selector:
    app: cluster1-pgbouncer
  type: LoadBalancer

Pulumi import is failing with following error

$ pulumi import kubernetes:core/v1:Service cluster1-pgbouncer cluster1-pgbouncer
Previewing import (dev)

View Live: https://app.pulumi.com/cshiv/test/dev/previews/<ID>

     Type                           Name                Plan       Info
 +   pulumi:pulumi:Stack            test-dev            create     1 error
 =   └─ kubernetes:core/v1:Service  cluster1-pgbouncer  import     1 error
 
Diagnostics:
  pulumi:pulumi:Stack (test-dev):
    error: preview failed
 
  kubernetes:core/v1:Service (cluster1-pgbouncer):
    error: Preview failed: resource 'cluster1-pgbouncer' does not exist
 

Import failed, try specifying the set of properties to import with.
This can be done by passing the property names with the --properties flag.

Below is the python code

$ cat __main__.py 
import pulumi
import pulumi_kubernetes as kubernetes
cluster1_pgbouncer = kubernetes.core.v1.Service("cluster1-pgbouncer",
    api_version="v1",
    kind="Service",
    metadata=kubernetes.meta.v1.ObjectMetaArgs(
        labels={
            "app": "cluster1-pgbouncer",
            "app.kubernetes.io/managed-by": "pulumi",
        },
        name="cluster1-pgbouncer",
        namespace="demo",
    ),
    spec=kubernetes.core.v1.ServiceSpecArgs(
        ports=[kubernetes.core.v1.ServicePortArgs(
            name="postgres",
            port=5432,
            protocol="TCP",
            target_port=5432,
        )],
        selector={
            "app": "cluster1-pgbouncer",
        },
        session_affinity="None",
        type="LoadBalancer",
    ))#,
    #opts=pulumi.ResourceOptions(protect=True))

I captured logs with higher verbosity and --logflow enabled still there were not good insights.

Any help would be greatly appreciated.

-- Chetan
kubernetes
pulumi

0 Answers