From 209b0a5a054937bbc9369a1f582c855a5f994538 Mon Sep 17 00:00:00 2001 From: John Houston Date: Sun, 11 Sep 2022 15:29:08 -0400 Subject: [PATCH 1/2] Add "field_manager" attribute to kubernetes_labels, kubernetes_annotations, kubernetes_config_map_v1_data --- .changelog/1831.txt | 3 +++ kubernetes/resource_kubernetes_annotations.go | 8 +++++++- kubernetes/resource_kubernetes_annotations_test.go | 7 +++++++ kubernetes/resource_kubernetes_config_map_v1_data.go | 8 +++++++- kubernetes/resource_kubernetes_config_map_v1_data_test.go | 7 +++++++ kubernetes/resource_kubernetes_labels.go | 8 +++++++- kubernetes/resource_kubernetes_labels_test.go | 7 +++++++ website/docs/r/annotations.html.markdown | 1 + website/docs/r/config_map_v1_data.html.markdown | 1 + website/docs/r/labels.html.markdown | 1 + 10 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 .changelog/1831.txt diff --git a/.changelog/1831.txt b/.changelog/1831.txt new file mode 100644 index 0000000000..2ddd4df225 --- /dev/null +++ b/.changelog/1831.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +Add "field_manager" attribute to kubernetes_labels, kubernetes_annotations, kubernetes_config_map_v1_data +``` diff --git a/kubernetes/resource_kubernetes_annotations.go b/kubernetes/resource_kubernetes_annotations.go index b997bdf616..6cef14ef06 100644 --- a/kubernetes/resource_kubernetes_annotations.go +++ b/kubernetes/resource_kubernetes_annotations.go @@ -69,6 +69,12 @@ func resourceKubernetesAnnotations() *schema.Resource { Description: "Force overwriting annotations that were created or edited outside of Terraform.", Optional: true, }, + "field_manager": { + Type: schema.TypeString, + Description: "Set the name of the field manager for the specified labels.", + Optional: true, + Default: defaultFieldManagerName, + }, }, } } @@ -261,7 +267,7 @@ func resourceKubernetesAnnotationsUpdate(ctx context.Context, d *schema.Resource types.ApplyPatchType, patchbytes, v1.PatchOptions{ - FieldManager: defaultFieldManagerName, + FieldManager: d.Get("field_manager").(string), Force: ptrToBool(d.Get("force").(bool)), }, ) diff --git a/kubernetes/resource_kubernetes_annotations_test.go b/kubernetes/resource_kubernetes_annotations_test.go index ebc1f0d238..c26f52deae 100644 --- a/kubernetes/resource_kubernetes_annotations_test.go +++ b/kubernetes/resource_kubernetes_annotations_test.go @@ -32,6 +32,7 @@ func TestAccKubernetesAnnotations_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "kind", "ConfigMap"), resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name), resource.TestCheckResourceAttr(resourceName, "annotations.%", "0"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, { @@ -43,6 +44,7 @@ func TestAccKubernetesAnnotations_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "annotations.%", "2"), resource.TestCheckResourceAttr(resourceName, "annotations.test1", "one"), resource.TestCheckResourceAttr(resourceName, "annotations.test2", "two"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, { @@ -54,6 +56,7 @@ func TestAccKubernetesAnnotations_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "annotations.%", "2"), resource.TestCheckResourceAttr(resourceName, "annotations.test1", "one"), resource.TestCheckResourceAttr(resourceName, "annotations.test3", "three"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, { @@ -63,6 +66,7 @@ func TestAccKubernetesAnnotations_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "kind", "ConfigMap"), resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name), resource.TestCheckResourceAttr(resourceName, "annotations.%", "0"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, }, @@ -77,6 +81,7 @@ func testAccKubernetesAnnotations_empty(name string) string { name = %q } annotations = {} + field_manager = "tftest" } `, name) } @@ -92,6 +97,7 @@ func testAccKubernetesAnnotations_basic(name string) string { "test1" = "one" "test2" = "two" } + field_manager = "tftest" } `, name) } @@ -107,6 +113,7 @@ func testAccKubernetesAnnotations_modified(name string) string { "test1" = "one" "test3" = "three" } + field_manager = "tftest" } `, name) } diff --git a/kubernetes/resource_kubernetes_config_map_v1_data.go b/kubernetes/resource_kubernetes_config_map_v1_data.go index bd74051d66..5bb0834839 100644 --- a/kubernetes/resource_kubernetes_config_map_v1_data.go +++ b/kubernetes/resource_kubernetes_config_map_v1_data.go @@ -53,6 +53,12 @@ func resourceKubernetesConfigMapV1Data() *schema.Resource { Description: "Force overwriting data that is managed outside of Terraform.", Optional: true, }, + "field_manager": { + Type: schema.TypeString, + Description: "Set the name of the field manager for the specified labels.", + Optional: true, + Default: defaultFieldManagerName, + }, }, } } @@ -179,7 +185,7 @@ func resourceKubernetesConfigMapV1DataUpdate(ctx context.Context, d *schema.Reso types.ApplyPatchType, patchbytes, v1.PatchOptions{ - FieldManager: defaultFieldManagerName, + FieldManager: d.Get("field_manager").(string), Force: ptrToBool(d.Get("force").(bool)), }, ) diff --git a/kubernetes/resource_kubernetes_config_map_v1_data_test.go b/kubernetes/resource_kubernetes_config_map_v1_data_test.go index 6bf4bab3aa..630429a6fa 100644 --- a/kubernetes/resource_kubernetes_config_map_v1_data_test.go +++ b/kubernetes/resource_kubernetes_config_map_v1_data_test.go @@ -30,6 +30,7 @@ func TestAccKubernetesConfigMapV1Data_basic(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name), resource.TestCheckResourceAttr(resourceName, "data.%", "0"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, { @@ -39,6 +40,7 @@ func TestAccKubernetesConfigMapV1Data_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "data.%", "2"), resource.TestCheckResourceAttr(resourceName, "data.test1", "one"), resource.TestCheckResourceAttr(resourceName, "data.test2", "two"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, { @@ -48,6 +50,7 @@ func TestAccKubernetesConfigMapV1Data_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "data.%", "2"), resource.TestCheckResourceAttr(resourceName, "data.test1", "one"), resource.TestCheckResourceAttr(resourceName, "data.test3", "three"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, { @@ -55,6 +58,7 @@ func TestAccKubernetesConfigMapV1Data_basic(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name), resource.TestCheckResourceAttr(resourceName, "data.%", "0"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, }, @@ -67,6 +71,7 @@ func testAccKubernetesConfigMapV1Data_empty(name string) string { name = %q } data = {} + field_manager = "tftest" } `, name) } @@ -80,6 +85,7 @@ func testAccKubernetesConfigMapV1Data_basic(name string) string { "test1" = "one" "test2" = "two" } + field_manager = "tftest" } `, name) } @@ -93,6 +99,7 @@ func testAccKubernetesConfigMapV1Data_modified(name string) string { "test1" = "one" "test3" = "three" } + field_manager = "tftest" } `, name) } diff --git a/kubernetes/resource_kubernetes_labels.go b/kubernetes/resource_kubernetes_labels.go index 0e675ce4e2..5b3a2147fe 100644 --- a/kubernetes/resource_kubernetes_labels.go +++ b/kubernetes/resource_kubernetes_labels.go @@ -69,6 +69,12 @@ func resourceKubernetesLabels() *schema.Resource { Description: "Force overwriting labels that were created or edited outside of Terraform.", Optional: true, }, + "field_manager": { + Type: schema.TypeString, + Description: "Set the name of the field manager for the specified labels.", + Optional: true, + Default: defaultFieldManagerName, + }, }, } } @@ -261,7 +267,7 @@ func resourceKubernetesLabelsUpdate(ctx context.Context, d *schema.ResourceData, types.ApplyPatchType, patchbytes, v1.PatchOptions{ - FieldManager: defaultFieldManagerName, + FieldManager: d.Get("field_manager").(string), Force: ptrToBool(d.Get("force").(bool)), }, ) diff --git a/kubernetes/resource_kubernetes_labels_test.go b/kubernetes/resource_kubernetes_labels_test.go index 488a3272ca..a9ceeea042 100644 --- a/kubernetes/resource_kubernetes_labels_test.go +++ b/kubernetes/resource_kubernetes_labels_test.go @@ -35,6 +35,7 @@ func TestAccKubernetesLabels_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "kind", "ConfigMap"), resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name), resource.TestCheckResourceAttr(resourceName, "labels.%", "0"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, { @@ -46,6 +47,7 @@ func TestAccKubernetesLabels_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "labels.%", "2"), resource.TestCheckResourceAttr(resourceName, "labels.test1", "one"), resource.TestCheckResourceAttr(resourceName, "labels.test2", "two"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, { @@ -57,6 +59,7 @@ func TestAccKubernetesLabels_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "labels.%", "2"), resource.TestCheckResourceAttr(resourceName, "labels.test1", "one"), resource.TestCheckResourceAttr(resourceName, "labels.test3", "three"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, { @@ -66,6 +69,7 @@ func TestAccKubernetesLabels_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "kind", "ConfigMap"), resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name), resource.TestCheckResourceAttr(resourceName, "labels.%", "0"), + resource.TestCheckResourceAttr(resourceName, "field_manager", "tftest"), ), }, }, @@ -103,6 +107,7 @@ func testAccKubernetesLabels_empty(name string) string { name = %q } labels = {} + field_manager = "tftest" } `, name) } @@ -118,6 +123,7 @@ func testAccKubernetesLabels_basic(name string) string { "test1" = "one" "test2" = "two" } + field_manager = "tftest" } `, name) } @@ -133,6 +139,7 @@ func testAccKubernetesLabels_modified(name string) string { "test1" = "one" "test3" = "three" } + field_manager = "tftest" } `, name) } diff --git a/website/docs/r/annotations.html.markdown b/website/docs/r/annotations.html.markdown index c34d9f790f..922c9e3928 100644 --- a/website/docs/r/annotations.html.markdown +++ b/website/docs/r/annotations.html.markdown @@ -35,6 +35,7 @@ The following arguments are supported: * `metadata` - (Required) Standard metadata of the resource to be annotated. * `annotations` - (Required) A map of annotations to apply to the resource. * `force` - (Optional) Force management of annotations if there is a conflict. +* `field_manager` - (Optional) The name of the [field manager](https://kubernetes.io/docs/reference/using-api/server-side-apply/#field-management). Defaults to `Terraform`. ## Nested Blocks diff --git a/website/docs/r/config_map_v1_data.html.markdown b/website/docs/r/config_map_v1_data.html.markdown index 99ae418e34..2fa32110b5 100644 --- a/website/docs/r/config_map_v1_data.html.markdown +++ b/website/docs/r/config_map_v1_data.html.markdown @@ -31,6 +31,7 @@ The following arguments are supported: * `metadata` - (Required) Standard metadata of the ConfigMap. * `data` - (Required) A map of data to apply to the ConfigMap. * `force` - (Optional) Force management of the configured data if there is a conflict. +* `field_manager` - (Optional) The name of the [field manager](https://kubernetes.io/docs/reference/using-api/server-side-apply/#field-management). Defaults to `Terraform`. ## Nested Blocks diff --git a/website/docs/r/labels.html.markdown b/website/docs/r/labels.html.markdown index f22c017c4b..b2e1f85d1b 100644 --- a/website/docs/r/labels.html.markdown +++ b/website/docs/r/labels.html.markdown @@ -35,6 +35,7 @@ The following arguments are supported: * `metadata` - (Required) Standard metadata of the resource to be labelled. * `labels` - (Required) A map of labels to apply to the resource. * `force` - (Optional) Force management of labels if there is a conflict. +* `field_manager` - (Optional) The name of the [field manager](https://kubernetes.io/docs/reference/using-api/server-side-apply/#field-management). Defaults to `Terraform`. ## Nested Blocks From 0602523619d2d86306b23bb640c3e6719ace12ea Mon Sep 17 00:00:00 2001 From: John Houston Date: Mon, 10 Oct 2022 14:10:39 -0400 Subject: [PATCH 2/2] Add validation for empty field_manager value --- kubernetes/resource_kubernetes_annotations.go | 10 ++++++---- kubernetes/resource_kubernetes_labels.go | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/kubernetes/resource_kubernetes_annotations.go b/kubernetes/resource_kubernetes_annotations.go index 6cef14ef06..967f48f4f8 100644 --- a/kubernetes/resource_kubernetes_annotations.go +++ b/kubernetes/resource_kubernetes_annotations.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-kubernetes/util" "k8s.io/apimachinery/pkg/api/errors" @@ -70,10 +71,11 @@ func resourceKubernetesAnnotations() *schema.Resource { Optional: true, }, "field_manager": { - Type: schema.TypeString, - Description: "Set the name of the field manager for the specified labels.", - Optional: true, - Default: defaultFieldManagerName, + Type: schema.TypeString, + Description: "Set the name of the field manager for the specified labels.", + Optional: true, + Default: defaultFieldManagerName, + ValidateFunc: validation.StringIsNotWhiteSpace, }, }, } diff --git a/kubernetes/resource_kubernetes_labels.go b/kubernetes/resource_kubernetes_labels.go index 5b3a2147fe..ade40905ed 100644 --- a/kubernetes/resource_kubernetes_labels.go +++ b/kubernetes/resource_kubernetes_labels.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-kubernetes/util" "k8s.io/apimachinery/pkg/api/errors" @@ -70,10 +71,11 @@ func resourceKubernetesLabels() *schema.Resource { Optional: true, }, "field_manager": { - Type: schema.TypeString, - Description: "Set the name of the field manager for the specified labels.", - Optional: true, - Default: defaultFieldManagerName, + Type: schema.TypeString, + Description: "Set the name of the field manager for the specified labels.", + Optional: true, + Default: defaultFieldManagerName, + ValidateFunc: validation.StringIsNotWhiteSpace, }, }, }