Skip to content

Revert on datasources throwing error for non-existent resources #2464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2464.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
data-sources: revert a recently introduced deviation on datasources where querying a non-existent resource would cause an error (#2434).
```
4 changes: 4 additions & 0 deletions kubernetes/data_source_kubernetes_config_map_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -54,6 +55,9 @@ func dataSourceKubernetesConfigMapV1Read(ctx context.Context, d *schema.Resource
log.Printf("[INFO] Reading config map %s", metadata.Name)
cfgMap, err := conn.CoreV1().ConfigMaps(metadata.Namespace).Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
log.Printf("[DEBUG] Received error: %#v", err)
return diag.FromErr(err)
}
Expand Down
28 changes: 28 additions & 0 deletions kubernetes/data_source_kubernetes_config_map_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ func TestAccKubernetesDataSourceConfigMapV1_basic(t *testing.T) {
})
}

func TestAccKubernetesDataSourceConfigMapV1_not_found(t *testing.T) {
dataSourceName := "data.kubernetes_config_map_v1.test"
name := fmt.Sprintf("ceci-n.est-pas-une-config-map-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{ // Use the data source to read the existing resource.
Config: testAccKubernetesDataSourceConfigMapV1_nonexistent(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "metadata.0.name", name),
resource.TestCheckResourceAttr(dataSourceName, "data.%", "0"),
),
},
},
})
}

// testAccKubernetesDataSourceConfigMapConfig_basic provides the terraform config
// used to test basic functionality of the config_map data source.
func testAccKubernetesDataSourceConfigMapV1_basic(name string) string {
Expand Down Expand Up @@ -82,3 +101,12 @@ func testAccKubernetesDataSourceConfigMapV1_read() string {
}
`
}

func testAccKubernetesDataSourceConfigMapV1_nonexistent(name string) string {
return fmt.Sprintf(`data "kubernetes_config_map_v1" "test" {
metadata {
name = "%s"
}
}
`, name)
}
4 changes: 4 additions & 0 deletions kubernetes/data_source_kubernetes_endpoints_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -45,6 +46,9 @@ func dataSourceKubernetesEndpointsV1Read(ctx context.Context, d *schema.Resource
log.Printf("[INFO] Reading endpoints %s", metadata.Name)
ep, err := conn.CoreV1().Endpoints(metadata.Namespace).Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
log.Printf("[DEBUG] Received error: %#v", err)
return diag.Errorf("Failed to read endpoint because: %s", err)
}
Expand Down
29 changes: 29 additions & 0 deletions kubernetes/data_source_kubernetes_endpoints_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ func TestAccKubernetesDataSourceEndpointsV1_basic(t *testing.T) {
})
}

func TestAccKubernetesDataSourceEndpointsV1_not_found(t *testing.T) {
dataSourceName := "data.kubernetes_endpoints_v1.test"
name := fmt.Sprintf("ceci-n.est-pas-une-endpoint-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesEndpointV1Destroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDataSourceEndpointsV1_nonexistent(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "metadata.0.name", name),
resource.TestCheckResourceAttr(dataSourceName, "subset.#", "0"),
),
},
},
})
}

func testAccKubernetesDataSourceEndpointsV1_read() string {
return `data "kubernetes_endpoints_v1" "test" {
metadata {
Expand All @@ -68,3 +88,12 @@ func testAccKubernetesDataSourceEndpointsV1_read() string {
}
`
}

func testAccKubernetesDataSourceEndpointsV1_nonexistent(name string) string {
return fmt.Sprintf(`data "kubernetes_endpoints_v1" "test" {
metadata {
name = "%s"
}
}
`, name)
}
4 changes: 4 additions & 0 deletions kubernetes/data_source_kubernetes_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
networking "k8s.io/api/networking/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -151,6 +152,9 @@ func dataSourceKubernetesIngressRead(ctx context.Context, d *schema.ResourceData
log.Printf("[INFO] Reading ingress %s", metadata.Name)
ing, err := conn.ExtensionsV1beta1().Ingresses(metadata.Namespace).Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
log.Printf("[DEBUG] Received error: %#v", err)
return diag.FromErr(err)
}
Expand Down
31 changes: 31 additions & 0 deletions kubernetes/data_source_kubernetes_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ func TestAccKubernetesDataSourceIngress_basic(t *testing.T) {
})
}

func TestAccKubernetesDataSourceIngress_not_found(t *testing.T) {
dataSourceName := "data.kubernetes_ingress.test"
name := fmt.Sprintf("ceci-n.est-pas-une-ingress-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
skipIfClusterVersionGreaterThanOrEqual(t, "1.22.0")
},
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDataSourceIngress_nonexistent(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "metadata.0.name", name),
resource.TestCheckResourceAttr(dataSourceName, "spec.#", "0"),
),
},
},
})
}

