3

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
  • 53
  • 5
  • are you sure your `KUBECONFIG` is pointing at the right cluster? I can't see anything immediately wrong here – jaxxstorm Mar 01 '22 at 14:54
  • Yep config file is at location ~/.kube/config.`$ kubectl get svc` `NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE` `cluster1-pgbouncer LoadBalancer 5432:31856/TCP 24h` – Chetan Mar 02 '22 at 11:14
  • What k8s version are you using? Your deployment/service syntax is correct. By running `pulumi import kubernetes:core/v1:Service ...` command you import a Service from the `default` k8s namespace. – mozello Mar 02 '22 at 18:04

1 Answers1

1

Add the namespace plus a slash to the resource id:

pulumi import kubernetes:core/v1:Service cluster1-pgbouncer demo/cluster1-pgbouncer
Gavin Haynes
  • 1,721
  • 11
  • 21