terrajet icon indicating copy to clipboard operation
terrajet copied to clipboard

can't get the correct terraform state file when importing the existed resources

Open bretagne-peiqi opened this issue 1 year ago • 3 comments

What happened?

importing existed resources with yaml file filled required fields and annotations: crossplane.io/external-name: xxx

expected to import the resource, but following the logs: the provider can't get the good result during oberservation, which means, it failed to get the right tfstate file and thought the resources does't existed and try to create one.

 cat terraform.tfstate
{
  "version": 4,
  "terraform_version": "1.1.5",
  "serial": 1,
  "lineage": "4a5f1ef0-9c2e-41fa-a0c6-3b3a92cc8666",
  "outputs": {},
  "resources": []
}

some logs:

DEBUG   provider-jet-alicloud   refresh ended   {"workspace": "/tmp/4a5f1ef0-9c2e-41fa-a0c6-3b3a92cc8666", "out": "{\"@level\":\"info\",\"@message\":\"Terraform 1.1.5\",\"@module\":\"terraform.ui\",\"@timestamp\":\"2022-08-17T05:36:22.570388Z\",\"terraform\":\"1.1.5\",\"type\":\"version\",\"ui\":\"1.0\"}\n{\"@level\":\"info\",\"@message\":\"Plan: 0 to add, 0 to change, 0 to destroy.\",\"@module\":\"terraform.ui\",\"@timestamp\":\"2022-08-17T05:36:25.251256Z\",\"changes\":{\"add\":0,\"change\":0,\"remove\":0,\"operation\":\"plan\"},\"type\":\"change_summary\"}\n{\"@level\":\"info\",\"@message\":\"Apply complete! Resources: 0 added, 0 changed, 0 destroyed.\",\"@module\":\"terraform.ui\",\"@timestamp\":\"2022-08-17T05:36:25.255911Z\",\"changes\":{\"add\":0,\"change\":0,\"remove\":0,\"operation\":\"apply\"},\"type\":\"change_summary\"}\n{\"@level\":\"info\",\"@message\":\"Outputs: 0\",\"@module\":\"terraform.ui\",\"@timestamp\":\"2022-08-17T05:36:25.255961Z\",\"outputs\":{},\"type\":\"outputs\"}\n"}
1.6607145853248382e+09  DEBUG   provider-jet-alicloud   Successfully requested creation of external resource    {"controller": "managed/managedk8s.alicloud.jet.crossplane.io/v1alpha1, kind=managedkubernetes", "request": "/k8s-infra-test", "uid": "4a5f1ef0-9c2e-41fa-a0c6-3b3a92cc8666", "version": "3686057787", "external-name": "k8s-infra-test", "external-name": "k8s-infra-test"}


How can we reproduce it?

my config file:

apiVersion: managedk8s.alicloud.jet.crossplane.io/v1alpha1
kind: ManagedKubernetes
metadata:
  name: k8s-infra-test
  annotations:
    external-name: k8s-infra-test
spec:
  forProvider:
    workerVswitchIds:
      - vsw-ufxxxxxxxxxxxxx
    securityGroupId: sg-uf63gxxxxxxxxxxxx
  providerConfigRef:
    name: default-mk8s
  deletionPolicy: Delete

bretagne-peiqi avatar Aug 17 '22 07:08 bretagne-peiqi

I am wondering the cmd := w.executor.CommandContext(ctx, "terraform", "apply", "-refresh-only", "-auto-approve", "-input=false", "-lock=false", "-json") whether can get the good tfstate file ?, because it generated the file with resources [], and provider thought the resource doesn't exist

bretagne-peiqi avatar Aug 17 '22 07:08 bretagne-peiqi

I had the same problem,who can support ?

huangyanhong avatar Aug 17 '22 07:08 huangyanhong

add terraform import resource can resolve this problem

huangyanhong avatar Aug 17 '22 15:08 huangyanhong