<p>I'm getting the following error message that I don't understand:</p>

<pre><code>Error: resource name may not be empty

  on main.tf line 48, in data "kubernetes_service" "spark_master_service":
  48: data "kubernetes_service" "spark_master_service" {
</code></pre>

<p>Related <code>data source</code>:</p>

<pre><code>data "kubernetes_service" "spark_master_service" {
    metadata {
        labels = {
            "app.kubernetes.io/component" = "master"
            "app.kubernetes.io/instance" = "spark"
            "app.kubernetes.io/name" = "spark"
        }
        namespace = var.namespace
    }
}
</code></pre>

<p>My <code>data</code> has a name so I can't quite figure out what Terraform is telling me. </p>

<p>This confusing error is coming from the fact that you can't use your metadata labels as an input to the data source and instead can only provide the namespace and name of the service to look it up.</p>

<p>If you look at the <a href="https://github.com/terraform-providers/terraform-provider-kubernetes/blob/1494642e997541fe09ac0eaa01aed2a62d016e45/kubernetes/data_source_kubernetes_service.go#L137-L145" rel="noreferrer">source for the data source</a> you can see that it is only using the <code>namespace</code> and <code>name</code> fields:</p>

<pre class="lang-golang prettyprint-override"><code>func dataSourceKubernetesServiceRead(d *schema.ResourceData, meta interface{}) error {
    om := meta_v1.ObjectMeta{
        Namespace: d.Get("metadata.0.namespace").(string),
        Name:      d.Get("metadata.0.name").(string),
    }
    d.SetId(buildId(om))

    return resourceKubernetesServiceRead(d, meta)
}
</code></pre>

<p>The <a href="https://www.terraform.io/docs/providers/kubernetes/d/service.html#arguments" rel="noreferrer">docs</a> do show that these are the only two arguments that should be used:</p>

<blockquote>
  <p>Arguments</p>
  
  <ul>
  <li>name - (Optional) Name of the service, must be unique. Cannot be    updated. For more info see Kubernetes reference</li>
  <li>namespace - (Optional) Namespace defines the space within which name of the    service must be unique.</li>
  </ul>
</blockquote>

<p>Unfortunately the docs state that both the namespace and the name are optional because they're using shared schema parts that are the same for much of the Kubernetes provider so Terraform isn't able to check that the <code>name</code> field is used because the underlying implementation requires it.</p>

<p>I've not looked too deeply into the Kubernetes provider but to me this seems like a bug and if a resource or data source has a different schema in practice to other resources then the schema should match that implementation. This might be tricky to do with so much of the Kubernetes schema being shared but without it you are losing a lot of the niceties of Terraform in that it should be pretty strongly typed and be able to give you better error messages when you are doing something wrong.</p>
