-
Notifications
You must be signed in to change notification settings - Fork 994
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not remove server side fields for datasources
It does not make sense to delete server side fields for datasources, the user should be responsible for not creating loops which would cause a perpetual diff. This allows reading the status field for the generic `kubernetes_resource` datasource. Thus this PR Fixes this issue : #1699 Signed-off-by: Julien DOCHE <julien.doche@datadoghq.com>
- Loading branch information
1 parent
83c23ce
commit aaf6fbe
Showing
5 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
manifest/test/acceptance/datasource_resource_status_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//go:build acceptance | ||
// +build acceptance | ||
|
||
package acceptance | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-provider-kubernetes/manifest/provider" | ||
"github.com/hashicorp/terraform-provider-kubernetes/manifest/test/helper/kubernetes" | ||
|
||
tfstatehelper "github.com/hashicorp/terraform-provider-kubernetes/manifest/test/helper/state" | ||
) | ||
|
||
func TestDataSourceKubernetesResource_ConfigMap(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
reattachInfo, err := provider.ServeTest(ctx, hclog.Default(), t) | ||
if err != nil { | ||
t.Errorf("Failed to create provider instance: %q", err) | ||
} | ||
|
||
name := randName() | ||
namespace := randName() | ||
|
||
k8shelper.CreateNamespace(t, namespace) | ||
defer k8shelper.DeleteResource(t, namespace, kubernetes.NewGroupVersionResource("v1", "namespaces")) | ||
|
||
// STEP 1: Create a Deployment to use as a data source | ||
tf := tfhelper.RequireNewWorkingDir(ctx, t) | ||
tf.SetReattachInfo(ctx, reattachInfo) | ||
defer func() { | ||
tf.Destroy(ctx) | ||
tf.Close() | ||
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "configmaps", namespace, name) | ||
}() | ||
|
||
tfvars := TFVARS{ | ||
"name": name, | ||
"namespace": namespace, | ||
} | ||
tfconfig := loadTerraformConfig(t, "datasource-resource-status/step1.tf", tfvars) | ||
tf.SetConfig(ctx, tfconfig) | ||
tf.Init(ctx) | ||
tf.Apply(ctx) | ||
|
||
k8shelper.AssertNamespacedResourceExists(t, "v1", "deployments", namespace, name) | ||
|
||
state, err := tf.State(ctx) | ||
if err != nil { | ||
t.Fatalf("Failed to retrieve terraform state: %q", err) | ||
} | ||
tfstate := tfstatehelper.NewHelper(state) | ||
|
||
// STEP 2: Read the Deployment from step 1 using a kubernetes_resource data source | ||
reattachInfo2, err := provider.ServeTest(ctx, hclog.Default(), t) | ||
if err != nil { | ||
t.Errorf("Failed to create additional provider instance: %q", err) | ||
} | ||
step2 := tfhelper.RequireNewWorkingDir(ctx, t) | ||
step2.SetReattachInfo(ctx, reattachInfo2) | ||
defer func() { | ||
step2.Destroy(ctx) | ||
step2.Close() | ||
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "deployments", namespace, name2) | ||
}() | ||
|
||
tfconfig = loadTerraformConfig(t, "datasource-resource-status/step2.tf", tfvars) | ||
step2.SetConfig(ctx, string(tfconfig)) | ||
step2.Init(ctx) | ||
step2.Apply(ctx) | ||
|
||
s2, err := step2.State(ctx) | ||
if err != nil { | ||
t.Fatalf("Failed to retrieve terraform state: %q", err) | ||
} | ||
tfstate := tfstatehelper.NewHelper(s2) | ||
|
||
// check that the data source has the status field defined | ||
tfstate.AssertAttributeNotEmpty(t, "data.kubernetes_resource.test_deploy.object.status") | ||
} |
34 changes: 34 additions & 0 deletions
34
manifest/test/acceptance/testdata/datasource-resource-status/step1.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
resource "kubernetes_deployment" "test_deploy" { | ||
api_version = "v1" | ||
|
||
metadata = { | ||
name = var.name | ||
namespace = var.namespace | ||
} | ||
|
||
spec { | ||
replicas = 1 | ||
|
||
selector { | ||
match_labels = { | ||
test = "test-status" | ||
} | ||
} | ||
|
||
template { | ||
metadata { | ||
labels = { | ||
test = "test-status" | ||
} | ||
} | ||
|
||
spec { | ||
container { | ||
image = "nginx:1.21.6" | ||
name = "test" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
manifest/test/acceptance/testdata/datasource-resource-status/step2.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
data "kubernetes_resource" "test_deploy" { | ||
api_version = "v1" | ||
kind = "Deployment" | ||
metadata { | ||
name = var.name | ||
namespace = var.namespace | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
manifest/test/acceptance/testdata/datasource-resource-status/variables.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# These variable declarations are only used for interactive testing. | ||
# The test code will template in different variable declarations with a default value when running the test. | ||
# | ||
# To set values for interactive runs, create a var-file and set values in it. | ||
# If the name of the var-file ends in '.auto.tfvars' (e.g. myvalues.auto.tfvars) | ||
# it will be automatically picked up and used by Terraform. | ||
# | ||
# DO NOT check in any files named *.auto.tfvars when making changes to tests. | ||
|
||
variable "name" { | ||
type = string | ||
} | ||
|
||
variable "namespace" { | ||
type = string | ||
} |