From 1c6190734fb6a6290329a30b09bd765283b3732c Mon Sep 17 00:00:00 2001 From: Ashley Dumaine Date: Thu, 24 Oct 2024 16:17:15 -0400 Subject: [PATCH] remove gob to save memory --- .golangci.yml | 14 +++++- .../linodemachine_controller_helpers.go | 44 ++++++++++++------- .../linodemachine_controller_helpers_test.go | 15 ------- controller/linodemachine_controller_test.go | 3 +- ...linodeplacementgroup_controller_helpers.go | 20 ++------- controller/linodevpc_controller_helpers.go | 25 +++++------ .../linodevpc_controller_helpers_test.go | 15 ------- 7 files changed, 57 insertions(+), 79 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index d78d1e2de..9342d2c4d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -61,6 +61,18 @@ linters-settings: # tokens count to trigger issue, 150 by default threshold: 100 + depguard: + rules: + main: + files: + - "$all" + - "!$test" + deny: + - pkg: "reflect" + desc: "Reflection is never clear." + - pkg: "gob" + desc: "Please convert types manually" + goconst: # minimal length of string constant, 3 by default min-len: 3 @@ -132,7 +144,7 @@ linters: - contextcheck - cyclop - decorder - # - depguard + - depguard - dogsled # - dupl - dupword diff --git a/controller/linodemachine_controller_helpers.go b/controller/linodemachine_controller_helpers.go index a13a2dcd1..158d60f0b 100644 --- a/controller/linodemachine_controller_helpers.go +++ b/controller/linodemachine_controller_helpers.go @@ -17,10 +17,8 @@ limitations under the License. package controller import ( - "bytes" "context" b64 "encoding/base64" - "encoding/gob" "errors" "fmt" "net/http" @@ -431,21 +429,35 @@ func getVPCInterfaceConfig(ctx context.Context, machineScope *scope.MachineScope } func linodeMachineSpecToInstanceCreateConfig(machineSpec infrav1alpha2.LinodeMachineSpec) *linodego.InstanceCreateOptions { - var buf bytes.Buffer - enc := gob.NewEncoder(&buf) - err := enc.Encode(machineSpec) - if err != nil { - return nil + interfaces := make([]linodego.InstanceConfigInterfaceCreateOptions, len(machineSpec.Interfaces)) + for idx, iface := range machineSpec.Interfaces { + interfaces[idx] = linodego.InstanceConfigInterfaceCreateOptions{ + IPAMAddress: iface.IPAMAddress, + Label: iface.Label, + Purpose: iface.Purpose, + Primary: iface.Primary, + SubnetID: iface.SubnetID, + IPRanges: iface.IPRanges, + } + } + privateIP := false + if machineSpec.PrivateIP != nil { + privateIP = *machineSpec.PrivateIP + } + return &linodego.InstanceCreateOptions{ + Region: machineSpec.Region, + Type: machineSpec.Type, + AuthorizedKeys: machineSpec.AuthorizedKeys, + AuthorizedUsers: machineSpec.AuthorizedUsers, + RootPass: machineSpec.RootPass, + Image: machineSpec.Image, + Interfaces: interfaces, + PrivateIP: privateIP, + Tags: machineSpec.Tags, + FirewallID: machineSpec.FirewallID, + DiskEncryption: linodego.InstanceDiskEncryption(machineSpec.DiskEncryption), + Group: machineSpec.Group, } - - var createConfig linodego.InstanceCreateOptions - dec := gob.NewDecoder(&buf) - err = dec.Decode(&createConfig) - if err != nil { - return nil - } - - return &createConfig } func setUserData(ctx context.Context, machineScope *scope.MachineScope, createConfig *linodego.InstanceCreateOptions, logger logr.Logger) error { diff --git a/controller/linodemachine_controller_helpers_test.go b/controller/linodemachine_controller_helpers_test.go index 789a4d960..f38c320c2 100644 --- a/controller/linodemachine_controller_helpers_test.go +++ b/controller/linodemachine_controller_helpers_test.go @@ -1,17 +1,14 @@ package controller import ( - "bytes" "context" b64 "encoding/base64" - "encoding/gob" "fmt" "testing" "github.com/go-logr/logr" "github.com/linode/linodego" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -62,18 +59,6 @@ func TestLinodeMachineSpecToCreateInstanceConfig(t *testing.T) { createConfig := linodeMachineSpecToInstanceCreateConfig(machineSpec) assert.NotNil(t, createConfig, "Failed to convert LinodeMachineSpec to InstanceCreateOptions") - - var buf bytes.Buffer - enc := gob.NewEncoder(&buf) - err := enc.Encode(createConfig) - require.NoError(t, err, "Failed to encode InstanceCreateOptions") - - var actualMachineSpec infrav1alpha2.LinodeMachineSpec - dec := gob.NewDecoder(&buf) - err = dec.Decode(&actualMachineSpec) - require.NoError(t, err, "Failed to decode LinodeMachineSpec") - - assert.Equal(t, machineSpec, actualMachineSpec) } func TestSetUserData(t *testing.T) { diff --git a/controller/linodemachine_controller_test.go b/controller/linodemachine_controller_test.go index cafb00058..474b2d0d2 100644 --- a/controller/linodemachine_controller_test.go +++ b/controller/linodemachine_controller_test.go @@ -1897,7 +1897,8 @@ var _ = Describe("machine in VPC", Label("machine", "VPC"), Ordered, func() { }, { Primary: true, - }})) + }, + })) }) It("creates a instance with pre defined vpc interface", func(ctx SpecContext) { linodeMachine := infrav1alpha2.LinodeMachine{ diff --git a/controller/linodeplacementgroup_controller_helpers.go b/controller/linodeplacementgroup_controller_helpers.go index 670de1c8f..4cb6f834b 100644 --- a/controller/linodeplacementgroup_controller_helpers.go +++ b/controller/linodeplacementgroup_controller_helpers.go @@ -17,9 +17,7 @@ limitations under the License. package controller import ( - "bytes" "context" - "encoding/gob" "errors" "github.com/go-logr/logr" @@ -77,19 +75,9 @@ func (r *LinodePlacementGroupReconciler) reconcilePlacementGroup(ctx context.Con } func linodePlacementGroupSpecToPGCreateConfig(pgSpec infrav1alpha2.LinodePlacementGroupSpec) *linodego.PlacementGroupCreateOptions { - var buf bytes.Buffer - enc := gob.NewEncoder(&buf) - err := enc.Encode(pgSpec) - if err != nil { - return nil + return &linodego.PlacementGroupCreateOptions{ + Region: pgSpec.Region, + PlacementGroupType: linodego.PlacementGroupType(pgSpec.PlacementGroupType), + PlacementGroupPolicy: linodego.PlacementGroupPolicy(pgSpec.PlacementGroupPolicy), } - - var createConfig linodego.PlacementGroupCreateOptions - dec := gob.NewDecoder(&buf) - err = dec.Decode(&createConfig) - if err != nil { - return nil - } - - return &createConfig } diff --git a/controller/linodevpc_controller_helpers.go b/controller/linodevpc_controller_helpers.go index 73613cb66..bc52d2b27 100644 --- a/controller/linodevpc_controller_helpers.go +++ b/controller/linodevpc_controller_helpers.go @@ -17,9 +17,7 @@ limitations under the License. package controller import ( - "bytes" "context" - "encoding/gob" "errors" "github.com/go-logr/logr" @@ -94,19 +92,16 @@ func updateVPCSpecSubnets(vpcScope *scope.VPCScope, vpc *linodego.VPC) { } func linodeVPCSpecToVPCCreateConfig(vpcSpec infrav1alpha2.LinodeVPCSpec) *linodego.VPCCreateOptions { - var buf bytes.Buffer - enc := gob.NewEncoder(&buf) - err := enc.Encode(vpcSpec) - if err != nil { - return nil + subnets := make([]linodego.VPCSubnetCreateOptions, len(vpcSpec.Subnets)) + for idx, subnet := range vpcSpec.Subnets { + subnets[idx] = linodego.VPCSubnetCreateOptions{ + Label: subnet.Label, + IPv4: subnet.IPv4, + } } - - var createConfig linodego.VPCCreateOptions - dec := gob.NewDecoder(&buf) - err = dec.Decode(&createConfig) - if err != nil { - return nil + return &linodego.VPCCreateOptions{ + Description: vpcSpec.Description, + Region: vpcSpec.Region, + Subnets: subnets, } - - return &createConfig } diff --git a/controller/linodevpc_controller_helpers_test.go b/controller/linodevpc_controller_helpers_test.go index 24086b372..bd41fd27a 100644 --- a/controller/linodevpc_controller_helpers_test.go +++ b/controller/linodevpc_controller_helpers_test.go @@ -1,12 +1,9 @@ package controller import ( - "bytes" - "encoding/gob" "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2" ) @@ -27,16 +24,4 @@ func TestLinodeVPCSpecToCreateVPCConfig(t *testing.T) { createConfig := linodeVPCSpecToVPCCreateConfig(vpcSpec) assert.NotNil(t, createConfig, "Failed to convert LinodeVPCSpec to VPCCreateOptions") - - var buf bytes.Buffer - enc := gob.NewEncoder(&buf) - err := enc.Encode(createConfig) - require.NoError(t, err, "Failed to encode VPCCreateOptions") - - var actualVPCSpec infrav1alpha2.LinodeVPCSpec - dec := gob.NewDecoder(&buf) - err = dec.Decode(&actualVPCSpec) - require.NoError(t, err, "Failed to decode LinodeVPCSpec") - - assert.Equal(t, vpcSpec, actualVPCSpec) }