Skip to content

Commit

Permalink
remove gob to save memory
Browse files Browse the repository at this point in the history
  • Loading branch information
AshleyDumaine committed Oct 25, 2024
1 parent 4e8e3ce commit 1c61907
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 79 deletions.
14 changes: 13 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -132,7 +144,7 @@ linters:
- contextcheck
- cyclop
- decorder
# - depguard
- depguard
- dogsled
# - dupl
- dupword
Expand Down
44 changes: 28 additions & 16 deletions controller/linodemachine_controller_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ limitations under the License.
package controller

import (
"bytes"
"context"
b64 "encoding/base64"
"encoding/gob"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 0 additions & 15 deletions controller/linodemachine_controller_helpers_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion controller/linodemachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
20 changes: 4 additions & 16 deletions controller/linodeplacementgroup_controller_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ limitations under the License.
package controller

import (
"bytes"
"context"
"encoding/gob"
"errors"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -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
}
25 changes: 10 additions & 15 deletions controller/linodevpc_controller_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ limitations under the License.
package controller

import (
"bytes"
"context"
"encoding/gob"
"errors"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -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
}
15 changes: 0 additions & 15 deletions controller/linodevpc_controller_helpers_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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)
}

0 comments on commit 1c61907

Please sign in to comment.