func testAccKubernetesDataSourceIngress_basic(name string) string {
return fmt.Sprintf(`resource "kubernetes_ingress" "test" {
metadata {
Expand Down Expand Up @@ -104,3 +126,12 @@ func testAccKubernetesDataSourceIngress_read() string {
}
`
}

func testAccKubernetesDataSourceIngress_nonexistent(name string) string {
return fmt.Sprintf(`data "kubernetes_ingress" "test" {
metadata {
name = "%s"
}
}
`, name)
}
4 changes: 4 additions & 0 deletions kubernetes/data_source_kubernetes_ingress_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
networking "k8s.io/api/networking/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -162,6 +163,9 @@ func dataSourceKubernetesIngressV1Read(ctx context.Context, d *schema.ResourceDa
log.Printf("[INFO] Reading ingress %s", metadata.Name)
ing, err := conn.NetworkingV1().Ingresses(metadata.Namespace).Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
log.Printf("[DEBUG] Received error: %#v", err)
return diag.FromErr(err)
}
Expand Down
31 changes: 31 additions & 0 deletions kubernetes/data_source_kubernetes_ingress_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ func TestAccKubernetesDataSourceIngressV1_basic(t *testing.T) {
})
}

func TestAccKubernetesDataSourceIngressV1_not_found(t *testing.T) {
dataSourceName := "data.kubernetes_ingress_v1.test"
name := fmt.Sprintf("ceci-n.est-pas-une-ingress-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
skipIfClusterVersionLessThan(t, "1.22.0")
},
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDataSourceIngressV1_nonexistent(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "metadata.0.name", name),
resource.TestCheckResourceAttr(dataSourceName, "spec.#", "0"),
),
},
},
})
}

func testAccKubernetesDataSourceIngressV1_basic(name string) string {
return fmt.Sprintf(`resource "kubernetes_ingress_v1" "test" {
metadata {
Expand Down Expand Up @@ -115,3 +137,12 @@ func testAccKubernetesDataSourceIngressV1_read() string {
}
`
}

func testAccKubernetesDataSourceIngressV1_nonexistent(name string) string {
return fmt.Sprintf(`data "kubernetes_ingress_v1" "test" {
metadata {
name = "%s"
}
}
`, name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -124,6 +125,9 @@ func dataSourceKubernetesMutatingWebhookConfigurationV1Read(ctx context.Context,
log.Printf("[INFO] Reading mutating webhook configuration %s", metadata.Name)
cfg, err := conn.AdmissionregistrationV1().MutatingWebhookConfigurations().Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
log.Printf("[DEBUG] Received error: %#v", err)
return diag.FromErr(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ func TestAccKubernetesDataSourceMutatingWebhookConfigurationV1_basic(t *testing.
})
}

func TestAccKubernetesDataSourceMutatingWebhookConfigurationV1_not_found(t *testing.T) {
dataSourceName := "data.kubernetes_mutating_webhook_configuration_v1.test"
name := fmt.Sprintf("ceci-n.est-pas-une-webhook-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDataSourceMutatingWebhookConfigurationV1_nonexistent(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "metadata.0.name", name),
resource.TestCheckResourceAttr(dataSourceName, "webhook.#", "0"),
),
},
},
})
}

func testAccKubernetesDataSourceMutatingWebhookConfigurationV1_basic(name string) string {
return fmt.Sprintf(`resource "kubernetes_mutating_webhook_configuration_v1" "test" {
metadata {
Expand Down Expand Up @@ -141,3 +161,12 @@ func testAccKubernetesDataSourceMutatingWebhookConfigurationV1_read() string {
}
`
}

func testAccKubernetesDataSourceMutatingWebhookConfigurationV1_nonexistent(name string) string {
return fmt.Sprintf(`data "kubernetes_mutating_webhook_configuration_v1" "test" {
metadata {
name = "%s"
}
}
`, name)
}
4 changes: 4 additions & 0 deletions kubernetes/data_source_kubernetes_namespace_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -51,6 +52,9 @@ func dataSourceKubernetesNamespaceV1Read(ctx context.Context, d *schema.Resource

namespace, err := conn.CoreV1().Namespaces().Get(ctx, metadata.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
log.Printf("[DEBUG] Received error: %#v", err)
return diag.FromErr(err)
}
Expand Down
29 changes: 29 additions & 0 deletions kubernetes/data_source_kubernetes_namespace_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package kubernetes

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

Expand All @@ -31,6 +33,24 @@ func TestAccKubernetesDataSourceNamespaceV1_basic(t *testing.T) {
})
}

func TestAccKubernetesDataSourceNamespaceV1_not_found(t *testing.T) {
dataSourceName := "data.kubernetes_namespace_v1.test"
name := fmt.Sprintf("ceci-n.est-pas-une-namespace-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDataSourceNamespaceV1_nonexistent(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "metadata.0.name", name),
resource.TestCheckResourceAttr(dataSourceName, "spec.#", "0"),
),
},
},
})
}

func testAccKubernetesDataSourceNamespaceV1_basic() string {
return `data "kubernetes_namespace_v1" "test" {
metadata {
Expand All @@ -39,3 +59,12 @@ func testAccKubernetesDataSourceNamespaceV1_basic() string {
}
`
}

func testAccKubernetesDataSourceNamespaceV1_nonexistent(name string) string {
return fmt.Sprintf(`data "kubernetes_namespace_v1" "test" {
metadata {
name = "%s"
}
}
`, name)
}
4 changes: 4 additions & 0 deletions kubernetes/data_source_kubernetes_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
Expand Down Expand Up @@ -89,6 +90,9 @@ func dataSourceKubernetesNodesRead(ctx context.Context, d *schema.ResourceData,
log.Printf("[INFO] Listing nodes")
nodesRaw, err := conn.CoreV1().Nodes().List(ctx, listOptions)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return diag.FromErr(err)
}
nodes := make([]interface{}, len(nodesRaw.Items))
Expand Down
Loading