juju / terraform-provider-juju

A Terraform provider for Juju
Apache License 2.0
19 stars 37 forks source link

panic: runtime error: invalid memory address or nil pointer dereference #520

Closed hemanthnakkina closed 2 months ago

hemanthnakkina commented 2 months ago

Description

Panic when running terraform plan that has storage_directives

Urgency

Blocker for our release

Terraform Juju Provider version

0.13.0-rc1

Terraform version

1.9.1

Juju version

3.5.2

Terraform Configuration(s)

git clone https://github.com/canonical/sunbeam-terraform.git
cd sunbeam-terraform
git fetch origin pull/83/head:BRANCH_NAME

Reproduce / Test

juju 3.5/stable + microk8s 

# Look at .github/workflows/deploy.yml
sudo snap install go --classic
git clone https://github.com/juju/terraform-provider-juju.git --branch 0.13.0-rc1
cd terraform-provider-juju
make install
cd -

sudo snap install --classic terraform

git clone https://github.com/canonical/sunbeam-terraform.git
cd sunbeam-terraform
git fetch origin pull/83/head:BRANCH_NAME
git checkout BNRACH_NAME

echo '{"credential": "microk8s", "enable-vault": true, "enable-barbican": true, "enable-designate": true, "nameservers": "testing.github.", "mysql-storage": { "database": "12G" } }' > terraform.tfvars.json
terraform init
TF_LOG=debug terraform apply -auto-approve -var-file=channels/edge.tfvars

Debug/Panic Output

https://github.com/canonical/sunbeam-terraform/actions/runs/9874509729/job/27269053143?pr=83

Stack trace from the terraform-provider-juju_v0.14.0 plugin:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1ef47e0]

goroutine 81 [running]:
github.com/juju/terraform-provider-juju/internal/provider.(*applicationResource).Create(0xc00090dba8, {0x29dd970, 0xc000be53b0}, {{{{0x29e7890, 0xc001085080}, {0x21c7b20, 0xc000bf5440}}, {0x29ee098, 0xc0008b7270}}, {{{0x29e7890, ...}, ...}, ...}, ...}, ...)
    /home/ubuntu/github-runner/_work/sunbeam-terraform/sunbeam-terraform/terraform-provider-juju/internal/provider/resource_application.go:581 +0x1820
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).CreateResource(0xc000246d20, {0x29dd970, 0xc000be53b0}, 0xc0008715e0, 0xc0008715b8)
    /home/ubuntu/go/pkg/mod/github.com/hashicorp/terraform-plugin-framework@v1.9.0/internal/fwserver/server_createresource.go:101 +0x578

Notes & References

The terraform plan has storage_directives where application mysql has storage_directives set explicitly with values in terraform.tfvvars.json. Some other applications like keystone, glance, ovn has storage_directives as {} to pick the defaults from charm metadata.yaml. The plan is applied with default parallelism.

hemanthnakkina commented 2 months ago

terraform_debug_output.log Attached the debug log in https://github.com/canonical/sunbeam-terraform/actions/runs/9874509729/job/27269053143?pr=83

hmlanigan commented 2 months ago

@hemanthnakkina we really need a small plan which reproduces this issue, not a full scale run of sunbeam.

hemanthnakkina commented 2 months ago

@hmlanigan I am not able to reproduce the problem when terraform plan is applied with -parallelism=1

520-tfplan.txt Please use the attached terraform plan and run the following command to reproduce the problem: terraform apply -auto-approve -parallelism=4

If parallelism is reduced to 2, I am not able to reproduce.

On a side note, I see the computed attribute storage for each application is filled with all the storage details of the model (irrespective of application has storage or not). Is it expected?

anvial commented 2 months ago

Hey, the problem was in improper handling of cannot convert storage details error. After fixing it, I was able to build sunbeam on my machine locally:

2024-07-12T14:08:24.995+0300 [TRACE] provider.terraform-provider-juju_v0.14.0: Received downstream response: diagnostic_error_count=0 diagnostic_warning_count=0 tf_req_duration_ms=5181 tf_req_id=c6494d76-b093-2e17-72c8-ceda4d4f9d3f @caller=/home/anvial/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.23.0/tfprotov6/internal/tf6serverlogging/downstream_request.go:42 @module=sdk.proto tf_rpc=ApplyResourceChange tf_proto_version=6.6 tf_provider_addr=registry.terraform.io/juju/juju tf_resource_type=juju_integration timestamp="2024-07-12T14:08:24.995+0300"
2024-07-12T14:08:24.995+0300 [TRACE] provider.terraform-provider-juju_v0.14.0: Served request: @caller=/home/anvial/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.23.0/tfprotov6/tf6server/server.go:878 tf_provider_addr=registry.terraform.io/juju/juju tf_req_id=c6494d76-b093-2e17-72c8-ceda4d4f9d3f tf_rpc=ApplyResourceChange @module=sdk.proto tf_proto_version=6.6 tf_resource_type=juju_integration timestamp="2024-07-12T14:08:24.995+0300"
2024-07-12T14:08:25.004+0300 [DEBUG] State storage *statemgr.Filesystem declined to persist a state snapshot
juju_integration.designate-to-neutron[0]: Creation complete after 5s [id=openstack:designate:dnsaas:neutron:external-dns]
2024-07-12T14:08:25.012+0300 [DEBUG] State storage *statemgr.Filesystem declined to persist a state snapshot
2024-07-12T14:08:25.013+0300 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = error reading from server: EOF"
2024-07-12T14:08:25.019+0300 [INFO]  provider: plugin process exited: plugin=.terraform/providers/registry.terraform.io/juju/juju/0.14.0/linux_amd64/terraform-provider-juju_v0.14.0 id=604282
2024-07-12T14:08:25.019+0300 [DEBUG] provider: plugin exited

Apply complete! Resources: 120 added, 0 changed, 0 destroyed.

Outputs:

ca-offer-url = "admin/openstack.certificate-authority"
cert-distributor-offer-url = "admin/openstack.cert-distributor"
cinder-ceph-offer-url = "admin/openstack.cinder-ceph"
keystone-endpoints-offer-url = "admin/openstack.keystone-endpoints"
keystone-offer-url = "admin/openstack.keystone-credentials"
nova-offer-url = "admin/openstack.nova"
ovn-relay-offer-url = "admin/openstack.ovn-relay"
rabbitmq-offer-url = "admin/openstack.rabbitmq"
┌─[±][test_branch ?:2 ✗][sunbeam-terraform][]

I'll prepare the PR right now.