banzaicloud / terraform-provider-k8s

Kubernetes Terraform provider with support for raw manifests
https://registry.terraform.io/providers/banzaicloud/k8s
MIT License
135 stars 30 forks source link

Panic on empty resource #36

Closed thomas-riccardi closed 4 years ago

thomas-riccardi commented 4 years ago

Describe the bug The k8s provider panics when given an empty resource definition. It happens regularly when working around #7 by manually splitting multi objects resources (see https://github.com/banzaicloud/terraform-provider-k8s/issues/7#issuecomment-591555068)

Steps to reproduce the issue:

resource "k8s_manifest" "empty" {
  content = ""
}

(it also happens with content = "#empty")

Actual behavior:

2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: panic: runtime error: invalid memory address or nil pointer dereference
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x16d36ca]
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: 
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: goroutine 236 [running]:
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: k8s.io/apimachinery/pkg/apis/meta/v1/unstructured.(*Unstructured).GetNamespace(...)
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3:      /home/circleci/go/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/apis/meta/v1/unstructured/unstructured.go:234
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: github.com/banzaicloud/terraform-provider-k8s/k8s.resourceK8sManifestCreate(0xc0001ca5b0, 0x177b560, 0xc00076c990, 0x2, 0x2b65d60)
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3:      /home/circleci/project/k8s/resource_k8s_manifest.go:68 +0x29a
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).Apply(0xc00026c100, 0xc0007204b0, 0xc000748620, 0x177b560, 0xc00076c990, 0xc000962401, 0xc0007b8c60, 0x40eac3)
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3:      /home/circleci/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk@v1.4.0/helper/schema/resource.go:305 +0x365
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Provider).Apply(0xc00026c180, 0xc0006b9918, 0xc0007204b0, 0xc000748620, 0xc00058e088, 0xc00083a010, 0x1848c40)
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3:      /home/circleci/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk@v1.4.0/helper/schema/provider.go:294 +0x99
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ApplyResourceChange(0xc0000ee050, 0x1e20f60, 0xc0007b83c0, 0xc0000b67e0, 0xc0000ee050, 0xc0007b83c0, 0xc00027ea80)
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3:      /home/circleci/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk@v1.4.0/internal/helper/plugin/grpc_provider.go:885 +0x882
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ApplyResourceChange_Handler(0x1a21b80, 0xc0000ee050, 0x1e20f60, 0xc0007b83c0, 0xc0000b6720, 0x0, 0x1e20f60, 0xc0007b83c0, 0xc000462140, 0x93)
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3:      /home/circleci/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk@v1.4.0/internal/tfplugin5/tfplugin5.pb.go:3189 +0x217
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: google.golang.org/grpc.(*Server).processUnaryRPC(0xc0000e8000, 0x1e3f180, 0xc00011d680, 0xc00076e000, 0xc00024a630, 0x2b2f200, 0x0, 0x0, 0x0)
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3:      /home/circleci/go/pkg/mod/google.golang.org/grpc@v1.23.1/server.go:995 +0x460
2020-03-05T11:13:44.091+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: google.golang.org/grpc.(*Server).handleStream(0xc0000e8000, 0x1e3f180, 0xc00011d680, 0xc00076e000, 0x0)
2020-03-05T11:13:44.092+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3:      /home/circleci/go/pkg/mod/google.golang.org/grpc@v1.23.1/server.go:1275 +0xd97
2020-03-05T11:13:44.092+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc0004f21f0, 0xc0000e8000, 0x1e3f180, 0xc00011d680, 0xc00076e000)
2020-03-05T11:13:44.092+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3:      /home/circleci/go/pkg/mod/google.golang.org/grpc@v1.23.1/server.go:710 +0xbb
2020-03-05T11:13:44.092+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3: created by google.golang.org/grpc.(*Server).serveStreams.func1
2020-03-05T11:13:44.092+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.3:      /home/circleci/go/pkg/mod/google.golang.org/grpc@v1.23.1/server.go:708 +0xa1
2020-03-05T11:13:44.094+0100 [DEBUG] plugin: plugin process exited: path=/home/thomas/Downloads/terraform-provider-k8s_v0.7.3 pid=9397 error="exit status 2"

Expected behavior

no crash

Additional context It seems that YAMLOrJSONDecoder Decode(&object) doesn't touch object (still nil pointer) when there is no yaml object in the input string. Then all calls to object.* will probably panic, object.GetNamespace() being the first.

I'm not sure what to do when the object is nil: we possibly still need to d.Setid() somehow to make terraform happy.

This will need to be handled as part of a proper fix for #7 too.

thomas-riccardi commented 4 years ago

Partial workaround: if the section is really empty (no comment), then adding compact() around the split() will help.

bonifaido commented 4 years ago

I think creating resources with empty content shouldn't be allowed, added a check in #39 to make this invalid.