Skip to content

Commit

Permalink
Switch to k8s.io/utils/ptr package (hashicorp#2377)
Browse files Browse the repository at this point in the history
* Switch from in-house pointer functions to the ones from the Kubernetes team from the package k8s.io/utils.
  • Loading branch information
arybolovlev authored Jan 2, 2024
1 parent 9daedb7 commit 870aada
Show file tree
Hide file tree
Showing 42 changed files with 185 additions and 168 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion kubernetes/resource_kubernetes_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/resource_kubernetes_config_map_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)),
})
}

Expand Down
3 changes: 2 additions & 1 deletion kubernetes/resource_kubernetes_config_map_v1_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion kubernetes/resource_kubernetes_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions kubernetes/resource_kubernetes_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion kubernetes/resource_kubernetes_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion kubernetes/resource_kubernetes_node_taint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/resource_kubernetes_secret_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)),
})
}

Expand Down
3 changes: 2 additions & 1 deletion kubernetes/resource_kubernetes_service_account_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(), ""),
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/resource_kubernetes_service_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion kubernetes/structure_api_service_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package kubernetes

import (
v1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
"k8s.io/utils/ptr"
)

// Flatteners
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions kubernetes/structure_cron_job_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions kubernetes/structure_cron_job_v1beta1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/structure_csi_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/structure_csi_driver_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 870aada

Please sign in to comment.