From 870aadac62a34752e664cc8f616f6d7fe1f49f1f Mon Sep 17 00:00:00 2001 From: Aleksandr Rybolovlev Date: Tue, 2 Jan 2024 14:12:41 +0100 Subject: [PATCH] Switch to k8s.io/utils/ptr package (#2377) * Switch from in-house pointer functions to the ones from the Kubernetes team from the package k8s.io/utils. --- README.md | 2 +- go.mod | 2 +- go.sum | 4 +- kubernetes/provider.go | 5 +- kubernetes/resource_kubernetes_annotations.go | 3 +- .../resource_kubernetes_config_map_v1.go | 5 +- .../resource_kubernetes_config_map_v1_data.go | 3 +- kubernetes/resource_kubernetes_env.go | 3 +- kubernetes/resource_kubernetes_env_test.go | 14 ++--- kubernetes/resource_kubernetes_labels.go | 3 +- kubernetes/resource_kubernetes_node_taint.go | 3 +- kubernetes/resource_kubernetes_secret_v1.go | 5 +- .../resource_kubernetes_service_account_v1.go | 3 +- .../resource_kubernetes_service_v1_test.go | 5 +- kubernetes/structure_api_service_spec.go | 3 +- kubernetes/structure_cron_job_v1.go | 9 +-- kubernetes/structure_cron_job_v1beta1.go | 9 +-- kubernetes/structure_csi_driver.go | 5 +- kubernetes/structure_csi_driver_v1.go | 5 +- kubernetes/structure_endpoints.go | 3 +- kubernetes/structure_endpointslice.go | 19 ++++--- .../structure_horizontal_pod_autoscaler.go | 5 +- .../structure_horizontal_pod_autoscaler_v2.go | 7 ++- ...cture_horizontal_pod_autoscaler_v2beta2.go | 7 ++- kubernetes/structure_job.go | 13 +++-- ...tructure_mutating_webhook_configuration.go | 3 +- .../structure_persistent_volume_claim.go | 5 +- .../structure_persistent_volume_spec.go | 7 ++- .../structure_pod_security_policy_spec.go | 5 +- kubernetes/structure_service_spec.go | 3 +- ...ucture_validating_webhook_configuration.go | 3 +- kubernetes/structures.go | 20 ------- .../structures_admissionregistration.go | 7 ++- kubernetes/structures_container.go | 23 ++++---- kubernetes/structures_container_test.go | 9 +-- kubernetes/structures_daemonset.go | 3 +- kubernetes/structures_deployment.go | 7 ++- kubernetes/structures_pod.go | 55 ++++++++++--------- kubernetes/structures_pod_test.go | 45 +++++++-------- .../structures_replication_controller.go | 3 +- kubernetes/structures_stateful_set.go | 7 ++- kubernetes/structures_tokenrequest.go | 3 +- 42 files changed, 185 insertions(+), 168 deletions(-) diff --git a/README.md b/README.md index 7b71eeaf20..c91f3022c9 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Please note: We take Terraform's security and our users' trust very seriously. I ## Requirements - [Terraform](https://www.terraform.io/downloads.html) 0.12.x -- [Go](https://golang.org/doc/install) 1.16.x (to build the provider plugin) +- [Go](https://golang.org/doc/install) 1.18+ (to build the provider plugin) ## Contributing to the provider diff --git a/go.mod b/go.mod index 331b36bca0..800ea97b63 100644 --- a/go.mod +++ b/go.mod @@ -136,7 +136,7 @@ require ( k8s.io/component-base v0.27.8 // indirect k8s.io/klog/v2 v2.90.1 // indirect k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect - k8s.io/utils v0.0.0-20230209194617-a36077c30491 + k8s.io/utils v0.0.0-20231127182322-b307cd553661 sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/kustomize/api v0.13.2 // indirect sigs.k8s.io/kustomize/kyaml v0.14.1 // indirect diff --git a/go.sum b/go.sum index 6600832adc..8aa2c13724 100644 --- a/go.sum +++ b/go.sum @@ -572,8 +572,8 @@ k8s.io/kubectl v0.27.8 h1:VipG0f9E1kGRGJYm2/kNv188RgDduvx1g2q1b20niHg= k8s.io/kubectl v0.27.8/go.mod h1:ZufZqfI5V7oBuGFALJHoTxypO0fewOwbadr4saUkRKo= k8s.io/kubernetes v1.27.8 h1:K848lTo/D0jvrxUlTvw4nNADixbhXLHgKNDP/KlFGy8= k8s.io/kubernetes v1.27.8/go.mod h1:PUXXrx0IhAi+kI9BMDqNJHUnLndVv9W0DkriqyjuJOs= -k8s.io/utils v0.0.0-20230209194617-a36077c30491 h1:r0BAOLElQnnFhE/ApUsg3iHdVYYPBjNSSOMowRZxxsY= -k8s.io/utils v0.0.0-20230209194617-a36077c30491/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +k8s.io/utils v0.0.0-20231127182322-b307cd553661 h1:FepOBzJ0GXm8t0su67ln2wAZjbQ6RxQGZDnzuLcrUTI= +k8s.io/utils v0.0.0-20231127182322-b307cd553661/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= sigs.k8s.io/kustomize/api v0.13.2 h1:kejWfLeJhUsTGioDoFNJET5LQe/ajzXhJGYoU+pJsiA= diff --git a/kubernetes/provider.go b/kubernetes/provider.go index ca725f062e..c81bd92966 100644 --- a/kubernetes/provider.go +++ b/kubernetes/provider.go @@ -26,6 +26,7 @@ import ( "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" + "k8s.io/utils/ptr" apimachineryschema "k8s.io/apimachinery/pkg/runtime/schema" _ "k8s.io/client-go/plugin/pkg/client/auth" @@ -640,7 +641,7 @@ func useAdmissionregistrationV1beta1(conn *kubernetes.Clientset) (bool, error) { err = discovery.ServerSupportsVersion(d, v1) if err == nil { log.Printf("[INFO] Using %s/v1", group) - useadmissionregistrationv1beta1 = ptrToBool(false) + useadmissionregistrationv1beta1 = ptr.To(false) return false, nil } @@ -655,7 +656,7 @@ func useAdmissionregistrationV1beta1(conn *kubernetes.Clientset) (bool, error) { } log.Printf("[INFO] Using %s/v1beta1", group) - useadmissionregistrationv1beta1 = ptrToBool(true) + useadmissionregistrationv1beta1 = ptr.To(true) return true, nil } diff --git a/kubernetes/resource_kubernetes_annotations.go b/kubernetes/resource_kubernetes_annotations.go index 409fb56108..c3f3ef9e52 100644 --- a/kubernetes/resource_kubernetes_annotations.go +++ b/kubernetes/resource_kubernetes_annotations.go @@ -21,6 +21,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/dynamic" "k8s.io/client-go/restmapper" + "k8s.io/utils/ptr" ) func resourceKubernetesAnnotations() *schema.Resource { @@ -367,7 +368,7 @@ func resourceKubernetesAnnotationsUpdate(ctx context.Context, d *schema.Resource patchbytes, v1.PatchOptions{ FieldManager: d.Get("field_manager").(string), - Force: ptrToBool(d.Get("force").(bool)), + Force: ptr.To(d.Get("force").(bool)), }, ) if err != nil { diff --git a/kubernetes/resource_kubernetes_config_map_v1.go b/kubernetes/resource_kubernetes_config_map_v1.go index 6a314e6496..51d798e1c2 100644 --- a/kubernetes/resource_kubernetes_config_map_v1.go +++ b/kubernetes/resource_kubernetes_config_map_v1.go @@ -14,6 +14,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" pkgApi "k8s.io/apimachinery/pkg/types" + "k8s.io/utils/ptr" ) func resourceKubernetesConfigMapV1() *schema.Resource { @@ -82,7 +83,7 @@ func resourceKubernetesConfigMapV1Create(ctx context.Context, d *schema.Resource ObjectMeta: metadata, BinaryData: expandBase64MapToByteMap(d.Get("binary_data").(map[string]interface{})), Data: expandStringMap(d.Get("data").(map[string]interface{})), - Immutable: ptrToBool(d.Get("immutable").(bool)), + Immutable: ptr.To(d.Get("immutable").(bool)), } log.Printf("[INFO] Creating new config map: %#v", cfgMap) @@ -160,7 +161,7 @@ func resourceKubernetesConfigMapV1Update(ctx context.Context, d *schema.Resource if d.HasChange("immutable") { ops = append(ops, &ReplaceOperation{ Path: "/immutable", - Value: ptrToBool(d.Get("immutable").(bool)), + Value: ptr.To(d.Get("immutable").(bool)), }) } diff --git a/kubernetes/resource_kubernetes_config_map_v1_data.go b/kubernetes/resource_kubernetes_config_map_v1_data.go index 85ddb01b29..abc242fe10 100644 --- a/kubernetes/resource_kubernetes_config_map_v1_data.go +++ b/kubernetes/resource_kubernetes_config_map_v1_data.go @@ -15,6 +15,7 @@ import ( v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" + "k8s.io/utils/ptr" ) func resourceKubernetesConfigMapV1Data() *schema.Resource { @@ -194,7 +195,7 @@ func resourceKubernetesConfigMapV1DataUpdate(ctx context.Context, d *schema.Reso patchbytes, v1.PatchOptions{ FieldManager: d.Get("field_manager").(string), - Force: ptrToBool(d.Get("force").(bool)), + Force: ptr.To(d.Get("force").(bool)), }, ) if err != nil { diff --git a/kubernetes/resource_kubernetes_env.go b/kubernetes/resource_kubernetes_env.go index e4f64a5c72..36151ffa04 100644 --- a/kubernetes/resource_kubernetes_env.go +++ b/kubernetes/resource_kubernetes_env.go @@ -16,6 +16,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/types" + "k8s.io/utils/ptr" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -512,7 +513,7 @@ func resourceKubernetesEnvUpdate(ctx context.Context, d *schema.ResourceData, m patchbytes, v1.PatchOptions{ FieldManager: d.Get("field_manager").(string), - Force: ptrToBool(d.Get("force").(bool)), + Force: ptr.To(d.Get("force").(bool)), }, ) if err != nil { diff --git a/kubernetes/resource_kubernetes_env_test.go b/kubernetes/resource_kubernetes_env_test.go index 31057dc2da..b134b2063b 100644 --- a/kubernetes/resource_kubernetes_env_test.go +++ b/kubernetes/resource_kubernetes_env_test.go @@ -15,7 +15,7 @@ import ( batchv1 "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - utils "k8s.io/utils/pointer" + "k8s.io/utils/ptr" ) func TestAccKubernetesEnv_DeploymentBasic(t *testing.T) { @@ -430,12 +430,12 @@ func createCronJobInitContainerEnv(t *testing.T, name, namespace string) error { } ctx := context.Background() - var failJobLimit *int32 = utils.Int32(2) - var startingDeadlineSeconds *int64 = utils.Int64(2) - var successfulJobsLimit *int32 = utils.Int32(2) - var boLimit *int32 = utils.Int32(2) - var ttl *int32 = utils.Int32(2) - var cronjob batchv1.CronJob = batchv1.CronJob{ + failJobLimit := ptr.To(int32(2)) + startingDeadlineSeconds := ptr.To(int64(2)) + successfulJobsLimit := ptr.To(int32(2)) + boLimit := ptr.To(int32(2)) + ttl := ptr.To(int32(2)) + cronjob := batchv1.CronJob{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: namespace, diff --git a/kubernetes/resource_kubernetes_labels.go b/kubernetes/resource_kubernetes_labels.go index 82171e7a73..f7c494451b 100644 --- a/kubernetes/resource_kubernetes_labels.go +++ b/kubernetes/resource_kubernetes_labels.go @@ -21,6 +21,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/dynamic" "k8s.io/client-go/restmapper" + "k8s.io/utils/ptr" ) func resourceKubernetesLabels() *schema.Resource { @@ -277,7 +278,7 @@ func resourceKubernetesLabelsUpdate(ctx context.Context, d *schema.ResourceData, patchbytes, v1.PatchOptions{ FieldManager: d.Get("field_manager").(string), - Force: ptrToBool(d.Get("force").(bool)), + Force: ptr.To(d.Get("force").(bool)), }, ) if err != nil { diff --git a/kubernetes/resource_kubernetes_node_taint.go b/kubernetes/resource_kubernetes_node_taint.go index 5fa19c52bf..887d80ae08 100644 --- a/kubernetes/resource_kubernetes_node_taint.go +++ b/kubernetes/resource_kubernetes_node_taint.go @@ -14,6 +14,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" + "k8s.io/utils/ptr" ) func resourceKubernetesNodeTaint() *schema.Resource { @@ -172,7 +173,7 @@ func resourceKubernetesNodeTaintUpdate(ctx context.Context, d *schema.ResourceDa } patchOpts := metav1.PatchOptions{ FieldManager: d.Get("field_manager").(string), - Force: ptrToBool(d.Get("force").(bool)), + Force: ptr.To(d.Get("force").(bool)), } node, err := nodeApi.Patch(ctx, nodeName, types.ApplyPatchType, patchBytes, patchOpts) if err != nil { diff --git a/kubernetes/resource_kubernetes_secret_v1.go b/kubernetes/resource_kubernetes_secret_v1.go index 248e7457ac..b533e81ddf 100644 --- a/kubernetes/resource_kubernetes_secret_v1.go +++ b/kubernetes/resource_kubernetes_secret_v1.go @@ -17,6 +17,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" pkgApi "k8s.io/apimachinery/pkg/types" + "k8s.io/utils/ptr" ) func resourceKubernetesSecretV1() *schema.Resource { @@ -125,7 +126,7 @@ func resourceKubernetesSecretV1Create(ctx context.Context, d *schema.ResourceDat } if v, ok := d.GetOk("immutable"); ok { - secret.Immutable = ptrToBool(v.(bool)) + secret.Immutable = ptr.To(v.(bool)) } log.Printf("[INFO] Creating new secret: %#v", secret) @@ -268,7 +269,7 @@ func resourceKubernetesSecretV1Update(ctx context.Context, d *schema.ResourceDat if d.HasChange("immutable") { ops = append(ops, &ReplaceOperation{ Path: "/immutable", - Value: ptrToBool(d.Get("immutable").(bool)), + Value: ptr.To(d.Get("immutable").(bool)), }) } diff --git a/kubernetes/resource_kubernetes_service_account_v1.go b/kubernetes/resource_kubernetes_service_account_v1.go index 11c327714a..f4bb756254 100644 --- a/kubernetes/resource_kubernetes_service_account_v1.go +++ b/kubernetes/resource_kubernetes_service_account_v1.go @@ -19,6 +19,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" pkgApi "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes" + "k8s.io/utils/ptr" ) func resourceKubernetesServiceAccountV1() *schema.Resource { @@ -88,7 +89,7 @@ func resourceKubernetesServiceAccountV1Create(ctx context.Context, d *schema.Res metadata := expandMetadata(d.Get("metadata").([]interface{})) svcAcc := corev1.ServiceAccount{ - AutomountServiceAccountToken: ptrToBool(d.Get("automount_service_account_token").(bool)), + AutomountServiceAccountToken: ptr.To(d.Get("automount_service_account_token").(bool)), ObjectMeta: metadata, ImagePullSecrets: expandLocalObjectReferenceArray(d.Get("image_pull_secret").(*schema.Set).List()), Secrets: expandServiceAccountSecrets(d.Get("secret").(*schema.Set).List(), ""), diff --git a/kubernetes/resource_kubernetes_service_v1_test.go b/kubernetes/resource_kubernetes_service_v1_test.go index 65f7b7028f..0cbf4b4b56 100644 --- a/kubernetes/resource_kubernetes_service_v1_test.go +++ b/kubernetes/resource_kubernetes_service_v1_test.go @@ -16,6 +16,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" + "k8s.io/utils/ptr" ) func TestAccKubernetesServiceV1_basic(t *testing.T) { @@ -504,14 +505,14 @@ func TestAccKubernetesServiceV1_nodePort(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "spec.0.type", "NodePort"), testAccCheckServiceV1Ports(&conf, []corev1.ServicePort{ { - AppProtocol: ptrToString("ssh"), + AppProtocol: ptr.To("ssh"), Name: "first", Port: int32(10222), Protocol: corev1.ProtocolTCP, TargetPort: intstr.FromInt(22), }, { - AppProtocol: ptrToString("terraform.io/kubernetes"), + AppProtocol: ptr.To("terraform.io/kubernetes"), Name: "second", Port: int32(10333), Protocol: corev1.ProtocolTCP, diff --git a/kubernetes/structure_api_service_spec.go b/kubernetes/structure_api_service_spec.go index cadd2e0739..38124d4330 100644 --- a/kubernetes/structure_api_service_spec.go +++ b/kubernetes/structure_api_service_spec.go @@ -5,6 +5,7 @@ package kubernetes import ( v1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1" + "k8s.io/utils/ptr" ) // Flatteners @@ -62,7 +63,7 @@ func expandAPIServiceV1Spec(l []interface{}) v1.APIServiceSpec { } if v, ok := m["port"].(int); ok && v > 0 { - obj.Service.Port = ptrToInt32(int32(v)) + obj.Service.Port = ptr.To(int32(v)) } } if v, ok := in["version"].(string); ok { diff --git a/kubernetes/structure_cron_job_v1.go b/kubernetes/structure_cron_job_v1.go index 1a2128fd7a..0a09164f41 100644 --- a/kubernetes/structure_cron_job_v1.go +++ b/kubernetes/structure_cron_job_v1.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" batch "k8s.io/api/batch/v1" + "k8s.io/utils/ptr" ) func flattenCronJobSpecV1(in batch.CronJobSpec, d *schema.ResourceData, meta interface{}) ([]interface{}, error) { @@ -69,7 +70,7 @@ func expandCronJobSpecV1(j []interface{}) (batch.CronJobSpec, error) { } if v, ok := in["failed_jobs_history_limit"].(int); ok && v != 1 { - obj.FailedJobsHistoryLimit = ptrToInt32(int32(v)) + obj.FailedJobsHistoryLimit = ptr.To(int32(v)) } if v, ok := in["schedule"].(string); ok && v != "" { @@ -87,15 +88,15 @@ func expandCronJobSpecV1(j []interface{}) (batch.CronJobSpec, error) { obj.JobTemplate = jtSpec if v, ok := in["starting_deadline_seconds"].(int); ok && v > 0 { - obj.StartingDeadlineSeconds = ptrToInt64(int64(v)) + obj.StartingDeadlineSeconds = ptr.To(int64(v)) } if v, ok := in["successful_jobs_history_limit"].(int); ok && v != 3 { - obj.SuccessfulJobsHistoryLimit = ptrToInt32(int32(v)) + obj.SuccessfulJobsHistoryLimit = ptr.To(int32(v)) } if v, ok := in["suspend"].(bool); ok { - obj.Suspend = ptrToBool(v) + obj.Suspend = ptr.To(v) } return obj, nil diff --git a/kubernetes/structure_cron_job_v1beta1.go b/kubernetes/structure_cron_job_v1beta1.go index c03ca2316e..fe8339899b 100644 --- a/kubernetes/structure_cron_job_v1beta1.go +++ b/kubernetes/structure_cron_job_v1beta1.go @@ -6,6 +6,7 @@ package kubernetes import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "k8s.io/api/batch/v1beta1" + "k8s.io/utils/ptr" ) func flattenCronJobSpecV1Beta1(in v1beta1.CronJobSpec, d *schema.ResourceData, meta interface{}) ([]interface{}, error) { @@ -66,7 +67,7 @@ func expandCronJobSpecV1Beta1(j []interface{}) (v1beta1.CronJobSpec, error) { } if v, ok := in["failed_jobs_history_limit"].(int); ok && v != 1 { - obj.FailedJobsHistoryLimit = ptrToInt32(int32(v)) + obj.FailedJobsHistoryLimit = ptr.To(int32(v)) } if v, ok := in["schedule"].(string); ok && v != "" { @@ -80,15 +81,15 @@ func expandCronJobSpecV1Beta1(j []interface{}) (v1beta1.CronJobSpec, error) { obj.JobTemplate = jtSpec if v, ok := in["starting_deadline_seconds"].(int); ok && v > 0 { - obj.StartingDeadlineSeconds = ptrToInt64(int64(v)) + obj.StartingDeadlineSeconds = ptr.To(int64(v)) } if v, ok := in["successful_jobs_history_limit"].(int); ok && v != 3 { - obj.SuccessfulJobsHistoryLimit = ptrToInt32(int32(v)) + obj.SuccessfulJobsHistoryLimit = ptr.To(int32(v)) } if v, ok := in["suspend"].(bool); ok { - obj.Suspend = ptrToBool(v) + obj.Suspend = ptr.To(v) } return obj, nil diff --git a/kubernetes/structure_csi_driver.go b/kubernetes/structure_csi_driver.go index 0dfc9ec827..85698928f4 100644 --- a/kubernetes/structure_csi_driver.go +++ b/kubernetes/structure_csi_driver.go @@ -6,6 +6,7 @@ package kubernetes import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" storage "k8s.io/api/storage/v1beta1" + "k8s.io/utils/ptr" ) func expandCSIDriverSpec(l []interface{}) storage.CSIDriverSpec { @@ -16,11 +17,11 @@ func expandCSIDriverSpec(l []interface{}) storage.CSIDriverSpec { obj := storage.CSIDriverSpec{} if v, ok := in["attach_required"].(bool); ok { - obj.AttachRequired = ptrToBool(v) + obj.AttachRequired = ptr.To(v) } if v, ok := in["pod_info_on_mount"].(bool); ok { - obj.PodInfoOnMount = ptrToBool(v) + obj.PodInfoOnMount = ptr.To(v) } if v, ok := in["volume_lifecycle_modes"].([]interface{}); ok && len(v) > 0 { diff --git a/kubernetes/structure_csi_driver_v1.go b/kubernetes/structure_csi_driver_v1.go index 8141009cc5..1c27f2fe79 100644 --- a/kubernetes/structure_csi_driver_v1.go +++ b/kubernetes/structure_csi_driver_v1.go @@ -6,6 +6,7 @@ package kubernetes import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" storage "k8s.io/api/storage/v1" + "k8s.io/utils/ptr" ) func expandCSIDriverV1Spec(l []interface{}) storage.CSIDriverSpec { @@ -16,11 +17,11 @@ func expandCSIDriverV1Spec(l []interface{}) storage.CSIDriverSpec { obj := storage.CSIDriverSpec{} if v, ok := in["attach_required"].(bool); ok { - obj.AttachRequired = ptrToBool(v) + obj.AttachRequired = ptr.To(v) } if v, ok := in["pod_info_on_mount"].(bool); ok { - obj.PodInfoOnMount = ptrToBool(v) + obj.PodInfoOnMount = ptr.To(v) } if v, ok := in["volume_lifecycle_modes"].([]interface{}); ok && len(v) > 0 { diff --git a/kubernetes/structure_endpoints.go b/kubernetes/structure_endpoints.go index f709c028c9..83928b215f 100644 --- a/kubernetes/structure_endpoints.go +++ b/kubernetes/structure_endpoints.go @@ -6,6 +6,7 @@ package kubernetes import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" api "k8s.io/api/core/v1" + "k8s.io/utils/ptr" ) func expandEndpointsAddresses(in *schema.Set) []api.EndpointAddress { @@ -23,7 +24,7 @@ func expandEndpointsAddresses(in *schema.Set) []api.EndpointAddress { r.IP = v } if v, ok := addrCfg["node_name"].(string); ok && v != "" { - r.NodeName = ptrToString(v) + r.NodeName = ptr.To(v) } addresses[i] = r } diff --git a/kubernetes/structure_endpointslice.go b/kubernetes/structure_endpointslice.go index a555454fe0..aeba28c25e 100644 --- a/kubernetes/structure_endpointslice.go +++ b/kubernetes/structure_endpointslice.go @@ -9,6 +9,7 @@ import ( v1 "k8s.io/api/core/v1" api "k8s.io/api/discovery/v1" "k8s.io/apimachinery/pkg/types" + "k8s.io/utils/ptr" ) func expandEndpointSliceEndpoints(in []interface{}) []api.Endpoint { @@ -26,16 +27,16 @@ func expandEndpointSliceEndpoints(in []interface{}) []api.Endpoint { r.Conditions = expandEndpointSliceConditions(v) } if v, ok := endpointConfig["hostname"].(string); ok && v != "" { - r.Hostname = ptrToString(v) + r.Hostname = ptr.To(v) } if v, ok := endpointConfig["node_name"].(string); ok && v != "" { - r.NodeName = ptrToString(v) + r.NodeName = ptr.To(v) } if v, ok := endpointConfig["target_ref"].([]interface{}); ok && len(v) != 0 { r.TargetRef = expandObjectReference(v) } if v, ok := endpointConfig["zone"].(string); ok && v != "" { - r.Zone = ptrToString(v) + r.Zone = ptr.To(v) } endpoints[i] = r @@ -78,20 +79,20 @@ func expandEndpointSlicePorts(in []interface{}) []api.EndpointPort { r := api.EndpointPort{} portCfg := port.(map[string]interface{}) if v, ok := portCfg["name"].(string); ok { - r.Name = ptrToString(v) + r.Name = ptr.To(v) } if v, ok := portCfg["port"].(string); ok { if v == "" { continue } v, _ := strconv.ParseInt(v, 10, 32) - r.Port = ptrToInt32(int32(v)) + r.Port = ptr.To(int32(v)) } if v, ok := portCfg["protocol"].(v1.Protocol); ok { r.Protocol = &v } if v, ok := portCfg["app_protocol"].(string); ok { - r.AppProtocol = ptrToString(v) + r.AppProtocol = ptr.To(v) } ports[i] = r } @@ -107,13 +108,13 @@ func expandEndpointSliceConditions(in []interface{}) api.EndpointConditions { cond := in[0].(map[string]interface{}) if v, ok := cond["ready"].(bool); ok { - obj.Ready = ptrToBool(v) + obj.Ready = ptr.To(v) } if v, ok := cond["serving"].(bool); ok { - obj.Serving = ptrToBool(v) + obj.Serving = ptr.To(v) } if v, ok := cond["terminating"].(bool); ok { - obj.Terminating = ptrToBool(v) + obj.Terminating = ptr.To(v) } return obj diff --git a/kubernetes/structure_horizontal_pod_autoscaler.go b/kubernetes/structure_horizontal_pod_autoscaler.go index 82261e955a..8aae142220 100644 --- a/kubernetes/structure_horizontal_pod_autoscaler.go +++ b/kubernetes/structure_horizontal_pod_autoscaler.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" api "k8s.io/api/autoscaling/v1" + "k8s.io/utils/ptr" ) func expandHorizontalPodAutoscalerSpec(in []interface{}) (*api.HorizontalPodAutoscalerSpec, error) { @@ -20,13 +21,13 @@ func expandHorizontalPodAutoscalerSpec(in []interface{}) (*api.HorizontalPodAuto spec.MaxReplicas = int32(v.(int)) } if v, ok := m["min_replicas"].(int); ok && v > 0 { - spec.MinReplicas = ptrToInt32(int32(v)) + spec.MinReplicas = ptr.To(int32(v)) } if v, ok := m["scale_target_ref"]; ok { spec.ScaleTargetRef = expandCrossVersionObjectReference(v.([]interface{})) } if v, ok := m["target_cpu_utilization_percentage"].(int); ok && v > 0 { - spec.TargetCPUUtilizationPercentage = ptrToInt32(int32(v)) + spec.TargetCPUUtilizationPercentage = ptr.To(int32(v)) } return spec, nil diff --git a/kubernetes/structure_horizontal_pod_autoscaler_v2.go b/kubernetes/structure_horizontal_pod_autoscaler_v2.go index fa5973f97e..10545faddb 100644 --- a/kubernetes/structure_horizontal_pod_autoscaler_v2.go +++ b/kubernetes/structure_horizontal_pod_autoscaler_v2.go @@ -10,6 +10,7 @@ import ( autoscalingv2 "k8s.io/api/autoscaling/v2" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/utils/ptr" ) func expandHorizontalPodAutoscalerV2Spec(in []interface{}) (*autoscalingv2.HorizontalPodAutoscalerSpec, error) { @@ -25,7 +26,7 @@ func expandHorizontalPodAutoscalerV2Spec(in []interface{}) (*autoscalingv2.Horiz } if v, ok := m["min_replicas"].(int); ok && v > 0 { - spec.MinReplicas = ptrToInt32(int32(v)) + spec.MinReplicas = ptr.To(int32(v)) } if v, ok := m["scale_target_ref"]; ok { @@ -68,7 +69,7 @@ func expandV2MetricTarget(m map[string]interface{}) autoscalingv2.MetricTarget { } case autoscalingv2.UtilizationMetricType: if v, ok := m["average_utilization"].(int); ok && v > 0 { - target.AverageUtilization = ptrToInt32(int32(v)) + target.AverageUtilization = ptr.To(int32(v)) } case autoscalingv2.ValueMetricType: if v, ok := m["value"].(string); ok && v != "0" && v != "" { @@ -235,7 +236,7 @@ func expandV2ScalingRules(in []interface{}) *autoscalingv2.HPAScalingRules { } if v, ok := r["stabilization_window_seconds"].(int); ok { - spec.StabilizationWindowSeconds = ptrToInt32(int32(v)) + spec.StabilizationWindowSeconds = ptr.To(int32(v)) } return spec diff --git a/kubernetes/structure_horizontal_pod_autoscaler_v2beta2.go b/kubernetes/structure_horizontal_pod_autoscaler_v2beta2.go index e37968d992..c28a5a5892 100644 --- a/kubernetes/structure_horizontal_pod_autoscaler_v2beta2.go +++ b/kubernetes/structure_horizontal_pod_autoscaler_v2beta2.go @@ -10,6 +10,7 @@ import ( autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/utils/ptr" ) func expandHorizontalPodAutoscalerV2Beta2Spec(in []interface{}) (*autoscalingv2beta2.HorizontalPodAutoscalerSpec, error) { @@ -25,7 +26,7 @@ func expandHorizontalPodAutoscalerV2Beta2Spec(in []interface{}) (*autoscalingv2b } if v, ok := m["min_replicas"].(int); ok && v > 0 { - spec.MinReplicas = ptrToInt32(int32(v)) + spec.MinReplicas = ptr.To(int32(v)) } if v, ok := m["scale_target_ref"]; ok { @@ -68,7 +69,7 @@ func expandV2Beta2MetricTarget(m map[string]interface{}) autoscalingv2beta2.Metr } case autoscalingv2beta2.UtilizationMetricType: if v, ok := m["average_utilization"].(int); ok && v > 0 { - target.AverageUtilization = ptrToInt32(int32(v)) + target.AverageUtilization = ptr.To(int32(v)) } case autoscalingv2beta2.ValueMetricType: if v, ok := m["value"].(string); ok && v != "0" && v != "" { @@ -235,7 +236,7 @@ func expandV2Beta2ScalingRules(in []interface{}) *autoscalingv2beta2.HPAScalingR } if v, ok := r["stabilization_window_seconds"].(int); ok { - spec.StabilizationWindowSeconds = ptrToInt32(int32(v)) + spec.StabilizationWindowSeconds = ptr.To(int32(v)) } return spec diff --git a/kubernetes/structure_job.go b/kubernetes/structure_job.go index 1846f2d0af..517a4c4cf1 100644 --- a/kubernetes/structure_job.go +++ b/kubernetes/structure_job.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" batchv1 "k8s.io/api/batch/v1" + "k8s.io/utils/ptr" ) func flattenJobV1Spec(in batchv1.JobSpec, d *schema.ResourceData, meta interface{}, prefix ...string) ([]interface{}, error) { @@ -66,15 +67,15 @@ func expandJobV1Spec(j []interface{}) (batchv1.JobSpec, error) { in := j[0].(map[string]interface{}) if v, ok := in["active_deadline_seconds"].(int); ok && v > 0 { - obj.ActiveDeadlineSeconds = ptrToInt64(int64(v)) + obj.ActiveDeadlineSeconds = ptr.To(int64(v)) } if v, ok := in["backoff_limit"].(int); ok && v >= 0 { - obj.BackoffLimit = ptrToInt32(int32(v)) + obj.BackoffLimit = ptr.To(int32(v)) } if v, ok := in["completions"].(int); ok && v > 0 { - obj.Completions = ptrToInt32(int32(v)) + obj.Completions = ptr.To(int32(v)) } if v, ok := in["completion_mode"].(string); ok && v != "" { @@ -83,11 +84,11 @@ func expandJobV1Spec(j []interface{}) (batchv1.JobSpec, error) { } if v, ok := in["manual_selector"]; ok { - obj.ManualSelector = ptrToBool(v.(bool)) + obj.ManualSelector = ptr.To(v.(bool)) } if v, ok := in["parallelism"].(int); ok && v >= 0 { - obj.Parallelism = ptrToInt32(int32(v)) + obj.Parallelism = ptr.To(int32(v)) } if v, ok := in["selector"].([]interface{}); ok && len(v) > 0 { @@ -105,7 +106,7 @@ func expandJobV1Spec(j []interface{}) (batchv1.JobSpec, error) { if err != nil { return obj, err } - obj.TTLSecondsAfterFinished = ptrToInt32(int32(i)) + obj.TTLSecondsAfterFinished = ptr.To(int32(i)) } return obj, nil diff --git a/kubernetes/structure_mutating_webhook_configuration.go b/kubernetes/structure_mutating_webhook_configuration.go index 3c777748be..3901819468 100644 --- a/kubernetes/structure_mutating_webhook_configuration.go +++ b/kubernetes/structure_mutating_webhook_configuration.go @@ -5,6 +5,7 @@ package kubernetes import ( admissionregistrationv1 "k8s.io/api/admissionregistration/v1" + "k8s.io/utils/ptr" ) func flattenMutatingWebhook(in admissionregistrationv1.MutatingWebhook) map[string]interface{} { @@ -109,7 +110,7 @@ func expandMutatingWebhook(in map[string]interface{}) admissionregistrationv1.Mu } if v, ok := in["timeout_seconds"].(int); ok { - obj.TimeoutSeconds = ptrToInt32(int32(v)) + obj.TimeoutSeconds = ptr.To(int32(v)) } return obj diff --git a/kubernetes/structure_persistent_volume_claim.go b/kubernetes/structure_persistent_volume_claim.go index 4e93bd8796..15c0d916fa 100644 --- a/kubernetes/structure_persistent_volume_claim.go +++ b/kubernetes/structure_persistent_volume_claim.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" corev1 "k8s.io/api/core/v1" + "k8s.io/utils/ptr" ) // Flatteners @@ -85,10 +86,10 @@ func expandPersistentVolumeClaimSpec(l []interface{}) (*corev1.PersistentVolumeC obj.VolumeName = v } if v, ok := in["storage_class_name"].(string); ok && v != "" { - obj.StorageClassName = ptrToString(v) + obj.StorageClassName = ptr.To(v) } if v, ok := in["volume_mode"].(string); ok && v != "" { - obj.VolumeMode = pointerOf(corev1.PersistentVolumeMode(v)) + obj.VolumeMode = ptr.To(corev1.PersistentVolumeMode(v)) } return obj, nil } diff --git a/kubernetes/structure_persistent_volume_spec.go b/kubernetes/structure_persistent_volume_spec.go index ea1574dee2..2915d8ed7f 100644 --- a/kubernetes/structure_persistent_volume_spec.go +++ b/kubernetes/structure_persistent_volume_spec.go @@ -5,6 +5,7 @@ package kubernetes import ( v1 "k8s.io/api/core/v1" + "k8s.io/utils/ptr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -579,10 +580,10 @@ func expandAzureDiskVolumeSource(l []interface{}) *v1.AzureDiskVolumeSource { DataDiskURI: in["data_disk_uri"].(string), } if v, ok := in["fs_type"].(string); ok { - obj.FSType = ptrToString(v) + obj.FSType = ptr.To(v) } if v, ok := in["read_only"].(bool); ok { - obj.ReadOnly = ptrToBool(v) + obj.ReadOnly = ptr.To(v) } if v, ok := in["kind"].(string); ok && in["kind"].(string) != "" { kind := v1.AzureDataDiskKind(v) @@ -717,7 +718,7 @@ func expandFCVolumeSource(l []interface{}) *v1.FCVolumeSource { in := l[0].(map[string]interface{}) obj := &v1.FCVolumeSource{ TargetWWNs: sliceOfString(in["target_ww_ns"].(*schema.Set).List()), - Lun: ptrToInt32(int32(in["lun"].(int))), + Lun: ptr.To(int32(in["lun"].(int))), } if v, ok := in["fs_type"].(string); ok { obj.FSType = v diff --git a/kubernetes/structure_pod_security_policy_spec.go b/kubernetes/structure_pod_security_policy_spec.go index e5dbadd3f3..d9c70bafca 100644 --- a/kubernetes/structure_pod_security_policy_spec.go +++ b/kubernetes/structure_pod_security_policy_spec.go @@ -10,6 +10,7 @@ import ( v1 "k8s.io/api/core/v1" v1beta1 "k8s.io/api/policy/v1beta1" + "k8s.io/utils/ptr" ) func flattenPodSecurityPolicySpec(in v1beta1.PodSecurityPolicySpec) []interface{} { @@ -221,7 +222,7 @@ func expandPodSecurityPolicySpec(in []interface{}) (v1beta1.PodSecurityPolicySpe } if v, ok := m["allow_privilege_escalation"].(bool); ok { - spec.AllowPrivilegeEscalation = ptrToBool(v) + spec.AllowPrivilegeEscalation = ptr.To(v) } if v, ok := m["allowed_capabilities"].([]interface{}); ok && len(v) > 0 { @@ -249,7 +250,7 @@ func expandPodSecurityPolicySpec(in []interface{}) (v1beta1.PodSecurityPolicySpe } if v, ok := m["default_allow_privilege_escalation"].(bool); ok { - spec.DefaultAllowPrivilegeEscalation = ptrToBool(v) + spec.DefaultAllowPrivilegeEscalation = ptr.To(v) } if v, ok := m["forbidden_sysctls"].([]interface{}); ok && len(v) > 0 { diff --git a/kubernetes/structure_service_spec.go b/kubernetes/structure_service_spec.go index a364b11cdd..ab0b869616 100644 --- a/kubernetes/structure_service_spec.go +++ b/kubernetes/structure_service_spec.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/intstr" + "k8s.io/utils/ptr" ) // Flatteners @@ -188,7 +189,7 @@ func expandSessionAffinityConfigClientIP(l []interface{}) *v1.ClientIPConfig { in := l[0].(map[string]interface{}) if v, ok := in["timeout_seconds"].(int); ok { - obj.TimeoutSeconds = ptrToInt32(int32(v)) + obj.TimeoutSeconds = ptr.To(int32(v)) } return obj diff --git a/kubernetes/structure_validating_webhook_configuration.go b/kubernetes/structure_validating_webhook_configuration.go index 90a433d7c1..7229f6cad6 100644 --- a/kubernetes/structure_validating_webhook_configuration.go +++ b/kubernetes/structure_validating_webhook_configuration.go @@ -5,6 +5,7 @@ package kubernetes import ( admissionregistrationv1 "k8s.io/api/admissionregistration/v1" + "k8s.io/utils/ptr" ) func flattenValidatingWebhook(in admissionregistrationv1.ValidatingWebhook) map[string]interface{} { @@ -100,7 +101,7 @@ func expandValidatingWebhook(in map[string]interface{}) admissionregistrationv1. } if v, ok := in["timeout_seconds"].(int); ok { - obj.TimeoutSeconds = ptrToInt32(int32(v)) + obj.TimeoutSeconds = ptr.To(int32(v)) } return obj diff --git a/kubernetes/structures.go b/kubernetes/structures.go index 14df5c2c1e..a62531f784 100644 --- a/kubernetes/structures.go +++ b/kubernetes/structures.go @@ -227,26 +227,6 @@ func flattenByteMapToStringMap(m map[string][]byte) map[string]string { return result } -func ptrToString(s string) *string { - return &s -} - -func ptrToBool(b bool) *bool { - return &b -} - -func ptrToInt32(i int32) *int32 { - return &i -} - -func ptrToInt64(i int64) *int64 { - return &i -} - -func pointerOf[A any](a A) *A { - return &a -} - func sliceOfString(slice []interface{}) []string { result := make([]string, len(slice)) for i, s := range slice { diff --git a/kubernetes/structures_admissionregistration.go b/kubernetes/structures_admissionregistration.go index 8efcd6cf89..bd6727505f 100644 --- a/kubernetes/structures_admissionregistration.go +++ b/kubernetes/structures_admissionregistration.go @@ -5,6 +5,7 @@ package kubernetes import ( admissionregistrationv1 "k8s.io/api/admissionregistration/v1" + "k8s.io/utils/ptr" ) func flattenServiceReference(in admissionregistrationv1.ServiceReference) []interface{} { @@ -42,11 +43,11 @@ func expandServiceReference(l []interface{}) *admissionregistrationv1.ServiceRef } if v, ok := in["path"].(string); ok && v != "" { - obj.Path = ptrToString(v) + obj.Path = ptr.To(v) } if v, ok := in["port"].(int); ok { - obj.Port = ptrToInt32(int32(v)) + obj.Port = ptr.To(int32(v)) } return obj @@ -88,7 +89,7 @@ func expandWebhookClientConfig(l []interface{}) admissionregistrationv1.WebhookC } if v, ok := in["url"].(string); ok && v != "" { - obj.URL = ptrToString(v) + obj.URL = ptr.To(v) } return obj diff --git a/kubernetes/structures_container.go b/kubernetes/structures_container.go index 867c37def4..15a2533245 100644 --- a/kubernetes/structures_container.go +++ b/kubernetes/structures_container.go @@ -11,6 +11,7 @@ import ( v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/util/intstr" + "k8s.io/utils/ptr" ) func flattenCapability(in []v1.Capability) []string { @@ -588,33 +589,33 @@ func expandContainerSecurityContext(l []interface{}) (*v1.SecurityContext, error in := l[0].(map[string]interface{}) obj := v1.SecurityContext{} if v, ok := in["allow_privilege_escalation"]; ok { - obj.AllowPrivilegeEscalation = ptrToBool(v.(bool)) + obj.AllowPrivilegeEscalation = ptr.To(v.(bool)) } if v, ok := in["capabilities"].([]interface{}); ok && len(v) > 0 { obj.Capabilities = expandSecurityCapabilities(v) } if v, ok := in["privileged"]; ok { - obj.Privileged = ptrToBool(v.(bool)) + obj.Privileged = ptr.To(v.(bool)) } if v, ok := in["read_only_root_filesystem"]; ok { - obj.ReadOnlyRootFilesystem = ptrToBool(v.(bool)) + obj.ReadOnlyRootFilesystem = ptr.To(v.(bool)) } if v, ok := in["run_as_group"].(string); ok && v != "" { i, err := strconv.ParseInt(v, 10, 64) if err != nil { return &obj, err } - obj.RunAsGroup = ptrToInt64(int64(i)) + obj.RunAsGroup = ptr.To(int64(i)) } if v, ok := in["run_as_non_root"]; ok { - obj.RunAsNonRoot = ptrToBool(v.(bool)) + obj.RunAsNonRoot = ptr.To(v.(bool)) } if v, ok := in["run_as_user"].(string); ok && v != "" { i, err := strconv.ParseInt(v, 10, 64) if err != nil { return &obj, err } - obj.RunAsUser = ptrToInt64(int64(i)) + obj.RunAsUser = ptr.To(int64(i)) } if v, ok := in["seccomp_profile"].([]interface{}); ok && len(v) > 0 { obj.SeccompProfile = expandSeccompProfile(v) @@ -671,7 +672,7 @@ func expandGRPC(l []interface{}) *v1.GRPCAction { obj.Port = int32(v) } if v, ok := in["service"].(string); ok { - obj.Service = ptrToString(v) + obj.Service = ptr.To(v) } return &obj } @@ -890,7 +891,7 @@ func expandConfigMapKeyRef(r []interface{}) *v1.ConfigMapKeySelector { obj.Name = v } if v, ok := in["optional"]; ok { - obj.Optional = ptrToBool(v.(bool)) + obj.Optional = ptr.To(v.(bool)) } return obj @@ -944,7 +945,7 @@ func expandSecretRef(r []interface{}) *v1.SecretEnvSource { obj.Name = v } if v, ok := in["optional"]; ok { - obj.Optional = ptrToBool(v.(bool)) + obj.Optional = ptr.To(v.(bool)) } return obj @@ -964,7 +965,7 @@ func expandSecretKeyRef(r []interface{}) *v1.SecretKeySelector { obj.Name = v } if v, ok := in["optional"]; ok { - obj.Optional = ptrToBool(v.(bool)) + obj.Optional = ptr.To(v.(bool)) } return obj } @@ -1007,7 +1008,7 @@ func expandConfigMapRef(r []interface{}) *v1.ConfigMapEnvSource { obj.Name = v } if v, ok := in["optional"]; ok { - obj.Optional = ptrToBool(v.(bool)) + obj.Optional = ptr.To(v.(bool)) } return obj diff --git a/kubernetes/structures_container_test.go b/kubernetes/structures_container_test.go index de44a25176..a1e109843a 100644 --- a/kubernetes/structures_container_test.go +++ b/kubernetes/structures_container_test.go @@ -8,6 +8,7 @@ import ( "testing" v1 "k8s.io/api/core/v1" + "k8s.io/utils/ptr" ) func TestFlattenSecretKeyRef(t *testing.T) { @@ -21,7 +22,7 @@ func TestFlattenSecretKeyRef(t *testing.T) { Name: "Secret1", }, Key: "key1", - Optional: ptrToBool(true), + Optional: ptr.To(true), }, []interface{}{ map[string]interface{}{ @@ -78,7 +79,7 @@ func TestExpandSecretKeyRef(t *testing.T) { Name: "Secret1", }, Key: "key1", - Optional: ptrToBool(true), + Optional: ptr.To(true), }, }, { @@ -121,7 +122,7 @@ func TestFlattenConfigMapKeyRef(t *testing.T) { Name: "configmap1", }, Key: "key1", - Optional: ptrToBool(true), + Optional: ptr.To(true), }, []interface{}{ map[string]interface{}{ @@ -178,7 +179,7 @@ func TestExpandConfigMapKeyRef(t *testing.T) { Name: "configmap1", }, Key: "key1", - Optional: ptrToBool(true), + Optional: ptr.To(true), }, }, { diff --git a/kubernetes/structures_daemonset.go b/kubernetes/structures_daemonset.go index a65d6e42c7..1483ec7146 100644 --- a/kubernetes/structures_daemonset.go +++ b/kubernetes/structures_daemonset.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/util/intstr" + "k8s.io/utils/ptr" ) func flattenDaemonSetSpec(in appsv1.DaemonSetSpec, d *schema.ResourceData, meta interface{}) ([]interface{}, error) { @@ -64,7 +65,7 @@ func expandDaemonSetSpec(daemonset []interface{}) (appsv1.DaemonSetSpec, error) in := daemonset[0].(map[string]interface{}) obj.MinReadySeconds = int32(in["min_ready_seconds"].(int)) - obj.RevisionHistoryLimit = ptrToInt32(int32(in["revision_history_limit"].(int))) + obj.RevisionHistoryLimit = ptr.To(int32(in["revision_history_limit"].(int))) if v, ok := in["selector"].([]interface{}); ok && len(v) > 0 { obj.Selector = expandLabelSelector(v) diff --git a/kubernetes/structures_deployment.go b/kubernetes/structures_deployment.go index 648229e649..689b4c8611 100644 --- a/kubernetes/structures_deployment.go +++ b/kubernetes/structures_deployment.go @@ -10,6 +10,7 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/intstr" + "k8s.io/utils/ptr" ) func flattenDeploymentSpec(in appsv1.DeploymentSpec, d *schema.ResourceData, meta interface{}) ([]interface{}, error) { @@ -79,15 +80,15 @@ func expandDeploymentSpec(deployment []interface{}) (*appsv1.DeploymentSpec, err obj.MinReadySeconds = int32(in["min_ready_seconds"].(int)) obj.Paused = in["paused"].(bool) - obj.ProgressDeadlineSeconds = ptrToInt32(int32(in["progress_deadline_seconds"].(int))) + obj.ProgressDeadlineSeconds = ptr.To(int32(in["progress_deadline_seconds"].(int))) if v, ok := in["replicas"].(string); ok && v != "" { i, err := strconv.ParseInt(v, 10, 32) if err != nil { return obj, err } - obj.Replicas = ptrToInt32(int32(i)) + obj.Replicas = ptr.To(int32(i)) } - obj.RevisionHistoryLimit = ptrToInt32(int32(in["revision_history_limit"].(int))) + obj.RevisionHistoryLimit = ptr.To(int32(in["revision_history_limit"].(int))) if v, ok := in["selector"].([]interface{}); ok && len(v) > 0 { obj.Selector = expandLabelSelector(v) diff --git a/kubernetes/structures_pod.go b/kubernetes/structures_pod.go index 49f90a4608..5d572453b1 100644 --- a/kubernetes/structures_pod.go +++ b/kubernetes/structures_pod.go @@ -14,6 +14,7 @@ import ( v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" ) // https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/#taint-based-evictions @@ -718,7 +719,7 @@ func expandPodSpec(p []interface{}) (*v1.PodSpec, error) { in := p[0].(map[string]interface{}) if v, ok := in["active_deadline_seconds"].(int); ok && v > 0 { - obj.ActiveDeadlineSeconds = ptrToInt64(int64(v)) + obj.ActiveDeadlineSeconds = ptr.To(int64(v)) } if v, ok := in["affinity"].([]interface{}); ok && len(v) > 0 { @@ -730,7 +731,7 @@ func expandPodSpec(p []interface{}) (*v1.PodSpec, error) { } if v, ok := in["automount_service_account_token"].(bool); ok { - obj.AutomountServiceAccountToken = ptrToBool(v) + obj.AutomountServiceAccountToken = ptr.To(v) } if v, ok := in["container"].([]interface{}); ok && len(v) > 0 { @@ -766,7 +767,7 @@ func expandPodSpec(p []interface{}) (*v1.PodSpec, error) { } if v, ok := in["enable_service_links"].(bool); ok { - obj.EnableServiceLinks = ptrToBool(v) + obj.EnableServiceLinks = ptr.To(v) } if v, ok := in["host_aliases"].([]interface{}); ok && len(v) > 0 { @@ -813,7 +814,7 @@ func expandPodSpec(p []interface{}) (*v1.PodSpec, error) { } if v, ok := in["runtime_class_name"].(string); ok && v != "" { - obj.RuntimeClassName = ptrToString(v) + obj.RuntimeClassName = ptr.To(v) } if v, ok := in["priority_class_name"].(string); ok { @@ -841,7 +842,7 @@ func expandPodSpec(p []interface{}) (*v1.PodSpec, error) { } if v, ok := in["share_process_namespace"]; ok { - obj.ShareProcessNamespace = ptrToBool(v.(bool)) + obj.ShareProcessNamespace = ptr.To(v.(bool)) } if v, ok := in["subdomain"].(string); ok { @@ -849,7 +850,7 @@ func expandPodSpec(p []interface{}) (*v1.PodSpec, error) { } if v, ok := in["termination_grace_period_seconds"].(int); ok { - obj.TerminationGracePeriodSeconds = ptrToInt64(int64(v)) + obj.TerminationGracePeriodSeconds = ptr.To(int64(v)) } if v, ok := in["toleration"].([]interface{}); ok && len(v) > 0 { @@ -898,19 +899,19 @@ func expandWindowsOptions(l []interface{}) *v1.WindowsSecurityContextOptions { obj := &v1.WindowsSecurityContextOptions{} if v, ok := in["gmsa_credential_spec"].(string); ok { - obj.GMSACredentialSpec = ptrToString(v) + obj.GMSACredentialSpec = ptr.To(v) } if v, ok := in["host_process"].(bool); ok { - obj.HostProcess = ptrToBool(v) + obj.HostProcess = ptr.To(v) } if v, ok := in["gmsa_credential_spec_name"].(string); ok { - obj.GMSACredentialSpecName = ptrToString(v) + obj.GMSACredentialSpecName = ptr.To(v) } if v, ok := in["run_as_username"].(string); ok { - obj.RunAsUserName = ptrToString(v) + obj.RunAsUserName = ptr.To(v) } return obj @@ -968,7 +969,7 @@ func expandDNSConfigOptions(options []interface{}) []v1.PodDNSConfigOption { opt.Name = v } if v, ok := in["value"].(string); ok { - opt.Value = ptrToString(v) + opt.Value = ptr.To(v) } opts[i] = opt } @@ -987,24 +988,24 @@ func expandPodSecurityContext(l []interface{}) (*v1.PodSecurityContext, error) { if err != nil { return obj, err } - obj.FSGroup = ptrToInt64(int64(i)) + obj.FSGroup = ptr.To(int64(i)) } if v, ok := in["run_as_group"].(string); ok && v != "" { i, err := strconv.ParseInt(v, 10, 64) if err != nil { return obj, err } - obj.RunAsGroup = ptrToInt64(int64(i)) + obj.RunAsGroup = ptr.To(int64(i)) } if v, ok := in["run_as_non_root"].(bool); ok { - obj.RunAsNonRoot = ptrToBool(v) + obj.RunAsNonRoot = ptr.To(v) } if v, ok := in["run_as_user"].(string); ok && v != "" { i, err := strconv.ParseInt(v, 10, 64) if err != nil { return obj, err } - obj.RunAsUser = ptrToInt64(int64(i)) + obj.RunAsUser = ptr.To(int64(i)) } if v, ok := in["seccomp_profile"].([]interface{}); ok && len(v) > 0 { obj.SeccompProfile = expandSeccompProfile(v) @@ -1097,7 +1098,7 @@ func expandKeyPath(in []interface{}) []v1.KeyToPath { if v, ok := p["mode"].(string); ok { m, err := strconv.ParseInt(v, 8, 32) if err == nil { - keyPaths[i].Mode = ptrToInt32(int32(m)) + keyPaths[i].Mode = ptr.To(int32(m)) } } if v, ok := p["path"].(string); ok { @@ -1121,7 +1122,7 @@ func expandDownwardAPIVolumeFile(in []interface{}) ([]v1.DownwardAPIVolumeFile, if err != nil { return dapivf, fmt.Errorf("DownwardAPI volume file: failed to parse 'mode' value: %s", err) } - dapivf[i].Mode = ptrToInt32(int32(m)) + dapivf[i].Mode = ptr.To(int32(m)) } if v, ok := p["path"].(string); ok { dapivf[i].Path = v @@ -1151,7 +1152,7 @@ func expandConfigMapVolumeSource(l []interface{}) (*v1.ConfigMapVolumeSource, er if err != nil { return obj, fmt.Errorf("ConfigMap volume: failed to parse 'default_mode' value: %s", err) } - obj.DefaultMode = ptrToInt32(int32(v)) + obj.DefaultMode = ptr.To(int32(v)) } if v, ok := in["name"].(string); ok { @@ -1159,7 +1160,7 @@ func expandConfigMapVolumeSource(l []interface{}) (*v1.ConfigMapVolumeSource, er } if opt, ok := in["optional"].(bool); ok { - obj.Optional = ptrToBool(opt) + obj.Optional = ptr.To(opt) } if v, ok := in["items"].([]interface{}); ok && len(v) > 0 { obj.Items = expandKeyPath(v) @@ -1180,7 +1181,7 @@ func expandDownwardAPIVolumeSource(l []interface{}) (*v1.DownwardAPIVolumeSource if err != nil { return obj, fmt.Errorf("Downward API volume: failed to parse 'default_mode' value: %s", err) } - obj.DefaultMode = ptrToInt32(int32(v)) + obj.DefaultMode = ptr.To(int32(v)) } if v, ok := in["items"].([]interface{}); ok && len(v) > 0 { @@ -1257,7 +1258,7 @@ func expandSecretVolumeSource(l []interface{}) (*v1.SecretVolumeSource, error) { if err != nil { return obj, fmt.Errorf("Secret volume: failed to parse 'default_mode' value: %s", err) } - obj.DefaultMode = ptrToInt32(int32(v)) + obj.DefaultMode = ptr.To(int32(v)) } if secret, ok := in["secret_name"].(string); ok { @@ -1265,7 +1266,7 @@ func expandSecretVolumeSource(l []interface{}) (*v1.SecretVolumeSource, error) { } if opt, ok := in["optional"].(bool); ok { - obj.Optional = ptrToBool(opt) + obj.Optional = ptr.To(opt) } if v, ok := in["items"].([]interface{}); ok && len(v) > 0 { obj.Items = expandKeyPath(v) @@ -1286,7 +1287,7 @@ func expandProjectedVolumeSource(l []interface{}) (*v1.ProjectedVolumeSource, er if err != nil { return obj, fmt.Errorf("Projected volume: failed to parse 'default_mode' value: %s", err) } - obj.DefaultMode = ptrToInt32(int32(v)) + obj.DefaultMode = ptr.To(int32(v)) } if v, ok := in["sources"].([]interface{}); ok && len(v) > 0 { srcs, err := expandProjectedSources(v) @@ -1354,7 +1355,7 @@ func expandProjectedSecret(secret map[string]interface{}) *v1.SecretProjection { s.Items = expandKeyPath(values) } if value, ok := secret["optional"].(bool); ok { - s.Optional = ptrToBool(value) + s.Optional = ptr.To(value) } return s } @@ -1380,7 +1381,7 @@ func expandProjectedConfigMap(configMap map[string]interface{}) *v1.ConfigMapPro s.Items = expandKeyPath(values) } if value, ok := configMap["optional"].(bool); ok { - s.Optional = ptrToBool(value) + s.Optional = ptr.To(value) } return s } @@ -1431,7 +1432,7 @@ func expandProjectedServiceAccountToken(sat map[string]interface{}) *v1.ServiceA s.Audience = value } if value, ok := sat["expiration_seconds"].(int); ok { - s.ExpirationSeconds = ptrToInt64(int64(value)) + s.ExpirationSeconds = ptr.To(int64(value)) } if value, ok := sat["path"].(string); ok { s.Path = value @@ -1462,7 +1463,7 @@ func expandTolerations(tolerations []interface{}) ([]*v1.Toleration, error) { if err != nil { return nil, fmt.Errorf("invalid toleration_seconds must be int or \"\", got \"%s\"", value) } - ts[i].TolerationSeconds = ptrToInt64(seconds) + ts[i].TolerationSeconds = ptr.To(seconds) } if value, ok := m["value"]; ok { ts[i].Value = value.(string) diff --git a/kubernetes/structures_pod_test.go b/kubernetes/structures_pod_test.go index 2336dc038a..ca35e15952 100644 --- a/kubernetes/structures_pod_test.go +++ b/kubernetes/structures_pod_test.go @@ -10,6 +10,7 @@ import ( "github.com/google/go-cmp/cmp" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/utils/ptr" ) func TestFlattenTolerations(t *testing.T) { @@ -57,7 +58,7 @@ func TestFlattenTolerations(t *testing.T) { []corev1.Toleration{ { Effect: "NoExecute", - TolerationSeconds: ptrToInt64(int64(120)), + TolerationSeconds: ptr.To(int64(120)), }, }, []interface{}{ @@ -133,7 +134,7 @@ func TestExpandTolerations(t *testing.T) { []*corev1.Toleration{ { Effect: "NoExecute", - TolerationSeconds: ptrToInt64(int64(120)), + TolerationSeconds: ptr.To(int64(120)), }, }, }, @@ -162,13 +163,13 @@ func TestFlattenSecretVolumeSource(t *testing.T) { }{ { &corev1.SecretVolumeSource{ - DefaultMode: ptrToInt32(int32(0644)), + DefaultMode: ptr.To(int32(0644)), SecretName: "secret1", - Optional: ptrToBool(true), + Optional: ptr.To(true), Items: []corev1.KeyToPath{ { Key: "foo.txt", - Mode: ptrToInt32(int32(0600)), + Mode: ptr.To(int32(0600)), Path: "etc/foo.txt", }, }, @@ -190,7 +191,7 @@ func TestFlattenSecretVolumeSource(t *testing.T) { }, { &corev1.SecretVolumeSource{ - DefaultMode: ptrToInt32(int32(0755)), + DefaultMode: ptr.To(int32(0755)), SecretName: "secret2", Items: []corev1.KeyToPath{ { @@ -248,13 +249,13 @@ func TestExpandSecretVolumeSource(t *testing.T) { }, }, &corev1.SecretVolumeSource{ - DefaultMode: ptrToInt32(int32(0644)), + DefaultMode: ptr.To(int32(0644)), SecretName: "secret1", - Optional: ptrToBool(true), + Optional: ptr.To(true), Items: []corev1.KeyToPath{ { Key: "foo.txt", - Mode: ptrToInt32(int32(0600)), + Mode: ptr.To(int32(0600)), Path: "etc/foo.txt", }, }, @@ -274,7 +275,7 @@ func TestExpandSecretVolumeSource(t *testing.T) { }, }, &corev1.SecretVolumeSource{ - DefaultMode: ptrToInt32(int32(0755)), + DefaultMode: ptr.To(int32(0755)), SecretName: "secret2", Items: []corev1.KeyToPath{ { @@ -360,12 +361,12 @@ func TestFlattenConfigMapVolumeSource(t *testing.T) { LocalObjectReference: corev1.LocalObjectReference{ Name: "configmap1", }, - DefaultMode: ptrToInt32(int32(0644)), - Optional: ptrToBool(true), + DefaultMode: ptr.To(int32(0644)), + Optional: ptr.To(true), Items: []corev1.KeyToPath{ { Key: "foo.txt", - Mode: ptrToInt32(int32(0600)), + Mode: ptr.To(int32(0600)), Path: "etc/foo.txt", }, }, @@ -390,7 +391,7 @@ func TestFlattenConfigMapVolumeSource(t *testing.T) { LocalObjectReference: corev1.LocalObjectReference{ Name: "configmap2", }, - DefaultMode: ptrToInt32(int32(0755)), + DefaultMode: ptr.To(int32(0755)), Items: []corev1.KeyToPath{ { Key: "bar.txt", @@ -450,12 +451,12 @@ func TestExpandConfigMapVolumeSource(t *testing.T) { LocalObjectReference: corev1.LocalObjectReference{ Name: "configmap1", }, - DefaultMode: ptrToInt32(int32(0644)), - Optional: ptrToBool(true), + DefaultMode: ptr.To(int32(0644)), + Optional: ptr.To(true), Items: []corev1.KeyToPath{ { Key: "foo.txt", - Mode: ptrToInt32(int32(0600)), + Mode: ptr.To(int32(0600)), Path: "etc/foo.txt", }, }, @@ -478,7 +479,7 @@ func TestExpandConfigMapVolumeSource(t *testing.T) { LocalObjectReference: corev1.LocalObjectReference{ Name: "configmap2", }, - DefaultMode: ptrToInt32(int32(0755)), + DefaultMode: ptr.To(int32(0755)), Items: []corev1.KeyToPath{ { Key: "bar.txt", @@ -585,8 +586,8 @@ func TestExpandCSIVolumeSource(t *testing.T) { }, ExpectedOutput: &corev1.CSIVolumeSource{ Driver: "secrets-store.csi.k8s.io", - ReadOnly: ptrToBool(true), - FSType: ptrToString("nfs"), + ReadOnly: ptr.To(true), + FSType: ptr.To("nfs"), VolumeAttributes: map[string]string{ "secretProviderClass": "azure-keyvault", }, @@ -638,8 +639,8 @@ func TestFlattenCSIVolumeSource(t *testing.T) { { Input: &corev1.CSIVolumeSource{ Driver: "secrets-store.csi.k8s.io", - ReadOnly: ptrToBool(true), - FSType: ptrToString("nfs"), + ReadOnly: ptr.To(true), + FSType: ptr.To("nfs"), VolumeAttributes: map[string]string{ "secretProviderClass": "azure-keyvault", }, diff --git a/kubernetes/structures_replication_controller.go b/kubernetes/structures_replication_controller.go index 08f25382da..723caf7cb5 100644 --- a/kubernetes/structures_replication_controller.go +++ b/kubernetes/structures_replication_controller.go @@ -6,6 +6,7 @@ package kubernetes import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" corev1 "k8s.io/api/core/v1" + "k8s.io/utils/ptr" ) func flattenReplicationControllerSpec(in corev1.ReplicationControllerSpec, d *schema.ResourceData, meta interface{}) ([]interface{}, error) { @@ -41,7 +42,7 @@ func expandReplicationControllerSpec(rc []interface{}) (*corev1.ReplicationContr } in := rc[0].(map[string]interface{}) obj.MinReadySeconds = int32(in["min_ready_seconds"].(int)) - obj.Replicas = ptrToInt32(int32(in["replicas"].(int))) + obj.Replicas = ptr.To(int32(in["replicas"].(int))) obj.Selector = expandStringMap(in["selector"].(map[string]interface{})) template, err := expandReplicationControllerTemplate(in["template"].([]interface{})) diff --git a/kubernetes/structures_stateful_set.go b/kubernetes/structures_stateful_set.go index c3a9265cc6..b1cbfd8b19 100644 --- a/kubernetes/structures_stateful_set.go +++ b/kubernetes/structures_stateful_set.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" v1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + "k8s.io/utils/ptr" ) // Expanders @@ -32,11 +33,11 @@ func expandStatefulSetSpec(s []interface{}) (*v1.StatefulSetSpec, error) { if err != nil { return obj, err } - obj.Replicas = ptrToInt32(int32(i)) + obj.Replicas = ptr.To(int32(i)) } if v, ok := in["revision_history_limit"].(int); ok { - obj.RevisionHistoryLimit = ptrToInt32(int32(v)) + obj.RevisionHistoryLimit = ptr.To(int32(v)) } if v, ok := in["selector"].([]interface{}); ok && len(v) > 0 { @@ -112,7 +113,7 @@ func expandStatefulSetSpecUpdateStrategy(s []interface{}) (*v1.StatefulSetUpdate if !ok { return ust, errors.New("failed to expand 'spec.update_strategy.rolling_update.partition'") } - u.Partition = ptrToInt32(int32(p)) + u.Partition = ptr.To(int32(p)) ust.RollingUpdate = &u } log.Printf("[DEBUG] Expanded StatefulSet.Spec.UpdateStrategy: %#v", ust) diff --git a/kubernetes/structures_tokenrequest.go b/kubernetes/structures_tokenrequest.go index d571889f00..4558956853 100644 --- a/kubernetes/structures_tokenrequest.go +++ b/kubernetes/structures_tokenrequest.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" authv1 "k8s.io/api/authentication/v1" "k8s.io/apimachinery/pkg/types" + "k8s.io/utils/ptr" ) // Flatteners @@ -57,7 +58,7 @@ func expandTokenRequestV1Spec(p []interface{}) *authv1.TokenRequestSpec { obj.BoundObjectRef = expandBoundObjectReference(in["bound_object_ref"].([]interface{})) if v, ok := in["expiration_seconds"].(int); v != 0 && ok { - obj.ExpirationSeconds = ptrToInt64(int64(v)) + obj.ExpirationSeconds = ptr.To(int64(v)) } return obj