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

Doesn't support multiple resources in the same YAML #7

Closed jemc closed 4 years ago

jemc commented 5 years ago

Describe the bug kubectl apply supports YAML files that contain more than one Kubernetes resource manifest, separated by --- lines. Moreover, this is quite common practice in the Kubernetes community. When I try to use such a YAML file with this terraform provider, I get an error message from terraform that says something like:

Error: expected to create 1 resource, got 12

Steps to reproduce the issue: Try to use a multi-resource YAML manifest like this one as the content for this terraform provider.

Expected behavior Expected to kubectl apply all of the resources in the YAML manifest, but got an error instead.

Additional context On Linux, using release v0.2.0.

pabloajz commented 5 years ago

It would be very nice if this provider could support this , as it is a very standard case in Kubernetes.

meanwhile I am doing a little workaround to apply a ngnix manifest with multiple resource in it

locals {
  resources = split("\n---\n",data.template_file.ngnix.rendered)
}

resource "k8s_manifest" "nginx-deployment" {
  count = length(local.resources)

  content = local.resources[count.index]
}
sagikazarmark commented 5 years ago

Nice workaround!

I'll definitely add this to the todo list, but focus have shifted away from this provider lately, so I'll have to ask your patience.

Feel free to provide PRs though.

thomas-riccardi commented 4 years ago

small warning: the plugin crashes when one section is empty (I didn't take the time to test it, but my file had 3 valid sections, then the 4th one was entirely comments):

k8s_manifest.api[3]: Creating...

Error: rpc error: code = Unavailable desc = transport is closing

Error: rpc error: code = Unavailable desc = transport is closing

Error: rpc error: code = Unavailable desc = transport is closing

Error: rpc error: code = Unavailable desc = transport is closing

panic: runtime error: invalid memory address or nil pointer dereference
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x16d331a]
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: 
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: goroutine 305 [running]:
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: k8s.io/apimachinery/pkg/apis/meta/v1/unstructured.(*Unstructured).GetNamespace(...)
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2:      /home/circleci/go/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/apis/meta/v1/unstructured/unstructured.go:234
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: github.com/banzaicloud/terraform-provider-k8s/k8s.resourceK8sManifestCreate(0xc0001865b0, 0x177a540, 0xc000456550, 0x2, 0x2b64d40)
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2:      /home/circleci/project/k8s/resource_k8s_manifest.go:62 +0x29a
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).Apply(0xc000270100, 0xc00066c460, 0xc00070a860, 0x177a540, 0xc000456550, 0xc0000d5301, 0xc0005d2480, 0x40eac3)
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2:      /home/circleci/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk@v1.4.0/helper/schema/resource.go:305 +0x365
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Provider).Apply(0xc000270180, 0xc000947918, 0xc00066c460, 0xc00070a860, 0xc0001e4868, 0xc00070c018, 0x1847c20)
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2:      /home/circleci/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk@v1.4.0/helper/schema/provider.go:294 +0x99
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ApplyResourceChange(0xc00026e050, 0x1e1fd40, 0xc000249b30, 0xc0000aa660, 0xc00026e050, 0xc000249b30, 0xc000906a80)
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2:      /home/circleci/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk@v1.4.0/internal/helper/plugin/grpc_provider.go:885 +0x882
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ApplyResourceChange_Handler(0x1a20a80, 0xc00026e050, 0x1e1fd40, 0xc000249b30, 0xc0000aa600, 0x0, 0x1e1fd40, 0xc000249b30, 0xc0006b4600, 0x595)
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2:      /home/circleci/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk@v1.4.0/internal/tfplugin5/tfplugin5.pb.go:3189 +0x217
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: google.golang.org/grpc.(*Server).processUnaryRPC(0xc0000e8000, 0x1e3df60, 0xc0000b7080, 0xc0006f2200, 0xc000248c30, 0x2b2e1e0, 0x0, 0x0, 0x0)
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2:      /home/circleci/go/pkg/mod/google.golang.org/grpc@v1.23.1/server.go:995 +0x460
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: google.golang.org/grpc.(*Server).handleStream(0xc0000e8000, 0x1e3df60, 0xc0000b7080, 0xc0006f2200, 0x0)
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2:      /home/circleci/go/pkg/mod/google.golang.org/grpc@v1.23.1/server.go:1275 +0xd97
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc0003341b0, 0xc0000e8000, 0x1e3df60, 0xc0000b7080, 0xc0006f2200)
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2:      /home/circleci/go/pkg/mod/google.golang.org/grpc@v1.23.1/server.go:710 +0xbb
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2: created by google.golang.org/grpc.(*Server).serveStreams.func1
2020-02-26T18:43:46.770+0100 [DEBUG] plugin.terraform-provider-k8s_v0.7.2:      /home/circleci/go/pkg/mod/google.golang.org/grpc@v1.23.1/server.go:708 +0xa1
2020-02-26T18:43:46.773+0100 [DEBUG] plugin: plugin process exited: path=/home/thomas/Downloads/terraform-provider-k8s_v0.7.2 pid=10245 error="exit status 2"
thomas-riccardi commented 4 years ago

It would be very nice if this provider could support this , as it is a very standard case in Kubernetes.

meanwhile I am doing a little workaround to apply a ngnix manifest with multiple resource in it

locals {
  resources = split("\n---\n",data.template_file.ngnix.rendered)
}

resource "k8s_manifest" "nginx-deployment" {
  count = length(local.resources)

  content = local.resources[count.index]
}

This doesn't work when the template variables depend on runtime values (such as coming from random_password):

Error: Invalid count argument

  on resources.tf line 222, in resource "k8s_manifest" "api":
 222:   count     = length(local.api_resources)

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.

Workaround over workaround: render the template with non-runtime data just to get the count value...

bonifaido commented 4 years ago

The new version won't support multiple resources in the same content block (and it is not relying on kubectl anymore). If it would the k8s provider would need to have the same logic that Terraform contains already (see the workaround).